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

Can this model resume from former checkpoint? #20

Open
scpoppin1207 opened this issue Oct 6, 2024 · 5 comments
Open

Can this model resume from former checkpoint? #20

scpoppin1207 opened this issue Oct 6, 2024 · 5 comments

Comments

@scpoppin1207
Copy link

I don't see any function or parameters to load from existed checkpoint.

@xiongxyowo
Copy link
Collaborator

Hi, you can add/modify the following code to the appropriate place to resume training:

# save meta-data
checkpoint = {'model': model.state_dict(), 'optim': optim.state_dict(), \
              'epoch': epoch, 'lr': scheduler.state_dict(),}
torch.save(checkpoint, path)

# load meta-data
checkpoint = torch.load(path)
model.load_state_dict(checkpoint['model'])
optim.load_state_dict(checkpoint['optim'])
epoch = checkpoint(['epoch'])
scheduler = CosineAnnealingLR(optim, args.epoch, eta_min=1.0e-7, last_epoch=epoch)

You may also refer to the pytorch tutorial.

@scpoppin1207
Copy link
Author

Thxs 👍

@scpoppin1207
Copy link
Author

model.load_state_dict(checkpoint['model'])
KeyError: 'model'

@scpoppin1207
Copy link
Author

It seems that there's no key named "model"

@xiongxyowo
Copy link
Collaborator

If you are using the original save code, the weights of the model are not wrapped in the model:

torch.save(model.state_dict(), os.path.join(args.save_path, 'SAM2-UNet-%d.pth' % (epoch + 1)))

The new save code above wraps the state dict in the model:

checkpoint = {'model': model.state_dict(), 'optim': optim.state_dict(), \
              'epoch': epoch, 'lr': scheduler.state_dict(),}
torch.save(checkpoint, 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

2 participants