How to use Learner.predict (list index out of range)

Hello,

I’ve recently started fast.ai and was successful in creating a model for a dataset that consisted of single-channel images (by stacking channels, converting them to 3-channel, and storing them). I was successful in creating the model, saving it and exporting it. My major problem is I cannot seem to understand what needs to go into the learner.predict() function:

My code:

The full notebook link is as follows: https://www.kaggle.com/namansood/fastai-image-channel

Thank you very much for your time

@DonutPancakes welcome to Fast.ai !!!

the issue seems to be with what you are passing to the predict function. You will want to feed the model tensor(s) or image as opposed to a file name. So try something like:

img = Image.open(’/data/train/1/1806.jpg’) and then feed that object into the predict function

Hope that helps!!

It didn’t work, this was the error that followed it

AssertionError: Expected an input of type in

  • <class ‘pathlib.PosixPath’>
  • <class ‘pathlib.Path’>
  • <class ‘str’>
  • <class ‘torch.Tensor’>
  • <class ‘numpy.ndarray’>
  • <class ‘bytes’>
  • <class ‘fastai.vision.core.PILImage’>
    but got <class ‘PIL.JpegImagePlugin.JpegImageFile’>

I also tried using PILImage.create() and encountered the following error

The notebook link is: https://www.kaggle.com/namansood/fastai-image-channel

1 Like

Was there ever a solution? I’m running into the same problem

1 Like

+1 I have the same issue here :point_up:

you should use a fastai version loss function to avoid these errors,in your case when you creating the learner replace F.crossentropy with loss_func=CrossEntropyLossFlat(),dont forget the parenthesis.

Furthermore you should pass a PILImage data to the predict() method,one approach to create one is:
img = PILImage.create(filename)
or use a upload widge:
from fastai.vision.widgets import *
btn_upload = widgets.FileUpload()
btn_upload
img = PILImage.create(btn_upload.data[-1])

then you can pass img to the learn_inf.predict():
learn_inf.predict(img)

Facing the same issue here for some reason that I cannot figure out yet