'FastAI : Getting Prediction of Image from Learner

I want to be able to predict the Class of a Single Image from the Learner and i always get an Index Out of Bound Exception . Here is the Code

data = ImageDataLoader.from_folder(path, train="Train", valid ="Valid",
        ds_tfms=get_transforms(), size=(256,256), bs=32, num_workers=4)
//Model is a Sequential One 
learn = Learner(data, model, loss_func = nn.CrossEntropyLoss(), metrics=accuracy)// The Model
learn.fit_one_cycle(100, lr_max=3e-3)
Img = //PIL Image Path
learn.predict(img)

The Model is able to Predict on ImageDataLoader but not on a Single Image .If anyone has any clue it would be much appreciated Here is a Link to FastAi but didnt solve the issue https://forums.fast.ai/t/how-to-use-learner-predict-list-index-out-of-range/81998/7

EDIT NOTE : I have tried to convert the Image to a tensor Flow but another error is given .Photo of the Error



Solution 1:[1]

I think that the problem is that data is a rank 4 tensor whereas Img is rank 3. In other words, it is missing the #points or batch dimension up front. In TF one can fix that with tf.expand_dims like so

img = tf.expand_dims(img, axis=0)

or fixing it when passing to the model

learn.predict(tf.expand_dims(img, axis=0))

You can also look at tf.newaxis (see the second code example here).

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 ATony