I’ve gone over the image segmentation notebook with the camvid dataset and exported the pickle model using learn.export() to deploy on a flask web app using a hosting service (which means the notebook and flask app are not run on the exact same environments), however when trying to use learn = load_learner(model_path) I get the error:
File ".local/lib/python3.6/site-packages/torch/serialization.py", line 538, in _load result = unpickler.load() AttributeError: Can't get attribute 'acc_camvid' on <module '__main__' (built-in)>
I believe this is because in the notebook, we defined our own metric called acc_camvid with the following function:
I put the function as well as the ‘codes’ array into a file called helpers.py and imported it into my flask app using from helpers import acc_camvid but I still get the error. So I don’t know how to add the attribute. I think I might have to recreate the model somehow but I’m not sure.
Rather than exporting the learner and then loading the learner for inference you can just save the weights, and while deployment create an exact same learner as you did for training and then load in the weights. (Here you will require to give data as well, use some dummy data.) I think this might solve your problem.
I thought about it and decided I don’t really need the custom metric as was shown in the tutorial so I just omitted it and exported the model as usual with `learn.export()’ and it worked fine.
I have to all this for loading the learner for prediction in the flask application because of load_learner() from .pkl file is not working. Please provide an elegant way of doing this for a single image. After getting the prediction, I am not able to convert it to numpy or OpenCV image format. And If I am trying to save the image, it is saving it a blank image.
Here’s how I solved the blank image issue. Please keep in mind I did this many months ago and cannot answer specific questions, all I have is my old code to reference.
This same line throws error. Actually, the problem here is acc_camid was the attribute of main. Therefore at the time of unpickling. It’s trying to find all the attributes. The way round of this error is to create a custom class at the time of pickling and import the same class at the time of inferencing. Or define the metrics=acc_camvid in the main attribute by defining it in the main of python file.