CNN#

pydantic model vision_architectures.blocks.cnn.CNNBlockConfig[source]#

Bases: CustomBaseModel

Show JSON schema
{
   "title": "CNNBlockConfig",
   "type": "object",
   "properties": {
      "in_channels": {
         "title": "In Channels",
         "type": "integer"
      },
      "out_channels": {
         "title": "Out Channels",
         "type": "integer"
      },
      "kernel_size": {
         "anyOf": [
            {
               "type": "integer"
            },
            {
               "items": {
                  "type": "integer"
               },
               "type": "array"
            }
         ],
         "title": "Kernel Size"
      },
      "padding": {
         "anyOf": [
            {
               "type": "integer"
            },
            {
               "items": {
                  "type": "integer"
               },
               "type": "array"
            },
            {
               "type": "string"
            }
         ],
         "default": "same",
         "title": "Padding"
      },
      "stride": {
         "default": 1,
         "title": "Stride",
         "type": "integer"
      },
      "conv_kwargs": {
         "additionalProperties": true,
         "default": {},
         "title": "Conv Kwargs",
         "type": "object"
      },
      "transposed": {
         "default": false,
         "description": "Whether to perform ConvTranspose instead of Conv",
         "title": "Transposed",
         "type": "boolean"
      },
      "normalization": {
         "anyOf": [
            {
               "type": "string"
            },
            {
               "type": "null"
            }
         ],
         "default": "batchnorm3d",
         "title": "Normalization"
      },
      "normalization_pre_args": {
         "default": [],
         "items": {},
         "title": "Normalization Pre Args",
         "type": "array"
      },
      "normalization_post_args": {
         "default": [],
         "items": {},
         "title": "Normalization Post Args",
         "type": "array"
      },
      "normalization_kwargs": {
         "additionalProperties": true,
         "default": {},
         "title": "Normalization Kwargs",
         "type": "object"
      },
      "activation": {
         "anyOf": [
            {
               "type": "string"
            },
            {
               "type": "null"
            }
         ],
         "default": "relu",
         "title": "Activation"
      },
      "activation_kwargs": {
         "additionalProperties": true,
         "default": {},
         "title": "Activation Kwargs",
         "type": "object"
      },
      "sequence": {
         "default": "CNA",
         "enum": [
            "C",
            "AC",
            "CA",
            "CD",
            "CN",
            "DC",
            "NC",
            "ACD",
            "ACN",
            "ADC",
            "ANC",
            "CAD",
            "CAN",
            "CDA",
            "CDN",
            "CNA",
            "CND",
            "DAC",
            "DCA",
            "DCN",
            "DNC",
            "NAC",
            "NCA",
            "NCD",
            "NDC",
            "ACDN",
            "ACND",
            "ADCN",
            "ADNC",
            "ANCD",
            "ANDC",
            "CADN",
            "CAND",
            "CDAN",
            "CDNA",
            "CNAD",
            "CNDA",
            "DACN",
            "DANC",
            "DCAN",
            "DCNA",
            "DNAC",
            "DNCA",
            "NACD",
            "NADC",
            "NCAD",
            "NCDA",
            "NDAC",
            "NDCA"
         ],
         "title": "Sequence",
         "type": "string"
      },
      "drop_prob": {
         "default": 0.0,
         "title": "Drop Prob",
         "type": "number"
      }
   },
   "required": [
      "in_channels",
      "out_channels",
      "kernel_size"
   ]
}

Config:
  • arbitrary_types_allowed: bool = True

  • extra: str = ignore

  • validate_default: bool = True

  • validate_assignment: bool = True

  • validate_return: bool = True

Fields:
Validators:
field in_channels: int [Required]#
Validated by:
field out_channels: int [Required]#
Validated by:
field kernel_size: int | tuple[int, ...] [Required]#
Validated by:
field padding: int | tuple[int, ...] | str = 'same'#
Validated by:
field stride: int = 1#
Validated by:
field conv_kwargs: dict[str, Any] = {}#
Validated by:
field transposed: bool = False#

Whether to perform ConvTranspose instead of Conv

Validated by:
field normalization: str | None = 'batchnorm3d'#
Validated by:
field normalization_pre_args: list = []#
Validated by:
field normalization_post_args: list = []#
Validated by:
field normalization_kwargs: dict = {}#
Validated by:
field activation: str | None = 'relu'#
Validated by:
field activation_kwargs: dict = {}#
Validated by:
field sequence: Literal['C', 'AC', 'CA', 'CD', 'CN', 'DC', 'NC', 'ACD', 'ACN', 'ADC', 'ANC', 'CAD', 'CAN', 'CDA', 'CDN', 'CNA', 'CND', 'DAC', 'DCA', 'DCN', 'DNC', 'NAC', 'NCA', 'NCD', 'NDC', 'ACDN', 'ACND', 'ADCN', 'ADNC', 'ANCD', 'ANDC', 'CADN', 'CAND', 'CDAN', 'CDNA', 'CNAD', 'CNDA', 'DACN', 'DANC', 'DCAN', 'DCNA', 'DNAC', 'DNCA', 'NACD', 'NADC', 'NCAD', 'NCDA', 'NDAC', 'NDCA'] = 'CNA'#
Validated by:
field drop_prob: float = 0.0#
Validated by:
validator validate  »  all fields[source]#

