TypeError: img should be PIL Image. Got __main__.Data

Anonymou5: <p>How can I fix the following error?</p>
<hr>
<p>TypeError                                 Traceback (most recent call last)<br>
 in ()<br>
86 ])<br>
87<br>
—&gt; 88 train_dataset =  data_transforms(train_dataset)<br>
89 class_names = train_dataset.classes<br>
90 print(class_names)</p>
<p>/home/test/.local/lib/python3.6/site-packages/torchvision/transforms/transforms.py in <strong>call</strong>(self, img)<br>
59     def <strong>call</strong>(self, img):<br>
60         for t in self.transforms:<br>
—&gt; 61             img = t(img)<br>
62         return img<br>
63</p>
<p>/home/test/.local/lib/python3.6/site-packages/torch/nn/modules/module.py in _call_impl(self, *input, **kwargs)<br>
1100         if not (self._backward_hooks or self._forward_hooks or self._forward_pre_hooks or _global_backward_hooks<br>
1101                 or _global_forward_hooks or _global_forward_pre_hooks):<br>
 → 1102             return forward_call(*input, **kwargs)<br>
1103         # Do not call functions when jit is used<br>
1104         full_backward_hooks, non_full_backward_hooks = [], []</p>
<p>/home/test/.local/lib/python3.6/site-packages/torchvision/transforms/transforms.py in forward(self, img)<br>
302             PIL Image or Tensor: Rescaled image.<br>
303         “”"<br>
 → 304         return F.resize(img, self.size, self.interpolation, self.max_size, self.antialias)<br>
305<br>
306     def <strong>repr</strong>(self):</p>
<p>/home/test/.local/lib/python3.6/site-packages/torchvision/transforms/functional.py in resize(img, size, interpolation, max_size, antialias)<br>
417             )<br>
418         pil_interpolation = pil_modes_mapping[interpolation]<br>
 → 419         return F_pil.resize(img, size=size, interpolation=pil_interpolation, max_size=max_size)<br>
420<br>
421     return F_t.resize(img, size=size, interpolation=interpolation.value, max_size=max_size, antialias=antialias)</p>
<p>/home/test/.local/lib/python3.6/site-packages/torchvision/transforms/functional_pil.py in resize(img, size, interpolation, max_size)<br>
231<br>
232     if not _is_pil_image(img):<br>
 → 233         raise TypeError(‘img should be PIL Image. Got {}’.format(type(img)))<br>
234     if not (isinstance(size, int) or (isinstance(size, Sequence) and len(size) in (1, 2))):<br>
235         raise TypeError(‘Got inappropriate size arg: {}’.format(size))</p>
<p>TypeError: img should be PIL Image. Got <strong>main</strong>.Data</p>
<p><strong>Code</strong></p>
<p>import torchvision.transforms as transforms<br>
import torchvision.datasets as datasets<br>
import torchvision<br>
import torch.optim as optim<br>
import torch.nn.functional as F<br>
import torch.nn as nn<br>
import torch<br>
import time<br>
import pandas as pd<br>
import os<br>
import numpy as np<br>
import matplotlib.pyplot as plt<br>
get_ipython().magic(‘matplotlib inline’)<br>
from <a href="http://torchvision.io" rel="noopener nofollow ugc">torchvision.io</a> import read_image<br>
from torchvision import datasets, transforms<br>
from torchvision import datasets<br>
from torch.utils.data.sampler import SubsetRandomSampler<br>
from torch.utils.data import Dataset<br>
from torch.utils.data import DataLoader<br>
from glob import glob</p>
<p>torch.cuda.is_available()</p>
<p>if torch.cuda.is_available():<br>
dev = “cuda:0”<br>
else:<br>
dev = “cpu”<br>
device = torch.device(dev)<br>
a = torch.zeros(4,3)<br>
a = a.to(device)</p>
<p>class Data(Dataset):<br>
def <strong>init</strong>(self, data=‘Train’ ):<br>
self.data=data<br>
self.CsvPath=’/home/test/PHOENIX-2014-T-release-v3/PHOENIX-2014-T/annotations/manual/PHOENIX-2014-T.’+(self.data).lower()+’.corpus.csv’<br>
self.Csv=pd.read_csv(self.CsvPath,sep=’|’)<br>
self.Datapath=’/home/test/PHOENIX-2014-T-release-v3/PHOENIX-2014-T/features/fullFrame-210x260px/’+(self.data).lower()+’/’</p>
<pre><code>def __len__(self):
    return len(self.Csv)

def getVideo(self,idx):

    paths=glob(self.Datapath+self.Csv.video.values[idx].split('/')[0]+'/*.png', recursive = True)
    Images = [torch.unsqueeze(read_image(i),-1)  for i in paths[::2]]

    out=torch.concat(Images,-1).transpose(0, -1)[: ,::2,::2,:]

    return out

def __getitem__(self, idx,maxi=100):

    video= self.getVideo(idx)
    return video
