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 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 depth_to_space(input, scale, data_format=None): ''' Uses phase shift algorithm to convert channels/depth for spatial resolution ''' if data_format is None: data_format = image_data_format() data_format = data_format.lower() input = _preprocess_conv2d_input(input, data_format) out = tf.depth_to_space(input, scale) out = _postprocess_conv2d_output(out, data_format) return out
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 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 call(self, inputs, training=None): inputs = _preprocess_conv2d_input(inputs, self.data_format) outputs = tf.nn.depthwise_conv2d(inputs, self.depthwise_kernel, strides=self._strides, padding=self._padding, rate=self.dilation_rate, data_format=self._data_format) outputs = _postprocess_conv2d_output(outputs, self.data_format) 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