'TypeError: 'module' object is not callable when runinning PIL images

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


Solution 1:[1]

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]

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Nilakshi