</code></pre>
<p>train_dataset = Data(‘Train’)<br>
test_dataset  = Data(‘Test’)</p>
<p><span class="hashtag">#Load</span> the dataset in batches<br>
<span class="hashtag">#create</span> dataset</p>
<p>batch_size = 100000<br>
img_height = 180<br>
img_width = 180</p>
<p>def imshow(inp, title=None):<br>
“”“Imshow for Tensor.”""<br>
inp = inp.numpy().transpose((1, 2, 0))<br>
mean = np.array([0.485, 0.456, 0.406])<br>
std = np.array([0.229, 0.224, 0.225])<br>
inp = std * inp + mean<br>
inp = np.clip(inp, 0, 1)<br>
plt.figure(figsize=(15,15))<br>
plt.imshow(inp)<br>
if title is not None:<br>
plt.title(title)<br>
plt.pause(0.001)  # pause a bit so that plots are updated</p>
<p>data_transforms = transforms.Compose([<br>
transforms.Resize(224),<br>
transforms.ToTensor(),<br>
transforms.Normalize([0.485, 0.456, 0.406], [0.229, 0.224, 0.225])<br>
])</p>
<p>train_dataset =  data_transforms(train_dataset)<br>
class_names = train_dataset.classes<br>
print(class_names)</p>
<p>dataloader = torch.utils.data.DataLoader(train_dataset, batch_size=5,shuffle=True)</p>
<p>for i in range(1):<br>
inputs, classes = next(iter(dataloader))</p>
<pre><code>out = torchvision.utils.make_grid(inputs)  # inputs: 5, 3, 224, 224  ---&gt; out: 3, 228, 1132
imshow(out, title=[class_names[x] for x in classes])
</code></pre>
<p>test_dataset =  data_transforms(test_dataset)<br>
class_names = test_dataset.classes<br>
print(class_names)</p>
<p>dataloader = torch.utils.data.DataLoader(test_dataset, batch_size=5,shuffle=True)</p>

ptrblck: <p><code>torchvision.transforms</code> expect <code>PIL.Image</code>s or tensors as their input, while you are passing a <code>Dataset</code> object to it.<br>
Take a look at the <a href="https://pytorch.org/tutorials/beginner/data_loading_tutorial.html">Data loading tutorial</a> for an example how to use transformations.</p>

Anonymou5: <p>Thanks a lot, after checking the tutorial i did some changes to the code as below:</p>
<blockquote>
<p>import torchvision.transforms as transforms<br>
import torchvision.datasets as datasets<br>
import torchvision<br>
import torch.optim as optim<br>
import torch.nn.functional as F<br>
import torch.nn as nn<br>
import torch<br>
import time<br>
import pandas as pd<br>
import os<br>
import numpy as np<br>
import matplotlib.pyplot as plt<br>
get_ipython().magic(‘matplotlib inline’)<br>
from <a href="http://torchvision.io" rel="noopener nofollow ugc">torchvision.io</a> import read_image<br>
from torchvision import datasets, transforms<br>
from torchvision import datasets<br>
from torch.utils.data.sampler import SubsetRandomSampler<br>
from torch.utils.data import Dataset<br>
from torch.utils.data import DataLoader<br>
from glob import glob</p>
<p>torch.cuda.is_available()</p>
<p>if torch.cuda.is_available():<br>
dev = “cuda:0”<br>
else:<br>
dev = “cpu”<br>
device = torch.device(dev)<br>
a = torch.zeros(4,3)<br>
a = a.to(device)</p>
<p>class Data(Dataset):<br>
def <strong>init</strong>(self, data=‘Train’,transform=None ):<br>
self.data=data<br>
self.CsvPath=‘/home/test/PHOENIX-2014-T-release-v3/PHOENIX-2014-T/annotations/manual/PHOENIX-2014-T.’+(self.data).lower()+‘.corpus.csv’<br>
self.Csv=pd.read_csv(self.CsvPath,sep=‘|’)<br>
self.Datapath=‘/home/test/PHOENIX-2014-T-release-v3/PHOENIX-2014-T/features/fullFrame-210x260px/’+(self.data).lower()+‘/’<br>
self.transform = transform</p>
<pre><code>def __len__(self):
    return len(self.Csv)

def getVideo(self,idx):

    paths=glob(self.Datapath+self.Csv.video.values[idx].split('/')[0]+'/*.png', recursive = True)
    Images = [torch.unsqueeze(read_image(i),-1)  for i in paths[::2]]

    out=torch.concat(Images,-1).transpose(0, -1)[: ,::2,::2,:]

    return out

def __getitem__(self, idx,maxi=100):

    video= self.getVideo(idx)
    return video
