Inconsistent behaviour for DataLoader torch '1.9.0'

Jean_Michel_Sarr: <p>Hi everyone, I have an issue with the behaviour of the <code>torch.utils.data.DataLoader</code> for a minimal custom <code>torch.utils.data.Dataset</code>.</p>
<p>Here is the dataset</p>
<pre><code class="lang-auto">class MyData(Dataset):
  'Characterizes a dataset for PyTorch'
  def __init__(self, data, targets):
        """"Initialization, here display serves to show an example
        if false, it means that we intend to feed the data to a model"""
        self.targets = targets
        self.data = data

  def __len__(self):
        'Denotes the total number of samples'
        return len(self.targets)
    
  def __getitem__(self, index):
        'Generates one sample of data'
        # Select sample
        X = self.data[index]      
        # Load data and get label
        y = self.targets[index]      
        return X,y  
</code></pre>
<p>But now, if I try to use it within a DataLoader, it doesn’t switch the number of channel to the second dimension of the tensor</p>
<p>Here is a minimal exemple:</p>
<pre><code class="lang-auto">train_data = datasets.CIFAR10('../data/cifar', train=True, transform=transforms.ToTensor(), download=False)
test_data = datasets.CIFAR10('../data/cifar', train=False, transform=transforms.ToTensor(), download=False)

source_data = MyData(train_data.data, train_data.targets)
source_loader = torch.utils.data.DataLoader(source_data, batch_size=1024)
c,d = next(iter(source_loader))
c.shape
</code></pre>
<p>It it will return shape</p>
<pre><code class="lang-auto">torch.Size([1024, 32, 32, 3])
</code></pre>
<p>Whereas, if I do it directly the cifar10 dataset. For instance</p>
<pre><code class="lang-auto">train_loader = DataLoader(train_data, batch_size=1024, shuffle=True)
a,b = next(iter(train_loader))
a.shape
</code></pre>
<p>will return</p>
<pre><code class="lang-auto">torch.Size([1024, 3, 32, 32])
</code></pre>
<p>I am using</p>
<ul>
<li>torch ‘1.9.0’</li>
<li>torchvision ‘0.10.0’</li>
</ul>

InnovArul: <aside class="quote no-group" data-username="Jean_Michel_Sarr" data-post="1" data-topic="142664">
<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/jean_michel_sarr/48/46117_2.png" class="avatar"> Jean_Michel_Sarr:</div>
<blockquote>
<pre><code class="lang-auto">source_data = MyData(train_data.data, train_data.targets)
</code></pre>
</blockquote>
</aside>
<p>Probably you are not applying <code>transform</code> sequence as <code>torchvision</code>’s <code>CIFAR10</code> does.<br>
Remember that only the raw data is passed as input to your newly written <code>MyData</code> class. All the other post-computations (such as Normalization) should be handled in <code>MyData</code>.</p><aside class="onebox githubblob" data-onebox-src="https://github.com/pytorch/vision/blob/main/torchvision/datasets/cifar.py#L113-L118">
  <header class="source">

      <a href="https://github.com/pytorch/vision/blob/main/torchvision/datasets/cifar.py#L113-L118" target="_blank" rel="noopener">github.com</a>
  </header>

  <article class="onebox-body">
    <h4><a href="https://github.com/pytorch/vision/blob/main/torchvision/datasets/cifar.py#L113-L118" target="_blank" rel="noopener">pytorch/vision/blob/main/torchvision/datasets/cifar.py#L113-L118</a></h4>



    <pre class="onebox"><code class="lang-py">
      <ol class="start lines" start="113" style="counter-reset: li-counter 112 ;">
          <li># doing this so that it is consistent with all other datasets</li>
          <li># to return a PIL Image</li>
          <li>img = Image.fromarray(img)</li>
          <li></li>
          <li>if self.transform is not None:</li>
          <li>    img = self.transform(img)</li>
      </ol>
    </code></pre>



  </article>

  <div class="onebox-metadata">
    
    
  </div>

  <div style="clear: both"></div>
</aside>