Fastai V1 - Predicting on Single New Image - Conversion to fastai_image from PNG_image

Hello,

I am trying to using Fastai’s V1 predict on a single new image that comes in. This works great on Fastai V0.7. I used val_tfms the following code, where SMILES is the string name for a png file. This is for V0.7:

    fn = f'{PATH1}test_images/%s.png' %SMILES
    trn_tfms, val_tfms = tfms_from_model(arch,sz)
    ds = FilesIndexArrayDataset([fn], np.array([0]), val_tfms, PATH1)
    dl = DataLoader(ds)

    log_preds = learn.predict_dl(dl)
    preds = np.argmax(log_preds, axis=1)  # from log probabilities to 0 or 1 - get the max on either side
    probs = np.exp(log_preds[:,1])       

    image = Image.open(fn)
    image.show()

However for V1, I cannot do this. I would like to know if this is possible. Again, I want to use it on a new image that comes in, not on an image in the dataset. Here is where I am right now. Using learn.predict, I am trying to use the following image:

I get this error when I run learn.predict(image) - PNGImageFile has no attribute ‘clone’. This tells me I need to change the image to a fastai image. I don’t know how to do that. Is this possible? Please let me know! Thanks!


42%20PM

2 Likes

Nevermind, I found the answer. For others, simply follow this jupyter notebook. You want to use the open_image function in fastai, which will convert the image to a fastai image: https://github.com/fastai/fastai_docs/blob/master/dev_nb/104c_single_image_pred.ipynb

Thank you for posting these docs.

4 Likes

Hi Bradley,

maybe you can help me.
I have the similar problem as you did.

The image i am trying to pass to predict is of type
<class ‘fastai.vision.image.Image’>
but I get this error
TypeError: ‘list’ object is not callable

Any idea!?

Hi, What does the error look like? Are you using fastai V1?

Hello,

yes I’m using V1 --> fastai-1.0.31

Here is my Code:

Here is the Error Message:

Thanks - I really appreciate your help!

You passed the transforms to tfms instead of ds_tfms, that’s why.

Thanks a lot! :slight_smile: