Reputation: 7676
I am attempting to import images for use with torch and torchvision. But I am receiving this error:
TypeError: Caught TypeError in DataLoader worker process 0.
Original Traceback (most recent call last):
File "c:\python38\lib\site-packages\torch\utils\data\_utils\worker.py", line 178, in _worker_loop
data = fetcher.fetch(index)
File "c:\python38\lib\site-packages\torch\utils\data\_utils\fetch.py", line 47, in fetch
return self.collate_fn(data)
File "c:\python38\lib\site-packages\torch\utils\data\_utils\collate.py", line 79, in default_collate
return [default_collate(samples) for samples in transposed]
File "c:\python38\lib\site-packages\torch\utils\data\_utils\collate.py", line 79, in <listcomp>
return [default_collate(samples) for samples in transposed]
File "c:\python38\lib\site-packages\torch\utils\data\_utils\collate.py", line 81, in default_collate
raise TypeError(default_collate_err_msg_format.format(elem_type))
TypeError: default_collate: batch must contain tensors, numpy arrays, numbers, dicts or lists; found <class 'PIL.Image.Image'>
Based on this post, I am converting them to Tensor:
Here is my code:
import torch
import torchvision
import torchvision.transforms
from torchvision import datasets, transforms
transform = transforms.Compose([
transforms.Resize(256),
transforms.ToTensor()
])
dataset = torchvision.datasets.ImageFolder('datasets')
dataloader = torch.utils.data.DataLoader(dataset,
batch_size=16,
shuffle=True,
num_workers=12)
tensor_dataset = []
for i, data in enumerate(dataloader, 0):
Tensor = torch.tensor(data)
tensor_dataset.append(Tensor.flatten)
The first last part is from https://github.com/TerragonDE/PyTorch but I have had no success. The data I am trying to load is from here:
http://www.cvlibs.net/datasets/kitti/
How can I solve this?
UPDATE:
Thanks @trialNerror, but now I am getting this error:
ValueError Traceback (most recent call last)
<ipython-input-6-aa72392b67e8> in <module>
1 for i, data in enumerate(dataloader, 0):
----> 2 Tensor = torch.tensor(data)
3 tensor_dataset.append(Tensor.flatten)
ValueError: only one element tensors can be converted to Python scalars
This is what I have found so far but am not sure how to apply it:
UPDATE 2:
The reason why I didn't end up using the dataloader is because I end up getting this error:
num_epochs = 10
loss_values = list()
for epoch in range(1, num_epochs):
for i, data in enumerate(train_array, 0):
outputs = model(data.unsqueeze(0))
loss = criterion(outputs,data.unsqueeze(0))
optimizer.zero_grad()
loss.backward()
optimizer.step()
print('Epoch - %d, loss - %0.5f '%(epoch, loss.item()))
loss_values.append(loss.item())
torch.Size([1, 16, 198, 660]) torch.Size([1, 32, 97, 328]) torch.Size([1, 1018112])
RuntimeError Traceback (most recent call last)
<ipython-input-106-5e6fa86df079> in <module>
4 for epoch in range(1, num_epochs):
5 for i, data in enumerate(train_array, 0):
----> 6 outputs = model(data.unsqueeze(0))
7 loss = criterion(outputs,data.unsqueeze(0))
8
c:\python38\lib\site-packages\torch\nn\modules\module.py in __call__(self, *input, **kwargs)
548 result = self._slow_forward(*input, **kwargs)
549 else:
--> 550 result = self.forward(*input, **kwargs)
551 for hook in self._forward_hooks.values():
552 hook_result = hook(self, input, result)
<ipython-input-90-467a3f84a03f> in forward(self, x)
29 print(out.shape)
30
---> 31 out = self.fc(out)
32 print(out.shape)
33
c:\python38\lib\site-packages\torch\nn\modules\module.py in __call__(self, *input, **kwargs)
548 result = self._slow_forward(*input, **kwargs)
549 else:
--> 550 result = self.forward(*input, **kwargs)
551 for hook in self._forward_hooks.values():
552 hook_result = hook(self, input, result)
c:\python38\lib\site-packages\torch\nn\modules\linear.py in forward(self, input)
85
86 def forward(self, input):
---> 87 return F.linear(input, self.weight, self.bias)
88
89 def extra_repr(self):
c:\python38\lib\site-packages\torch\nn\functional.py in linear(input, weight, bias)
1608 if input.dim() == 2 and bias is not None:
1609 # fused op is marginally faster
-> 1610 ret = torch.addmm(bias, input, weight.t())
1611 else:
1612 output = input.matmul(weight.t())
RuntimeError: size mismatch, m1: [1 x 1018112], m2: [512 x 10] at C:\w\b\windows\pytorch\aten\src\TH/generic/THTensorMath.cpp:41
I realize that if you have m1: [a * b] and m2: [c * d] then b and c have to be the same value, but I am not sure, what is the best way to resize my images?
Upvotes: 0
Views: 6527
Reputation: 7676
Please note that I had wanted to automatically load all PNG images in a directory as pytorch tensors. I had previously looked at posts like this (and many other web pages):
Loading a huge dataset batch-wise to train pytorch
But instead I ended up using this where I am using Image.open
one by one on all the images in the directory instead of the torch DataLoader
:
import numpy as np
from PIL import Image
import matplotlib.pyplot as plt
image = Image.open("datasets/image_02/data/my_image.png").convert('RGB')
import torch
import torch.nn as nn
import torch.nn.functional as F
from torchvision import transforms as tf
transforms = tf.Compose([tf.Resize(400),
tf.ToTensor()])
img_tensor = transforms(image)
Upvotes: 1
Reputation: 3553
Your transform
variable is unused, it should be passed to the Dataset constructor:
`dataset = torchvision.datasets.ImageFolder('datasets', transform=transform)`
Because of that, the ToTensor
is never applied to your data, and thus they remain PIL images, not tensors.
Upvotes: 1