Exporting a model for local inference mode

I trained a multi-category image classifier on colab and exported the same. I am trying to do inference on a standalone windows machine. Loading the learner:
inf_learner=load_learner(‘model.pkl’) is giving the following error:

cannot instantiate ‘PosixPath’ on your system

I used same technique with fastai1 and it used to work fine.
Please let me know where I am missing out on detail.

Regards

1 Like

Hi @tapashettisr
I faced the same issue.
It’s because load_learner() method needs you to provide Linux path of the ‘model.pkl’ file, which can’t be done in windows.
I was suggested to use PureWindowsPath instead of PosixPath but it didn’t work either.
Basically it’s better suited for Linux machine.

I’m having the same issue as tapashettisr.
Trained Multi-category image classifier on colab and exported the model to windows.
Get the error
NotImplementedError: cannot instantiate ‘PosixPath’ on your system

It works fine using fastai1 but not the latest fastai

Thanks,

I’ve encountered this problem too and hacked my way around it:

def load_posix_learner(path):
    save = pathlib.PosixPath
    pathlib.PosixPath = pathlib.WindowsPath
    
    learn = load_learner(path)
    
    pathlib.PosixPath = save
    return learn

The thing is that loading the model with WindowsPath actually works, you just need to ‘force’ it to use this implementation. I’m not sure if the above ‘fix’ actually restores the PosixPath implementation, I haven’t tested this.

3 Likes

i do that and have issue!


what can i do?

1 Like

I have the same exact issue. Any solutions?

Without an explanation of what you did to create the model (and on which platform) and on which platform you’re trying to load it’s impossible to diagnose this error.

Maybe start a new thread, explain what exactly you were doing and try to create a notebook that demonstrates the error?

I installed fastai on local Ubuntu machine following the Fastai GitHub page. I trained a multi-category model on Google Colab . I copied this model to the local machine and tried loading the model:
learn_infer=load_learner(path/‘export.pkl’)
I am getting the following error:

AttributeError: Can’t get attribute ‘FlattenedLoss’ on <module ‘fastai.layers’ from ‘/home/lenovo/miniconda3/envs/fastai2/lib/python3.8/site-packages/fastai/layers.py’>
Is there an issue with my installation??

This doesn’t look like path issue.
As it says, it’s unable to use FlattenedLoss. Are you sure it works before exporting it?

Yes on Colab it works perfectly before export.
I think the issue is with export.
I tried loading the exported model on colab
inf_learner=load_learner(path/‘model.pkl’)
got the error: Can’t get attribute ‘get_x’ on <module ‘main’>
However the same model was working for inference before exporting.
Is it unpickling issue?

Update:
Ii finish training on Colab and export the model then load the same model using load_learner() then the inference is running fine. However if I terminate the runtime and on a new Colab session I load the saved model it is throwing the error
Can’t get attribute ‘get_x’ on <module ‘main’>

1 Like

This worked like a charm on Windows with one minute change in the code.

I replaced “model_path” with the “path” variable defined in the function

Thank you!

1 Like

… oh my, of course that has to be the path variable from the function arguments. I’ll edit it in my answer, just in case anyone else stumbles upon this.

Hey Sunil, were you able to resolve the issue as I am also getting the same error. I tried to implement the suggestions but not useful

1 Like

Hi,
I defined get_x and get_y functions again in the main during interpretation before importing the model. With that the issue gets resolved.
However I have no explanation as to why these definitions are required. The model would have already saved them. If anyone has explanation please post them.

Regards

Sorry couldn’t get what you mean by main as I ma working only on the jupyter notebook and importing all the required libraries. I have functions get_x and get_y only in the data block API used for the dataloaders

Do you mean copying these 2 functions in the new python file which contains the widgets code used for creating the app as Jeremy suggested in his class

Yes,
you copy these two function definitions to the script which you use for inference

Sorry but it didn’t worked for me.

def get_x®: return path/‘Images’/r[‘ImageName’]
def get_y®: return tensor([r[‘x’],r[‘y’]])
learn_inf = load_learner(path/‘Fire-detector.pkl’,cpu=True)

1 Like

Hey Claton,
Can you please share how your path look like. Did you change with the Image path ( dependent feature) or the independent path i.e fetching continous value form data frame. If possible can you please share your work repository. This is all when creating a separate python file for the web deployment through binder

I am also getting the same error.
I am able to load the model back in the same notebook in which it was saved.
But I am not able to load it in any other python file/notebook.
I tried defining get_x and get_y before calling load_learner in the separate file as suggested, but that does not work either.
Need help.