Batch predicting with just test set


(Austin) #1

I previously trained a resnet34 model and have the weights.h5 file saved. With the current version of fastai, do I still need to have train and valid folders in order to import my learner and predict on the test set? Also, I’m currently looping through every test image and using learn.predict_array, but is there a way to batch predict on a test folder?

Example of what I’m currently doing to load/predict:

PATH = '/path/to/model/'
sz = 224
arch=resnet34
tfms = tfms_from_model(resnet34, sz, aug_tfms=transforms_side_on, max_zoom=1.1)
data = ImageClassifierData.from_paths(PATH, tfms=tfms, bs=64)
learn = ConvLearner.pretrained(arch, data, precompute=False)
learn.unfreeze()
learn.load('224_all')

imgs = sorted(glob(os.path.join(test_path, '*.jpg')))
preds = []
_,val_tfms = tfms_from_model(resnet34, 224)
for n, i in enumerate(imgs):
        im = val_tfms(open_image(i))[None]
        preds.append(1-np.argmax(learn.predict_array(im)[0]))

There must be a cleaner way to do this by now, no?

Also, I’d just like to confirm that the learn.save() is just saving the weights and not the entire model? And if that’s the case, is there a way to save the model+weights together?