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