Error in loading state_dict for customed model

I had problems when loading the weights of model. Here’s some parts of the model:

class InceptionV4(nn.Module):

   def __init__(self, num_classes=1001):
       super(InceptionV4, self).__init__()
       # Special attributs
       self.input_space = None
       self.input_size = (299, 299, 3)
       self.mean = None
       self.std = None
       # Modules
       self.features = nn.Sequential(
           BasicConv2d(3, 32, kernel_size=3, stride=2),
           BasicConv2d(32, 32, kernel_size=3, stride=1),
           BasicConv2d(32, 64, kernel_size=3, stride=1, padding=1),
       self.avg_pool = nn.AvgPool2d(8, count_include_pad=False)
       self.last_linear = nn.Linear(1536, num_classes)

I have tried to save the weights, something like, weight_name) and then reload again model.load_state_dict(torch.load(weight_name)) but got these errors:

Missing key(s) in state_dict: "features.0.conv.weight", "", "", "", "", "features.1.conv.weight", "", "", "", "", "features.2.conv.weight", "

and also:

Unexpected key(s) in state_dict: "conv.0.conv1.0.weight", "conv.0.conv1.0.bias", "conv.0.conv1.2.weight", "conv.0.conv1.2.bias", "conv.0.conv1.2.running_mean", "conv.0.conv1.2.running_var", "conv.0.conv1.2.num_batches_tracked", "conv.0.conv2.0.weight", "conv.0.conv2.0.bias", "conv.0.conv2.2.weight", "conv.0.conv2.2.bias", "conv.0.conv2.2.running_mean", "conv.0.conv2.2.running_var", "conv.0.conv2.2.num_batches_tracked", "conv.1.conv1.0.weight", "conv.1.conv1.0.bias", "conv.1.conv1.2.weight", "conv.1.conv1.2.bias", "conv.1.conv1.2.running_mean", "conv.1.conv1.2.running_var", "conv.1.conv1.2.num_batches_tracked

Any hints on this? Thanks in advance.

AS state_dict states, it saves a dictionary. When you load an state dict, pytorch matchs layers by name. If you change names you won’t be able to automatically load previous state dicts.

To do so, you have to load the dict and then iterate over keys changing their name to match new model names.

Is there any way to save the weights of the layers(like feature.0.weight) instead of their components?

You can iterate through parameters and build the state dict your self.
for p,n in model.named_parameters()
build the dictionary…