Code to use saved model on test images

I’m looking for the complete code to re-use my saved model on test images that the model has never seen, in order to get a prediction.

The closest I’ve got so far is the code below, but it does not work right, it returns the same lines once for each file in my test folder (12 files):
(‘test/snaps172653_-1.png’, ‘down’, 0.7188118)
(‘test/snaps172653_-1.png’, ‘down’, 0.7188118)
(‘test/snaps172653_-1.png’, ‘down’, 0.7188118)
(‘test/snaps172653_-1.png’, ‘up’, 0.28118828)
(‘test/snaps172653_-1.png’, ‘down’, 0.7188118)
(‘test/snaps172653_-1.png’, ‘up’, 0.28118828)
(‘test/snaps172653_-1.png’, ‘up’, 0.28118828)
(‘test/snaps172653_-1.png’, ‘down’, 0.7188118)
(‘test/snaps172653_-1.png’, ‘up’, 0.28118828)
(‘test/snaps172653_-1.png’, ‘down’, 0.7188118)
(‘test/snaps172653_-1.png’, ‘down’, 0.7188118)
(‘test/snaps172653_-1.png’, ‘up’, 0.28118828)

from fastai.imports import *
from fastai.transforms import *
from fastai.conv_learner import *
from fastai.model import *
from fastai.dataset import *
from fastai.sgdr import *
from fastai.plots import *
PATH = "/media/hugues/M.2 windows/Hugues/Snaps/"
sz=224
arch=resnet34
data = ImageClassifierData.from_paths(PATH, tfms=tfms_from_model(arch, sz), test_name='test')
learn = ConvLearner.pretrained(arch, data, precompute=False)
learn.load('up_down_2minslippage_scaled_enlarged_skipped50k')

trn_tfms, val_tfms = tfms_from_model(arch,sz) # get transformations
learn.precompute=False # We'll pass in a raw image, not activations
preds = learn.predict_array(im[None])
preds

log_preds = learn.predict(is_test=True)
preds = np.argmax(log_preds, axis=1)
itemIndex = 0
for maxIndex in preds:
    print((data.test_ds.fnames[itemIndex], 
             data.classes[maxIndex], 
             np.exp(log_preds[itemIndex][maxIndex])))
itemIndex = itemIndex + 1

Try indenting
itemIndex = itemIndex + 1
also, you can use
for itemIndex, maxIndex in enumerate(preds): print((data.test_ds.fnames[itemIndex], data.classes[maxIndex], np.exp(log_preds[itemIndex][maxIndex])))

Thanks @sjdlloyd, that was quite a newb mistake, I should have seen it. I also realised the central part of my code is duplicated and not necessary. So the code below works fine and provide prediction on each file in test folder along the probability.

from fastai.imports import *
from fastai.transforms import *
from fastai.conv_learner import *
from fastai.model import *
from fastai.dataset import *
from fastai.sgdr import *
from fastai.plots import *
PATH = "/media/Snaps/"
sz=224
arch=resnet34
data = ImageClassifierData.from_paths(PATH, tfms=tfms_from_model(arch, sz), test_name='test')
learn = ConvLearner.pretrained(arch, data, precompute=False)
learn.load('name_of_your_model')

log_preds = learn.predict(is_test=True)
preds = np.argmax(log_preds, axis=1)
itemIndex = 0
for maxIndex in preds:
    print((data.test_ds.fnames[itemIndex], 
             data.classes[maxIndex], 
             np.exp(log_preds[itemIndex][maxIndex])))
    itemIndex = itemIndex + 1
1 Like