Albumentations for image augmentation

Hi all,

I would like to use albumentations for image augmentation. I would like to transform from “transforms.Compose” to “A.Compose” but I don’t know how to do it for this simple example bellow.

Can someone please show me with this simple example bellow how to use albumentations.

Thank you for your help.

t_transforms = transforms.Compose([transforms.Grayscale(num_output_channels = 1),
transforms.Resize((224, 224)),
transforms.RandomAffine(degrees = 50, translate = (0.3, 0.3), scale = (0.45, 1), shear = (-45, 45, -45, 45))
transforms.RandomCrop(224),
transforms.RandomHorizontalFlip(),
transforms.ToTensor(),
transforms.Normalize((MEAN,), (STANDARD_DEVIATION,))
])

Here you can find example: PyTorch and Albumentations for image classification - Albumentations Documentation

The fragment of code:

train_transform = A.Compose(
    [
        A.SmallestMaxSize(max_size=160),
        A.ShiftScaleRotate(shift_limit=0.05, scale_limit=0.05, rotate_limit=15, p=0.5),
        A.RandomCrop(height=128, width=128),
        A.RGBShift(r_shift_limit=15, g_shift_limit=15, b_shift_limit=15, p=0.5),
        A.RandomBrightnessContrast(p=0.5),
        A.Normalize(mean=(0.485, 0.456, 0.406), std=(0.229, 0.224, 0.225)),
        ToTensorV2(),
    ]
)
train_dataset = CatsVsDogsDataset(images_filepaths=train_images_filepaths, transform=train_transform)

Thanks a lot @ksz for your answer and your precious help.

I have three questions:

  1. Is there an instruction in albumentations that looks like “transforms.Grayscale(num_output_channels = 1)”

  2. Is it interesting to use albumentations instead of transforms proposed by pytorch?

  3. Is it possible to use at the same time albumentations and transforms or only one should be used?

To be honest I never used Albumentations.

  1. Is there an instruction in albumentations that looks like “transforms.Grayscale(num_output_channels = 1)

ToGray(always_apply=False, p=1.0). Here is a demo with code: https://albumentations-demo.herokuapp.com/

  1. Is it interesting to use albumentations instead of transforms proposed by pytorch?

In my opinion it depends on the problem. In classic approaches usually not, but if you need extreme augmentation that this library can help.

3 .Is it possible to use at the same time albumentations and transforms or only one should be used?

I don’t know, but probably you can use both, one after another. But why do you need to combine them?

@ksz Thanks a lot for your precious help.

I try every day to learn more and more about pytorch, and recently I have discovered albumentations. From what I have read it contains some advantages when we want to do image augmentation for image segmentation for example, if I am not mistaken, as described here:

I want to combine albumentations and transforms because I wanted to know if it was possible and whether there are any benefits to combining them.

Once again thanks a lot for your answers.