How to use the cnn learner to predict a numpy image

learn = create_cnn(data, models.resnet34, metrics=accuracy)
learn.predict(img)
img is a numpy.ndarray

import numpy as np
item=ItemBase(Tensor(np.rollaxis(img,2)))
learn.predict(item)
Exception: Not implemented: you can't apply transforms to this type of item (ItemBase)

Please help me.

You are almost there but ItemBase don’t know how to apply transforms, so you should replace it with Image

from fastai.vision import *

x = np.random.randn(3, 256, 256)
img = Image(torch.from_numpy(x).float())
learn.predict(img)

here Image is not from PIL but from fastai.vision.image.Image

1 Like

Thanks, It’s very neat. By the way, How do you know such thing like this? The docs_src notebook? I’m confused about “Image” can do transforms things.

In many places you will find

img = open_image(<path_to_img>)
learn.predict(img)

Here, if you do type(img) you will get fastai.vision.image.Image. By this I know predict() expects Image which inherits ItemBase, I saw it in the source code. If the code is giving bugs then going through inner code clears lots of them.

Sounds like MNIST dataset in kaggle?

I recently tried same thing on kaggle MNIST dataset, where they have data in excel file and you load it as numpy array.
Here is what I did:
https://www.kaggle.com/heye0507/fastai-1-0-with-customized-itemlist

Basic idea is to use pil2tensor() which is a fastai vision function. Then you can call the vision.Image class on top of that

trivial example:

img_pixel = np.random.randn(3,256,256)
img = vision.Image(pil2tensor(img_pixel,np.float32)._div(255)) #assume pixel value is 0-255
1 Like

@heye0507 Thank you for your help, I forgot the part of _div(255). @bharat0to