首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >AttributeError:'MpoImageFile‘对象没有属性'shape’

AttributeError:'MpoImageFile‘对象没有属性'shape’
EN

Stack Overflow用户
提问于 2020-01-06 14:18:17
回答 1查看 995关注 0票数 2
代码语言:javascript
复制
images, labels = next(iter(self.loader))
grid = torchvision.utils.make_grid(images)

图像,标签=下一步(iter(self.loader))

触发错误。

我有一个自定义dataset类,其中从url加载每个图像(RGB):

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

我用了一些变体。

当我使用cv2读取有路径的图像时,代码就会工作。但是,当我从url中读取图片时,它不起作用。请注意,我验证了urls没有损坏。

这是回溯:

代码语言:javascript
复制
/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'
EN

回答 1

Stack Overflow用户

发布于 2020-01-07 08:03:45

为了处理所有内容,您必须将一个numpy数组传递给转换而不是PIL图像。所以:

代码语言:javascript
复制
image = Image.open(urllib.request.urlopen(URL))
image = np.array(image)
票数 3
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/59613693

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档