imgutils.preprocess.torchvision

This module provides utilities for creating and parsing torchvision transforms. It includes functionality for registering custom transforms, handling interpolation modes, and converting between different transform representations.

The module supports common image transformations like resize, center crop, tensor conversion and normalization. It provides a flexible framework for extending with additional transforms.

register_torchvision_transform

imgutils.preprocess.torchvision.register_torchvision_transform(name: str)[source]

Register a torchvision transform creation function.

Parameters:

name – Name of the transform

Returns:

Decorator function

register_torchvision_parse

imgutils.preprocess.torchvision.register_torchvision_parse(name: str)[source]

Register a torchvision transform parsing function.

Parameters:

name – Name of the transform parser

Returns:

Decorator function

create_torchvision_transforms

imgutils.preprocess.torchvision.create_torchvision_transforms(tvalue: list | dict)[source]

Create torchvision transforms from config.

Parameters:

tvalue – Transform configuration as list or dict

Returns:

Composed transforms or single transform

Raises:

TypeError – If tvalue has unsupported type

Example:
>>> from imgutils.preprocess import create_torchvision_transforms
>>>
>>> create_torchvision_transforms({
...     'type': 'resize',
...     'size': 384,
...     'interpolation': 'bicubic',
... })
Resize(size=384, interpolation=bicubic, max_size=None, antialias=True)
>>> create_torchvision_transforms({
...     'type': 'resize',
...     'size': (224, 256),
...     'interpolation': 'bilinear',
... })
Resize(size=(224, 256), interpolation=bilinear, max_size=None, antialias=True)
>>> create_torchvision_transforms({'type': 'center_crop', 'size': 224})
CenterCrop(size=(224, 224))
>>> create_torchvision_transforms({'type': 'to_tensor'})
ToTensor()
>>> create_torchvision_transforms({'type': 'maybe_to_tensor'})
MaybeToTensor()
>>> create_torchvision_transforms({'type': 'normalize', 'mean': 0.5, 'std': 0.5})
Normalize(mean=0.5, std=0.5)
>>> create_torchvision_transforms({
...     'type': 'normalize',
...     'mean': [0.485, 0.456, 0.406],
...     'std': [0.229, 0.224, 0.225],
... })
Normalize(mean=tensor([0.4850, 0.4560, 0.4060]), std=tensor([0.2290, 0.2240, 0.2250]))
>>> create_torchvision_transforms([
...     {'antialias': True,
...      'interpolation': 'bicubic',
...      'max_size': None,
...      'size': 384,
...      'type': 'resize'},
...     {'size': (224, 224), 'type': 'center_crop'},
...     {'type': 'maybe_to_tensor'},
...     {'mean': 0.5, 'std': 0.5, 'type': 'normalize'}
... ])
Compose(
    Resize(size=384, interpolation=bicubic, max_size=None, antialias=True)
    CenterCrop(size=(224, 224))
    MaybeToTensor()
    Normalize(mean=0.5, std=0.5)
)

Note

Currently the following transforms are supported:

  • torchvision.transforms.Resize

  • torchvision.transforms.CenterCrop

  • torchvision.transforms.ToTensor

  • timm.data.MaybeToTensor

  • torchvision.transforms.Normalize

parse_torchvision_transforms

imgutils.preprocess.torchvision.parse_torchvision_transforms(value)[source]

Parse torchvision transforms into config dict.

Parameters:

value – Transform object to parse

Returns:

Transform configuration as list or dict

Raises:

TypeError – If transform type is not supported

Example:
>>> from timm.data import MaybeToTensor
>>> from torchvision.transforms import Resize, InterpolationMode, CenterCrop, ToTensor, Normalize
>>>
>>> from imgutils.preprocess import parse_torchvision_transforms
>>>
>>> parse_torchvision_transforms(Resize(
...     size=384,
...     interpolation=InterpolationMode.BICUBIC,
... ))
{'type': 'resize', 'size': 384, 'interpolation': 'bicubic', 'max_size': None, 'antialias': True}
>>> parse_torchvision_transforms(Resize(
...     size=(224, 256),
...     interpolation=InterpolationMode.BILINEAR,
... ))
{'type': 'resize', 'size': (224, 256), 'interpolation': 'bilinear', 'max_size': None, 'antialias': True}
>>> parse_torchvision_transforms(CenterCrop(size=224))
{'type': 'center_crop', 'size': (224, 224)}
>>> parse_torchvision_transforms(ToTensor())
{'type': 'to_tensor'}
>>> parse_torchvision_transforms(MaybeToTensor())
{'type': 'maybe_to_tensor'}
>>> parse_torchvision_transforms(Normalize(mean=0.5, std=0.5))
{'type': 'normalize', 'mean': 0.5, 'std': 0.5}
>>> parse_torchvision_transforms(Normalize(
...     mean=[0.485, 0.456, 0.406],
...     std=[0.229, 0.224, 0.225],
... ))
{'type': 'normalize', 'mean': [0.485, 0.456, 0.406], 'std': [0.229, 0.224, 0.225]}
>>> parse_torchvision_transforms(Compose([
...     Resize(
...         size=384,
...         interpolation=Image.BICUBIC,
...     ),
...     CenterCrop(size=224),
...     MaybeToTensor(),
...     Normalize(mean=0.5, std=0.5),
... ]))
[{'antialias': True,
  'interpolation': 'bicubic',
  'max_size': None,
  'size': 384,
  'type': 'resize'},
 {'size': (224, 224), 'type': 'center_crop'},
 {'type': 'maybe_to_tensor'},
 {'mean': 0.5, 'std': 0.5, 'type': 'normalize'}]