import os
import torch
import torchvision
import torchvision.transforms as transforms
from torchvision import datasets
import numpy as np
import matplotlib.pyplot as plt


class MyDataset(torch.utils.data.Dataset):

      def __init__(self,dataset =  None, transform= None):
        self.MNIST = dataset
        self.transform = transform

      def __getitem__(self, index):

        data, target = self.MNIST[index]
       

        if self.transform is not None:
           

           tr_data = self.transform(data)
           
            
        return data, tr_data,  target, index
    
      def __len__(self):
          
        return len(self.MNIST)
    
    
    
train_dataset = datasets.MNIST(root='./data', train=True, download=True,
                               transform=transforms.Compose([transforms.ToTensor(),transforms.Normalize((0.5,), (0.5,))]))

train_transform=transforms.Compose([transforms.ToPILImage(),transforms.RandomHorizontalFlip(p=1),
                              transforms.ToTensor(),transforms.Normalize((0.5,), (0.5,))])
    
trainset = MyDataset(dataset=train_dataset, transform = train_transform)
trainloader = torch.utils.data.DataLoader(trainset, batch_size=24, shuffle=False, num_workers=2)

data, tr_data, target, index= iter(trainloader).next()


def imshow(inp, title=None):

     inp = inp.numpy().transpose((1, 2, 0))
     mean = np.array([0.5])
    std = np.array([0.5])
    inp = std * inp + mean
    inp = np.clip(inp, 0, 1)
    plt.imshow(inp)
    if title is not None:
        plt.title(title)
    plt.pause(0.001)  # pause a bit so that plots are updated
    
    
out = torchvision.utils.make_grid(tr_data)
imshow(out, title=[target[x] for x in target])

Now directly applying the same transformations on datasets.MNIST and checking the error between my custom datasets transformation and inbuilt mnist datasets (pytorch) . I am getting error value of tensor(0.9961)(for 1st sample)  while both the dataloaders shuffle flags is false
new_dataset = datasets.MNIST(root='./data', train=True, download=True,
                       transform=transforms.Compose([transforms.RandomHorizontalFlip(p=1),transforms.ToTensor(),transforms.Normalize((0.5,), (0.5,))]))


loader = torch.utils.data.DataLoader(new_dataset, batch_size=24, shuffle=False, num_workers=2)

data_new, target_new = iter(loader).next()

new_out = torchvision.utils.make_grid(data_new)

imshow(new_out, title=[target_new[x] for x in target_new])

print(torch.max(data_new[0]-tr_data[0]))