ValueError: tile cannot extend outside image pls help

AAYUSH_LAKHANI: <p>I am loading NumPy arrays as images in PyTorch while training the model, it’s giving me this error, I tried everything but couldn’t figure out pls help…, I am training a classifier model…</p>
<hr>
<pre><code>ValueError                                Traceback (most recent call last)
&lt;ipython-input-12-b4d3f7be01c1&gt; in &lt;module&gt;
      1 # training
----&gt; 2 trained_model = train(n_epochs, np.Inf, loaders, model, optimizer, criterion)

&lt;ipython-input-10-b4d180a2c041&gt; in train(n_epochs, valid_loss_min_input, loaders, model, optimizer, criterion, device, checkpoint_path, best_model_path)
     29         ###################
     30         model.train()
---&gt; 31         for batch_idx, (data, target) in enumerate(loaders['train']):
     32             # move to gpu
     33             data, target = data.to(device), target.to(device)

G:\anaconda3\envs\data_env\lib\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 \

G:\anaconda3\envs\data_env\lib\site-packages\torch\utils\data\dataloader.py in _next_data(self)
    383     def _next_data(self):
    384         index = self._next_index()  # may raise StopIteration
--&gt; 385         data = self._dataset_fetcher.fetch(index)  # may raise StopIteration
    386         if self._pin_memory:
    387             data = _utils.pin_memory.pin_memory(data)

G:\anaconda3\envs\data_env\lib\site-packages\torch\utils\data\_utils\fetch.py in fetch(self, possibly_batched_index)
     42     def fetch(self, possibly_batched_index):
     43         if self.auto_collation:
---&gt; 44             data = [self.dataset[idx] for idx in possibly_batched_index]
     45         else:
     46             data = self.dataset[possibly_batched_index]

G:\anaconda3\envs\data_env\lib\site-packages\torch\utils\data\_utils\fetch.py in &lt;listcomp&gt;(.0)
     42     def fetch(self, possibly_batched_index):
     43         if self.auto_collation:
---&gt; 44             data = [self.dataset[idx] for idx in possibly_batched_index]
     45         else:
     46             data = self.dataset[possibly_batched_index]

G:\anaconda3\envs\data_env\lib\site-packages\torchvision\datasets\folder.py in __getitem__(self, index)
    135         sample = self.loader(path)
    136         if self.transform is not None:
--&gt; 137             sample = self.transform(sample)
    138         if self.target_transform is not None:
    139             target = self.target_transform(target)

G:\anaconda3\envs\data_env\lib\site-packages\torchvision\transforms\transforms.py in __call__(self, img)
     59     def __call__(self, img):
     60         for t in self.transforms:
---&gt; 61             img = t(img)
     62         return img
     63 

&lt;ipython-input-3-88cdee8f0d6c&gt; in __call__(self, img)
      7         im = np.asarray(img)
      8         im = detect(im)
----&gt; 9         img = Image.fromarray(im)
     10         img = img.resize(size=(128, 128))
     11         return img

G:\anaconda3\envs\data_env\lib\site-packages\PIL\Image.py in fromarray(obj, mode)
   2768             obj = obj.tostring()
   2769 
-&gt; 2770     return frombuffer(mode, size, obj, "raw", rawmode, 0, 1)
   2771 
   2772 

G:\anaconda3\envs\data_env\lib\site-packages\PIL\Image.py in frombuffer(mode, size, data, decoder_name, *args)
   2708             return im
   2709 
-&gt; 2710     return frombytes(mode, size, data, decoder_name, args)
   2711 
   2712 

G:\anaconda3\envs\data_env\lib\site-packages\PIL\Image.py in frombytes(mode, size, data, decoder_name, *args)
   2648 
   2649     im = new(mode, size)
-&gt; 2650     im.frombytes(data, decoder_name, args)
   2651     return im
   2652 

G:\anaconda3\envs\data_env\lib\site-packages\PIL\Image.py in frombytes(self, data, decoder_name, *args)
    795         # unpack data
    796         d = _getdecoder(self.mode, decoder_name, args)
--&gt; 797         d.setimage(self.im)
    798         s = d.decode(data)
    799 

ValueError: tile cannot extend outside the image
</code></pre>
<p><div class="lightbox-wrapper"><a class="lightbox" href="https://discuss.pytorch.org/uploads/default/original/3X/0/2/0200576b5bbe9495ca7560caf5390c1e46c210ca.jpeg" data-download-href="https://discuss.pytorch.org/uploads/default/0200576b5bbe9495ca7560caf5390c1e46c210ca" title="test"><img src="https://discuss.pytorch.org/uploads/default/original/3X/0/2/0200576b5bbe9495ca7560caf5390c1e46c210ca.jpeg" alt="test" data-base62-sha1="hHGz94nTqkelI9syDE7ErODYfM" width="556" height="500" data-dominant-color="474748"><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">test</span><span class="informations">563×506 30.4 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>In the custom function, I am trying to preprocess the image and then calling it<br>
It works fine when I test out the code loading a small batch for display</p>

ptrblck: <p>It seems this error is raised by <code>PIL</code> and <a href="https://github.com/python-pillow/Pillow/issues/3044#issuecomment-375207867">this</a> might be a valid workaround.</p>

Roshan_IIITD: <p><a class="mention" href="/u/aayush_lakhani">@AAYUSH_LAKHANI</a> I also faced the same problem. Did you get any solution to it?</p>