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 load a pretrained model? #25

Open
kamadforge opened this issue Nov 27, 2020 · 4 comments
Open

How to load a pretrained model? #25

kamadforge opened this issue Nov 27, 2020 · 4 comments

Comments

@kamadforge
Copy link

kamadforge commented Nov 27, 2020

It seems that although there is a flag for pre-trained model in the trainer.py, but it is not used to load the model and the training proceeds from scratch.
Note: I ended up loading it using the load checkpoint function.

@akamaster
Copy link
Owner

True, this needs to be corrected. Are you willing to make a contribution?

@YihaoChan
Copy link

YihaoChan commented Jan 22, 2022

Hi, I solved this problem. Hope this helps you.

import torch
from resnet import *
import dill  # in order to save Lambda Layer

# your devices
device_ids = [0, 1]

# the network architecture coresponding to the checkpoint
model = resnet20()

# remember to set map_location
check_point = torch.load('resnet20-12fca82f.th', map_location='cuda:%d' % device_ids[0])

# cause the model are saved from Parallel, we need to wrap it
model = torch.nn.DataParallel(model, device_ids=device_ids)
model.load_state_dict(check_point['state_dict'])

# pay attention to .module! without this, if you load the model, it will be attached with [Parallel.module]
# that will lead to some trouble!
torch.save(model.module, 'resnet20_check_point.pth', pickle_module=dill)

# load the converted pretrained model
net = torch.load('resnet20_check_point.pth', map_location='cuda:%d' % device_ids[0])
x = torch.rand(size=(1, 3, 32, 32)).cuda(device_ids[0])
out = net(x)
print(out)

@YihaoChan
Copy link

It seems that although there is a flag for pre-trained model in the trainer.py, but it is not used to load the model and the training proceeds from scratch. Note: I ended up loading it using the load checkpoint function.

A year passed...Hope you have solved this problem. If haven't, have a try on my solution. Good luck!

@zhmzm
Copy link

zhmzm commented Oct 17, 2022

Hi, I solved this problem. Hope this helps you.

import torch
from resnet import *
import dill  # in order to save Lambda Layer

# your devices
device_ids = [0, 1]

# the network architecture coresponding to the checkpoint
model = resnet20()

# remember to set map_location
check_point = torch.load('resnet20-12fca82f.th', map_location='cuda:%d' % device_ids[0])

# cause the model are saved from Parallel, we need to wrap it
model = torch.nn.DataParallel(model, device_ids=device_ids)
model.load_state_dict(check_point['state_dict'])

# pay attention to .module! without this, if you load the model, it will be attached with [Parallel.module]
# that will lead to some trouble!
torch.save(model.module, 'resnet20_check_point.pth', pickle_module=dill)

# load the converted pretrained model
net = torch.load('resnet20_check_point.pth', map_location='cuda:%d' % device_ids[0])
x = torch.rand(size=(1, 3, 32, 32)).cuda(device_ids[0])
out = net(x)
print(out)
model = model.cuda()
param = torch.load("pretrained_models/resnet56-4bfd9763.th")
model.load_state_dict(param['state_dict'])

Thanks, this also works.

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