Identical Transformation on Input and Label?

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:

# 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__)
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),
}


Here’s an image of the input and the label
Thanks a lot for the help!

Hi

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.

bests,
Nik

Alternatively to the seeding approach, you could also use the functional API as explained here.

1 Like