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

How can I fix the following error?


TypeError Traceback (most recent call last)
in ()
86 ])
87
—> 88 train_dataset = data_transforms(train_dataset)
89 class_names = train_dataset.classes
90 print(class_names)

/home/test/.local/lib/python3.6/site-packages/torchvision/transforms/transforms.py in call(self, img)
59 def call(self, img):
60 for t in self.transforms:
—> 61 img = t(img)
62 return img
63

/home/test/.local/lib/python3.6/site-packages/torch/nn/modules/module.py in _call_impl(self, *input, **kwargs)
1100 if not (self._backward_hooks or self._forward_hooks or self._forward_pre_hooks or _global_backward_hooks
1101 or _global_forward_hooks or _global_forward_pre_hooks):
→ 1102 return forward_call(*input, **kwargs)
1103 # Do not call functions when jit is used
1104 full_backward_hooks, non_full_backward_hooks = [], []

/home/test/.local/lib/python3.6/site-packages/torchvision/transforms/transforms.py in forward(self, img)
302 PIL Image or Tensor: Rescaled image.
303 “”"
→ 304 return F.resize(img, self.size, self.interpolation, self.max_size, self.antialias)
305
306 def repr(self):

/home/test/.local/lib/python3.6/site-packages/torchvision/transforms/functional.py in resize(img, size, interpolation, max_size, antialias)
417 )
418 pil_interpolation = pil_modes_mapping[interpolation]
→ 419 return F_pil.resize(img, size=size, interpolation=pil_interpolation, max_size=max_size)
420
421 return F_t.resize(img, size=size, interpolation=interpolation.value, max_size=max_size, antialias=antialias)

/home/test/.local/lib/python3.6/site-packages/torchvision/transforms/functional_pil.py in resize(img, size, interpolation, max_size)
231
232 if not _is_pil_image(img):
→ 233 raise TypeError(‘img should be PIL Image. Got {}’.format(type(img)))
234 if not (isinstance(size, int) or (isinstance(size, Sequence) and len(size) in (1, 2))):
235 raise TypeError(‘Got inappropriate size arg: {}’.format(size))

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

Code

import torchvision.transforms as transforms
import torchvision.datasets as datasets
import torchvision
import torch.optim as optim
import torch.nn.functional as F
import torch.nn as nn
import torch
import time
import pandas as pd
import os
import numpy as np
import matplotlib.pyplot as plt
get_ipython().magic(‘matplotlib inline’)
from torchvision.io import read_image
from torchvision import datasets, transforms
from torchvision import datasets
from torch.utils.data.sampler import SubsetRandomSampler
from torch.utils.data import Dataset
from torch.utils.data import DataLoader
from glob import glob

torch.cuda.is_available()

if torch.cuda.is_available():
dev = “cuda:0”
else:
dev = “cpu”
device = torch.device(dev)
a = torch.zeros(4,3)
a = a.to(device)

class Data(Dataset):
def init(self, data=‘Train’ ):
self.data=data
self.CsvPath=’/home/test/PHOENIX-2014-T-release-v3/PHOENIX-2014-T/annotations/manual/PHOENIX-2014-T.’+(self.data).lower()+’.corpus.csv’
self.Csv=pd.read_csv(self.CsvPath,sep=’|’)
self.Datapath=’/home/test/PHOENIX-2014-T-release-v3/PHOENIX-2014-T/features/fullFrame-210x260px/’+(self.data).lower()+’/’

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

train_dataset = Data(‘Train’)
test_dataset = Data(‘Test’)

#Load the dataset in batches
#create dataset

batch_size = 100000
img_height = 180
img_width = 180

def imshow(inp, title=None):
“”“Imshow for Tensor.”""
inp = inp.numpy().transpose((1, 2, 0))
mean = np.array([0.485, 0.456, 0.406])
std = np.array([0.229, 0.224, 0.225])
inp = std * inp + mean
inp = np.clip(inp, 0, 1)
plt.figure(figsize=(15,15))
plt.imshow(inp)
if title is not None:
plt.title(title)
plt.pause(0.001) # pause a bit so that plots are updated

data_transforms = transforms.Compose([
transforms.Resize(224),
transforms.ToTensor(),
transforms.Normalize([0.485, 0.456, 0.406], [0.229, 0.224, 0.225])
])

train_dataset = data_transforms(train_dataset)
class_names = train_dataset.classes
print(class_names)

dataloader = torch.utils.data.DataLoader(train_dataset, batch_size=5,shuffle=True)

for i in range(1):
inputs, classes = next(iter(dataloader))

out = torchvision.utils.make_grid(inputs)  # inputs: 5, 3, 224, 224  ---> out: 3, 228, 1132
imshow(out, title=[class_names[x] for x in classes])

test_dataset = data_transforms(test_dataset)
class_names = test_dataset.classes
print(class_names)

dataloader = torch.utils.data.DataLoader(test_dataset, batch_size=5,shuffle=True)

