How do I get data, transforms(data) from the dataloader at the same time?

username: TanmDL
cooked: <p>class MyDataset(torch.utils.data.Dataset):</p>
<pre><code>def __init__(self,dataset =  None, transform= None):
    self.MNIST = dataset
    #print(self.MNIST)
    self.transform = transform

def __getitem__(self, index):
    data, target = self.MNIST[index]
    #print(img.shape)
    if self.transform is not None:
        #print(data.size())
        #tr_data = TF.to_pil_image(data)
        #print(tr_data.size())
        #tr_data = TF.hflip(tr_data)   #### 10 degree rotations
        #print(tr_data.size())
        #tr_data = TF.to_tensor(tr_data)
        
        tr_data = self.transform(data)
       
        
    return data, tr_data,  target, index

def __len__(self):
    return len(self.MNIST)
</code></pre>
<p>train_dataset = datasets.MNIST(root=’./data’, train=True, download=True,<br>
transform=transforms.Compose([transforms.ToTensor(),transforms.Normalize((0.5,), (0.5,))]))</p>
<p>trin_transform=transforms.Compose([transforms.RandomHorizontalFlip(p=1),<br>
transforms.ToTensor(),transforms.Normalize((0.5,), (0.5,))])</p>
<p>trainset = MyDataset(dataset=train_dataset, transform = trin_transform)</p>
<p>trainloader = torch.utils.data.DataLoader(trainset, batch_size=24, shuffle=False, num_workers=2)</p>
<p>The error is coming like “TypeError: img should be PIL Image. Got &lt;class ‘torch.Tensor’&gt;”</p>

username: TanmDL
cooked: <p><a class="mention" href="/u/ptrblck">@ptrblck</a> please check it.</p>

username: ptrblck
cooked: <p>While line of code is raising this error and how did you create <code>self.MNIST</code> as well as <code>self.transform</code>?</p>

username: TanmDL
cooked: <p>How do I share code?</p>

username: ptrblck
cooked: <p>You can wrap it into three backticks ``` and then post it here.<br>
Also, there is a “Preformatted text” button in the reply field.</p>

username: TanmDL
cooked: <aside class="quote no-group" data-username="ptrblck" data-post="5" data-topic="79064">
<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/ptrblck/48/1823_2.png" class="avatar"> ptrblck:</div>
<blockquote>
<p>d then post it here.</p>
</blockquote>
</aside>
<p>thank you . I am trying the sharing the code. It woule be better to figure it out where I am making the erros.</p>

username: TanmDL
cooked: <pre><code class="lang-python">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])
</code></pre>
<p><strong>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</strong></p>
<pre><code class="lang-python">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]))
</code></pre>

username: TanmDL
cooked: <p>I think the error is coming due to the these two functions<br>
<strong>transforms.ToPILImage() and transforms.ToTensor()</strong></p>

username: ptrblck
cooked: <p>Thanks for the code.<br>
I’ve added the proper formatting to copy-paste the code and cannot reproduce the issue, as your code runs fine on my machine.</p>

username: TanmDL
cooked: <p>Thank you.<br>
check the error on the last line code<br>
<strong>print(torch.max(data_new[0]-tr_data[0])) while applying same transformations both datasets class(MyDataset and datasets.MNIST). It should come to zero value but I am getting tensor(0.9961)</strong></p>

username: ptrblck
cooked: <p>Ah OK, I missed this and thought the code should reproduce the initial error.<br>
The mismatch will most likely be created as you are transforming the data twice in your custom approach.<br>
You are passing the transformations the first time when creating <code>train_dataset</code> and then another transformation to <code>MyDataset</code>, which will be applied again.</p>

username: TanmDL
cooked: <p>Thank you for the reply. But How could I solve the problem?</p>

username: ptrblck
cooked: <p>Apply the transformation only once.<br>
I’m not sure, what your custom dataset is supposed to do besides applying another transformation, which will create this mismatch.</p>

username: TanmDL
cooked: <aside class="quote no-group" data-username="ptrblck" data-post="13" data-topic="79064">
<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/ptrblck/48/1823_2.png" class="avatar"> ptrblck:</div>
<blockquote>
<p>mation only once.<br>
I’m not sure, what your custom dataset is supposed to do besides applying another transformation, which will create this mismatch.</p>
</blockquote>
</aside>
<p>Okay I am trying and let you know. I think I need to pass two tranormations: first transformation (to Tensor and Normalisation for data only ) and last transformation (data augmentation) as it is.</p>

username: TanmDL
cooked: <aside class="quote no-group" data-username="TanmDL" data-post="14" data-topic="79064">
<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/tanmdl/48/15295_2.png" class="avatar"> TanmDL:</div>
<blockquote>
<p>g and let you kno</p>
</blockquote>
</aside>
<p>do u know  any better approach to tackle this issue?</p>

username: TanmDL
cooked: <p>Thank you. The error was coming due to the seond time normalisation. <img src="https://discuss.pytorch.org/images/emoji/apple/slight_smile.png?v=9" title=":slight_smile:" class="emoji" alt=":slight_smile:"> Thank you</p>