Search code examples
pythonazure-machine-learning-service

How to register model from the Azure ML Pipeline Script step


I am running the pipeline.submit() in AzureML, which has a PythonScriptStep. Inside this step, I download a model from tensorflow-hub, retrain it and save it as a .zip, and finally, I would like to register it in the Azure ML. But as inside the script I do not have a workspace, Model.register() is not the case. So I am trying to use Run.register_model() method as below:

os.replace(os.path.join('.', archive_name + '.zip'), 
           os.path.join('.', 'outputs', archive_name + '.zip'))

print(os.listdir('./outputs'))
print('========================')

run_context = Run.get_context()
finetuning_model = run_context.register_model(model_name='finetuning_similarity_model',
                                              model_path=os.path.join(archive_name+'.zip'),
                                              tags={},
                                              description="Finetuning Similarity model")

But then I have got an error:

ErrorResponse { "error": { "message": "Could not locate the provided model_path retrained.zip in the set of files uploaded to the run:

despite I have the retrained .zip in the ./outputs dir as we can see from the log:

['retrained.zip']
========================

I guess that I am doing something wrong?


Solution

  • I was able to fix the same issue (ModelPathNotFoundException) by explicitly uploading the model into the run history record before trying to register the model:

    run.upload_file("outputs/my_model.pickle", "outputs/my_model.pickle")
    

    Which I found surprising because this wasn't mentioned in many of the official examples and according to the upload_file() documentation:

    Runs automatically capture file in the specified output directory, which defaults to "./outputs" for most run types. Use upload_file only when additional files need to be uploaded or an output directory is not specified.