DataLoader transformations not working

username: blackbirdbarber
cooked: <p>I have this code where I tested Normalize and LinearTranformation.<br>
<code>torchvision.transforms.Normalize</code> and <code>torchvision.transforms.LinearTransformation</code> to be more precise.</p>
<pre><code class="lang-auto">import torch.nn as nn
import torch.nn.functional as F
from torch.utils.data import DataLoader, Dataset, TensorDataset
from torch.optim import *
import torchvision

trans = torch.tensor([[1/128., 1.], [1., 1.]])
dl = DataLoader(
  torchvision.datasets.MNIST('/data/mnist', train=True, download=True,
                             transform=torchvision.transforms.Compose([
                               torchvision.transforms.ToTensor(),
                               torchvision.transforms.Normalize((0.5,), (0.5,)),
                               torchvision.transforms.LinearTransformation(trans)  
                             ])), shuffle=False)



tensor = dl.dataset.data
tensor = tensor.to(dtype=torch.float32)
tr = tensor.reshape(tensor.size(0), -1) 
# tr = tr/128 # tr = tr/255
targets = dl.dataset.targets
targets = targets.to(dtype=torch.long)

x_train = tr[0:50000-1]
print(x_train[0])
</code></pre>
<p>The output <code>print(x_train[0])</code> is always the same, even if I eliminate these two transformations, or if I eliminate any of these two. Do you have any idea why is this so?</p>
<p>By <code>LinearTransformation</code> I tried to do a transform that would be identical to scaling down the tensor values like <code>tr = tr/128</code> for instance in the commented part of the code.</p>

username: krishnamurthy
cooked: <p>You are accessing the dataset directly. You need to access it via the <code>DataLoader</code> object, for the transforms to be applied.</p>
<p>For example, you could do:</p>
<pre><code class="lang-auto">next(iter(dl))
</code></pre>
<p>This should return a Tensor to which transforms have been applied.</p>
<p>More generally, to iterate through your dataset, do:</p>
<pre><code class="lang-auto">for i, sample in enumerate(dl):
    print(sample) # Or any other processing you'd like to perform
</code></pre>
<p><strong>Aside</strong></p>
<p>Your usage of <code>LinearTransformation()</code> appears to be inconsistent with the latest API. <code>LinearTransformation()</code> expects a <code>transformation_matrix</code> and a <code>mean_vector</code>. It seems like you’re providing only the <code>transformation_matrix</code>.</p>

username: blackbirdbarber
cooked: <aside class="quote no-group" data-username="krishnamurthy" data-post="2" data-topic="49417">
<div class="title">
<div class="quote-controls"></div>
<img loading="lazy" alt="" width="24" height="24" src="https://discuss.pytorch.org/letter_avatar_proxy/v4/letter/k/c0e974/48.png" class="avatar"> krishnamurthy:</div>
<blockquote>
<p>You need to access it via the <code>DataLoader</code> object, for the transforms to be applied.</p>
</blockquote>
</aside>
<p>I tried to find where I could match the example from PyTorch website and I found an instance <a href="https://pytorch.org/docs/stable/tensorboard.html#torch-utils-tensorboard" rel="noopener nofollow ugc">having <code>Compose</code></a>:<br>
<div class="lightbox-wrapper"><a class="lightbox" href="https://discuss.pytorch.org/uploads/default/original/2X/c/c4c3e208ee129a0b1348b2840d2d6a87f25c9177.png" data-download-href="https://discuss.pytorch.org/uploads/default/c4c3e208ee129a0b1348b2840d2d6a87f25c9177" title="image"><img src="https://discuss.pytorch.org/uploads/default/original/2X/c/c4c3e208ee129a0b1348b2840d2d6a87f25c9177.png" alt="image" data-base62-sha1="s4Fgdar6clpZG2RSvihy7M5Aduf" width="690" height="349" data-dominant-color="E8E9DF"><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">image</span><span class="informations">972×493 40.6 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></p>
<p>I am using the <code>DataLoader</code> already, where exactly the <a href="https://pytorch.org/docs/stable/_modules/torch/utils/data/dataloader.html" rel="noopener nofollow ugc">first parameter</a> is a <code>Dataset</code>.<br>
I provided that first parameter because <code>torchvision.datasets.MNIST(...</code> is a <code>Dataset</code>.</p>

username: blackbirdbarber
cooked: <aside class="quote no-group" data-username="krishnamurthy" data-post="2" data-topic="49417">
<div class="title">
<div class="quote-controls"></div>
<img loading="lazy" alt="" width="24" height="24" src="https://discuss.pytorch.org/letter_avatar_proxy/v4/letter/k/c0e974/48.png" class="avatar"> krishnamurthy:</div>
<blockquote>
<p>Your usage of <code>LinearTransformation()</code> appears to be inconsistent with the latest API. <code>LinearTransformation()</code> expects a <code>transformation_matrix</code> and a <code>mean_vector</code> . It seems like you’re providing only the <code>transformation_matrix</code> .</p>
</blockquote>
</aside>
<p>Yes, I tried to do that but if I recall, when I tried to set the mean, I got this error.</p>
<blockquote>
<p>TypeError: <strong>init</strong>() takes 2 positional arguments but 3 were given<br>
Seams that I cannot provide <a href="https://pytorch.org/docs/stable/torchvision/transforms.html?highlight=torchvision%20transforms%20lineartransformation#torchvision.transforms.LinearTransformation" rel="noopener nofollow ugc">second parameter</a> at all.</p>
</blockquote>
<p>PyTorch ver 1.0.0</p>

username: blackbirdbarber
cooked: <p>Does using the transform in here:</p>
<pre><code class="lang-auto">dl = DataLoader(
  torchvision.datasets.MNIST('/data/mnist', train=True, download=True,
                             transform=torchvision.transforms.Compose([
                               torchvision.transforms.ToTensor(),
                               torchvision.transforms.Normalize((0.5,), (0.5,)),
                               torchvision.transforms.LinearTransformation(trans)  
                             ])), shuffle=False)
</code></pre>
<p>really has any sense?<br>
Try the example and you will see nothing will be transformed. Am I missing something obvious?</p>

username: krishnamurthy
cooked: <aside class="quote no-group" data-username="blackbirdbarber" data-post="5" data-topic="49417">
<div class="title">
<div class="quote-controls"></div>
<img loading="lazy" alt="" width="24" height="24" src="https://discuss.pytorch.org/letter_avatar_proxy/v4/letter/b/6de8d8/48.png" class="avatar"> blackbirdbarber:</div>
<blockquote>
<p>Try the example and you will see nothing will be transformed. Am I missing something obvious?</p>
</blockquote>
</aside>
<p>Your dataloader transforms the data. What’s incorrect is that you’re accessing the train set directly, and not the output of the dataloader.</p>
<p>More concretely, you must not access <code>tr[0]</code> directly. You should rather access the dataloader output. (accessing the zeroth index is as simple as calling <code>next(iter(dl))</code>)</p>