简体   繁体   中英

Azure machine learning unable to load pytorch Model from the outputs folder

I am unable to load saved pytorch model from the outputs folder in my other scripts. I am using following lines of code to save the model:

    os.makedirs("./outputs/model", exist_ok=True)
    
    torch.save({
        'model_state_dict': copy.deepcopy(model.state_dict()),
        'optimizer_state_dict': optimizer.state_dict()
        }, './outputs/model/best-model.pth')
    
    new_run.upload_file("outputs/model/best-model.pth", "outputs/model/best-model.pth")
    saved_model = new_run.register_model(model_name='pytorch-model', model_path='outputs/model/best-model.pth')

and using the following code to access it:

global model
best_model_path = 'outputs/model/best-model.pth'
model_checkpoint = torch.load(best_model_path)
model.load_state_dict(model_checkpoint['model_state_dict'], strict = False)

but when I run the above mentioned code, I get this error: No such file or directory: './outputs/model/best-model.pth'

Also I want to know is there a way to get the saved model from Azure Models? I have tried to get it by using following lines of code:

from azureml.core.model import Model
model = Model(ws, "Pytorch-model")

but it returns Model type object which returns error on model.eval() (error: Model has no such attribute eval()).

There is no global output folder. If you want to use a Model in a new script you need to give the script the model as an input or register the model and download the model from the new script.

The Model object form from azureml.core.model import Model is not your pytorch Model. 1

You can use model.register(...) to register your model. And model.download(...) to download you model. Than you can use pytorch to load you model. 2

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM