def deconv3d(x, kernel, output_shape, strides=(1, 1, 1), border_mode='valid', dim_ordering='default', image_shape=None, filter_shape=None): '''3D deconvolution (transposed convolution). # Arguments kernel: kernel tensor. output_shape: desired dimensions of output. strides: strides tuple. border_mode: string, "same" or "valid". dim_ordering: "tf" or "th". Whether to use Theano or TensorFlow dimension ordering in inputs/kernels/ouputs. ''' flip_filters = False if dim_ordering == 'default': dim_ordering = image_dim_ordering() if dim_ordering not in {'th', 'tf'}: raise ValueError('Unknown dim_ordering ' + str(dim_ordering)) if dim_ordering == 'tf': output_shape = (output_shape[0], output_shape[4], output_shape[1], output_shape[2], output_shape[3]) x = _preprocess_conv3d_input(x, dim_ordering) kernel = _preprocess_conv3d_kernel(kernel, dim_ordering) kernel = kernel.dimshuffle((1, 0, 2, 3, 4)) th_border_mode = _preprocess_border_mode(border_mode) if hasattr(kernel, '_keras_shape'): kernel_shape = kernel._keras_shape else: # Will only work if `kernel` is a shared variable. kernel_shape = kernel.eval().shape filter_shape = _preprocess_conv3d_filter_shape(dim_ordering, filter_shape) filter_shape = tuple(filter_shape[i] for i in (1, 0, 2, 3, 4)) conv_out = T.nnet.abstract_conv.conv3d_grad_wrt_inputs( x, kernel, output_shape, filter_shape=filter_shape, border_mode=th_border_mode, subsample=strides, filter_flip=not flip_filters) conv_out = _postprocess_conv3d_output(conv_out, x, border_mode, kernel_shape, strides, dim_ordering) return conv_out
def deconv3d(x, kernel, output_shape, strides=(1, 1, 1), padding='valid', data_format=None, filter_shape=None): '''3D deconvolution (transposed convolution). # Arguments kernel: kernel tensor. output_shape: desired dimensions of output. strides: strides tuple. padding: string, "same" or "valid". data_format: "channels_last" or "channels_first". Whether to use Theano or TensorFlow dimension ordering in inputs/kernels/ouputs. ''' flip_filters = False if data_format is None: data_format = image_data_format() if data_format not in {'channels_first', 'channels_last'}: raise ValueError('Unknown data_format: ' + str(data_format)) if data_format == 'channels_last': output_shape = (output_shape[0], output_shape[4], output_shape[1], output_shape[2], output_shape[3]) x = _preprocess_conv3d_input(x, data_format) kernel = _preprocess_conv3d_kernel(kernel, data_format) kernel = kernel.dimshuffle((1, 0, 2, 3, 4)) th_padding = _preprocess_padding(padding) if hasattr(kernel, '_keras_shape'): kernel_shape = kernel._keras_shape else: # Will only work if `kernel` is a shared variable. kernel_shape = kernel.eval().shape filter_shape = _preprocess_conv3d_filter_shape(filter_shape, data_format) filter_shape = tuple(filter_shape[i] for i in (1, 0, 2, 3, 4)) conv_out = T.nnet.abstract_conv.conv3d_grad_wrt_inputs( x, kernel, output_shape, filter_shape=filter_shape, border_mode=th_padding, subsample=strides, filter_flip=not flip_filters) conv_out = _postprocess_conv3d_output(conv_out, x, padding, kernel_shape, strides, data_format) return conv_out