def conv2d(x, kernel, strides=(1, 1), border_mode='valid', filter_dilation=(1, 1)): """ Dimension is ordered by TH input shape: (samples, input_depth, rows, cols) TH kernel shape: (depth, input_depth, rows, cols) Parameters ---------- border_mode: string "same", "valid" or "full". Note ---- dim_ordering : tf-tensorflow (defaults), th-theano TH input shape: (samples, input_depth, conv_dim1, conv_dim2, conv_dim3) TF input shape: (samples, conv_dim1, conv_dim2, conv_dim3, input_depth) --- TH kernel shape: (out_depth, input_depth, kernel_dim1, kernel_dim2, kernel_dim3) TF kernel shape: (kernel_dim1, kernel_dim2, kernel_dim3, input_depth, out_depth) Only support float32 on CPU """ # store original information for calculating output_shape image_shape = get_shape(x) kernel_shape = get_shape(kernel) strides, border_mode, filter_dilation = __validate_strides_padding_dilation( strides, border_mode, filter_dilation, ndim=2) # convert to TF order is_float64 = False if 'float64' in x.dtype.name: # only conv in float32 x = tf.cast(x, 'float32') is_float64 = True if 'float64' in kernel.dtype.name: kernel = tf.cast(kernel, 'float32') if filter_dilation == (1, 1): x = tf.nn.conv2d(x, kernel, strides=(1, ) + strides + (1, ), padding=border_mode) else: assert filter_dilation[0] == filter_dilation[1] assert strides == (1, 1), 'Invalid strides for dilated convolution' x = tf.nn.atrous_conv2d(x, kernel, filter_dilation[0], padding=border_mode) # ====== estimate output shape ====== # if is_float64: x = tf.cast(x, 'float64') add_shape( x, get_conv_output_shape(image_shape, kernel_shape, border_mode, strides, filter_dilation)) return x
def conv2d(x, kernel, strides=(1, 1), border_mode='valid', filter_dilation=(1, 1)): """ Dimension is ordered by TH input shape: (samples, input_depth, rows, cols) TH kernel shape: (depth, input_depth, rows, cols) Parameters ---------- border_mode: string "same", "valid" or "full". Note ---- dim_ordering : tf-tensorflow (defaults), th-theano TH input shape: (samples, input_depth, conv_dim1, conv_dim2, conv_dim3) TF input shape: (samples, conv_dim1, conv_dim2, conv_dim3, input_depth) --- TH kernel shape: (out_depth, input_depth, kernel_dim1, kernel_dim2, kernel_dim3) TF kernel shape: (kernel_dim1, kernel_dim2, kernel_dim3, input_depth, out_depth) Warning ------- For "same" or "half" border_mode, the shape of output only equal to input if kernel shape is odd. """ image_shape = get_shape(x) kernel_shape = get_shape(kernel) if _any(i % 2 == 0 for i in kernel_shape[:-2]): print('[WARNING] Kernel shape %s contains even values, the output shape is ' 'different from the input shape in "same"-border_mode.' % str(kernel_shape[:-2])) strides, border_mode, filter_dilation = __validate_strides_padding_dilation( strides, border_mode, filter_dilation, ndim=2) # ====== convert input to theano format ====== # x = __img_theano_format(x) kernel = __ker_theano_format(kernel) conv_out = T.nnet.conv2d(x, kernel, border_mode=border_mode, subsample=strides, input_shape=(image_shape[0], image_shape[-1]) + image_shape[1:-1], filter_shape=(kernel_shape[-1], kernel_shape[-2]) + kernel_shape[:-2], filter_dilation=filter_dilation) # if border_mode == 'half': # if kernel_shape[2] % 2 == 0: # conv_out = conv_out[:, :, :(x.shape[2] + strides[0] - 1) // strides[0], :] # if kernel_shape[3] % 2 == 0: # conv_out = conv_out[:, :, :, :(x.shape[3] + strides[1] - 1) // strides[1]] # ====== estimate output shape ====== # conv_out = __img_tensorflow_format(conv_out) add_shape(conv_out, get_conv_output_shape(image_shape, kernel_shape, border_mode, strides, filter_dilation)) return conv_out
def _initialize(self, x): input_shape = K.get_shape(x) # ====== validate init arguments ====== # ndim = len(input_shape) - 2 self.ndim = ndim # padding if isinstance(self.pad, (tuple, list, int)): self.pad = as_tuple(self.pad, ndim, int) elif self.pad is None: self.pad = (0, ) * ndim # strides if self.strides is None: self.strides = (0, ) * ndim else: self.strides = as_tuple(self.strides, ndim, int) # dilation if self.dilation is None: self.dilation = (1, ) * ndim else: self.dilation = as_tuple(self.dilation, ndim, int) # filter size self.filter_size = as_tuple(self.filter_size, ndim, int) # ====== create config ====== # config = NNConfig(input_shape=input_shape) # TF kernel shape: (kernel_dim1, kernel_dim2, ..., input_depth, out_depth) kernel_shape = self.filter_size + (input_shape[-1], self.num_filters) # weights config.create_params(self.W_init, shape=kernel_shape, name='W', nnops=self, roles=WEIGHT) if self.b_init is not None: if self.untie_biases: output_shape = get_conv_output_shape( input_shape, kernel_shape, border_mode=self.pad, subsample=self.strides, filter_dilation=self.dilation) biases_shape = output_shape[1:] else: biases_shape = (self.num_filters, ) config.create_params(self.b_init, shape=biases_shape, name='b', nnops=self, roles=BIAS) return config
def conv3d(x, kernel, strides=(1, 1, 1), border_mode='valid', filter_dilation=(1, 1, 1)): """ Run on cuDNN if available. border_mode: string, "same" or "valid". Note ---- dim_ordering : tf-tensorflow (__img_theano_format(x) TH input shape: (samples, input_depth, conv_dim1, conv_dim2, conv_dim3) TF input shape: (samples, conv_dim1, conv_dim2, conv_dim3, input_depth) --- TH kernel shape: (out_depth, input_depth, kernel_dim1, kernel_dim2, kernel_dim3) TF kernel shape: (kernel_dim1, kernel_dim2, kernel_dim3, input_depth, out_depth) """ # get and convert volume_shape to theano format volume_shape = get_shape(x) # get and convert filter_shape to theano format kernel_shape = get_shape(kernel) if _any(i % 2 == 0 for i in kernel_shape[:-2]): print('[WARNING] Kernel shape %s contains even values, the output shape is ' 'different from the input shape in "same"-border_mode.' % str(kernel_shape[:-2])) strides, border_mode, filter_dilation = __validate_strides_padding_dilation( strides, border_mode, filter_dilation, ndim=3) # ====== convert input to theano format ====== # x = __img_theano_format(x) kernel = __ker_theano_format(kernel) # call convolution conv_out = T.nnet.conv3d(x, kernel, border_mode=border_mode, subsample=strides, input_shape=(volume_shape[0], volume_shape[-1]) + volume_shape[1:-1], filter_shape=(kernel_shape[-1], kernel_shape[-2]) + kernel_shape[:-2], filter_dilation=filter_dilation) # if border_mode == 'half': # if kernel_shape[2] % 2 == 0: # conv_out = conv_out[:, :, :(x.shape[2] + strides[0] - 1) // strides[0], :, :] # if kernel_shape[3] % 2 == 0: # conv_out = conv_out[:, :, :, :(x.shape[3] + strides[1] - 1) // strides[1], :] # if kernel_shape[4] % 2 == 0: # conv_out = conv_out[:, :, :, :, :(x.shape[4] + strides[2] - 1) // strides[2]] # back to theano form # convert back to tensorflow shape conv_out = __img_tensorflow_format(conv_out) # infer output shape output_shape = get_conv_output_shape(volume_shape, kernel_shape, border_mode, strides, filter_dilation) add_shape(conv_out, output_shape) return conv_out
def conv3d(x, kernel, strides=(1, 1, 1), border_mode='valid', filter_dilation=(1, 1, 1)): """ Note ---- dim_ordering : tf-tensorflow (defaults), th-theano TH input shape: (samples, input_depth, conv_dim1, conv_dim2, conv_dim3) TF input shape: (samples, conv_dim1, conv_dim2, conv_dim3, input_depth) --- TH kernel shape: (out_depth, input_depth, kernel_dim1, kernel_dim2, kernel_dim3) TF kernel shape: (kernel_dim1, kernel_dim2, kernel_dim3, input_depth, out_depth) """ volume_shape = get_shape(x) kernel_shape = get_shape(kernel) strides, border_mode, filter_dilation = __validate_strides_padding_dilation( strides, border_mode, filter_dilation, ndim=3) # no dilation for tensorflow if filter_dilation != (1, 1, 1): raise Exception("tensorflow has not supported 3D-dilation yet.") # convert to TF order is_float64 = False if 'float64' in x.dtype.name: # only conv in float32 x = tf.cast(x, 'float32') is_float64 = True if 'float64' in kernel.dtype.name: kernel = tf.cast(kernel, 'float32') # ====== estimate output shape ====== # if is_float64: x = tf.cast(x, 'float64') x = tf.nn.conv3d(x, kernel, (1, ) + strides + (1, ), border_mode) add_shape( x, get_conv_output_shape(volume_shape, kernel_shape, border_mode, strides, filter_dilation)) return x