otx.algo.modules#
Common module implementations.
Functions
|
Build activation layer. |
|
Build padding layer. |
|
Build normalization layer. |
Classes
|
A conv2d block that bundles conv/norm/activation layers. |
|
A conv3d block that bundles conv/norm/activation layers. |
|
Depthwise separable convolution module. |
|
Copy and modified from facebookresearch/detr. |
- class otx.algo.modules.Conv2dModule(in_channels: int | tuple[int, ...], out_channels: int, kernel_size: int | tuple[int, ...], stride: int | tuple[int, ...] = 1, padding: int | tuple[int, ...] = 0, dilation: int | tuple[int, int] = 1, groups: int = 1, bias: bool | str = 'auto', normalization: tuple[str, ~torch.nn.modules.module.Module] | ~torch.nn.modules.module.Module | None = None, activation: ~typing.Callable[[...], ~torch.nn.modules.module.Module] | ~torch.nn.modules.module.Module | None = <class 'torch.nn.modules.activation.ReLU'>, inplace: bool = True, with_spectral_norm: bool = False, padding_mode: str = 'zeros')[source]#
Bases:
ConvModule
A conv2d block that bundles conv/norm/activation layers.
Initialize internal Module state, shared by both nn.Module and ScriptModule.
- class otx.algo.modules.Conv3dModule(in_channels: int | tuple[int, ...], out_channels: int, kernel_size: int | tuple[int, ...], stride: int | tuple[int, ...] = 1, padding: int | tuple[int, ...] = 0, dilation: int | tuple[int, int] = 1, groups: int = 1, bias: bool | str = 'auto', normalization: tuple[str, ~torch.nn.modules.module.Module] | ~torch.nn.modules.module.Module | None = None, activation: ~typing.Callable[[...], ~torch.nn.modules.module.Module] | ~torch.nn.modules.module.Module | None = <class 'torch.nn.modules.activation.ReLU'>, inplace: bool = True, with_spectral_norm: bool = False, padding_mode: str = 'zeros')[source]#
Bases:
ConvModule
A conv3d block that bundles conv/norm/activation layers.
Initialize internal Module state, shared by both nn.Module and ScriptModule.
- class otx.algo.modules.DepthwiseSeparableConvModule(in_channels: int, out_channels: int, kernel_size: int | tuple[int, int], stride: int | tuple[int, int] = 1, padding: int | tuple[int, int] = 0, dilation: int | tuple[int, int] = 1, normalization: tuple[str, ~torch.nn.modules.module.Module] | None = None, activation: ~torch.nn.modules.module.Module | None = <class 'torch.nn.modules.activation.ReLU'>, dw_normalization: tuple[str, ~torch.nn.modules.module.Module] | None = None, dw_activation: ~torch.nn.modules.module.Module | None = None, pw_normalization: tuple[str, ~torch.nn.modules.module.Module] | None = None, pw_activation: ~torch.nn.modules.module.Module | None = None, **kwargs)[source]#
Bases:
Module
Depthwise separable convolution module.
See https://arxiv.org/pdf/1704.04861.pdf for details.
This module can replace a ConvModule with the conv block replaced by two conv block: depthwise conv block and pointwise conv block. The depthwise conv block contains depthwise-conv/norm/activation layers. The pointwise conv block contains pointwise-conv/norm/activation layers. It should be noted that there will be norm/activation layer in the depthwise conv block if normalization and activation are specified.
- Parameters:
in_channels (int) – Number of channels in the input feature map. Same as that in
nn._ConvNd
.out_channels (int) – Number of channels produced by the convolution. Same as that in
nn._ConvNd
.kernel_size (int | tuple[int]) – Size of the convolving kernel. Same as that in
nn._ConvNd
.stride (int | tuple[int]) – Stride of the convolution. Same as that in
nn._ConvNd
. Default: 1.padding (int | tuple[int]) – Zero-padding added to both sides of the input. Same as that in
nn._ConvNd
. Default: 0.dilation (int | tuple[int]) – Spacing between kernel elements. Same as that in
nn._ConvNd
. Default: 1.normalization (tuple[str, nn.Module] | None) – Normalization layer module for both depthwise ConvModule and pointwise ConvModule. Defaults to None.
activation (nn.Module | None) – Activation layer module for both depthwise ConvModule and pointwise ConvModule. Defaults to
nn.ReLU
.dw_normalization (tuple[str, nn.Module] | None) – Normalization layer module of depthwise ConvModule. If it is None, it will be the same as
normalization
. If it is already instanstiated to nn.Module, it’s parameters will be realigned with in_channels. Defaults to None.dw_activation (nn.Module | None) – Activation layer module of depthwise ConvModule. If it is None, it will be the same as
activation
. Defaults to None.pw_normalization (tuple[str, nn.Module] | None) – Normalization layer module of pointwise ConvModule. If it is None, it will be the same as
normalization
. If it is already instanstiated to nn.Module, it’s parameters will be realigned with in_channels. Defaults to None.pw_activation (nn.Module | None) – Activation layer module of pointwise ConvModule. If it is None, it will be the same as
activation
. Defaults to None.kwargs (optional) – Other shared arguments for depthwise and pointwise ConvModule. See ConvModule for ref.
Initialize internal Module state, shared by both nn.Module and ScriptModule.
- class otx.algo.modules.FrozenBatchNorm2d(num_features: int, eps: float = 1e-05)[source]#
Bases:
Module
Copy and modified from facebookresearch/detr.
BatchNorm2d where the batch statistics and the affine parameters are fixed. Copy-paste from torchvision.misc.ops with added eps before rqsrt, without which any other models than torchvision.models.resnet[18,34,50,101] produce nans.
Initialize FrozenBatchNorm2d.
- Parameters:
- otx.algo.modules.build_activation_layer(activation: Callable[[...], Module] | Module | None, inplace: bool = True) Module | None [source]#
Build activation layer.
- Parameters:
activation (Callable[..., nn.Module] | nn.Module | None) – Activation layer module. If None or pre-instanstiated module is given, return it as is. If callable is given, create the layer.
inplace (bool) – Whether to use inplace mode for activation. Default: True.
- Returns:
Created activation layer.
- Return type:
nn.Module
- otx.algo.modules.build_norm_layer(normalization: Callable[[...], Module] | tuple[str, Module] | Module | None, num_features: int, postfix: int | str = '', layer_name: str | None = None, requires_grad: bool = True, eps: float = 1e-05, **kwargs) tuple[str, Module] [source]#
Build normalization layer.
- Parameters:
normalization (Callable[..., nn.Module] | tuple[str, nn.Module] | nn.Module | None) – Normalization layer module. If tuple or None is given, return it as is. If nn.Module is given, return it with empty name string. If callable is given, create the layer.
num_features (int) – Number of input channels.
postfix (int | str) – The postfix to be appended into norm abbreviation to create named layer.
layer_name (str | None) – The name of the layer. Defaults to None.
requires_grad (bool) – Whether stop gradient updates. Defaults to True.
eps (float) – A value added to the denominator for numerical stability. Defaults to 1e-5.
- Returns:
The first element is the layer name consisting of abbreviation and postfix, e.g., bn1, gn. The second element is the created norm layer.
- Return type:
- otx.algo.modules.build_padding_layer(cfg: dict, *args, **kwargs) Module [source]#
Build padding layer.
- Parameters:
cfg (dict) – The padding layer config, which should contain: - type (str): Layer type. - layer args: Args needed to instantiate a padding layer.
- Returns:
Created padding layer.
- Return type:
nn.Module