MCPcopy Create free account
hub / github.com/MegEngine/MegEngine / deformable_conv2d

Function deformable_conv2d

imperative/python/megengine/functional/nn.py:449–520  ·  view source on GitHub ↗

r"""Deformable Convolution. Args: inp: input feature map. weight: convolution kernel. weight usually has shape ``(out_channels, in_channels, height, width)``. offset: input offset to kernel, channel of this tensor should match the deformable settings.

(
    inp: Tensor,
    weight: Tensor,
    offset: Tensor,
    mask: Tensor,
    bias: Optional[Tensor] = None,
    stride: Union[int, Tuple[int, int]] = 1,
    padding: Union[int, Tuple[int, int]] = 0,
    dilation: Union[int, Tuple[int, int]] = 1,
    groups: int = 1,
    conv_mode="cross_correlation",
    compute_mode="default",
)

Source from the content-addressed store, hash-verified

447
448
449def deformable_conv2d(
450 inp: Tensor,
451 weight: Tensor,
452 offset: Tensor,
453 mask: Tensor,
454 bias: Optional[Tensor] = None,
455 stride: Union[int, Tuple[int, int]] = 1,
456 padding: Union[int, Tuple[int, int]] = 0,
457 dilation: Union[int, Tuple[int, int]] = 1,
458 groups: int = 1,
459 conv_mode="cross_correlation",
460 compute_mode="default",
461) -> Tensor:
462 r"""Deformable Convolution.
463
464 Args:
465 inp: input feature map.
466 weight: convolution kernel.
467 weight usually has shape ``(out_channels, in_channels, height, width)``.
468 offset: input offset to kernel, channel of this tensor should match the deformable settings.
469 mask: input mask to kernel, channel of this tensor should match the deformable settings.
470 bias: bias added to the result of convolution (if given).
471 stride: stride of the 2D convolution operation. Default: 1
472 padding: size of the paddings added to the input on both sides of its
473 spatial dimensions. Only zero-padding is supported. Default: 0
474 dilation: dilation of the 2D convolution operation. Default: 1
475 groups: number of groups into which the input and output channels are divided,
476 so as to perform a ``grouped convolution``. When ``groups`` is not 1,
477 ``in_channels`` and ``out_channels`` must be divisible by groups,
478 and the shape of weight should be ``(groups, out_channel // groups,
479 in_channels // groups, height, width)``. Default: 1
480 conv_mode: supports "cross_correlation". Default: "cross_correlation"
481 compute_mode: when set to "default", no special requirements will be
482 placed on the precision of intermediate results. When set to "float32",
483 "float32" would be used for accumulator and intermediate result, but only
484 effective when input and output are of float16 dtype.
485
486 Returns:
487 output tensor.
488 """
489 assert (
490 conv_mode.lower() == "cross_correlation"
491 or conv_mode.name == "CROSS_CORRELATION"
492 )
493 if amp._enabled:
494 inp, weight, offset, mask, bias = cast_tensors(inp, weight, offset, mask, bias)
495 else:
496 offset = offset.astype("float32")
497 mask = mask.astype("float32")
498
499 stride_h, stride_w = expand_hw(stride)
500 pad_h, pad_w = expand_hw(padding)
501 dilate_h, dilate_w = expand_hw(dilation)
502
503 compute_mode = _config._get_actual_op_param(compute_mode, _config.__compute_mode)
504 sparse_type = "dense" if groups == 1 else "group"
505 op = builtin.DeformableConv(
506 stride_h=stride_h,

Callers 1

calc_convMethod · 0.85

Calls 5

cast_tensorsFunction · 0.85
expand_hwFunction · 0.85
get_execution_strategyFunction · 0.70
applyFunction · 0.50
astypeMethod · 0.45

Tested by

no test coverage detected