Inference on single Image

I have trained a classifier for images with 7 classes. The model works perfectly fine on colab. I want to deploy the same on laptop. I went about as follows:

  1. Export the model using learn.export(). to dataPath
  2. learn=load_learner(dataPath) on my laptop.
  3. I read the image from disk stored at imgPath
    arr = plt.imread(“imgPath/1.Col_ST_48/IMG_20190801_134227.jpg”)
  4. Convert the image(ndarray) to fastai Image: img = pil2tensor(arr,dtype= np.float32)
  5. Do prediction: learn.predict(Image(img))
    I get the first class always as predicted class irrespective of image. Am I missing something?Please help.
1 Like

Try

learn.export()
learn = load_learner(dataPath)
img = open_image(imgPath)
learn.predict(img)

This has always worked for me!

4 Likes

Thanks, it works.

Thanks a lot. It helped while running the inference on colab. But if run the same on windows pc on learn = load_learner(dataPath) I get the following warning:
C:\Users\tapas_XXXX\Miniconda3\envs\fastai_v1\lib\site-packages\torch\serialization.py:434: SourceChangeWarning: source code of class ‘torch.nn.modules.loss.CrossEntropyLoss’ has changed. you can retrieve the original source code by accessing the object’s source attribute or set torch.nn.Module.dump_patches = True and use the patch tool to revert the changes.

  • warnings.warn(msg, SourceChangeWarning)*
    C:\Users\XXXX\Miniconda3\envs\fastai_v1\lib\site-packages\torch\serialization.py:434: SourceChangeWarning: source code of class ‘torch.nn.modules.conv.Conv2d’ has changed. you can retrieve the original source code by accessing the object’s source attribute or set torch.nn.Module.dump_patches = True and use the patch tool to revert the changes.
  • warnings.warn(msg, SourceChangeWarning)*

If I ignore this warning during prediction I get error: AttributeError: ‘ReLU’ object has no attribute 'threshold’
Is it an issue of version. I found fastai version 1.0.55 is running in my PC as well as colab.
Any insights??

I have never run into this so I can’t be for certain, but it seems to arise form your version of PyTorch. Do you have the same version on both colab and your local machine?

I found the solution. Colab has Pytorch 1.1.0 and PC had 1.0.0.