Base method for validating the model after creation.

pydantic model vision_architectures.blocks.cnn.MultiResCNNBlockConfig[source]#

Bases: CNNBlockConfig

Show JSON schema
{
   "title": "MultiResCNNBlockConfig",
   "type": "object",
   "properties": {
      "in_channels": {
         "title": "In Channels",
         "type": "integer"
      },
      "out_channels": {
         "title": "Out Channels",
         "type": "integer"
      },
      "kernel_size": {
         "default": 3,
         "title": "Kernel Size",
         "type": "integer"
      },
      "padding": {
         "const": "same",
         "default": "same",
         "title": "Padding",
         "type": "string"
      },
      "stride": {
         "default": 1,
         "title": "Stride",
         "type": "integer"
      },
      "conv_kwargs": {
         "additionalProperties": true,
         "default": {},
         "title": "Conv Kwargs",
         "type": "object"
      },
      "transposed": {
         "default": false,
         "description": "Whether to perform ConvTranspose instead of Conv",
         "title": "Transposed",
         "type": "boolean"
      },
      "normalization": {
         "anyOf": [
            {
               "type": "string"
            },
            {
               "type": "null"
            }
         ],
         "default": "batchnorm3d",
         "title": "Normalization"
      },
      "normalization_pre_args": {
         "default": [],
         "items": {},
         "title": "Normalization Pre Args",
         "type": "array"
      },
      "normalization_post_args": {
         "default": [],
         "items": {},
         "title": "Normalization Post Args",
         "type": "array"
      },
      "normalization_kwargs": {
         "additionalProperties": true,
         "default": {},
         "title": "Normalization Kwargs",
         "type": "object"
      },
      "activation": {
         "anyOf": [
            {
               "type": "string"
            },
            {
               "type": "null"
            }
         ],
         "default": "relu",
         "title": "Activation"
      },
      "activation_kwargs": {
         "additionalProperties": true,
         "default": {},
         "title": "Activation Kwargs",
         "type": "object"
      },
      "sequence": {
         "default": "CNA",
         "enum": [
            "C",
            "AC",
            "CA",
            "CD",
            "CN",
            "DC",
            "NC",
            "ACD",
            "ACN",
            "ADC",
            "ANC",
            "CAD",
            "CAN",
            "CDA",
            "CDN",
            "CNA",
            "CND",
            "DAC",
            "DCA",
            "DCN",
            "DNC",
            "NAC",
            "NCA",
            "NCD",
            "NDC",
            "ACDN",
            "ACND",
            "ADCN",
            "ADNC",
            "ANCD",
            "ANDC",
            "CADN",
            "CAND",
            "CDAN",
            "CDNA",
            "CNAD",
            "CNDA",
            "DACN",
            "DANC",
            "DCAN",
            "DCNA",
            "DNAC",
            "DNCA",
            "NACD",
            "NADC",
            "NCAD",
            "NCDA",
            "NDAC",
            "NDCA"
         ],
         "title": "Sequence",
         "type": "string"
      },
      "drop_prob": {
         "default": 0.0,
         "title": "Drop Prob",
         "type": "number"
      },
      "kernel_sizes": {
         "default": [
            3,
            5,
            7
         ],
         "items": {
            "anyOf": [
               {
                  "type": "integer"
               },
               {
                  "items": {
                     "type": "integer"
                  },
                  "type": "array"
               }
            ]
         },
         "title": "Kernel Sizes",
         "type": "array"
      },
      "filter_ratios": {
         "default": [
            1,
            2,
            3
         ],
         "description": "Ratio of filters to out_channels for each conv layer. Will be scaled to sum to 1.",
         "items": {
            "type": "number"
         },
         "title": "Filter Ratios",
         "type": "array"
      }
   },
   "required": [
      "in_channels",
      "out_channels"
   ]
}

Config:
  • arbitrary_types_allowed: bool = True

  • extra: str = ignore

  • validate_default: bool = True

  • validate_assignment: bool = True

  • validate_return: bool = True

Fields:
Validators:
field kernel_sizes: tuple[int | tuple[int, ...], ...] = (3, 5, 7)#
Validated by:
field filter_ratios: tuple[float, ...] = (1, 2, 3)#

