2
images, labels = next(iter(self.loader))
grid = torchvision.utils.make_grid(images)

图片,标签=下一个(iter(self.loader))

触发错误。

我有一个自定义数据集类,我从 url 加载每个图像(RGB):

图像 = Image.open(urllib.request.urlopen(URL))

我应用了一些标注变换。

当我使用 cv2 读取具有路径的图像时,该代码有效。但是,当我从 url 读取图像时,它不起作用。请注意,我已验证网址没有损坏。

这是回溯:

/usr/local/lib/python3.6/dist-packages/torch/utils/data/dataloader.py in __next__(self)
    344     def __next__(self):
    345         index = self._next_index()  # may raise StopIteration
--> 346         data = self._dataset_fetcher.fetch(index)  # may raise StopIteration
    347         if self._pin_memory:
    348             data = _utils.pin_memory.pin_memory(data)

/usr/local/lib/python3.6/dist-packages/torch/utils/data/_utils/fetch.py in fetch(self, possibly_batched_index)
     42     def fetch(self, possibly_batched_index):
     43         if self.auto_collation:
---> 44             data = [self.dataset[idx] for idx in possibly_batched_index]
     45         else:
     46             data = self.dataset[possibly_batched_index]

/usr/local/lib/python3.6/dist-packages/torch/utils/data/_utils/fetch.py in <listcomp>(.0)
     42     def fetch(self, possibly_batched_index):
     43         if self.auto_collation:
---> 44             data = [self.dataset[idx] for idx in possibly_batched_index]
     45         else:
     46             data = self.dataset[possibly_batched_index]

/content/transform_dataset.py in __getitem__(self, idx)
     49     labels = torch.from_numpy(item[2:].values.astype("float32"))
     50     #print("self.root,item,self.image_transform,self.transform,self.size", self.root,item,self.image_transform,self.transform,self.size)
---> 51     image = load_image(self.root,item.ID,item.URL,self.image_transform)
     52     return image, labels
     53 

/content/transform_dataset.py in load_image(root, ID, URL, image_transform)
     81     print(image.shape)
     82     image = cv2.cvtColor(image,cv2.COLOR_BGR2RGB)
---> 83   image = image_transform(image=image)["image"]
     84   return image

/usr/local/lib/python3.6/dist-packages/albumentations/core/composition.py in __call__(self, **data)
    169                                               convert_keypoints_to_albumentations, data)
    170 
--> 171             data = t(**data)
    172 
    173             if dual_start_end is not None and idx == dual_start_end[1]:

/usr/local/lib/python3.6/dist-packages/albumentations/core/transforms_interface.py in __call__(self, **kwargs)
     26         if (random.random() < self.p) or self.always_apply:
     27             params = self.get_params()
---> 28             params = self.update_params(params, **kwargs)
     29             if self.targets_as_params:
     30                 targets_as_params = {k: kwargs[k] for k in self.targets_as_params}

/usr/local/lib/python3.6/dist-packages/albumentations/core/transforms_interface.py in update_params(self, params, **kwargs)
     66         if hasattr(self, 'interpolation'):
     67             params['interpolation'] = self.interpolation
---> 68         params.update({'cols': kwargs['image'].shape[1], 'rows': kwargs['image'].shape[0]})
     69         return params
     70 

AttributeError: 'MpoImageFile' object has no attribute 'shape'
4

1 回答 1

3

为了使用albumentations,您必须将一个numpy数组传递给转换而不是PIL图像。所以:

image = Image.open(urllib.request.urlopen(URL))
image = np.array(image)
于 2020-01-07T08:03:45.557 回答