How to extract patches from an image batch using transforms.compose

maito: <p>Hi,</p>
<p>I have seen <a href="https://discuss.pytorch.org/t/using-a-special-function-as-a-transform/42760">this</a> and <a href="https://discuss.pytorch.org/t/how-to-resize-and-pad-in-a-torchvision-transforms-compose/71850/3">this</a> post, based on them I wrote the following code:</p>
<pre><code class="lang-auto">def init_cifar_dataloader(root, batchSize):
    normalize = transforms.Normalize((0.5, 0.5, 0.5), (0.5, 0.5, 0.5))
    transform_train = transforms.Compose([
        MyPatch(32,32,3),
        transforms.ToTensor(),
        normalize
    ])
    transform_test = transforms.Compose([
        MyPatch(32,32,3),
        transforms.ToTensor(),
        normalize
    ])
    
    dataset = dset.ImageFolder(root="test/0",transform=transform_train)
    print('dataset',len(dataset))

    trainset, testset = torch.utils.data.random_split(dataset, [8, 8])
    print(type(trainset))

    train_loader = DataLoader(trainset,
                              batch_size=batchSize, shuffle=True, num_workers=4, pin_memory=True)
    
    test_loader = DataLoader(testset,
                             batch_size=batchSize * 8, shuffle=False, num_workers=4, pin_memory=True)
    print(f'val set: {len(test_loader.dataset)}')

    dataiter = iter(train_loader)
    images, labels = dataiter.next()
    
#     print('trainloader',train_loader)

    
#     patch = patches(images, 32, 32, 3)
#     print(images.shape, patch.shape)

#     imshow(make_grid(patch[0:16,:,:]))
    return train_loader, test_loader

def imshow(img):
    img = img / 2 + 0.5     # unnormalize
    npimg = img.numpy()
    plt.imshow(np.transpose(npimg, (1, 2, 0)))

def patches(img, kernel, stride, channels):
    print(type(img))
    img = transforms.ToTensor()(img).unsqueeze(0)
    
    patches = img.unfold(2,kernel, stride).unfold(3,kernel, stride)
    patches = patches.contiguous().view(-1, channels, kernel, kernel)
    patches = transforms.ToPILImage()(patches.squeeze_(0))
    return patches   


class MyPatch(object):
    def __init__(self, kernel, stride, channels):
        self.kernel = kernel
        self.stride = stride
        self.channels = channels
    def __call__(self,img):
        return  patches(img, self.kernel, self.stride, self.channels)


</code></pre>
<p>However I get <code>TypeError: pic should be PIL Image or ndarray. Got &lt;class 'torch.Tensor'&gt;'</code></p>
<p>Full error:</p>
<pre><code class="lang-auto">---------------------------------------------------------------------------
ValueError                                Traceback (most recent call last)
&lt;ipython-input-190-d3a2cf03c1d9&gt; in &lt;module&gt;
     83 
     84 
---&gt; 85 init_cifar_dataloader('test',8)

&lt;ipython-input-190-d3a2cf03c1d9&gt; in init_cifar_dataloader(root, batchSize)
     48 
     49     dataiter = iter(train_loader)
---&gt; 50     images, labels = dataiter.next()
     51 
     52     print('trainloader',train_loader)

/apps/python3/lib/python3.7/site-packages/torch/utils/data/dataloader.py in __next__(self)
    343 
    344     def __next__(self):
--&gt; 345         data = self._next_data()
    346         self._num_yielded += 1
    347         if self._dataset_kind == _DatasetKind.Iterable and \

/apps/python3/lib/python3.7/site-packages/torch/utils/data/dataloader.py in _next_data(self)
    854             else:
    855                 del self._task_info[idx]
--&gt; 856                 return self._process_data(data)
    857 
    858     def _try_put_index(self):

/apps/python3/lib/python3.7/site-packages/torch/utils/data/dataloader.py in _process_data(self, data)
    879         self._try_put_index()
    880         if isinstance(data, ExceptionWrapper):
--&gt; 881             data.reraise()
    882         return data
    883 

/apps/python3/lib/python3.7/site-packages/torch/_utils.py in reraise(self)
    393             # (https://bugs.python.org/issue2651), so we work around it.
    394             msg = KeyErrorMessage(msg)
--&gt; 395         raise self.exc_type(msg)

ValueError: Caught ValueError in DataLoader worker process 0.
Original Traceback (most recent call last):
  File "/apps/python3/lib/python3.7/site-packages/torch/utils/data/_utils/worker.py", line 178, in _worker_loop
    data = fetcher.fetch(index)
  File "/apps/python3/lib/python3.7/site-packages/torch/utils/data/_utils/fetch.py", line 44, in fetch
    data = [self.dataset[idx] for idx in possibly_batched_index]
  File "/apps/python3/lib/python3.7/site-packages/torch/utils/data/_utils/fetch.py", line 44, in &lt;listcomp&gt;
    data = [self.dataset[idx] for idx in possibly_batched_index]
  File "/apps/python3/lib/python3.7/site-packages/torch/utils/data/dataset.py", line 257, in __getitem__
    return self.dataset[self.indices[idx]]
  File "/apps/python3/lib/python3.7/site-packages/torchvision/datasets/folder.py", line 137, in __getitem__
    sample = self.transform(sample)
  File "/apps/python3/lib/python3.7/site-packages/torchvision/transforms/transforms.py", line 61, in __call__
    img = t(img)
  File "&lt;ipython-input-190-d3a2cf03c1d9&gt;", line 82, in __call__
    return  patches(img, self.kernel, self.stride, self.channels)
  File "&lt;ipython-input-190-d3a2cf03c1d9&gt;", line 71, in patches
    patches = transforms.ToPILImage()(patches.squeeze_(0))
  File "/apps/python3/lib/python3.7/site-packages/torchvision/transforms/transforms.py", line 127, in __call__
    return F.to_pil_image(pic, self.mode)
  File "/apps/python3/lib/python3.7/site-packages/torchvision/transforms/functional.py", line 104, in to_pil_image
    raise ValueError('pic should be 2/3 dimensional. Got {} dimensions.'.format(pic.ndimension()))
ValueError: pic should be 2/3 dimensional. Got 4 dimensions.
</code></pre>
<p>Any help is welcome!</p>

Nikronic: <p>Hi,<br>
I think this is the line:</p>
<aside class="quote no-group" data-username="maito" data-post="1" data-topic="90156">
<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/cdc98d/48.png" class="avatar"> maito:</div>
<blockquote>
<p><code>patches = transforms.ToPILImage()(patches.squeeze_(0))</code></p>
</blockquote>
</aside>
<p>The reason is that let’s say you have an image of size <code>[3, 100, 100]</code> and then you extract 4 patches, then <code>patches</code> would be <code>[4, 3, h, w]</code> and this cannot be converted to a single image using <code>ToPILImage</code> as it only accepts 2D or 3D images.</p>
<p>I suggest that you write your own custom transformation by accepting tensors instead of converting to tensor then extracting to patches then again to image. After all, you are again converting it to tensor to use <code>normalize</code> so why not directly implement patch extraction for normalized tensor?<br>
Also, it terms of performance, this approach will be faster as you no longer have to deal with conversion overhead.</p>
<p>Bests</p>