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


I’ve recently started 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:

Thank you very much for your time

@DonutPancakes welcome to !!!

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 =’/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 ‘’>
    but got <class ‘PIL.JpegImagePlugin.JpegImageFile’>

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

The notebook link is:

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 import *
btn_upload = widgets.FileUpload()
img = PILImage.create([-1])

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