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 _normalize_data_format(data_format): if data_format is None: data_format = image_data_format() if data_format == 'channels_last': return 'nxc' if data_format == 'channels_first': return 'ncx' if data_format in ['nxc', 'ncx']: return data_format raise ValueError("Unrecognized data_format '{}'".format(data_format))
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
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() if data_format == 'channels_first': data_format = 'NCHW' else: data_format = 'NHWC' data_format = data_format.lower() out = tf.depth_to_space(input, scale, data_format=data_format) return out
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) b, k, row, col = input.shape out_channels = k // (scale**2) x = T.reshape(input, (b, scale, scale, out_channels, row, col)) x = T.transpose(x, (0, 3, 4, 1, 5, 2)) out = T.reshape(x, (b, out_channels, row * scale, col * scale)) out = _postprocess_conv2d_output(out, input, None, None, None, data_format) return out
def _bias_add(X, data_format): x, bias = X from keras.backend import image_data_format, ndim, reshape 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 ndim(bias) != 1 and ndim(bias) != ndim(x) - 1: raise ValueError('Unexpected bias dimensions %d, ' 'expect to be 1 or %d dimensions' % (ndim(bias), ndim(x) - 1)) bias_shape = tuple(bias.size()) ndim_x = len(x.size()) ndim_bias = len(bias_shape) if ndim_x == 5: if data_format == 'channels_first': if ndim_bias == 1: bias = reshape(bias, (1, bias_shape[0], 1, 1, 1)) else: bias = reshape(bias, (1, bias_shape[3]) + bias_shape[:3]) elif data_format == 'channels_last': if ndim_bias == 1: bias = reshape(bias, (1, 1, 1, 1, bias_shape[0])) else: bias = reshape(bias, (1,) + bias_shape) elif ndim_x == 4: if data_format == 'channels_first': if ndim_bias == 1: bias = reshape(bias, (1, bias_shape[0], 1, 1)) else: bias = reshape(bias, (1, bias_shape[2]) + bias_shape[:2]) elif data_format == 'channels_last': if ndim_bias == 1: bias = reshape(bias, (1, 1, 1, bias_shape[0])) else: bias = reshape(bias, (1,) + bias_shape) elif ndim_x == 3: if data_format == 'channels_first': if ndim_bias == 1: bias = reshape(bias, (1, bias_shape[0], 1)) else: bias = reshape(bias, (1, bias_shape[1], bias_shape[0])) elif data_format == 'channels_last': if ndim_bias == 1: bias = reshape(bias, (1, 1, bias_shape[0])) else: bias = reshape(bias, (1,) + bias_shape) return x.add(bias.expand_as(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 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) b, k, row, col = input.shape output_shape = (b, k // (scale ** 2), row * scale, col * scale) out = T.zeros(output_shape) r = scale for y, x in itertools.product(range(scale), repeat=2): out = T.inc_subtensor(out[:, :, y::r, x::r], input[:, r * y + x:: r * r, :, :]) out = _postprocess_conv2d_output(out, input, None, None, None, data_format) return out
def bias_add(x, bias, data_format=None): if data_format is None: data_format = image_data_format() if data_format not in _CONV_DATA_FORMAT: raise PlaidMLKerasException( 'Unrecognized data_format given to bias_add: "{}"; '.format(data_format) + 'only "channels_first" and "channels_last" recognized.') if ndim(x) > 2: if data_format == 'channels_first': try: bias_dims = bias.tensor.shape.dims except AttributeError: bias_dims = bias.shape x += reshape(bias, (1, bias_dims[0]) + (1,) * (ndim(x) - 2)) elif data_format == 'channels_last': x += bias else: x += bias return x
def bias_add(x, bias, data_format=None): logger.debug('bias_add(x: {}, bias: {}, data_format: {})'.format(x, bias, data_format)) if data_format is None: data_format = image_data_format() if data_format not in _CONV_DATA_FORMAT: raise PlaidMLKerasException( "Unrecognized data_format given to bias_add: '{}'; ".format(data_format) + "only 'channels_first' and 'channels_last' recognized.") if ndim(x) > 2: if data_format == 'channels_first': try: bias_dims = bias.tensor.shape.dims except AttributeError: bias_dims = bias.shape x += reshape(bias, (1, bias_dims[0]) + (1,) * (ndim(x) - 2)) elif data_format == 'channels_last': x += bias else: x += bias return x
def depth_to_space(input, scale, data_format=None): """ Uses phase shift algorithm to convert channels/depth for spatial resolution. # Arguments input: Input tensor scale: n `int` that is `>= 2`. The size of the spatial block. data_format: 'channels_first' or 'channels_last'. Whether to use Theano or TensorFlow dimension ordering in inputs/kernels/ouputs. # Returns TODO (PR welcome): Filling this section. """ 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 spatial_reflection_2d_padding(x, padding=((1, 1), (1, 1)), data_format=None): """ Pad the 2nd and 3rd dimensions of a 4D tensor. :param x: Input tensor :param padding: Shape of padding to use :param data_format: Tensorflow vs Theano convention ('channels_last', 'channels_first') :return: Tensorflow tensor """ assert len(padding) == 2 assert len(padding[0]) == 2 assert len(padding[1]) == 2 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_first': pattern = [[0, 0], [0, 0], list(padding[0]), list(padding[1])] else: pattern = [[0, 0], list(padding[0]), list(padding[1]), [0, 0]] return tf.pad(x, pattern, "REFLECT")