Hello,
I was doing some research with this pix2pix implmenetation https://github.com/junyanz/pytorch-CycleGAN-and-pix2pix and was hoping to be able to deploy the trained models for further research and speed tests. To be honest this is my first approach in saving and loading a model. I found two topics on google about the issue below but nothing worked for me. I fear I might do something fundamentally wrong.
I inserted this code (from the pytorch tutorial site):

checkpoint = {‘model’: net.module.cpu().model,
‘state_dict’: net.module.cpu().state_dict()}
torch.save(checkpoint, save_path)

I’m using this code to load it:

def load_checkpoint(filepath):
checkpoint = torch.load(filepath)
print(checkpoint[‘model’])
print(checkpoint[‘state_dict’])
model = checkpoint[‘model’]
model.load_state_dict(checkpoint[‘state_dict’])
for parameter in model.parameters():
parameter.requires_grad = False
model.eval()
return model

model = load_checkpoint(‘5_net_G.pth’)

For debugging I printed out the model and dictionary. This is the model:

Sequential(
(0): ReflectionPad2d((3, 3, 3, 3))
(1): Conv2d(3, 64, kernel_size=(7, 7), stride=(1, 1), bias=False)
(2): BatchNorm2d(64, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)
(3): ReLU(inplace)
(4): Conv2d(64, 128, kernel_size=(3, 3), stride=(2, 2), padding=(1, 1), bias=False)
(5): BatchNorm2d(128, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)
(6): ReLU(inplace)
(7): Conv2d(128, 256, kernel_size=(3, 3), stride=(2, 2), padding=(1, 1), bias=False)
(8): BatchNorm2d(256, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)
(9): ReLU(inplace)
(10): ResnetBlock(
(conv_block): Sequential(
(0): ReflectionPad2d((1, 1, 1, 1))
(1): Conv2d(256, 256, kernel_size=(3, 3), stride=(1, 1), bias=False)
(2): BatchNorm2d(256, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)
(3): ReLU(inplace)
(4): Dropout(p=0.5)
(5): ReflectionPad2d((1, 1, 1, 1))
(6): Conv2d(256, 256, kernel_size=(3, 3), stride=(1, 1), bias=False)
(7): BatchNorm2d(256, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)
)
)
(11): ResnetBlock(
(conv_block): Sequential(
(0): ReflectionPad2d((1, 1, 1, 1))
(1): Conv2d(256, 256, kernel_size=(3, 3), stride=(1, 1), bias=False)
(2): BatchNorm2d(256, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)
(3): ReLU(inplace)
(4): Dropout(p=0.5)
(5): ReflectionPad2d((1, 1, 1, 1))
(6): Conv2d(256, 256, kernel_size=(3, 3), stride=(1, 1), bias=False)
(7): BatchNorm2d(256, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)
)
)
(12): ResnetBlock(
(conv_block): Sequential(
(0): ReflectionPad2d((1, 1, 1, 1))
(1): Conv2d(256, 256, kernel_size=(3, 3), stride=(1, 1), bias=False)
(2): BatchNorm2d(256, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)
(3): ReLU(inplace)
(4): Dropout(p=0.5)
(5): ReflectionPad2d((1, 1, 1, 1))
(6): Conv2d(256, 256, kernel_size=(3, 3), stride=(1, 1), bias=False)
(7): BatchNorm2d(256, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)
)
)
(13): ResnetBlock(
(conv_block): Sequential(
(0): ReflectionPad2d((1, 1, 1, 1))
(1): Conv2d(256, 256, kernel_size=(3, 3), stride=(1, 1), bias=False)
(2): BatchNorm2d(256, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)
(3): ReLU(inplace)
(4): Dropout(p=0.5)
(5): ReflectionPad2d((1, 1, 1, 1))
(6): Conv2d(256, 256, kernel_size=(3, 3), stride=(1, 1), bias=False)
(7): BatchNorm2d(256, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)
)
)
(14): ResnetBlock(
(conv_block): Sequential(
(0): ReflectionPad2d((1, 1, 1, 1))
(1): Conv2d(256, 256, kernel_size=(3, 3), stride=(1, 1), bias=False)
(2): BatchNorm2d(256, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)
(3): ReLU(inplace)
(4): Dropout(p=0.5)
(5): ReflectionPad2d((1, 1, 1, 1))
(6): Conv2d(256, 256, kernel_size=(3, 3), stride=(1, 1), bias=False)
(7): BatchNorm2d(256, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)
)
)
(15): ResnetBlock(
(conv_block): Sequential(
(0): ReflectionPad2d((1, 1, 1, 1))
(1): Conv2d(256, 256, kernel_size=(3, 3), stride=(1, 1), bias=False)
(2): BatchNorm2d(256, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)
(3): ReLU(inplace)
(4): Dropout(p=0.5)
(5): ReflectionPad2d((1, 1, 1, 1))
(6): Conv2d(256, 256, kernel_size=(3, 3), stride=(1, 1), bias=False)
(7): BatchNorm2d(256, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)
)
)
(16): ConvTranspose2d(256, 128, kernel_size=(3, 3), stride=(2, 2), padding=(1, 1), output_padding=(1, 1), bias=False)
(17): BatchNorm2d(128, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)
(18): ReLU(inplace)
(19): ConvTranspose2d(128, 64, kernel_size=(3, 3), stride=(2, 2), padding=(1, 1), output_padding=(1, 1), bias=False)
(20): BatchNorm2d(64, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)
(21): ReLU(inplace)
(22): ReflectionPad2d((3, 3, 3, 3))
(23): Conv2d(64, 3, kernel_size=(7, 7), stride=(1, 1))
(24): Tanh()
)

But in the code line model = checkpoint[‘model’] I get the following error:

Error(s) in loading state_dict for Sequential:
Missing key(s) in state_dict: “1.weight”, “2.weight”, “2.bias”, “2.running_mean”,…

I would be glad for any suggestions. Thanks in advance!
Cheers!