</code></pre>
<p>train_dataset = Data(‘Train’,<br>
transform=transforms.Compose([<br>
transforms.RandomResizedCrop(224),<br>
transforms.RandomHorizontalFlip(),<br>
transforms.ToTensor(),<br>
transforms.Normalize([0.485, 0.456, 0.406], [0.229, 0.224, 0.225])<br>
]))<br>
test_dataset  = Data(‘Test’,<br>
transform=transforms.Compose([<br>
transforms.RandomResizedCrop(224),<br>
transforms.RandomHorizontalFlip(),<br>
transforms.ToTensor(),<br>
transforms.Normalize([0.485, 0.456, 0.406], [0.229, 0.224, 0.225])<br>
]))</p>
<p><span class="hashtag-raw">#Load</span> the dataset in batches<br>
<span class="hashtag-raw">#create</span> dataset</p>
<p>batch_size = 100000<br>
img_height = 180<br>
img_width = 180</p>
<p>def imshow(inp, title=None):<br>
“”“Imshow for Tensor.”“”<br>
inp = inp.numpy().transpose((1, 2, 0))<br>
mean = np.array([0.485, 0.456, 0.406])<br>
std = np.array([0.229, 0.224, 0.225])<br>
inp = std * inp + mean<br>
inp = np.clip(inp, 0, 1)<br>
plt.figure(figsize=(15,15))<br>
plt.imshow(inp)<br>
if title is not None:<br>
plt.title(title)<br>
plt.pause(0.001)  # pause a bit so that plots are updated</p>
<p>dataloader = torch.utils.data.DataLoader(train_dataset, batch_size=5,shuffle=True)</p>
<p>for i in range(1):<br>
inputs = next(iter(dataloader))</p>
<pre><code>out = torchvision.utils.make_grid(inputs)  # inputs: 5, 3, 224, 224  ---&gt; out: 3, 228, 1132
imshow(out)
</code></pre>
<p>dataloader = torch.utils.data.DataLoader(test_dataset, batch_size=5,shuffle=True)</p>
</blockquote>
<p>Now i am having the below error:</p>
<blockquote>
<p>RuntimeError: stack expects each tensor to be equal size, but got [95, 130, 105, 3] at entry 0 and [47, 130, 105, 3] at entry 1</p>
</blockquote>
<p>any suggestions?</p>

ptrblck: <p>I don’t know which line of code raises the error, but make sure <code>torch.stack</code> gets tensor inputs which have the same shape in all dimensions.</p>

Anonymou5: <aside class="quote no-group" data-username="ptrblck" data-post="4" data-topic="149659">
<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/ptrblck/48/1823_2.png" class="avatar"> ptrblck:</div>
<blockquote>
<p>tensor inputs which have the same shape in all dimensions.</p>
</blockquote>
</aside>
<p>Here is the full error:</p>
<blockquote>
<hr>
<p>RuntimeError                              Traceback (most recent call last)<br>
 in ()<br>
96<br>
97 for i in range(1):<br>
—&gt; 98     inputs = next(iter(dataloader))<br>
99<br>
100     out = torchvision.utils.make_grid(inputs)  # inputs: 5, 3, 224, 224  —&gt; out: 3, 228, 1132</p>
<p>/home/test/.local/lib/python3.6/site-packages/torch/utils/data/dataloader.py in <strong>next</strong>(self)<br>
519             if self._sampler_iter is None:<br>
520                 self._reset()<br>
 → 521             data = self._next_data()<br>
522             self._num_yielded += 1<br>
523             if self._dataset_kind == _DatasetKind.Iterable and \</p>
<p>/home/test/.local/lib/python3.6/site-packages/torch/utils/data/dataloader.py in _next_data(self)<br>
559     def _next_data(self):<br>
560         index = self._next_index()  # may raise StopIteration<br>
 → 561         data = self._dataset_fetcher.fetch(index)  # may raise StopIteration<br>
562         if self._pin_memory:<br>
563             data = _utils.pin_memory.pin_memory(data)</p>
<p>/home/test/.local/lib/python3.6/site-packages/torch/utils/data/_utils/fetch.py in fetch(self, possibly_batched_index)<br>
50         else:<br>
51             data = self.dataset[possibly_batched_index]<br>
—&gt; 52         return self.collate_fn(data)</p>
<p>/home/test/.local/lib/python3.6/site-packages/torch/utils/data/<em>utils/collate.py in default_collate(batch)<br>
54             storage = elem.storage().<em>new_shared(numel)<br>
55             out = elem.new(storage)<br>
—&gt; 56         return torch.stack(batch, 0, out=out)<br>
57     elif elem_type.<strong>module</strong> == ‘numpy’ and elem_type.<strong>name</strong> != 'str</em>’ <br>
58             and elem_type.<strong>name</strong> != 'string</em>’:</p>
<p>RuntimeError: stack expects each tensor to be equal size, but got [112, 130, 105, 3] at entry 0 and [75, 130, 105, 3] at entry 1</p>
</blockquote>
<p>any tutorial in order to make the tensors inputs to the same size?</p>

ptrblck: <p>It seems as if you are not using the defined transformations, since the failing samples have 4 dimensions (could you explain what which dimension refers to), have different shapes, and also seem to be in the channels-last format.<br>
In your training and testing transforms you are defining <code>RandomResizedCrops(224)</code> which doesn’t seem to be used as this shape cannot be found in the currently used samples.</p>