Pickle File not found while loading

Viewed 119

I have saved a classifier model with pickle using the following code-

import pickle
with open('model.pickle', 'wb') as fid:
    pickle.dump(clf, fid) 

This is the score.py file where I am loading the pickle model and the same file is called during deployment. Also note that, all these files (code, pickle file and related files) are in the same directory.

# Initialize the deployment environment
def init():
    global obj
    with open('./model.pickle', 'rb') as f:
        obj = pickle.load(f)

This is getting stored in some directory var/azureml-app

After running this deployment code, this is the error I am getting-

open('/mnt/batch/tasks/shared/LS_root/mounts/clusters/model.pickle', 'rb') as f:
    FileNotFoundError: [Errno 2] No such file or directory: '/mnt/batch/tasks/model.pickle'
1 Answers

I figured out the way to solve this error.

Instead of loading the model as

 with open('./skm.pickle', 'rb') as f:
        obj = pickle.load(f)

You need to load the model as

 model_path = os.path.join(os.getenv('AZUREML_MODEL_DIR'), 'skm.pickle')
 model = joblib.load(model_path)

The AZUREML_MODEL_DIR will take the environment directory of the model where it is stored.

This will solve the error.

Related