transforms

NormalizeImg

class NormalizeImg(mean=(0.485, 0.456, 0.406), std=(0.229, 0.224, 0.225))

normalize a 3 channel image or a batch of 3 channel images.

Inputs are expected to be floats in range 0.0:1.0 of shape (B?, C, W, H) where B? is an optional batch dimension.

Parameters
  • mean (Sequence[float]) – mean for each channel, defaults to imagenet defaults as outlined in torchvision.

  • std (Sequence[float]) – std for each channel, defaults to imagenet defaults as outlined in torchvision.

Example

>>> from hearth.vision.transforms import NormalizeImg
>>> _ = torch.manual_seed(0)
>>>
>>> # use torchvision imagenet defaults...
>>> normalize = NormalizeImg()
>>> single_image = torch.rand(3, 64, 64)
>>> normalized_single_image = normalize(single_image)
>>> normalized_single_image.mean((1, 2))
tensor([0.0448, 0.2087, 0.4471])
>>> normalized_single_image.std((1, 2))
tensor([1.2547, 1.2854, 1.2874])
>>> batch_images = torch.rand(5, 3, 64, 64)
>>> normalized_batch_images = normalize(batch_images)
>>> normalized_batch_images.mean((0, 2, 3))
tensor([0.0778, 0.1930, 0.4113])
>>> normalized_batch_images.std((0, 2, 3))
tensor([1.2605, 1.2804, 1.2793])
forward(x)

Defines the computation performed at every call.

Should be overridden by all subclasses.

Note

Although the recipe for forward pass needs to be defined within this function, one should call the Module instance afterwards instead of this since the former takes care of running the registered hooks while the latter silently ignores them.

Return type

Tensor

training: bool

RandomFlipOrient

class RandomFlipOrient(p=0.5)

randomly flip orientation of a single image

Parameters

p (float) – probability of flipping orientaion. Defaults to 0.5.

Example

>>> from hearth.vision.transforms import RandomFlipOrient
>>>
>>> # set p=1.0 to ensure stuff happens
>>> transform = RandomFlipOrient(p=1.0)
>>> img = torch.randint(0, 255, size=(3, 128, 256))
>>> transform(img).shape
torch.Size([3, 256, 128])
forward(img)

Defines the computation performed at every call.

Should be overridden by all subclasses.

Note

Although the recipe for forward pass needs to be defined within this function, one should call the Module instance afterwards instead of this since the former takes care of running the registered hooks while the latter silently ignores them.

Return type

Tensor

training: bool

ResizeCrop

class ResizeCrop(size, crop_noise=0.0)

resize a single image by scaling smallest dimension and center cropping with optional shift.

Parameters
  • size (int) – new size for square image.

  • noise – for shifting the crop window along the longest dimension Defaults to 0.0 (no noise).

Example

>>> import torch
>>> from hearth.vision.transforms import ResizeCrop
>>>
>>> resize = ResizeCrop(224, crop_noise=0.5)
>>> img = torch.randint(0, 255, size=(3, 1024, 986))
>>> resize(img).shape
torch.Size([3, 224, 224])
forward(img)

Defines the computation performed at every call.

Should be overridden by all subclasses.

Note

Although the recipe for forward pass needs to be defined within this function, one should call the Module instance afterwards instead of this since the former takes care of running the registered hooks while the latter silently ignores them.

Return type

Tensor

training: bool