torchvision.transforms expect PIL.Images or tensors as their input, while you are passing a Dataset object to it.
Take a look at the Data loading tutorial for an example how to use transformations.

Thanks a lot, after checking the tutorial i did some changes to the code as below:

import torchvision.transforms as transforms
import torchvision.datasets as datasets
import torchvision
import torch.optim as optim
import torch.nn.functional as F
import torch.nn as nn
import torch
import time
import pandas as pd
import os
import numpy as np
import matplotlib.pyplot as plt
get_ipython().magic(‘matplotlib inline’)
from torchvision.io import read_image
from torchvision import datasets, transforms
from torchvision import datasets
from torch.utils.data.sampler import SubsetRandomSampler
from torch.utils.data import Dataset
from torch.utils.data import DataLoader
from glob import glob

torch.cuda.is_available()

if torch.cuda.is_available():
dev = “cuda:0”
else:
dev = “cpu”
device = torch.device(dev)
a = torch.zeros(4,3)
a = a.to(device)

class Data(Dataset):
def init(self, data=‘Train’,transform=None ):
self.data=data
self.CsvPath=’/home/test/PHOENIX-2014-T-release-v3/PHOENIX-2014-T/annotations/manual/PHOENIX-2014-T.’+(self.data).lower()+’.corpus.csv’
self.Csv=pd.read_csv(self.CsvPath,sep=’|’)
self.Datapath=’/home/test/PHOENIX-2014-T-release-v3/PHOENIX-2014-T/features/fullFrame-210x260px/’+(self.data).lower()+’/’
self.transform = transform

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

train_dataset = Data(‘Train’,
transform=transforms.Compose([
transforms.RandomResizedCrop(224),
transforms.RandomHorizontalFlip(),
transforms.ToTensor(),
transforms.Normalize([0.485, 0.456, 0.406], [0.229, 0.224, 0.225])
]))
test_dataset = Data(‘Test’,
transform=transforms.Compose([
transforms.RandomResizedCrop(224),
transforms.RandomHorizontalFlip(),
transforms.ToTensor(),
transforms.Normalize([0.485, 0.456, 0.406], [0.229, 0.224, 0.225])
]))

#Load the dataset in batches
#create dataset

batch_size = 100000
img_height = 180
img_width = 180

def imshow(inp, title=None):
“”“Imshow for Tensor.”""
inp = inp.numpy().transpose((1, 2, 0))
mean = np.array([0.485, 0.456, 0.406])
std = np.array([0.229, 0.224, 0.225])
inp = std * inp + mean
inp = np.clip(inp, 0, 1)
plt.figure(figsize=(15,15))
plt.imshow(inp)
if title is not None:
plt.title(title)
plt.pause(0.001) # pause a bit so that plots are updated

dataloader = torch.utils.data.DataLoader(train_dataset, batch_size=5,shuffle=True)

for i in range(1):
inputs = next(iter(dataloader))

out = torchvision.utils.make_grid(inputs)  # inputs: 5, 3, 224, 224  ---> out: 3, 228, 1132
imshow(out)

dataloader = torch.utils.data.DataLoader(test_dataset, batch_size=5,shuffle=True)

Now i am having the below error:

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

any suggestions?

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

Here is the full error:


RuntimeError Traceback (most recent call last)
in ()
96
97 for i in range(1):
—> 98 inputs = next(iter(dataloader))
99
100 out = torchvision.utils.make_grid(inputs) # inputs: 5, 3, 224, 224 —> out: 3, 228, 1132

/home/test/.local/lib/python3.6/site-packages/torch/utils/data/dataloader.py in next(self)
519 if self._sampler_iter is None:
520 self._reset()
→ 521 data = self._next_data()
522 self._num_yielded += 1
523 if self._dataset_kind == _DatasetKind.Iterable and \

/home/test/.local/lib/python3.6/site-packages/torch/utils/data/dataloader.py in _next_data(self)
559 def _next_data(self):
560 index = self._next_index() # may raise StopIteration
→ 561 data = self._dataset_fetcher.fetch(index) # may raise StopIteration
562 if self._pin_memory:
563 data = _utils.pin_memory.pin_memory(data)

/home/test/.local/lib/python3.6/site-packages/torch/utils/data/_utils/fetch.py in fetch(self, possibly_batched_index)
50 else:
51 data = self.dataset[possibly_batched_index]
—> 52 return self.collate_fn(data)

/home/test/.local/lib/python3.6/site-packages/torch/utils/data/utils/collate.py in default_collate(batch)
54 storage = elem.storage().new_shared(numel)
55 out = elem.new(storage)
—> 56 return torch.stack(batch, 0, out=out)
57 elif elem_type.module == ‘numpy’ and elem_type.name != 'str

58 and elem_type.name != 'string
’:

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

any tutorial in order to make the tensors inputs to the same size?

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.
In your training and testing transforms you are defining RandomResizedCrops(224) which doesn’t seem to be used as this shape cannot be found in the currently used samples.