Identical Transformation on Input and Label?

username: Solitude
cooked: <p>Hi, I would like to have the identical transformation on the input and label but I can’t figure it out. Here’s my code:</p>
<pre><code class="lang-auto"># Pytorch
import torch
import torchvision
import torch.nn as nn
import torch.nn.functional as F
import torchvision.transforms.functional as TF
from torchvision import datasets, models, transforms
from torch.utils.data import Dataset, DataLoader
import torch.optim as optim
# External
import BatchMaker
import numpy as np
import PIL
from PIL import Image
import cv2
import matplotlib.pyplot as plt
import seaborn as sns
# Built-Ins
import math
import random
import time
import os
import glob
# Version control
print("PyTorch Version: ",torch.__version__)
print("Torchvision Version: ",torchvision.__version__)
</code></pre>
<pre><code class="lang-auto">batch_size = 1

training_file = "training.csv"
testing_file = "testing.csv"

Generator = BatchMaker.BatchMaker
train_inputs, train_labels = Generator(training_file)
test_inputs, test_labels = Generator(testing_file)

transform = transforms.Compose([
    CreateDataset()
    transforms.ToPILImage(),
#     transforms.Resize((165, 220)),
    transforms.RandomRotation(degrees=45),
    transforms.ColorJitter(brightness=0.5, contrast=0.5, saturation=0.5),
    transforms.RandomHorizontalFlip(p=0.5),
    transforms.RandomVerticalFlip(p=0.5),
    transforms.ToTensor()
])

class CreateDataset(Dataset):
    def __init__(self, inputs, labels, transform=transform):
        self.inputs = torch.FloatTensor(inputs)
        self.labels = torch.FloatTensor(labels)
        self.transform = transform
        
    def __getitem__(self, index):
        x = self.inputs[index]
        y = self.labels[index]

        return x, y

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

    
# Get the data, transform it
data = {
   'train':
   CreateDataset(train_inputs, train_labels),
#    'val':
#    CreateDataset(val_inputs, val_labels),
    'test':
   CreateDataset(test_inputs, test_labels)
}



# img_num = 0
# for img, label in data['test']:
#     print(img.shape)
#     save_image(data['test'][0][0], 'img'+str(img_num)+'.png')
#     img_num += 1
#     if img_num == 10:
#         break

# Load Data in batches, shuffled
dataloaders = {
   'train': DataLoader(data['train'], batch_size=batch_size, shuffle=True, drop_last=True),
#    'val': DataLoader(data['val'], batch_size=batch_size, shuffle=True, drop_last=True),
    'test': DataLoader(data['test'], batch_size=batch_size, shuffle=True, drop_last=True),
}
</code></pre>
<p><div class="lightbox-wrapper"><a class="lightbox" href="https://discuss.pytorch.org/uploads/default/original/3X/c/5/c538ec4c62157d6ce2175af0b50c5539b7d84123.png" data-download-href="https://discuss.pytorch.org/uploads/default/c538ec4c62157d6ce2175af0b50c5539b7d84123" title="download (4)"><img src="https://discuss.pytorch.org/uploads/default/optimized/3X/c/5/c538ec4c62157d6ce2175af0b50c5539b7d84123_2_690x215.png" alt="download (4)" data-base62-sha1="s8I19ZXhBgjJ2SkkeHLRcsLNmi7" width="690" height="215" srcset="https://discuss.pytorch.org/uploads/default/optimized/3X/c/5/c538ec4c62157d6ce2175af0b50c5539b7d84123_2_690x215.png, https://discuss.pytorch.org/uploads/default/optimized/3X/c/5/c538ec4c62157d6ce2175af0b50c5539b7d84123_2_1035x322.png 1.5x, https://discuss.pytorch.org/uploads/default/optimized/3X/c/5/c538ec4c62157d6ce2175af0b50c5539b7d84123_2_1380x430.png 2x" data-dominant-color="4E4E58"><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">download (4)</span><span class="informations">1645×513 119 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><br>
Here’s an image of the input and the label<br>
Thanks a lot for the help!</p>

username: Nikronic
cooked: <p>Hi</p>
<p>I had similar problem and I solved it. Here is the link to the post. Although, I have used a subset of original transformation pipeline, still I wanted to have identical for that subset.</p><aside class="quote quote-modified" data-post="4" data-topic="47550">
  <div class="title">
    <div class="quote-controls"></div>
    <img loading="lazy" alt="" width="24" height="24" src="https://discuss.pytorch.org/user_avatar/discuss.pytorch.org/nikronic/48/71873_2.png" class="avatar">
    <a href="https://discuss.pytorch.org/t/use-a-subset-of-composed-transforms-with-same-random-seed/47550/4">Use a subset of Composed Transforms with same random seed</a> <a class="badge-category__wrapper " href="/c/vision/5"><span data-category-id="5" style="--category-badge-color: #AB9364; --category-badge-text-color: #FFFFFF;" data-drop-close="true" class="badge-category " title="Topics related to either pytorch/vision or vision research related topics"><span class="badge-category__name">vision</span></span></a>
  </div>
  <blockquote>
    The problem solved using feeding same seed value before applying each Compose of transforms. 


def __getitem__(self,index):      
        img = Image.open(self.data[index]).convert('RGB')
        target = Image.open(self.data_labels[index])
        
        seed = np.random.randint(2147483647) # make a seed with numpy generator 
        random.seed(seed) # apply this seed to img tranfsorms
        if self.transform is not None:
            img = self.transform(img)
            
        random.s…
  </blockquote>
</aside>

<p>bests,<br>
Nik</p>

username: ptrblck
cooked: <p>Alternatively to the seeding approach, you could also use the functional API as explained <a href="https://discuss.pytorch.org/t/torchvision-transfors-how-to-perform-identical-transform-on-both-image-and-target/10606/7">here</a>.</p>