Make Model Predict on a Single Image

I have trained a Model From ImageDataLoader.from_folder ,and the model resulted in very good accuracy , and I want to make the model predict on a Single Image . I have seen other threads but none actually solve the issue . To Overcome this issue , i should put the Image in a folder and the Use ImagaDataLoader on the Folder and the predict on the Folder . Which is not a feasible option . When i convert the Image to PIL , and run " learn.predict (img )" the Result is an index out of bound Exception . Would be glad if someone can help me solve this issue .

Here is a good blog post that talks about model inference: Inference With | Just Stir It Some More

If this doesn’t solve your problem, some code would be helpful to understand what you are trying a little better

I have a model that does Binary Classification (covid or Normal). The Model was trained succesfully with Batches , and now i want to predict using just Single Image , not run a Test Batch . I have Followed the Link , but it gives me this error

I think what you are doing seems valid to me. Can you show your learn.dls and how your dls object was created? It seems like that might be where the issue is in this case.

I have run " learn.dls" and here is the result →
<module ‘’ from ‘/usr/local/lib/python3.7/dist-packages/fastai/data/’>

Just as Note : The folder i work on has 3 folders (Train,Test, Valid) and within each one (Normal , Covid).
The Way i set up the testing data (After i trained the model ) . is like this →

testingdata = ImageDataLoaders.from_folder(path,train=‘test’,
ds_tfms=aug_transforms(), size=256, bs=32, num_workers=4)

and the Way i predict Successfully the Batch is Like this :
preds,y = learn.get_preds(dl=testingdata.train)

.The Above Code works , but i want to share the approach with you in hope that it could help ,and be able to predict on a Single Image

Sorry , about that i didn’t know that , i thought that this is asif contacting the Support Team as i am a new user. Apologies again

No problem @KareemAfifi - thanks for understanding! :slight_smile:

1 Like

If there is anyone who solved this issue before and know how to solve it .Please type your answer here for future users