Inference/prediction using my trained model, suggestions needed

I have trained a model I want to use, and now want to create a code snippet for the prediction in a separate python script.
The shortest I ended up with so far is the code below, which is not ideal because

  • I have to create dummy directories with dummy images even though I don’t want to train the model
  • I have to copy the model manually after I run fastai once so it has created the models directory

Do you have any suggestions how to make this more elegant (ideally without creating the dummy directory with dummy images) ?

### Initialize once

#To make this work, I need to create and enter dummy data into the PATH directory:
#        mkdir -p data/train/dog; mkdir -p data/train/notdog;
#        echo > data/train/dog/0.jpg
#        echo > data/train/notdog/0.jpg
#        mkdir -p data/valid/dog; mkdir -p data/valid/notdog;
#        echo > data/valid/dog/0.jpg
#        echo > data/valid/notdog/0.jpg

arch = resnet34
sz = 224
PATH = "data"
air_data = ImageClassifierData.from_paths(PATH, tfms=tfms_from_model(arch, sz))
air_learn = ConvLearner.pretrained(arch, air_data, precompute=False)


# After fast.ai created the models directory, i need to manaully copy my pretrained model
# mv 224_all_mymodel.h5 data/models/224_all.h5

air_learn.load('224_all')

trn_tfms, air_val_tfms = tfms_from_model(arch, sz)



### Prediction code for each image, this I can run after I initialised once as above

filename="image.jpg"

im = air_val_tfms(open_image(filename))  # Load Image using fastai open_image in dataset.py

log_preds_single = air_learn.predict_array(im[None])  # Predict Image
maxP = np.argmax(log_preds_single, axis=1)  # Pick the index with highest log probability
probs_single = np.exp(log_preds_single)  # If you want the probabilities of the classes
actualclass = air_data.classes[maxP[0]]  # Look up tactualPT   return actualclass

I did this for dogs/cats from lesson one and it seemed to work well so you could try the same.

Save your trained PyTorch model like this:

torch.save(learn.model,'cats_dogs_trained_model.pt')

Then in a different notebook I predicted on an image like this.

Load the model and predict on an image like this:

from fastai.core import *
from fastai.transforms import *
from fastai.dataset import *

model = torch.load('cats_dogs_trained_model.pt')
model = model.eval()

arch = resnet34
sz = 224
_, tfms = tfms_from_model(arch, sz)

filename = 'dog.479.jpg'
image = Variable(torch.Tensor(tfms(open_image(filename))[None]))

pred = model(image).data.numpy()
'dog' if np.argmax(pred,axis=-1)[0] == 1 else 'cat'
5 Likes

Stephen:
I followed your code, but get the error:

“expected 2D or 3D input (got 4D input)”

when do the “pred” statement. Any advice?

Not sure. Are you using images that have an Alpha channel?

I am using the same jpg images from dogscats image set, so there is no alpha-channel as in png-image.

When you saved the model, torch.save(), is/are there any parameter(s) that must be set?

I am doing it on CPU (without GPU). I am looking forward to your response.

I’m not sure what could be happening. You’d have to take a look at the image data after you load it to see why it is 4D. You could try removing the [None] as that is their to add a batch dimension so it seems that in your case that is unnecessary for some reason.

Hi,
any body can tell me how can we make prediction for multiclass from .pt model .
model is trained suucessfully and also save with the extention .pt