Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

How to resume training ? #33

Open
cmdrootaccess opened this issue May 30, 2020 · 6 comments
Open

How to resume training ? #33

cmdrootaccess opened this issue May 30, 2020 · 6 comments

Comments

@cmdrootaccess
Copy link

Hello,

how to resume training on a saved .pth ?

@akhil451
Copy link

if(model_name=='u2net'):
    net = U2NET(3, 1)
elif(model_name=='u2netp'):
    net = U2NETP(3,1)
net.load_state_dict(torch.load(saved_model_dir))

if torch.cuda.is_available():
    net.cuda()

After instantiating a model, you can load the .pth file after that.

@chenyangh
Copy link
Collaborator

chenyangh commented Jun 22, 2020

@akhil451 Thanks for the answer. In addition, to resume the training from where exactly it was, one usually needs to save and load the optimizer (especially for Adam). https://pytorch.org/tutorials/beginner/saving_loading_models.html

@cmdrootaccess
Copy link
Author

@chenyangh @akhil451 Thanks

@muhammadabdullah34907
Copy link

@chenyangh how about fine tuning the u2net.pth model by loading optimizer ? How to do load the optimizer for pretrained model ?

@chenyangh
Copy link
Collaborator

chenyangh commented Jul 3, 2020

@muhammadabdullah34907 I don't think we have saved optimizer during the training. If you simply load the released pre-trained model and keep training it with a newly initialized Adam optimizer, it may diverge in the beginning.
In the case you want to train the model from scratch yourself, you can add under this line something like this:' torch.save(optimizer.state_dict(),, "some_file_name"))`. Just make sure that the model checkpoint and the optimizer states are matching. You can look at this tutorial: https://pytorch.org/tutorials/beginner/saving_loading_models.html for more details.

@cmdrootaccess
Copy link
Author

@chenyangh how about saving the optimizer in one .pth together with the model state like this ?

torch.save({
            'epoch': epoch,
            'model_state_dict': model.state_dict(),
            'optimizer_state_dict': optimizer.state_dict(),
            'loss': loss,
            ...
            }, PATH)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants