Inference using load_learner

You need to assign the value to the file attribute first. Python will then add this attibute to the object. Then it becomes available.

learn = load_learner(modelpath, file='resnet34-acc.82775', 
                     test=ImageList.from_folder(path/'test'))
learn.file = 'resnet34-acc.82775'

print(learn.file)
>>> resnet34-acc.82775

Else, sorry I may have not understood your task.

1 Like

Thank you, I see what you mean. Yes this way it is working, thank you!

I’m in a similar place to many people on this thread. Having trained & saved a segmentation learner, I want to load it and use it for inference. I’m using learn.predict(img), and I don’t mind doing one at a time and resizing manually. The question I have is about normalization. I created by data with normalizing with imagenet_stats. Now, for inference, how can I apply the imagenet_stats normalization by hand to the images I feed to learn.predict?
As in the tutorial, data is created with:

data = (src.transform(get_transforms(), size=size, tfm_y=True)
        .databunch(bs=bs)
        .normalize(imagenet_stats)
)

Now how do I normalize for inference? Thank you.

1 Like

I found the below function somewhere on the net which takes a more direct route for single images. I was also confused about if normalization was applied automatically at inference time. I did some tests and confirmed the result is the same as loading using imagedatabunch with ds_tfms=None. So it seems like the predict method will apply the same resize method and normalization as what was used during the build, but not the transforms, which makes sense.

def cv2_RGB_to_fai_img(self,img: np.ndarray) -> vision.image.Image:
   #expects RGB cv2 array
   return vision.image.Image(torch.tensor(np.ascontiguousarray(img).transpose(2,0,1)).float()/255)

#load image
pil_img = Image.open(os.path.join(img_folder,file_name))
rgb_img = np.array(pil_img.convert('RGB'))

#load model and predict
learn = load_learner(os.path.join(os.getcwd(),model_folder), 'learn.pkl')
prediction = learn.predict(cv2_RGB_to_fai_img(rgb_img))