# Image file reading：TypeError: batch must contain tensors, numbers, dicts or lists; found \<class 'PIL.Image.Image'\>

**URL:** <https://discuss.pytorch.org/t/image-file-reading-typeerror-batch-must-contain-tensors-numbers-dicts-or-lists-found-class-pil-image-image/9909>\
**Category:** Uncategorized\
**Created:** [November 14, 2017, 9:19am UTC](https://discuss.pytorch.org/t/image-file-reading-typeerror-batch-must-contain-tensors-numbers-dicts-or-lists-found-class-pil-image-image/9909 "2017-11-14T09:19:55Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![dingwang](https://discuss.pytorch.org/user_avatar/discuss.pytorch.org/dingwang/32/3021_2.png) [@dingwang](https://discuss.pytorch.org/u/dingwang)\
**Post date:** [November 14, 2017, 9:19am UTC](https://discuss.pytorch.org/t/image-file-reading-typeerror-batch-must-contain-tensors-numbers-dicts-or-lists-found-class-pil-image-image/9909/1 "2017-11-14T09:19:56Z")

</div>

I have image dataset. when I use my dataloader, I get the following error:

> File “resnet\_cub.py”, line 138, in test  
> for i,(input\_x, target) in enumerate(val\_loader):  
> File “/root/Util/miniconda/envs/py3.5/lib/python3.5/site-packages/torch/utils/data/dataloader.py”, line 179, in **next**  
> batch = self.collate\_fn([self.dataset[i] for i in indices])  
> File “/root/Util/miniconda/envs/py3.5/lib/python3.5/site-packages/torch/utils/data/dataloader.py”, line 109, in default\_collate  
> return [default\_collate(samples) for samples in transposed]  
> File “/root/Util/miniconda/envs/py3.5/lib/python3.5/site-packages/torch/utils/data/dataloader.py”, line 109, in   
> return [default\_collate(samples) for samples in transposed]  
> File “/root/Util/miniconda/envs/py3.5/lib/python3.5/site-packages/torch/utils/data/dataloader.py”, line 112, in default\_collate  
> .format(type(batch[0]))))  
> TypeError: batch must contain tensors, numbers, dicts or lists; found \<class ‘PIL.Image.Image’\>

My dataloader code is this:

> class MyDataset(Dataset):  
> def **init** (self, root, set, transform=None, target\_transform = None):  
> if set == ‘train’:  
> fname = os.path.join(root, ‘train1.txt’)  
> elif set ==‘test’:  
> fname = os.path.join(root, ‘test1.txt’)  
> fn = open(fname, ‘r’)  
> imgs =   
> for line in fn.readlines():  
> words = line.split()  
> imgs.append((words[0],int(words[1])))  
> self.root = root  
> self.imgs = imgs  
> self.transform = transform  
> self.target\_transform = transform

```
def __getitem__ (self, index):
	fn, label = self.imgs[index]
	img = Image.open(os.path.join(self.root,'images',fn)).convert('RGB')
	if self.transform is not None:
		img = self.transform(img)
	return img, torch.Tensor(label)

def __len__ (self):
	return len(self.imgs) 

```

> def test():

```
val_dataset = MyDataset(dataroot, 'train')

val_loader = DataLoader(dataset = val_dataset, batch_size = 8, shuffle = False)

pre_label = []
for i,(input_x, target) in enumerate(val_loader):
	input_var = torch.autograd.Variable(input_x)
	target_var = torch.autograd.Variable(target)
	print('.....')

```

---

<div class="post-metadata">

**Author:** ![BartolomeD](https://discuss.pytorch.org/user_avatar/discuss.pytorch.org/bartolomed/32/3365_2.png) [@BartolomeD](https://discuss.pytorch.org/u/BartolomeD)\
**Post date:** [November 14, 2017, 12:24pm UTC](https://discuss.pytorch.org/t/image-file-reading-typeerror-batch-must-contain-tensors-numbers-dicts-or-lists-found-class-pil-image-image/9909/2 "2017-11-14T12:24:59Z")

</div>

The error states that the DataLoader receives a PIL image. This is because there are no transforms made (`transform=None`) on the image. The ` __getitem__ ` method of MyDataset passes an unprocessed PIL image to the DataLoader, whereas it should receive a tensor.

You can add a transform that creates a tensor from the PIL image by adding `transform`:

```
from torchvision import transforms

transform = transforms.Compose([
    # you can add other transformations in this list
    transforms.ToTensor()
])

```

as an argument to MyDataset.

---

<div class="post-metadata">

**Author:** ![dingwang](https://discuss.pytorch.org/user_avatar/discuss.pytorch.org/dingwang/32/3021_2.png) [@dingwang](https://discuss.pytorch.org/u/dingwang)\
**Post date:** [November 15, 2017, 6:24am UTC](https://discuss.pytorch.org/t/image-file-reading-typeerror-batch-must-contain-tensors-numbers-dicts-or-lists-found-class-pil-image-image/9909/3 "2017-11-15T06:24:21Z")

</div>

The issue was solved in the way you show.Thank you! (\*￣︶￣)