Ratio of filters to out_channels for each conv layer. Will be scaled to sum to 1.

Validated by:
field padding: Literal['same'] = 'same'#
Validated by:
field kernel_size: int = 3#
Validated by:
validator scale_filter_ratios  »  filter_ratios[source]#
validator validate  »  all fields[source]#

Base method for validating the model after creation.

class vision_architectures.blocks.cnn.CNNBlock3D(config={}, checkpointing_level=0, **kwargs)[source]#

Bases: _CNNBlock

__init__(config={}, checkpointing_level=0, **kwargs)[source]#

Initialize internal Module state, shared by both nn.Module and ScriptModule.

class vision_architectures.blocks.cnn.CNNBlock2D(config={}, checkpointing_level=0, **kwargs)[source]#

Bases: _CNNBlock

__init__(config={}, checkpointing_level=0, **kwargs)[source]#

Initialize internal Module state, shared by both nn.Module and ScriptModule.

class vision_architectures.blocks.cnn.MultiResCNNBlock3D(config={}, checkpointing_level=0, **kwargs)[source]#

Bases: _MultiResCNNBlock

__init__(config={}, checkpointing_level=0, **kwargs)[source]#

Initialize internal Module state, shared by both nn.Module and ScriptModule.

class vision_architectures.blocks.cnn.MultiResCNNBlock2D(config={}, checkpointing_level=0, **kwargs)[source]#

Bases: _MultiResCNNBlock

__init__(config={}, checkpointing_level=0, **kwargs)[source]#

Initialize internal Module state, shared by both nn.Module and ScriptModule.

class vision_architectures.blocks.cnn.TensorSplittingConv(conv, num_splits, optimize_num_splits=True)[source]#

Bases: Module

Convolution layer that operates on splits of a tensor on desired device and concatenates the results to give a lossless output. This is useful for large tensors that cannot fit in memory.

__init__(conv, num_splits, optimize_num_splits=True)[source]#

Initialize internal Module state, shared by both nn.Module and ScriptModule.

get_receptive_field()#

Calculate the receptive field of the convolution layer.

Return type:

tuple[int, ...]

get_edge_context()#

Calculate the context size required to eliminate edge effects when merging the conv outputs into one.

get_input_shape(input_shape)[source]#

Get the input shape of the convolution layer.

Return type:

tuple[int, ...]

get_optimized_num_splits(input_shape)[source]#

Optimize the number of splits for each dimension based on the input shape and number of splits

Example

Let’s say input shape is (110, 110) and num_splits is (12, 12). The input will first be padded to (120, 120) and then split into splits of size (10+overlap, 10+overlap) each. However, if you notice, the padding that was also equal to 10, and therefore was completely unnecessary as the same result can be achieved by using num_splits = (11, 11) and reducing 144-121=23 splits to be processed.

Parameters:

input_shape (tuple[int, ...] | Size | Tensor) – Shape of the input tensor. If a tensor is provided, its shape will be used.

Return type:

tuple[int, ...]

Returns:

Tuple of optimized number of splits for each dimension.

pad_input_for_divisibility(x, num_splits=None)[source]#

Pad the input at the end of every spatial dimension such that it is perfectly divisible by the number of splits.

Return type:

Tensor

get_split_size(input_shape, num_splits=None)[source]#

Calculate the split size for each dimension based on the input shape and number of splits.

Parameters:

input_shape (tuple[int, ...] | Size | Tensor) – Shape of the input tensor. If a tensor is provided, its shape will be used.

Return type:

tuple[int, ...]

Returns:

Tuple of split sizes for each dimension.

get_split_stride(input_shape, num_splits=None)[source]#

Calculate the split stride for each dimension based on the input shape and context size.

Return type:

tuple[int, ...]

pad_input_for_context(x)[source]#

Pad the input with the context size for consistent merging.

Return type:

Tensor

forward(x)[source]#
Forward pass through the convolution layer with tensor splitting parallelism. Main convolution occurs on it’s

device, but the output is built on the input tensor’s device.

Parameters:

x (Tensor) – Input tensor of shape (batch_size, in_channels, [z], y, x).

Return type:

Tensor

Returns:

Output tensor of shape (batch_size, out_channels, [z], y, x).

extra_repr()[source]#

Return the extra representation of the module.

To print customized extra information, you should re-implement this method in your own modules. Both single-line and multi-line strings are acceptable.

vision_architectures.blocks.cnn.add_tsp_to_module(module, num_splits_2d=None, num_splits_3d=None, strict=True)[source]#
Return type:

Module

vision_architectures.blocks.cnn.remove_tsp_from_module(module)[source]#
Return type:

Module