【发布时间】:2020-04-19 02:40:37
【问题描述】:
尝试将显着性图放到图像上并制作一个新的数据集
trainloader = utilsxai.load_data_cifar10(batch_size=1,test=False)
testloader = utilsxai.load_data_cifar10(batch_size=128, test=True)
这个 load_cifar10 是 torchvision
data = trainloader.dataset.data
trainloader.dataset.data = (data * sal_maps_hf).reshape(data.shape)
sal_maps_hf 形状与 (50000,32,32,3)
和 trainloader 形状 与 (50000,32,32,3)
但是当我运行这个
for idx,img in enumerate(trainloader):
----------------------------------- ---------------------------- KeyError Traceback(最近一次调用 最后)~/venv/lib/python3.7/site-packages/PIL/Image.py 在 fromarray(obj, mode) 2644 typekey = (1, 1) + shape[2:], arr["typestr"] -> 2645模式,rawmode = _fromarray_typemap[typekey] 2646除了KeyError:
KeyError: ((1, 1, 3), '
在处理上述异常的过程中,又发生了一个异常:
TypeError Traceback(最近调用 最后)在 ----> 1 show_images(trainloader)
在 show_images(trainloader) 1 def show_images(trainloader): ----> 2 for idx,(img,target) in enumerate(trainloader): 3 img = img.squeeze() 4 #pritn(img) 5 img = torch.tensor(img)
~/venv/lib/python3.7/site-packages/torch/utils/data/dataloader.py 下一个(自己) 344 def 下一个(自我): 345 index = self._next_index() # 可能会引发 StopIteration --> 346 data = self._dataset_fetcher.fetch(index) # 可能会引发 StopIteration 347如果self._pin_memory: 348 数据 = _utils.pin_memory.pin_memory(数据)
~/venv/lib/python3.7/site-packages/torch/utils/data/_utils/fetch.py 在 fetch(self, possible_batched_index) 42 def fetch(self, possible_batched_index): 43 如果 self.auto_collation: ---> 44 个数据 = [self.dataset[idx] for idx in possible_batched_index] 45 其他: 46 数据 = self.dataset[possibly_batched_index]
~/venv/lib/python3.7/site-packages/torch/utils/data/_utils/fetch.py 在 (.0) 42 def fetch(self, possible_batched_index): 43 如果 self.auto_collation: ---> 44 个数据 = [self.dataset[idx] for idx in possible_batched_index] 45 其他: 46 数据 = self.dataset[possibly_batched_index]
~/venv/lib/python3.7/site-packages/torchvision/datasets/cifar.py 在 getitem(自我,索引) 120 # 这样做是为了与所有其他数据集保持一致 121 # 返回一个 PIL 图像 --> 122 img = Image.fromarray(img) 123 124 如果 self.transform 不是 None:
~/venv/lib/python3.7/site-packages/PIL/Image.py in fromarray(obj, 模式)2645 模式,原始模式 = _fromarray_typemap[typekey] 2646,除了 KeyError: -> 2647 raise TypeError("Cannot handle this data type") 2648 else: 2649 rawmode = mode
TypeError: 无法处理此数据类型
trainloader.dataset.__getitem__
获取数据集 CIFAR10 的项 数据点数:50000 根目录:/mnt/3CE35B99003D727B/input/pytorch/data 拆分:火车 StandardTransform 转换:撰写( 调整大小(大小=32,插值=PIL.Image.BILINEAR) 到张量() )
【问题讨论】:
-
您确定您的
dataloader将数据存储为nd.array或torch.tensor?您的数据似乎存储为PIL.Images。 -
data=trainloader.dataset.data 说 numpy.ndarray
-
我认为类型无关紧要,只需分配新数据集就可以了..
-
但是你得到的错误来自
PIL.Image...你确定dataset.__getitem__实际上使用dataset.data吗?是否有可能有额外的数据表示?您必须查看数据集的代码。 -
trainloader.dataset.__getitem__ ::
标签: python image-processing pytorch torchvision dataloader