mindspore.ops.deformable_conv2d

View Source On Gitee
mindspore.ops.deformable_conv2d(x, weight, offsets, kernel_size, strides, padding, bias=None, dilations=(1, 1, 1, 1), groups=1, deformable_groups=1, modulated=True)[source]

Given 4D tensor inputs x, weight and offsets, compute a 2D deformable convolution. The deformable convolution operation can be expressed as follow:

Deformable Convolution v1:

y(p)=k=1Kwkx(p+pk+Δpk)

Deformable Convolution v2:

y(p)=k=1Kwkx(p+pk+Δpk)Δmk

Where Δpk and Δmk are the learnable offset and modulation scalar for the k-th location. For details, please refer to Deformable ConvNets v2: More Deformable, Better Results and Deformable Convolutional Networks.

Parameters
  • x (Tensor) – A 4D tensor of input image. With the format "NCHW", the shape is (N,Cin,Hin,Win). Dtype: float16 or float32.

  • weight (Tensor) – A 4D tensor of learnable filters. Must have the same type as x. The shape is (Cout,Cin/groups,Hf,Wf).

  • offsets (Tensor) – A 4D tensor of x-y coordinates offset and mask. With the format "NCHW", the shape is (batch,3deformable_groupsHfWf,Hout,Wout). Note the C dimension is stored in the order of (offset_x, offset_y, mask). Must have the same type as x.

  • kernel_size (tuple[int]) – A tuple of 2 integers. The size of kernel.

  • strides (tuple[int]) – A tuple of 4 integers. The stride of the sliding window for each dimension of input. The dimension order is interpreted according to the data format of x. The N and C dimensions must be set to 1.

  • padding (tuple[int]) – A tuple of 4 integers. The number of pixels to add to each (top, bottom, left, right) side of the input.

  • bias (Tensor, optional) – An 1D tensor of additive biases to the filter outputs. The shape is (Cout). Default: None .

  • dilations (tuple[int], optional) – A tuple of 4 integers. The dilation factor for each dimension of input. The dimension order is interpreted according to the data format of x. The N and C dimensions must be set to 1. Default: (1, 1, 1, 1) .

  • groups (int, optional) – An integer of type int32. The number of blocked connections from input channels to output channels. In_channels and out_channels must both be divisible by groups. Default: 1 .

  • deformable_groups (int, optional) – An integer of type int32. The number of deformable group partitions. In_channels must be divisible by deformable_groups. Default: 1 .

  • modulated (bool, optional) – Specifies version of DeformableConv2D, True means v2, False means v1, currently only supports v2. Default: True .

Returns

Tensor, A 4D Tensor of output feature map. With the same type as x. With the format "NCHW", the shape is (N,Cout,Hout,Wout).

Hout=Hin+padding[0]+padding[1](Hf1)×dilations[2]1stride[0]+1Wout=Win+padding[2]+padding[3](Wf1)×dilations[3]1stride[1]+1

Raises
  • TypeError – If strides, padding, kernel_size or dilations is not a tuple with integer elements.

  • TypeError – If modulated is not a bool.

  • ValueError – If the tuple size of strides, padding, kernel_size or dilations is not expected.

  • ValueError – The N or C dimensions of strides or dilations is not set to 1.

  • ValueError – If modulated is not set to True.

Warning

This is an experimental API that is subject to change or deletion.

Supported Platforms:

Ascend GPU CPU

Examples

>>> import numpy as np
>>> from mindspore import Tensor, ops
>>> from mindspore import dtype as mstype
>>> x = Tensor(np.ones((4, 3, 10, 10)), mstype.float32)
>>> kh, kw = 3, 3
>>> weight = Tensor(np.ones((5, 3, kh, kw)), mstype.float32)
>>> offsets = Tensor(np.ones((4, 3 * kh * kw, 8, 8)), mstype.float32)
>>> output = ops.deformable_conv2d(x, weight, offsets, (kh, kw), (1, 1, 1, 1), (0, 0, 0, 0))
>>> print(output.shape)
(4, 5, 8, 8)