Inference/prediction using my trained model, suggestions needed


#1

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

#2

Wow great work icloud there mate. I will connect google classroom back with you after running this myself and tell you about any improvements there can be made or any other modifications.


(Stephen Johnson) #3

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'