0
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)

please help me out with this TypeError: 'module' object is not callable error. as i am trying to read a image and sliced into tiles, and trying to run my CNN inference into the tiles.

----> 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

edited the code as below and ended up with the following error.

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

Error is

/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__'
  • Seems like you you're calling the `Image` module with this line: `t_img = Image(pil2tensor(tile[:,:,:3],np.float32).div_(255))`. Is that the line that raises the error? – Nicolas Gervais Apr 02 '20 at 11:36
  • Yes, thats the one rising error, i am converting PIL image to tensor, to give tensor to my model for inference – mullermuttu Apr 02 '20 at 11:38
  • You need `from PIL import Image`, and depending on what your tensor is, you need `Image.fromarray(tensor)` or `Image.frombuffer()` – Mark Setchell Apr 02 '20 at 11:40
  • @MarkSetchell thanks for the quick reply, i tried importing Image, but still it dint solve my problem. i didnt get how to use Image.fromarray(tensor) – mullermuttu Apr 02 '20 at 11:56
  • Please update your question with your latest and greatest code and the corresponding errors messages so folk can see where you have got to. Thank you. – Mark Setchell Apr 02 '20 at 12:50
  • find the updated code and error in the question section – mullermuttu Apr 02 '20 at 14:03
  • I don't know Torchvision at all but maybe this helps... https://discuss.pytorch.org/t/pytorch-pil-to-tensor-and-vice-versa/6312/8 – Mark Setchell Apr 03 '20 at 16:12

1 Answers1

0

For me, following piece of code worked.

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

Whereas following code resulted in 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]
nnr
  • 11
  • 2
  • 1
    As it’s currently written, your answer is unclear. Please [edit] to add additional details that will help others understand how this addresses the question asked. You can find more information on how to write good answers [in the help center](/help/how-to-answer). – Ade_1 Apr 02 '22 at 18:23