TypeError: img should be PIL Image. Got <class 'torch.Tensor'>; (Conversion from Torch Tensor to PIL Image)

username: Mona_Jalal
cooked: <p>How can I fix the following error?</p>
<pre><code class="lang-auto">test_data = data_transforms['test'](test_data)

File "/scratch/sjn-p3/anaconda/anaconda3/lib/python3.6/site-packages/torchvision-0.2.1-py3.6.egg/torchvision/transforms/transforms.py", line 49, in __call__

File "/scratch/sjn-p3/anaconda/anaconda3/lib/python3.6/site-packages/torchvision-0.2.1-py3.6.egg/torchvision/transforms/transforms.py", line 175, in __call__

File "/scratch/sjn-p3/anaconda/anaconda3/lib/python3.6/site-packages/torchvision-0.2.1-py3.6.egg/torchvision/transforms/functional.py", line 189, in resize

TypeError: img should be PIL Image. Got &lt;class 'torch.Tensor'&gt;;
</code></pre>
<p>related code is:</p>
<pre><code class="lang-auto">data_transforms = {
    'train': transforms.Compose([
        transforms.RandomResizedCrop(224),
        transforms.RandomHorizontalFlip(),
        transforms.RandomRotation(20),
        transforms.ToTensor(),
        transforms.Normalize([0.485, 0.456, 0.406], [0.229, 0.224, 0.225])
    ]),
    'test': transforms.Compose([
        transforms.Resize(224),
        transforms.ToTensor(),
        transforms.Normalize([0.485, 0.456, 0.406], [0.229, 0.224, 0.225])
    ])
}
</code></pre>
<p>and</p>
<pre><code class="lang-auto">image_datasets = {x: datasets.ImageFolder(os.path.join(data_dir, x),
                                          data_transforms[x])
                  for x in ['train']}

test_data, test_target = image_datasets['train'][idx]
</code></pre>

username: Mona_Jalal
cooked: <p>Fixed by using the following line:</p>
<pre><code class="lang-auto">test_data = data_transforms['test'](transforms.ToPILImage()(test_data))
</code></pre>
<p>Using this tutorial: <a href="https://pytorch.org/docs/master/torchvision/transforms.html#conversion-transforms" rel="nofollow noopener">https://pytorch.org/docs/master/torchvision/transforms.html#conversion-transforms</a></p>

username: namanmehta1994
cooked: <p>What does [idx] do in the following line of code.</p>
<p>test_data, test_target = image_datasets[‘train’][idx].<br>
Thanks</p>

username: drewkerwin
cooked: <p>Follow up question for Raw images using DatasetFolder</p>
<p>If I use transforms.ToTensor(), I get the following error:<br>
img should be PIL Image. Got &lt;class ‘torch.Tensor’&gt;</p>
<p>If I used transforms.ToPILImage() I get the following error:<br>
TypeError: default_collate: batch must contain tensors, numpy arrays, numbers, dicts or lists; found &lt;class ‘PIL.Image.Image’&gt;</p>
<p>If I change the return type of __raw_loader to PIL image:<br>
TypeError: pic should be Tensor or ndarray. Got &lt;class ‘PIL.Image.Image’&gt;.</p>
<p>Kind of going in circles here. What is the proper way to load a raw image using DatasetFolder?</p>
<p>Code:</p>
<pre><code class="lang-auto">import torch
import os
import pandas as pd

import numpy as np
import matplotlib.pyplot as plt
import torchvision
from torch.utils.data import Dataset, DataLoader
from torchvision import transforms, utils
from torchvision.datasets import DatasetFolder

class RawFolder(DatasetFolder):

    EXTENSIONS = ('.raw')

    def __init__(self, root, transform=None, target_transform=None, loader=None):
        

        super(RawFolder, self).__init__(root, self.__raw_loader, 
                                         self.EXTENSIONS,
                                         transform=transform,
                                         target_transform=target_transform)

    @staticmethod
    def __raw_loader(filename):
        data = np.fromfile(filename, dtype='float32').reshape(80,180)
        return Image.fromarray(data)

    def __check_file(filename):
        return os.path.isfile(filename) == True


data_transform = transforms.Compose([
        transforms.ToPILImage(),
        transforms.Grayscale(num_output_channels=1)
    ])


the_dataset = RawFolder(root=rootdir, transform=data_transform)

dataset_loader = torch.utils.data.DataLoader(the_dataset,
                                             batch_size=64, 
                                             shuffle=False,
                                             num_workers=0)

classes = ['1']

import numpy as np
# files already created, training and testing can start here.
# re-run first cell if necessary
def goshow(img):
    img = img / 2 + 0.5  # unnormalize
    plt.imshow(np.transpose(img, (1, 2, 0)))  # convert from Tensor image
    
