Tensor Splitter and Merger#

pydantic model vision_architectures.utils.splitter_merger.SplitterConfig[source]#

Bases: CustomBaseModel

Show JSON schema
{
   "title": "SplitterConfig",
   "type": "object",
   "properties": {
      "split_dims": {
         "default": 3,
         "description": "Number of spatial dimensions.",
         "title": "Split Dims",
         "type": "integer"
      },
      "split_size": {
         "anyOf": [
            {
               "type": "integer"
            },
            {
               "items": {
                  "type": "integer"
               },
               "type": "array"
            }
         ],
         "title": "Split Size"
      },
      "stride": {
         "anyOf": [
            {
               "type": "integer"
            },
            {
               "items": {
                  "type": "integer"
               },
               "type": "array"
            }
         ],
         "title": "Stride"
      },
      "extend_mode": {
         "anyOf": [
            {
               "enum": [
                  "pad",
                  "wrap"
               ],
               "type": "string"
            },
            {
               "type": "null"
            }
         ],
         "default": "pad",
         "description": "Whether to pad or wrap the input tensor to get correct windows. If None, exact divisibility is expected",
         "title": "Extend Mode"
      },
      "raise_large_stride_error": {
         "default": true,
         "title": "Raise Large Stride Error",
         "type": "boolean"
      }
   },
   "required": [
      "split_size",
      "stride"
   ]
}

Config:
  • arbitrary_types_allowed: bool = True

  • extra: str = ignore

  • validate_default: bool = True

  • validate_assignment: bool = True

  • validate_return: bool = True

Fields:
Validators:
field split_dims: int = 3#

Number of spatial dimensions.

Validated by:
field split_size: int | tuple[int, ...] [Required]#
Validated by:
field stride: int | tuple[int, ...] [Required]#
Validated by:
field extend_mode: Optional[Literal['pad', 'wrap']] = 'pad'#

Whether to pad or wrap the input tensor to get correct windows. If None, exact divisibility is expected

Validated by:
field raise_large_stride_error: bool = True#
Validated by:
validator validate  »  all fields[source]#

Base method for validating the model after creation.

class vision_architectures.utils.splitter_merger.Splitter(config={}, **kwargs)[source]#

Bases: object

__init__(config={}, **kwargs)[source]#
get_expanded_shape(input_shape)[source]#

Get the shape of the input tensor after padding / wrapping.

Parameters:

input_shape (tuple[int, ...] | Size | Tensor) – The shape of the input tensor. Only the last “split_dims” dimensions are considered. If a tensor is passed, its shape will be used.

Return type:

tuple[int, ...]

Returns:

A tuple containing the shape of the input tensor after padding / wrapping.

get_positions(input_shape)[source]#

Get the top-left coordinates of all the splits that will be generated using the config.

Parameters:

input_shape (tuple[int, ...] | Size | Tensor) – The shape of the input tensor. Only the last “split_dims” dimensions are considered. If a tensor is passed, its shape will be used.

Return type:

Tensor

Returns:

A tensor of shape (num_splits, split_dims) containing the top-left coordinates of each split.

get_num_splits(input_shape)[source]#

Get the number of splits that will be generated using the config.

Parameters:

input_shape (tuple[int, ...] | Size | Tensor) – The shape of the input tensor. Only the last “split_dims” dimensions are considered. If a tensor is passed, its shape will be used.

Return type:

int

Returns:

The number of splits that will be generated.

expand(x)[source]#

Expand the input tensor to the shape after padding / wrapping.

Parameters:

x (Tensor) – The input tensor.

Return type:

Tensor

Returns:

The expanded tensor.

split(x)[source]#

Split the input tensor into smaller tensors using the config.

Parameters:

x (Tensor) – The input tensor.

Yields:

A tensor of shape (*split_size) for each split.

Return type:

Generator[Tensor, None, None]

__call__(x)[source]#

Split the input tensor into smaller tensors using the config.

Parameters:

x (Tensor) – The input tensor.

Yields:

A tensor of shape (*split_size) for each split.

Return type:

Generator[Tensor, None, None]

class vision_architectures.utils.splitter_merger.Merger[source]#

Bases: object

__init__()[source]#