Transforms aren't the same for images and masks

v21: <p>I wanted to transform images and masks. but when I tried to visualize them some of the images and masks don’t have the same flips or orientations.</p>
<pre><code class="lang-auto">class UNetDataset(Dataset):
    
    def __init__(self, image_dir, mask_dir, transform=None):
        self.image_dir = image_dir
        self.mask_dir = mask_dir
        self.transform = transform
        self.images = os.listdir(image_dir)
        
    def __len__(self):
        return len(self.images)
    
        
    
    def __getitem__(self, index):
        image_path = os.path.join(self.image_dir, self.images[index])
        mask_path = os.path.join(self.mask_dir, self.images[index].replace(".jpg", "_mask.gif"))
        
        image = np.array(Image.open(image_path).convert("RGB"))
        mask = np.array(Image.open(mask_path).convert("L"), dtype=np.float32)
        mask[mask == 255.0] = 1.0
        
        if self.transform is not None:

            new_image = self.transform(image)
            new_mask = self.transform(mask)
            
        return new_image, new_mask

</code></pre>
<pre><code class="lang-auto">train_transform = transforms.Compose([
    transforms.ToPILImage(),
    transforms.Resize((IMAGE_HEIGHT, IMAGE_WIDTH)),
    transforms.RandomHorizontalFlip(p=0.5),
    
    transforms.ToTensor()
]
)
</code></pre>
<p>for example like this</p>
<p><div class="lightbox-wrapper"><a class="lightbox" href="https://discuss.pytorch.org/uploads/default/original/3X/5/4/54fa594741daa7569219c19a28042f9bf71d8c4d.png" data-download-href="https://discuss.pytorch.org/uploads/default/54fa594741daa7569219c19a28042f9bf71d8c4d" title="fsdfsdf"><img src="https://discuss.pytorch.org/uploads/default/optimized/3X/5/4/54fa594741daa7569219c19a28042f9bf71d8c4d_2_663x500.png" alt="fsdfsdf" data-base62-sha1="c7KtAszrLg8NKc3tUzQNS1L2Yvr" width="663" height="500" srcset="https://discuss.pytorch.org/uploads/default/optimized/3X/5/4/54fa594741daa7569219c19a28042f9bf71d8c4d_2_663x500.png, https://discuss.pytorch.org/uploads/default/optimized/3X/5/4/54fa594741daa7569219c19a28042f9bf71d8c4d_2_994x750.png 1.5x, https://discuss.pytorch.org/uploads/default/original/3X/5/4/54fa594741daa7569219c19a28042f9bf71d8c4d.png 2x" data-dominant-color="BCABB2"><div class="meta"><svg class="fa d-icon d-icon-far-image svg-icon" aria-hidden="true"><use href="#far-image"></use></svg><span class="filename">fsdfsdf</span><span class="informations">1029×775 179 KB</span><svg class="fa d-icon d-icon-discourse-expand svg-icon" aria-hidden="true"><use href="#discourse-expand"></use></svg></div></a></div></p>
<p>This is what I use to visualize</p>
<pre><code class="lang-auto">def show_random_images(dataset, n):
    fig, ax = plt.subplots(n, 2, figsize=(10, 5*n))
    random_numbers = random.sample(range(0, len(dataset)), n)
    for i in range(n):
        random_number = random_numbers[i]
        img = dataset[random_number][0].permute(1,2,0)
        mask = dataset[random_number][1].permute(1,2,0)
        ax[i,0].imshow(img)
        ax[i,0].set_axis_off()
        ax[i,1].imshow(mask)
        ax[i,1].set_axis_off()
    
    
    plt.show()
</code></pre>

ptrblck: <p>This is expected if you call into random transformations separately with your image and mask.<br>
Take a look at <a href="https://discuss.pytorch.org/t/torchvision-transfors-how-to-perform-identical-transform-on-both-image-and-target/10606/7">this post</a> which shows how the functional API can be used to transform both objects using the same “random” parameter.</p>