def call(self, inputs): if self.data_format is None: data_format = image_data_format() if self.data_format not in {'channels_first', 'channels_last'}: raise ValueError('Unknown data_format ' + str(data_format)) x = _preprocess_conv2d_input(inputs, self.data_format) padding = _preprocess_padding(self.padding) strides = (1,) + self.strides + (1,) outputs = tf.nn.depthwise_conv2d(inputs, self.depthwise_kernel, strides=strides, padding=padding, rate=self.dilation_rate) if self.bias: outputs = K.bias_add( outputs, self.bias, data_format=self.data_format) if self.activation is not None: return self.activation(outputs) return outputs
def N_conv(x, kernel, strides=(1, 1), padding='valid', data_format=None, dilation_rate=(1, 1)): data_format = normalize_data_format(data_format) x, tf_data_format = _preprocess_conv2d_input(x, data_format) padding = _preprocess_padding(padding) # TF 2 arg conversion kwargs = {} if _is_tf_1(): kwargs['dilation_rate'] = dilation_rate else: kwargs['dilations'] = dilation_rate x = conv2d_cosnorm(x, kernel, strides=strides, padding=padding) if data_format == 'channels_first' and tf_data_format == 'NHWC': x = tf.transpose(x, (0, 3, 1, 2)) # NHWC -> NCHW return x
def extract_image_patches(x, ksizes, ssizes, padding='same', data_format='tf'): ''' Extract the patches from an image # Parameters x : The input image ksizes : 2-d tuple with the kernel size ssizes : 2-d tuple with the strides size padding : 'same' or 'valid' data_format : 'channels_last' or 'channels_first' # Returns The (k_w,k_h) patches extracted TF ==> (batch_size,w,h,k_w,k_h,c) TH ==> (batch_size,w,h,c,k_w,k_h) ''' kernel = [1, ksizes[0], ksizes[1], 1] strides = [1, ssizes[0], ssizes[1], 1] padding = _preprocess_padding(padding) if data_format == 'channels_first': x = KTF.permute_dimensions(x, (0, 2, 3, 1)) bs_i, w_i, h_i, ch_i = KTF.int_shape(x) patches = tf.extract_image_patches(x, kernel, strides, [1, 1, 1, 1], padding) # Reshaping to fit Theano bs, w, h, ch = KTF.int_shape(patches) patches = tf.reshape( tf.transpose( tf.reshape(patches, [-1, w, h, tf.floordiv(ch, ch_i), ch_i]), [0, 1, 2, 4, 3]), [-1, w, h, ch_i, ksizes[0], ksizes[1]]) if data_format == 'channels_last': patches = KTF.permute_dimensions(patches, [0, 1, 2, 4, 5, 3]) return patches
def __init__(self, kernel_size, strides=(1, 1, 1), padding='valid', group_multiplier=1, group_size=1, data_format=None, activation=None, use_bias=True, group_depthwise_initializer='glorot_uniform', bias_initializer='zeros', dilation_rate=(1, 1, 1), group_depthwise_regularizer=None, bias_regularizer=None, activity_regularizer=None, group_depthwise_constraint=None, bias_constraint=None, **kwargs): super(GroupDepthwiseConv3D, self).__init__(filters=None, kernel_size=kernel_size, strides=strides, padding=padding, data_format=data_format, activation=activation, use_bias=use_bias, bias_regularizer=bias_regularizer, dilation_rate=dilation_rate, activity_regularizer=activity_regularizer, bias_constraint=bias_constraint, **kwargs) self.group_multiplier = group_multiplier self.group_size = group_size self.group_depthwise_initializer = initializers.get( group_depthwise_initializer) self.group_depthwise_regularizer = regularizers.get( group_depthwise_regularizer) self.group_depthwise_constraint = constraints.get( group_depthwise_constraint) self.bias_initializer = initializers.get(bias_initializer) self.dilation_rate = dilation_rate self._padding = _preprocess_padding(self.padding) self._strides = (1, ) + self.strides + (1, ) if self.data_format == 'channels_first': self.channel_axis = 1 self._data_format = "NCDHW" else: self.channel_axis = -1 self._data_format = "NDHWC" self.input_dim = None self.kernel = None self.bias = None self.group_num = None
def conv2d(x, kernel, strides=(1, 1), padding='valid', data_format=None, dilation_rate=(1, 1)): """2D convolution. # Arguments x: Tensor or variable. kernel: kernel tensor. strides: strides tuple. padding: string, `"same"`, `"causal"` or `"valid"`. data_format: string, `"channels_last"` or `"channels_first"`. Whether to use Theano or TensorFlow/CNTK data format for inputs/kernels/outputs. dilation_rate: tuple of 2 integers. # Returns A tensor, result of 2D convolution. # Raises ValueError: If `data_format` is neither `"channels_last"` nor `"channels_first"`. """ data_format = K.normalize_data_format(data_format) # ADDED kernel_shape = kernel.get_shape().as_list() if padding == 'causal': if data_format != 'channels_last': raise ValueError('When using causal padding in `conv2d`, ' '`data_format` must be "channels_last" ' '(temporal data).') # causal (dilated) convolution: left_pad = dilation_rate[0] * (kernel_shape[0] - 1) x = temporal_2d_padding(x, (left_pad, 0), data_format=data_format) padding = 'valid' # ADDED till here x, tf_data_format = _preprocess_conv2d_input(x, data_format) padding = _preprocess_padding(padding) x = tf.nn.convolution(input=x, filter=kernel, dilation_rate=dilation_rate, strides=strides, padding=padding, data_format=tf_data_format) if data_format == 'channels_first' and tf_data_format == 'NHWC': x = tf.transpose(x, (0, 3, 1, 2)) # NHWC -> NCHW return x
def separable_conv2d(x, depthwise_kernel, pointwise_kernel, strides=(1, 1), padding='valid', data_format=None, dilation_rate=(1, 1)): """2D convolution with separable filters. # Arguments x: input tensor depthwise_kernel: convolution kernel for the depthwise convolution. pointwise_kernel: kernel for the 1x1 convolution. strides: strides tuple (length 2). padding: string, `"same"` or `"valid"`. data_format: string, `"channels_last"` or `"channels_first"`. dilation_rate: tuple of integers, dilation rates for the separable convolution. # Returns Output tensor. # Raises ValueError: If `data_format` is neither `"channels_last"` nor `"channels_first"`. """ data_format = normalize_data_format(data_format) # ADDED kernel_shape = depthwise_kernel.get_shape().as_list() if padding == 'causal': if data_format != 'channels_last': raise ValueError('When using causal padding in `conv1d`, ' '`data_format` must be "channels_last" ' '(temporal data).') left_pad = dilation_rate * (kernel_shape[0] - 1) x = temporal_padding(x, (left_pad, 0)) padding = 'valid' # ADDED till here x, tf_data_format = _preprocess_conv2d_input(x, data_format) padding = _preprocess_padding(padding) if tf_data_format == 'NHWC': strides = (1,) + strides + (1,) else: strides = (1, 1) + strides x = tf.nn.separable_conv2d(x, depthwise_kernel, pointwise_kernel, strides=strides, padding=padding, rate=dilation_rate, data_format=tf_data_format) if data_format == 'channels_first' and tf_data_format == 'NHWC': x = tf.transpose(x, (0, 3, 1, 2)) # NHWC -> NCHW return x
def deconv3d(x, kernel, output_shape, strides=(1, 1, 1), padding='valid', data_format='default', image_shape=None, filter_shape=None): '''3D deconvolution (i.e. transposed convolution). # Arguments x: input tensor. kernel: kernel tensor. output_shape: 1D int tensor for the output shape. strides: strides tuple. padding: string, "same" or "valid". data_format: "tf" or "th". Whether to use Theano or TensorFlow dimension ordering for inputs/kernels/ouputs. # Returns A tensor, result of transposed 3D convolution. # Raises ValueError: if `data_format` is neither `tf` or `th`. ''' if data_format == 'default': data_format = image_data_format() if data_format not in {'channels_first', 'channels_last'}: raise ValueError('Unknown data_format ' + str(data_format)) x = _preprocess_conv3d_input(x, data_format) output_shape = _preprocess_deconv_output_shape(x, output_shape, data_format) kernel = _preprocess_conv3d_kernel(kernel, data_format) kernel = tf.transpose(kernel, (0, 1, 2, 4, 3)) padding = _preprocess_padding(padding) strides = (1, ) + strides + (1, ) x = tf.nn.conv3d_transpose(x, kernel, output_shape, strides, padding=padding) return _postprocess_conv3d_output(x, data_format)
def pool2d_argmax(x: 'Tensor', pool_size: tuple, strides: tuple = (1, 1), padding: str = 'valid', data_format: str = None) -> tuple: """ 2D Pooling that returns indexes too. Args: x: Tensor or variable. pool_size: tuple of 2 integers. strides: tuple of 2 integers. padding: string, `"same"` or `"valid"`. data_format: string, `"channels_last"` or `"channels_first"`. Returns: A tensor, result of 2D pooling. Raises: ValueError: if `data_format` is neither `"channels_last"` or `"channels_first"`. """ # get the normalized data format data_format = K.common.normalize_data_format(data_format) # pre-process the input tensor x, tf_data_format = _preprocess_conv2d_input(x, data_format) padding = _preprocess_padding(padding) # update strides and pool size based on data format if tf_data_format == 'NHWC': strides = (1, ) + strides + (1, ) pool_size = (1, ) + pool_size + (1, ) else: strides = (1, 1) + strides pool_size = (1, 1) + pool_size # get the values and the indexes from the max pool operation x, idx = tf.nn.max_pool_with_argmax(x, pool_size, strides, padding) # update shapes if necessary if data_format == 'channels_first' and tf_data_format == 'NHWC': # NHWC -> NCHW x = tf.transpose(x, (0, 3, 1, 2)) # NHWC -> NCHW idx = tf.transpose(idx, (0, 3, 1, 2)) return x, idx
def __init__(self, kernel_size, strides=(1, 1, 1), padding='valid', depth_multiplier=1, groups=None, data_format=None, activation=None, use_bias=True, depthwise_initializer='glorot_uniform', bias_initializer='zeros', dilation_rate = (1, 1, 1), depthwise_regularizer=None, bias_regularizer=None, activity_regularizer=None, depthwise_constraint=None, bias_constraint=None, **kwargs): super(DepthwiseConv3D, self).__init__( filters=None, kernel_size=kernel_size, strides=strides, padding=padding, data_format=data_format, activation=activation, use_bias=use_bias, bias_regularizer=bias_regularizer, dilation_rate=dilation_rate, activity_regularizer=activity_regularizer, bias_constraint=bias_constraint, **kwargs) self.depth_multiplier = depth_multiplier self.groups = groups self.depthwise_initializer = initializers.get(depthwise_initializer) self.depthwise_regularizer = regularizers.get(depthwise_regularizer) self.depthwise_constraint = constraints.get(depthwise_constraint) self.bias_initializer = initializers.get(bias_initializer) self.dilation_rate = dilation_rate self._padding = _preprocess_padding(self.padding) self._strides = (1,) + self.strides + (1,) self._data_format = "NDHWC" self.input_dim = None
def conv1d(x, kernel, strides=1, padding='valid', data_format=None, dilation_rate=1): """1D convolution. # Arguments x: Tensor or variable. kernel: kernel tensor. strides: stride integer. padding: string, `"same"`, `"causal"` or `"valid"`. data_format: string, `"channels_last"` or `"channels_first"`. dilation_rate: integer dilate rate. # Returns A tensor, result of 1D convolution. # Raises ValueError: If `data_format` is neither `"channels_last"` nor `"channels_first"`. """ data_format = normalize_data_format(data_format) kernel_shape = kernel.get_shape().as_list() if padding == 'causal': if data_format != 'channels_last': raise ValueError('When using causal padding in `conv1d`, ' '`data_format` must be "channels_last" ' '(temporal data).') # causal (dilated) convolution: left_pad = dilation_rate * (kernel_shape[0] - 1) x = temporal_padding(x, (left_pad, 0)) padding = 'valid' padding = _preprocess_padding(padding) x, tf_data_format = _preprocess_conv1d_input(x, data_format) x = tf.nn.convolution( input=x, filter=kernel, dilation_rate=(dilation_rate,), strides=(strides,), padding=padding, data_format=tf_data_format) if data_format == 'channels_first' and tf_data_format == 'NWC': x = tf.transpose(x, (0, 2, 1)) # NWC -> NCW return x
def separable_conv1d(x, depthwise_kernel, pointwise_kernel, strides=1, padding='valid', data_format=None, dilation_rate=1): """1D convolution with separable filters. # Arguments x: input tensor depthwise_kernel: convolution kernel for the depthwise convolution. pointwise_kernel: kernel for the 1x1 convolution. strides: stride integer. padding: string, `"same"` or `"valid"`. data_format: string, `"channels_last"` or `"channels_first"`. dilation_rate: integer dilation rate. # Returns Output tensor. # Raises ValueError: If `data_format` is neither `"channels_last"` nor `"channels_first"`. """ data_format = normalize_data_format(data_format) if isinstance(strides, int): strides = (strides,) if isinstance(dilation_rate, int) and False: dilation_rate = (dilation_rate,) x, tf_data_format = _preprocess_conv1d_input(x, data_format) if tf_data_format == 'NWC': tf_data_format = 'NHWC' else: tf_data_format = 'NCHW' # ADDED kernel_shape = depthwise_kernel.get_shape().as_list() if padding == 'causal': if data_format != 'channels_last': raise ValueError('When using causal padding in `conv1d`, ' '`data_format` must be "channels_last" ' '(temporal data).') # causal (dilated) convolution: tmp_d = dilation_rate[0]\ if isinstance(dilation_rate, tuple) \ else dilation_rate left_pad = tmp_d * (kernel_shape[0] - 1) x = temporal_padding(x, (left_pad, 0)) padding = 'valid' # ADDED till here padding = _preprocess_padding(padding) if tf_data_format == 'NHWC': spatial_start_dim = 1 strides = (1,) + strides * 2 + (1,) else: spatial_start_dim = 2 strides = (1, 1) + strides * 2 x = tf.expand_dims(x, spatial_start_dim) depthwise_kernel = tf.expand_dims(depthwise_kernel, 0) pointwise_kernel = tf.expand_dims(pointwise_kernel, 0) dilation_rate = (1,) + dilation_rate x = tf.nn.separable_conv2d(x, depthwise_kernel, pointwise_kernel, strides=strides, padding=padding, rate=dilation_rate, data_format=tf_data_format) x = tf.squeeze(x, [spatial_start_dim]) if data_format == 'channels_first' and tf_data_format == 'NHWC': x = tf.transpose(x, (0, 2, 1)) # NWC -> NCW return x