def call(self, inputs, training=None):
        inputs = _preprocess_conv3d_input(inputs, self.data_format)

        if self.data_format == 'channels_last':
            dilation = (1, ) + self.dilation_rate + (1, )
        else:
            dilation = self.dilation_rate + (1, ) + (1, )

        inputs = tf.split(inputs[0],
                          self.input_dim // self.group_size,
                          axis=1 if self._data_format == 'NCDHW' else 4)
        outputs = tf.concat([
            tf.nn.conv3d(inp,
                         self.depthwise_kernel,
                         strides=self._strides,
                         padding=self._padding,
                         dilations=dilation,
                         data_format=self._data_format) for inp in inputs
        ],
                            axis=1 if format == 'NCDHW' else 4)

        if self.bias is not None:
            outputs = K.bias_add(outputs,
                                 self.bias,
                                 data_format=self.data_format)

        if self.activation is not None:
            return self.activation(outputs)

        return outputs
Пример #2
0
    def call(self, inputs, training=None):
        inputs = _preprocess_conv3d_input(inputs, self.data_format)

        if self.data_format == 'channels_last':
            dilation = (1,) + tuple(self.dilation_rate) + (1,)
        else:
            dilation = tuple(self.dilation_rate) + (1,) + (1,)

        if self._data_format == 'NCDHW':
            outputs = tf.concat(
                [tf.nn.conv3d(inputs[0][:, i:i+self.input_dim//self.groups, :, :, :], self.depthwise_kernel[:, :, :, i:i+self.input_dim//self.groups, :],
                    strides=self._strides,
                    padding=self._padding,
                    dilations=dilation,
                    data_format=self._data_format) for i in range(0, self.input_dim, self.input_dim//self.groups)], axis=1)

        else:
            outputs = tf.concat(
                [tf.nn.conv3d(inputs[0][:, :, :, :, i:i+self.input_dim//self.groups], self.depthwise_kernel[:, :, :, i:i+self.input_dim//self.groups, :],
                    strides=self._strides,
                    padding=self._padding,
                    dilations=dilation,
                    data_format=self._data_format) for i in range(0, self.input_dim, self.input_dim//self.groups)], axis=-1)

        if self.bias is not None:
            outputs = K.bias_add(
                outputs,
                self.bias,
                data_format=self.data_format)

        if self.activation is not None:
            return self.activation(outputs)

        return outputs
Пример #3
0
    def call(self, inputs, training=None):
        inputs = _preprocess_conv3d_input(inputs, self.data_format)

        if self.data_format == 'channels_last':
            dilation = (1, ) + self.dilation_rate + (1, )
        else:
            dilation = self.dilation_rate + (1, ) + (1, )

        inputs = tf.split(inputs[0], self.group_num, axis=self.channel_axis)
        outputs = tf.concat([
            tf.nn.conv3d(inp,
                         self.kernel[i, :, :, :, :, :],
                         strides=self._strides,
                         padding=self._padding,
                         dilations=dilation,
                         data_format=self._data_format)
            for i, inp in enumerate(inputs)
        ],
                            axis=self.channel_axis)

        if self.bias is not None:
            outputs = K.bias_add(outputs,
                                 self.bias,
                                 data_format=self.data_format)

        if self.activation is not None:
            return self.activation(outputs)

        return outputs
Пример #4
0
def deconv3d(x,
             kernel,
             output_shape,
             strides=(1, 1, 1),
             border_mode='valid',
             dim_ordering='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.
        border_mode: string, "same" or "valid".
        dim_ordering: "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 `dim_ordering` is neither `tf` or `th`.
    '''
    if dim_ordering == 'default':
        dim_ordering = image_dim_ordering()
    if dim_ordering not in {'th', 'tf'}:
        raise ValueError('Unknown dim_ordering ' + str(dim_ordering))

    x = _preprocess_conv3d_input(x, dim_ordering)
    output_shape = _preprocess_deconv_output_shape(x, output_shape,
                                                   dim_ordering)
    kernel = _preprocess_conv3d_kernel(kernel, dim_ordering)
    kernel = tf.transpose(kernel, (0, 1, 2, 4, 3))
    padding = _preprocess_border_mode(border_mode)
    strides = (1, ) + strides + (1, )

    x = tf.nn.conv3d_transpose(x,
                               kernel,
                               output_shape,
                               strides,
                               padding=padding)
    return _postprocess_conv3d_output(x, dim_ordering)
    def call(self, inputs, training=None):
        inputs = _preprocess_conv3d_input(inputs, self.data_format)

        if self.data_format == 'channels_last':
            b, d, h, w, c = inputs[0].shape
            channels_l = [
                tf.expand_dims(inputs[0][:, :, :, :, i], -1)
                for i in range(0, c)
            ]
            dilation = (1, ) + self.dilation_rate + (1, )
        else:
            b, c, d, h, w = inputs[0].shape
            channels_l = [
                tf.expand_dims(inputs[0][:, i, :, :, :], 1)
                for i in range(0, c)
            ]
            dilation = self.dilation_rate + (1, ) + (1, )

        outputs = tf.convert_to_tensor([
            tf.nn.conv3d(inp_c,
                         self.depthwise_kernel,
                         strides=self._strides,
                         padding=self._padding,
                         dilations=dilation,
                         data_format=self._data_format) for inp_c in channels_l
        ])

        # Add original channels dim at the end of the tensor
        outputs = tf.transpose(outputs, (1, 2, 3, 4, 5, 0))
        # Convert to tensor [batch, depth, height, width, new_channels/depth_multiplier * original_channels]
        shape = outputs.get_shape().as_list()

        outputs = tf.reshape(
            outputs, [-1, shape[1], shape[2], shape[3], shape[4] * shape[5]])
        print(outputs.shape)

        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
Пример #6
0
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 = 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)
Пример #7
0
    def call(self, inputs, training=None):
        inputs = _preprocess_conv3d_input(inputs, self.data_format)

        if self.data_format == 'channels_last':
            dilation = (1,) + self.dilation_rate + (1,)
        else:
            dilation = self.dilation_rate + (1,) + (1,)


        outputs = tf.nn.conv3d(inputs[0], self.depthwise_kernel, strides=self._strides, padding= self._padding, dilations = dilation, data_format=self._data_format)

        if self.bias is not None:
            outputs = K.bias_add(
                outputs,
                self.bias,
                data_format=self.data_format)

        if self.activation is not None:
            return self.activation(outputs)

        return outputs