""" Contains classes for image transformations for classification datasets."""
import albumentations as A
import torch
import cv2
from torchvision import transforms
from ..base import BaseTransforms
[docs]class ResizeRandomCropFlipHVToTensor(BaseTransforms):
"""
A class that applies resizing to (256,256), random cropping to size (224,224), random flipping, and tensor conversion to images.
"""
def __call__(self, sample):
"""
Apply the transformation to the input sample.
:param sample: Input image
:type sample: numpy.ndarray
:return: Transformed image
:rtype: torch.Tensor
"""
data_transforms = transforms.Compose([
transforms.ToPILImage(),
transforms.Resize(256),
transforms.RandomCrop(224),
transforms.RandomHorizontalFlip(),
transforms.RandomVerticalFlip(),
transforms.ToTensor(), # transform the image from H x W x C to C x H x W
])
return data_transforms(sample)
[docs]class ResizeCenterCropFlipHVToTensor(BaseTransforms):
"""
A class that applies resizing to (256,256), center cropping to size (224,224), random HV flipping, and tensor conversion to images.
"""
def __call__(self, sample):
"""
Apply the transformation to the input sample.
:param sample: Input image
:type sample: numpy.ndarray
:return: Transformed image
:rtype: torch.Tensor
"""
data_transforms = transforms.Compose([
transforms.ToPILImage(),
transforms.Resize(256),
transforms.CenterCrop(224),
transforms.RandomHorizontalFlip(),
transforms.RandomVerticalFlip(),
transforms.ToTensor(), # transform the image from H x W x C to C x H x W
])
return data_transforms(sample)
[docs]class ResizeCenterCropToTensor(BaseTransforms):
"""
A class that applies resizing to (256,256), center cropping to size (224,224), and tensor conversion to images.
"""
def __call__(self, sample):
"""
Apply the transformation to the input sample.
:param sample: Input image
:type sample: numpy.ndarray
:return: Transformed image
:rtype: torch.Tensor
"""
data_transforms = transforms.Compose([
transforms.ToPILImage(),
transforms.Resize(256),
transforms.CenterCrop(224),
transforms.ToTensor(),
])
return data_transforms(sample)
[docs]class Resize1ToTensor(BaseTransforms):
"""
A class that applies fixed resizing to (224,224) and tensor conversion to images.
"""
def __call__(self, sample):
data_transforms = transforms.Compose([
transforms.ToPILImage(),
transforms.Resize((224, 224)),
transforms.ToTensor(),
])
return data_transforms(sample)
[docs]class GrayToRGB(BaseTransforms):
"""
A class that converts grayscale images to RGB format [height, width, channels].
"""
# Convert numpy array from gray to rgb loaded as [height, width, channels]
def __call__(self, sample):
if sample.ndim == 2:
return cv2.cvtColor(sample, cv2.COLOR_GRAY2RGB)
else:
return sample
[docs]class ConvertToRGBResizeCenterCropToTensor(BaseTransforms):
"""
A class that converts an image to RGB format, applies resizing to size (256,256), center cropping to size (224,224), and tensor conversion.
"""
def __call__(self, sample):
"""
Apply the transformation to the input sample.
:param sample: Input image
:type sample: numpy.ndarray
:return: Transformed image
:rtype: torch.Tensor
"""
sample = sample[:, :, :3]
data_transforms = transforms.Compose([
transforms.ToPILImage(),
transforms.Resize(256),
transforms.CenterCrop(224),
transforms.ToTensor(),
])
return data_transforms(sample)
[docs]class RandomFlipHVToTensor(BaseTransforms):
"""
A class that applies random flipping and tensor conversion to images.
"""
def __call__(self, sample):
"""
Apply the transformation to the input sample.
:param sample: Input image
:type sample: numpy.ndarray
:return: Transformed image
:rtype: torch.Tensor
"""
data_transforms = transforms.Compose([
transforms.ToPILImage(),
transforms.RandomHorizontalFlip(),
transforms.RandomVerticalFlip(),
transforms.ToTensor(), # transform the image from H x W x C to C x H x W
])
return data_transforms(sample)