Source code for aitlas.transforms.segmentation
"""Classes and methods for image transformations for segmentation tasks.
For semantic segmentation tasks the shape of the input is (N, 3, H, W);
The shape of the output/mask is (N, num_classes, H, W), where N is the number of images"""
import albumentations as A
from torchvision import transforms
import torch
import numpy as np
from ..base import BaseTransforms
[docs]class MinMaxNormTranspose(BaseTransforms):
"""
MinMax Normalization and transposing a given sample.
"""
def __call__(self, sample):
"""
MinMax Normalization and transposing a given sample.
:param sample: input sample
:type sample: tensor
:return: normalized and transposed tensor
:rtype: tensor
"""
return torch.tensor(sample.transpose(2, 0, 1), dtype=torch.float32) / 255
[docs]class Transpose(BaseTransforms):
"""
Transposes a given sample.
"""
def __call__(self, sample):
"""
Transposes a given sample.
:param sample: input sample
:type sample: tensor
:return: normalized and transposed tensor
:rtype: tensor
"""
return torch.tensor(sample.transpose(2, 0, 1), dtype=torch.float32)
[docs]class MinMaxNorm(BaseTransforms):
"""
MinMax-Normalization of a given sample.
"""
def __call__(self, sample):
"""
MinMax-Normalization of a given sample.
:param sample: input sample
:type sample: tensor
:return: normalized and transposed tensor
:rtype: tensor
"""
return torch.tensor(sample, dtype=torch.float32) / 255
[docs]class Pad(BaseTransforms):
"""
Applies padding to a given sample.
"""
def __call__(self, sample):
"""
Applies padding to a given sample.
:param sample: input sample
:type sample: tensor
:return: padded tensor
:rtype: tensor
"""
data_transforms = transforms.Compose(
[transforms.ToPILImage(), transforms.Pad(4), transforms.ToTensor()]
)
return data_transforms(sample)
[docs]class ResizeToTensor(BaseTransforms):
"""
Resizes and converts a given sample to a tensor.
"""
def __call__(self, sample):
"""
Resizes and converts the given sample to a tensor.
:param sample: input sample
:type sample: tensor
:return: resized tensor
"""
data_transforms = transforms.Compose(
[
transforms.ToPILImage(),
transforms.Resize((256, 256)),
transforms.ToTensor(),
]
)
return data_transforms(sample)
[docs]class ResizePerChannelToTensor(BaseTransforms):
def __call__(self, sample):
"""Applies resize transformations per channel. This is useful for multichannel images.
:param sample: input sample (channel, h, w)
:type sample: tensor
:return: resized tensor
"""
data_transforms = transforms.Compose(
[
transforms.ToPILImage(),
transforms.Resize((256, 256)),
transforms.ToTensor(),
]
)
x = []
# apply transformations to each channel
for ch in sample:
x.append(data_transforms(ch))
# this is the multichannel transformed image (a torch tensor)
return torch.cat(x)