Dataloader splitting

merkede: <p>How do I split images in dataloader with pytorch</p>

ptrblck: <p><code>unfold</code> works on the specified dimension, so you would most likely have to increase the specified dimensions assuming the batch dimension is <code>dim0</code> and you don’t want to <code>unfold</code> it.</p>

merkede: <aside class="quote no-group quote-modified" data-username="merkede" data-post="1" data-topic="148630">
<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/m/278dde/48.png" class="avatar"> merkede:</div>
<blockquote>
<p>x = mnist_train[0][0][-1, :, :]</p>
</blockquote>
</aside>
<p>In the code that I found on this forum, it seems that I am setting x to be only one image (28 by 28).</p>
<p>x = mnist_train[0][0][-1, :, :]</p>
<p>But I want to do make patches for each of the images, not just one (whereas ‘x’ currently in only one 28by28 image). I then want to flatten and put through a linear layer to get a matrix.<br>
Someone suggested einops - but I’m not sure how to code this either.</p>
<p>Sorry, I’m new to deep learning.</p>
<p>Any help is appreciated.</p>

Matias_Vasquez: <p>What <span class="mention">@ptrblick</span> means is that you can use the same code you are using for all of the images, just taking into account the right dimensions.</p>
<p>So if you only have one image (like <code>x</code> in your example), you only have <code>HxW = [28x28]</code>.</p>
<p>If you had 1 RGB image, then you have to include the Channel → <code>CxHxW=[1x28x28]</code>.</p>
<p>Now if you have a batch of RGB images, this becomes → <code>[BxCxHxW]</code>.</p>
<p>For the Dataset you are using, the shape is <code>BxHxW=[60000x28x28]</code>, meaning you have 60000 images (no channel, becausse they are grayscale) of 28x28 pixels.</p>
<p>The first value you are passing to <code>unfold</code> defines in which dimension you will do the unfolding. So if you want to leave the Batch alone, and perform the same operation you just have to shift one to the right.</p>
<pre><code class="lang-python"># Dim 0 is the Batch
x = mnist_train.data.unfold(1, 7, 7).unfold(2, 7, 7)
# The new shape is
# torch.Size([60000, 4, 4, 7, 7])
</code></pre>
<p>If you now wanted to do this with many images with RGB, you also have to take this extra dimension into consideration</p>
<pre><code class="lang-python"># Dim 0 is the Batch
# Dim 1 is the Channel
x = mnist_train.data.unfold(2, 7, 7).unfold(3, 7, 7)
# The new shape is
# torch.Size([60000, CHANNEL, 4, 4, 7, 7])
</code></pre>
<p>You can compare to see that the results are the same</p>
<pre><code class="lang-python"># I tried a couple of images to see if I get the same results
img = 900

mnist_train = torchvision.datasets.FashionMNIST( root="…/data", train=True, transform=transforms.Compose([transforms.ToTensor()]), download=True)

x = mnist_train[img][0][-1, :, :]
x = x.unfold(0, 7, 7).unfold(1, 7, 7)

y = mnist_train.data.unfold(1, 7, 7).unfold(2, 7, 7)

print((y[img]/255 == x).all())
# Output:
# tensor(True)
</code></pre>
<p>If you want then to flatten the results, you can do something like:</p>
<pre><code class="lang-python">y = y.flatten(start_dim=3)
# The new shape is
# torch.Size([60000, 4, 4, 49])
</code></pre>
<p>If you also want the patches to be flat you can use this:</p>
<pre><code class="lang-python">y.reshape(-1, 16, 49)
# The new shape is
# torch.Size([60000, 16, 49])
</code></pre>
<p>Hope this helps <img src="https://discuss.pytorch.org/images/emoji/apple/smile.png?v=12" title=":smile:" class="emoji" alt=":smile:" loading="lazy" width="20" height="20"></p>