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:
- Created 2 years ago
- Comments:5
Top 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 >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
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:
Thank you so much!