pythonfunctionvariablesalbumentations

Data augmentation basic python


I am a beginner in python programming and was learning how to code a project from YouTube when I got stuck on the following code.

The full code is here: https://github.com/nikhilroxtomar/Retina-Blood-Vessel-Segmentation-using-UNET-in-TensorFlow/blob/main/data.py and the YouTube where it appears is at 24:30 - https://youtu.be/tpbWZVY2dng?t=1470 )

from albumentations import HorizontalFlip
def augment_data(images, masks, save_path, augment=True):

  for idx, (x, y) in tqdm(enumerate(zip(images, masks)), total=len(images)):
      """ Extracting names """
      name = x.split("/")[-1].split(".")[0]

      """ Reading image and mask """
      x = cv2.imread(x, cv2.IMREAD_COLOR)
      y = imageio.mimread(y)[0]

      if augment == True:
          aug = HorizontalFlip(p=1.0)
          augmented = aug(image=x, mask=y)
          x1 = augmented["image"]
          y1 = augmented["mask"]

This part is what I don't understand

if augment == True:
      aug = HorizontalFlip(p=1.0)
      augmented = aug(image=x, mask=y)
      x1 = augmented["image"]
      y1 = augmented["mask"]

How is aug being used to take input parameters of an image? Is augmented being used as a dictionary? Can you please explain how?


Solution

  • aug is instance of albumentations.augmentations.transforms.HorizontalFlip class

    Then if you look at the source code you will see that it inherits from albumentations.core.transforms_interface.DualTransform class which looking at the source code inherits from BasicTransform class.

    Looking at the BasicTransform class you can see it implements __call__() method. It takes variable number of keyword arguments **kwargs and after some processing returns kwargs (i.e. when you call aug()). kwargs is a dict with arguments you pass. In your case the keys are image and mask.

    As a side note, instead of if augment == True: it should be just if augment: