【问题标题】:TypeError: 'module' object is not callable when runinning PIL images类型错误:运行 PIL 图像时“模块”对象不可调用
【发布时间】:2022-04-03 00:43:09
【问题描述】:
import skimage.io 
import time

def get_pred(learner, tile):
#     pdb.set_trace()
    t_img = Image(pil2tensor(tile[:,:,:3],np.float32).div_(255))
    outputs = learner.predict(t_img)
    im = image2np(outputs[2].sigmoid())
    im = (im*255).astype('uint8')
    return im

urls = ['/content/drive/My Drive/Mexico/Testing/Sentinel-2 L1C from 2020-03-30.jpg']

import image_slicer
from image_slicer import join

num_tiles = 16
tiles = image_slicer.slice(urls[0], num_tiles)


result_all = []
for url in a_i:
  t1 = time.time()
  test_tile = cv2.imread(url)
  result = get_pred(inference_learner, test_tile )
  print(f'GPU inference took {t2-t1:.2f} secs')
  fig, (ax1, ax2) = plt.subplots(1,2, figsize=(10,5))
  ax1.imshow(test_tile)
  ax2.imshow(result)
  ax1.axis('off')
  ax2.axis('off')
  plt.show()
  # print(result`enter code here`.shape)
  result_all.append(result)

请帮我解决这个 TypeError: 'module' object is not callable 错误。因为我正在尝试读取图像并切成瓷砖,并试图将我的 CNN 推理运行到瓷砖中。

----> 6     t_img = Image(pil2tensor(tile[:,:,:3],np.float32).div_(255))
      7     outputs = learner.predict(t_img)
      8     im = image2np(outputs[2].sigmoid())

TypeError: 'module' object is not callable

编辑了如下代码,结果出现以下错误。

import skimage.io 
import time
from torchvision import transforms    


def get_pred(learner, tile):
    # pdb.set_trace()
    t_img = (pil2tensor(tile[:,:,:3],np.float32).div_(255))
    # t_img = Image(transforms.ToTensor()((tile[:,:,:3],np.float32).div_(255)))
    t_img = Image.fromarray(t_img)
    print(t_img)
    outputs = learner.predict(t_img)
    im = image2np(outputs[2].sigmoid())
    im = (im*255).astype('uint8')
    return im

错误是

/usr/local/lib/python3.6/dist-packages/PIL/Image.py in fromarray(obj, mode)
   2668     .. versionadded:: 1.1.6
   2669     """
-> 2670     arr = obj.__array_interface__
   2671     shape = arr["shape"]
   2672     ndim = len(shape)

AttributeError: 'Tensor' object has no attribute '__array_interface__'

【问题讨论】:

  • 您似乎正在使用此行调用Image 模块:t_img = Image(pil2tensor(tile[:,:,:3],np.float32).div_(255))。这是引发错误的那一行吗?
  • 是的,这是一个上升的错误,我正在将 PIL 图像转换为张量,以将张量提供给我的模型进行推理
  • 你需要from PIL import Image,根据你的张量,你需要Image.fromarray(tensor)Image.frombuffer()
  • @MarkSetchell 感谢您的快速回复,我尝试导入图像,但它仍然解决了我的问题。我不知道如何使用 Image.fromarray(tensor)
  • 请用你最新最好的代码和相应的错误消息更新你的问题,这样人们就可以看到你去了哪里。谢谢。

标签: python image typeerror conv-neural-network satellite-image


【解决方案1】:

对我来说,以下代码有效。

from fastai.vision import Image
test_image = Image(pil2tensor(arr, dtype=np.float32).div_(255))
img_segment = learn.predict(test_image)[0]

而以下代码导致 TypeError: 'module' object is not callable

from PIL import Image
test_image = Image(pil2tensor(arr, dtype=np.float32).div_(255))
img_segment = learn.predict(test_image)[0]

【讨论】:

  • 正如目前所写,您的答案尚不清楚。请edit 添加其他详细信息,以帮助其他人了解这如何解决所提出的问题。你可以找到更多关于如何写好答案的信息in the help center
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2020-12-31
  • 1970-01-01
  • 1970-01-01
  • 2021-03-25
  • 2018-01-22
  • 2013-09-19
  • 1970-01-01
相关资源
最近更新 更多