Is the problem solved? I also encountered the same problem, thank you.
Load the state_dict
via:
model = MyModel()
state_dict = torch.load("last_brain1.pth")['state_dict']
model.load_state_dict(state_dict)
model
is still a dict
instead of a nn.Module
class.
Could you print(model)
after running my lines of code?
model
should be initialized as your model class, e.g.:
class MyModel(nn.Module):
def __init__(self):
# your layer definitions here
def forward(self, x):
# your forward pass here
model = MyModel()
state_dict = torch.load("last_brain1.pth")['state_dict']
model.load_state_dict(state_dict)
class Net(nn.Module):
def init(self):
super(Net, self).init()
self.conv1 = nn.Conv2d(3, 32, 3)
self.pool = nn.MaxPool2d(2, 2)
self.conv2 = nn.Conv2d(32, 64, 3)
self.fc1 = nn.Linear(64 14 14, 30)
self.fc2 = nn.Linear(30, 3)
# self.softmax = nn.Softmax(dim=1)
def forward(self, x):
x = self.pool(F.relu(self.conv1(x))) # output size: [batch_size, 32, 255, 255]
x = self.pool(F.relu(self.conv2(x))) # output size: [batch_size, 64, 126, 126]
x = x.view(-1, 64 14 14) # output size: [batch_size, 64126126]
x = F.relu(self.fc1(x))
x = self.fc2(x)
# x=self.softmax(x)
return x
x = torch.randn(1, 3, 64, 64) # (batch size or #of images,channels RGB,width,height)
model = Net()
output = model(x)
model = Net()
state_dict = torch.load(“last_brain1.pth”)[‘state_dict’]
model.load_state_dict(state_dict)
print(model)
here is my code but still not working
What kind error do you get and which line of code raises it?
Could you also show, how you’ve created last_brain1.pth
?
Maybe 'state_dict'
refers to something else than the model.state_dict()
?
This code works:
x = torch.randn(1, 3, 64, 64) # (batch size or #of images,channels RGB,width,height)
model = Net()
output = model(x)
torch.save({'state_dict': model.state_dict()}, 'tmp.pt')
model = Net()
state_dict = torch.load('tmp.pt')['state_dict']
model.load_state_dict(state_dict)
print(model)
Could you try to find differences between your code and mine?
you mean first i save my model according to ur code and then load it?
I assume you have already saved your model, so my code is just to show that initializing the model and loading the state_dict
does work and the error might still be somewhere else in your code.
If a pretrained model is saved as Dataparallel can it be loaded as nn.Sequential model?
nn.DataParallel
stores the actual model in the .module
attribute.
If you store the state_dict
from the model.module
, you’ll be able to directly use it without wrapping the model into nn.DataParallel
.
what are the advantages and disadvantages of just using the python pickle
@ptrblck what are the advantages and disadvantages of just using the python pickle module vs the load/save stuff pytorch has built?
Internally pickle
(or a custom pickle module you provide) will be used, so you could also manually store the data most likely.
However, the _save
method will apply a specific format to load it properly.
I’ll answer in the other threads separately, but I would recommend to keep “similar” questions in one place.
I’m trying! That’s why I try to reference questions between each other but I don’t have control that other people ask repeated questions I will try to not make this issue worse. Appreciate the advice.
Hi Ptrblck,
I want to save my data which are 2000x11x11x11 tensor. I used
torch.save(TargetGAN,Path, _use_new_zipfile_serialization=False)
but gave me this error :
TypeError: save() got an unexpected keyword argument ‘_use_new_zipfile_serialization’
I need to use _use_new_zipfile_serialization=False, otherwise could not load the data on the GPU and will consider it as a zip file which I don’t want.
Many thanks
This argument as added in PyTorch 1.4.0
, so you might want to update your Pytorch version, if you want to use it.