question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Applying the same random transformation on the images and on the masks for segmentation task

See original GitHub issue

🐛 Bug

I’m working on a segmentation task and I’d like to apply the same random transformation on the images and on the masks. When I inspect the datasets, the images and masks don’t match. In the following code I just used HorizontalFlip for simplicity. Thank you very much in advance!

To Reproduce

from PIL import Image
from torchvision.transforms import Resize
import matplotlib.pyplot as plt
from albumentations.augmentations.transforms import HorizontalFlip

class MyDataset(Dataset):

    def __init__(self, list_images_input, list_images_target, transform=None):
        super(MyDataset, self).__init__()
        self.images_input = list_images_input
        self.images_target = list_images_target
        self.resize = Resize(224)
        self.transform = transform

    def __len__(self):
        return len(self.images_input)

    def __getitem__(self, index):
        img_path = str(self.images_input[index])
        img = Image.open(img_path).convert('RGB')
        seg_path = self.images_target[index]
        seg = Image.open(seg_path)
        seg = seg.convert('L')
        img = self.resize(img)
        seg = self.resize(seg)
        img = np.array(img)
        seg = np.array(seg)

        if self.transform:
            transformed = self.transform(image=img, mask=seg)
            img = transformed['image']
            seg = transformed['mask']
        seg = (seg > 0)
        return img, seg

def visualize_label_onto_image(img, label, alpha):
    plt.imshow(img)
    plt.imshow(label.squeeze(), cmap='Greys', alpha=alpha)
    plt.axis('off')
    plt.show()

def read_list_images_path_from_txt(txt_path):
  with open(txt_path) as f:
    content = f.readlines()
    content = [x.strip() for x in content]
    return content
  f.close()

input_path='input.txt'
target_path='target.txt'

imgs_training_input_split=read_list_images_path_from_txt(input_path)
imgs_training_target_split=read_list_images_path_from_txt(target_path)

affine_transform = HorizontalFlip()
training_dataset_plain = MyDataset(imgs_training_input_split, imgs_training_target_split)
training_dataset_affine = MyDataset(imgs_training_input_split, imgs_training_target_split, transform=affine_transform)

for i in range(10):
    visualize_label_onto_image(training_dataset_plain[i][0], training_dataset_plain[i][1], 0.3)
    visualize_label_onto_image(training_dataset_affine[i][0], training_dataset_affine[i][1], 0.3)

Environment

  • Albumentations version 0.5.2
  • Python version 3.7.5

Issue Analytics

  • State:closed
  • Created 2 years ago
  • Comments:5

github_iconTop GitHub Comments

1reaction
Dipetcommented, Nov 8, 2021

visualize_label_onto_image(training_dataset_plain[i][0], training_dataset_plain[i][1], 0.3)

There you get different image and mask, because it is 2 different calls of transforms. Change to:

res = training_dataset_plain[i]
visualize_label_onto_image(res[0], res][1], 0.3)
0reactions
emataocommented, Nov 8, 2021

Thank you so much!

Read more comments on GitHub >

github_iconTop Results From Across the Web

How to perform identical transform on both image and target ...
Cant apply both 2 independent transform to image and binary mask. Using PyTorch Transformers. Apply same transformation to CycleGAN for same ...
Read more >
Using Albumentations for a semantic segmentation task
This operation may be performed using PadIfNeeded transformation. It pads both the image and the mask on all four sides. Padding type (zero,...
Read more >
Pytorch transforms.Compose usage for pair of images in ...
Compose() in my segmentation task. But I'm not sure how to use the same (almost) random transforms for both the image and the...
Read more >
Applying transforms to both image and mask : r/pytorch - Reddit
I want to use data augmentation, but I can't seem to find a way to apply the same transformations to images and masks....
Read more >
Random Image transforms for segmentation masks not ...
I'm working on an image segmentation task with multiple labels. Here's how I've set up the data bunch:
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found