# Visualize a few images
dataiter = iter(dataset_loader)
images, labels = dataiter.next()
images = images.numpy() # convert images to numpy for display

# plot the images in the batch, along with the corresponding labels
fig = plt.figure(figsize=(25, 12))

# display some images
for idx in np.arange(25):
    ax = fig.add_subplot(5, 5, idx+1, xticks=[], yticks=[])
    goshow(images[idx])
    ax.set_title(classes[labels[idx]])
</code></pre>
<p>Error for the last case mentioned above:<br>
Errors always occur here: images, labels = dataiter.next()</p>
<pre><code class="lang-auto">TypeError                                 Traceback (most recent call last)
&lt;ipython-input-148-ffafba196b64&gt; in &lt;module&gt;
     55 # Visualize a few images
     56 dataiter = iter(dataset_loader)
---&gt; 57 images, labels = dataiter.next()
     58 images = images.numpy() # convert images to numpy for display
     59 

~\anaconda3\lib\site-packages\torch\utils\data\dataloader.py in __next__(self)
    344     def __next__(self):
    345         index = self._next_index()  # may raise StopIteration
--&gt; 346         data = self._dataset_fetcher.fetch(index)  # may raise StopIteration
    347         if self._pin_memory:
    348             data = _utils.pin_memory.pin_memory(data)

~\anaconda3\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]

~\anaconda3\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]

~\anaconda3\lib\site-packages\torchvision\datasets\folder.py in __getitem__(self, index)
    138         sample = self.loader(path)
    139         if self.transform is not None:
--&gt; 140             sample = self.transform(sample)
    141         if self.target_transform is not None:
    142             target = self.target_transform(target)

~\anaconda3\lib\site-packages\torchvision\transforms\transforms.py in __call__(self, img)
     68     def __call__(self, img):
     69         for t in self.transforms:
---&gt; 70             img = t(img)
     71         return img
     72 

~\anaconda3\lib\site-packages\torchvision\transforms\transforms.py in __call__(self, pic)
    134 
    135         """
--&gt; 136         return F.to_pil_image(pic, self.mode)
    137 
    138     def __repr__(self):

~\anaconda3\lib\site-packages\torchvision\transforms\functional.py in to_pil_image(pic, mode)
    117     """
    118     if not(isinstance(pic, torch.Tensor) or isinstance(pic, np.ndarray)):
--&gt; 119         raise TypeError('pic should be Tensor or ndarray. Got {}.'.format(type(pic)))
    120 
    121     elif isinstance(pic, torch.Tensor):

TypeError: pic should be Tensor or ndarray. Got &lt;class 'PIL.Image.Image'&gt;.
</code></pre>

username: drewkerwin
cooked: <p>Seems like the answer is not avoid transforming the image…<br>
transform=None</p>

username: Hemantr05
cooked: <p><a class="mention" href="/u/drewkerwin">@drewkerwin</a> You can feed image as ndarray before transforming it.<br>
And then you can use:</p>
<blockquote>
<p>ToPILImage() → Resize() → ToTensor()</p>
</blockquote>
<p>It should work then.</p>

username: Mona_Jalal
cooked: <aside class="quote no-group" data-username="Mona_Jalal" data-post="2" data-topic="30494">
<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/mona_jalal/48/25715_2.png" class="avatar"> Mona_Jalal:</div>
<blockquote>
<p><code>transforms.ToPILImage()</code></p>
</blockquote>
</aside>
<p>I am having this problem here:</p><aside class="onebox githubgist" data-onebox-src="https://gist.github.com/monajalal/8f0226aba24c98235667a5f0a1216aca">
  <header class="source">

      <a href="https://gist.github.com/monajalal/8f0226aba24c98235667a5f0a1216aca" target="_blank" rel="noopener nofollow ugc">gist.github.com</a>
  </header>

  <article class="onebox-body">
    <h4><a href="https://gist.github.com/monajalal/8f0226aba24c98235667a5f0a1216aca" target="_blank" rel="noopener nofollow ugc">https://gist.github.com/monajalal/8f0226aba24c98235667a5f0a1216aca</a></h4>

  <h5>VAE_training.ipynb</h5>
  <pre><code class="Jupyter Notebook">{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",</code></pre>
    This file has been truncated. <a href="https://gist.github.com/monajalal/8f0226aba24c98235667a5f0a1216aca" target="_blank" rel="noopener nofollow ugc">show original</a>

<p>
</p>

  </article>

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

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

<p>And I am not sure how to overcome it.<br>
Could you please help me with this?<br>
Having the following line didn’t differ:<br>
<code>        ##data = transforms.ToPILImage()(data) # with or without still get the pil / tensor error</code></p>

username: Sumit_Vaish
cooked: <p>I think problem is in your Dataset class</p>