pic should be PIL Image or ndarray. Got <class ‘torch.Tensor‘>

今天遇到图像转换成张量的问题:

Traceback (most recent call last):
File “D:\PythonPRO\PytorchPro\G1\r9.py”, line 151, in
for j, data in enumerate(valid_loader):
File “D:\ProgramData\Anaconda3\envs\pytorch_1.7.1_gpu\lib\site-packages\torch\utils\data\dataloader.py”, line 436, in next
data = self._next_data()
File “D:\ProgramData\Anaconda3\envs\pytorch_1.7.1_gpu\lib\site-packages\torch\utils\data\dataloader.py”, line 476, in _next_data
data = self._dataset_fetcher.fetch(index) # may raise StopIteration
File “D:\ProgramData\Anaconda3\envs\pytorch_1.7.1_gpu\lib\site-packages\torch\utils\data_utils\fetch.py”, line 44, in fetch
data = [self.dataset[idx] for idx in possibly_batched_index]
File “D:\ProgramData\Anaconda3\envs\pytorch_1.7.1_gpu\lib\site-packages\torch\utils\data_utils\fetch.py”, line 44, in
data = [self.dataset[idx] for idx in possibly_batched_index]
File “D:\PythonPRO\PytorchPro\G1\tools\my_dataset.py”, line 35, in getitem
img = self.transform(img) # 在这里做transform,转为tensor等等
File “D:\ProgramData\Anaconda3\envs\pytorch_1.7.1_gpu\lib\site-packages\torchvision\transforms\transforms.py”, line 67, in call
img = t(img)
File “D:\ProgramData\Anaconda3\envs\pytorch_1.7.1_gpu\lib\site-packages\torchvision\transforms\transforms.py”, line 104, in call
return F.to_tensor(pic)
File “D:\ProgramData\Anaconda3\envs\pytorch_1.7.1_gpu\lib\site-packages\torchvision\transforms\functional.py”, line 68, 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

Process finished with exit code 1

解决:

经过仔细检查,还真有一段多写了一个transforms.ToTensor()。
pic should be PIL Image or ndarray. Got <class ‘torch.Tensor‘>_第1张图片

你可能感兴趣的:(问题,np问题)