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

huangapple go评论111阅读模式
英文:

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

问题

>images, labels = next(iter(self.loader))
触发错误。

我有一个自定义的数据集类,在其中从URL加载每个RGB图像:
>image = Image.open(urllib.request.urlopen(URL))

然后我应用一些albumentations转换。

当我使用cv2读取路径上的图像时,代码可以正常工作。
但是,当我从URL读取图像时,它无法工作。
请注意,我已验证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 this.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

/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 this.always_apply:
27 params = self.get_params()
---> 28 params = this.update_params(params, **kwargs)
29 if this.targets_as_params:
30 targets_as_params = {k: kwargs[k] for k in this.targets_as_params}

/usr/local/lib/python3.6/dist-packages/albumentations/core/transforms_interface.py in update_params(this, params, **kwargs)
66 if hasattr(this, 'interpolation'):
67 params['interpolation'] = this.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'

英文:
images, labels = next(iter(self.loader))
grid = torchvision.utils.make_grid(images)

>images, labels = next(iter(self.loader))

triggers the error.

I have a custom dataset class where I load each image (RGB) from an url :
>image = Image.open(urllib.request.urlopen(URL))

and I apply some albumentations transforms.

The code works when I read an image for which I have a path using cv2.
However, it doesn't work when I read an image from the url.
Note that I verified that the urls aren't broken.

Here's the traceback:

/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
--&gt; 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:
---&gt; 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 &lt;listcomp&gt;(.0)
     42     def fetch(self, possibly_batched_index):
     43         if self.auto_collation:
---&gt; 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(&quot;float32&quot;))
     50     #print(&quot;self.root,item,self.image_transform,self.transform,self.size&quot;, self.root,item,self.image_transform,self.transform,self.size)
---&gt; 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)
---&gt; 83   image = image_transform(image=image)[&quot;image&quot;]
     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 
--&gt; 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() &lt; self.p) or self.always_apply:
     27             params = self.get_params()
---&gt; 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, &#39;interpolation&#39;):
     67             params[&#39;interpolation&#39;] = self.interpolation
---&gt; 68         params.update({&#39;cols&#39;: kwargs[&#39;image&#39;].shape[1], &#39;rows&#39;: kwargs[&#39;image&#39;].shape[0]})
     69         return params
     70 

AttributeError: &#39;MpoImageFile&#39; object has no attribute &#39;shape&#39;

答案1

得分: 3

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

image = Image.open(urllib.request.urlopen(URL))
image = np.array(image)
英文:

In order to work with albumentations, you must pass a numpy array to the transforms not a PIL image. So:

image = Image.open(urllib.request.urlopen(URL))
image = np.array(image)

huangapple
  • 本文由 发表于 2020年1月6日 22:18:17
  • 转载请务必保留本文链接:https://go.coder-hub.com/59613693.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定