Пример #1
0
def conv2d(input,
           filter,
           strides,
           padding,
           dilation_rate=None,
           data_format='NHWC',
           name=None,
           **kwargs):
    """Compute 2D convolution according to the given 4D ``input`` and ``filter``.

    For **NHWC** format, filter should be as ``[filter_height, filter_width, in_channels, out_channels]``.

    For **NCHW** format, filter should be as ``[out_channels, in_channels, filter_height, filter_width]``.

    Parameters
    ----------
    input : Tensor
        The input tensor.
    filter : Tensor
        The filter tensor.
    strides : list of int
        The strides with length 4.
    padding : str
        The padding algorithm. ``VALID`` or ``SAME``.
    dilation_rate : list of int or None
        The dilation rates with with length 4.
    data_format : str
        The data format. ``NHWC`` or ``NCHW``.
    name : str
        The optional name for this operator.

    Returns
    -------
    Tensor
        The output tensor.

    """
    if filter.shape is None:
        raise ValueError('filter must have a valid shape.')
    else:
        if len(filter.shape) != 4:
            raise ValueError('filter must be a 4D Tensor.')
    if len(strides) != 4:
        raise ValueError('strides must be a list with length 4.')
    if dilation_rate is not None:
        if len(dilation_rate) != 4:
            raise ValueError(' dilation_rate must be a list with length 4.')

    if data_format == 'NHWC':
        output = ops.Conv2d(
            [input, filter],
            num_output=filter.shape[3],
            kernel_size=filter.shape[0:2],
            stride=strides[1:3],
            dilation=dilation_rate[1:3] if dilation_rate is not None else 1,
            padding=padding,
            data_format=data_format)
        return output
    elif data_format == 'NCHW':
        output = ops.Conv2d(
            [input, filter],
            num_output=filter.shape[0],
            kernel_size=filter.shape[2:4],
            stride=strides[2:4],
            dilation=dilation_rate[2:4] if dilation_rate is not None else 1,
            padding=padding,
            data_format=data_format)
        return output
    else:
        raise ValueError('Unknown data format: {}'.format(data_format))
Пример #2
0
 def Setup(self, bottom):
     super(ConvolutionLayer, self).Setup(bottom)
     return ops.Conv2d(bottom + [blob['data'] for blob in self._blobs], **self._param)
Пример #3
0
 def LayerSetup(self, bottom):
     inputs = [bottom] + [blob['data'] for blob in self._blobs]
     return _ops.Conv2d(inputs, **self.arguments)