当我TypeError: pic should be PIL Image or ndarray. Got <class 'numpy.ndarray'>
尝试通过. 和DataLoader
的版本分别是torch
和。Python 的版本在机器上。torchvision
1.0.1
0.2.2.post3
3.7.1
Windows 10
这是代码:
class AndroDataset(Dataset):
def __init__(self, csv_path):
self.transform = transforms.Compose([transforms.ToTensor()])
csv_data = pd.read_csv(csv_path)
self.csv_path = csv_path
self.features = []
self.classes = []
self.features.append(csv_data.iloc[:, :-1].values)
self.classes.append(csv_data.iloc[:, -1].values)
def __getitem__(self, index):
# the error occurs here
return self.transform(self.features[index]), self.transform(self.classes[index])
def __len__(self):
return len(self.features)
我设置了加载器:
training_data = AndroDataset('android.csv')
train_loader = DataLoader(dataset=training_data, batch_size=batch_size, shuffle=True)
这是完整的错误堆栈跟踪:
Traceback (most recent call last):
File "C:\Program Files\JetBrains\PyCharm 2018.1.2\helpers\pydev\pydevd.py", line 1758, in <module>
main()
File "C:\Program Files\JetBrains\PyCharm 2018.1.2\helpers\pydev\pydevd.py", line 1752, in main
globals = debugger.run(setup['file'], None, None, is_module)
File "C:\Program Files\JetBrains\PyCharm 2018.1.2\helpers\pydev\pydevd.py", line 1147, in run
pydev_imports.execfile(file, globals, locals) # execute the script
File "C:\Program Files\JetBrains\PyCharm 2018.1.2\helpers\pydev\_pydev_imps\_pydev_execfile.py", line 18, in execfile
exec(compile(contents+"\n", file, 'exec'), glob, loc)
File "C:/Users/talha/Documents/PyCharmProjects/DeepAndroid/deep_test_conv1d.py", line 231, in <module>
main()
File "C:/Users/talha/Documents/PyCharmProjects/DeepAndroid/deep_test_conv1d.py", line 149, in main
for i, (images, labels) in enumerate(train_loader):
File "C:\Users\talha\Documents\PyCharmProjects\DeepAndroid\venv\lib\site-packages\torch\utils\data\dataloader.py", line 615, in __next__
batch = self.collate_fn([self.dataset[i] for i in indices])
File "C:\Users\talha\Documents\PyCharmProjects\DeepAndroid\venv\lib\site-packages\torch\utils\data\dataloader.py", line 615, in <listcomp>
batch = self.collate_fn([self.dataset[i] for i in indices])
File "C:/Users/talha/Documents/PyCharmProjects/DeepAndroid/deep_test_conv1d.py", line 102, in __getitem__
return self.transform(self.features[index]), self.transform(self.classes[index])
File "C:\Users\talha\Documents\PyCharmProjects\DeepAndroid\venv\lib\site-packages\torchvision\transforms\transforms.py", line 60, in __call__
img = t(img)
File "C:\Users\talha\Documents\PyCharmProjects\DeepAndroid\venv\lib\site-packages\torchvision\transforms\transforms.py", line 91, in __call__
return F.to_tensor(pic)
File "C:\Users\talha\Documents\PyCharmProjects\DeepAndroid\venv\lib\site-packages\torchvision\transforms\functional.py", line 50, in to_tensor
raise TypeError('pic should be PIL Image or ndarray. Got {}'.format(type(pic)))
TypeError: pic should be PIL Image or ndarray. Got <class 'numpy.ndarray'>