Tong-ZHAO / Pixel2Mesh-Pytorch

Final project for RecVis 18
127 stars 34 forks source link

Failed to load the trained model #7

Closed sunshineatnoon closed 5 years ago

sunshineatnoon commented 5 years ago

Hi, Thanks for open-sourcing this awesome project. However, when I try to load the log/2019-01-18T02:32:24.320546/network_4.pth, I got the following error:

RuntimeError: Error(s) in loading state_dict for P2M_Model:
Missing key(s) in state_dict: "GCN_0.blocks.0.conv1.weight", "GCN_0.blocks.0.conv2.weight", "GCN_0.blocks.1.conv1.weight", "GCN_0.blocks.1.conv2.weight", "GCN_0.blocks.2.conv1.weight", "GCN_0.blocks.2.conv2.weight", "GCN_0.blocks.3.conv1.weight", "GCN_0.blocks.3.conv2.weight", "GCN_0.blocks.4.conv1.weight", "GCN_0.blocks.4.conv2.weight", "GCN_0.blocks.5.conv1.weight", "GCN_0.blocks.5.conv2.weight", "GCN_0.conv1.weight", "GCN_0.conv2.weight", "GCN_1.blocks.0.conv1.weight", "GCN_1.blocks.0.conv2.weight", "GCN_1.blocks.1.conv1.weight", "GCN_1.blocks.1.conv2.weight", "GCN_1.blocks.2.conv1.weight", "GCN_1.blocks.2.conv2.weight", "GCN_1.blocks.3.conv1.weight", "GCN_1.blocks.3.conv2.weight", "GCN_1.blocks.4.conv1.weight", "GCN_1.blocks.4.conv2.weight", "GCN_1.blocks.5.conv1.weight", "GCN_1.blocks.5.conv2.weight", "GCN_1.conv1.weight", "GCN_1.conv2.weight", "GCN_2.blocks.0.conv1.weight", "GCN_2.blocks.0.conv2.weight", "GCN_2.blocks.1.conv1.weight", "GCN_2.blocks.1.conv2.weight", "GCN_2.blocks.2.conv1.weight", "GCN_2.blocks.2.conv2.weight", "GCN_2.blocks.3.conv1.weight", "GCN_2.blocks.3.conv2.weight", "GCN_2.blocks.4.conv1.weight", "GCN_2.blocks.4.conv2.weight", "GCN_2.blocks.5.conv1.weight", "GCN_2.blocks.5.conv2.weight", "GCN_2.conv1.weight", "GCN_2.conv2.weight", "GConv.weight". 
Unexpected key(s) in state_dict: "GCN_0.blocks.0.conv1.weight_1", "GCN_0.blocks.0.conv1.weight_2", "GCN_0.blocks.0.conv2.weight_1", "GCN_0.blocks.0.conv2.weight_2", "GCN_0.blocks.1.conv1.weight_1", "GCN_0.blocks.1.conv1.weight_2", "GCN_0.blocks.1.conv2.weight_1", "GCN_0.blocks.1.conv2.weight_2", "GCN_0.blocks.2.conv1.weight_1", "GCN_0.blocks.2.conv1.weight_2", "GCN_0.blocks.2.conv2.weight_1", "GCN_0.blocks.2.conv2.weight_2", "GCN_0.blocks.3.conv1.weight_1", "GCN_0.blocks.3.conv1.weight_2", "GCN_0.blocks.3.conv2.weight_1", "GCN_0.blocks.3.conv2.weight_2", "GCN_0.blocks.4.conv1.weight_1", "GCN_0.blocks.4.conv1.weight_2", "GCN_0.blocks.4.conv2.weight_1", "GCN_0.blocks.4.conv2.weight_2", "GCN_0.blocks.5.conv1.weight_1", "GCN_0.blocks.5.conv1.weight_2", "GCN_0.blocks.5.conv2.weight_1", "GCN_0.blocks.5.conv2.weight_2", "GCN_0.conv1.weight_1", "GCN_0.conv1.weight_2", "GCN_0.conv2.weight_1", "GCN_0.conv2.weight_2", "GCN_1.blocks.0.conv1.weight_1", "GCN_1.blocks.0.conv1.weight_2", "GCN_1.blocks.0.conv2.weight_1", "GCN_1.blocks.0.conv2.weight_2", "GCN_1.blocks.1.conv1.weight_1", "GCN_1.blocks.1.conv1.weight_2", "GCN_1.blocks.1.conv2.weight_1", "GCN_1.blocks.1.conv2.weight_2", "GCN_1.blocks.2.conv1.weight_1", "GCN_1.blocks.2.conv1.weight_2", "GCN_1.blocks.2.conv2.weight_1", "GCN_1.blocks.2.conv2.weight_2", "GCN_1.blocks.3.conv1.weight_1", "GCN_1.blocks.3.conv1.weight_2", "GCN_1.blocks.3.conv2.weight_1", "GCN_1.blocks.3.conv2.weight_2", "GCN_1.blocks.4.conv1.weight_1", "GCN_1.blocks.4.conv1.weight_2", "GCN_1.blocks.4.conv2.weight_1", "GCN_1.blocks.4.conv2.weight_2", "GCN_1.blocks.5.conv1.weight_1", "GCN_1.blocks.5.conv1.weight_2", "GCN_1.blocks.5.conv2.weight_1", "GCN_1.blocks.5.conv2.weight_2", "GCN_1.conv1.weight_1", "GCN_1.conv1.weight_2", "GCN_1.conv2.weight_1", "GCN_1.conv2.weight_2", "GCN_2.blocks.0.conv1.weight_1", "GCN_2.blocks.0.conv1.weight_2", "GCN_2.blocks.0.conv2.weight_1", "GCN_2.blocks.0.conv2.weight_2", "GCN_2.blocks.1.conv1.weight_1", "GCN_2.blocks.1.conv1.weight_2", "GCN_2.blocks.1.conv2.weight_1", "GCN_2.blocks.1.conv2.weight_2", "GCN_2.blocks.2.conv1.weight_1", "GCN_2.blocks.2.conv1.weight_2", "GCN_2.blocks.2.conv2.weight_1", "GCN_2.blocks.2.conv2.weight_2", "GCN_2.blocks.3.conv1.weight_1", "GCN_2.blocks.3.conv1.weight_2", "GCN_2.blocks.3.conv2.weight_1", "GCN_2.blocks.3.conv2.weight_2", "GCN_2.blocks.4.conv1.weight_1", "GCN_2.blocks.4.conv1.weight_2", "GCN_2.blocks.4.conv2.weight_1", "GCN_2.blocks.4.conv2.weight_2", "GCN_2.blocks.5.conv1.weight_1", "GCN_2.blocks.5.conv1.weight_2", "GCN_2.blocks.5.conv2.weight_1", "GCN_2.blocks.5.conv2.weight_2", "GCN_2.conv1.weight_1", "GCN_2.conv1.weight_2", "GCN_2.conv2.weight_1", "GCN_2.conv2.weight_2", "GConv.weight_1", "GConv.weight_2". 
Tong-ZHAO commented 5 years ago

Hi, thanks for the feedback. Did you use evaluate.py to load the model? What's more if you load the model on CPU, please use: network.load_state_dict(torch.load(opt.modelPath, map_location='cpu')). Please tell me if it does not solve your problem.

vbcpascal commented 5 years ago

I have the same problem here. I seems that each key needed have been saved as key_1 and key_2.

xiayizhan2017 commented 5 years ago

@Tong-ZHAO hi, I printed the key of log/2019-01-18T02:32:24.320546/network_4.pth, GCN_0.blocks.o.conv1.weight_1 GCN_0.blocks.o.conv1.weight_2 GCN_0.blocks.o.conv1.bias and I printed the key of model, GCN_0.blocks.o.conv1.weight GCN_0.blocks.o.conv1.bias It is different.

Tong-ZHAO commented 5 years ago

Hi all, thanks for your comments. It seems that I forgot to update the file gcn_layers.py in the last commit. I feel really sorry >.< I'm on holiday and I'll update the repo once I go back. For a quick hint, you can simply replace the function GraphConvolution by the following one and see what happens. Normally it should work.

class GraphConvolution(Module):
 """Simple GCN layer
Similar to https://arxiv.org/abs/1609.02907
"""

def __init__(self, in_features, out_features, adjs, bias=True, use_cuda = True):
    super(GraphConvolution, self).__init__()
    self.in_features = in_features
    self.out_features = out_features

    adj0 = torch_sparse_tensor(*adjs[0], use_cuda)
    adj1 = torch_sparse_tensor(*adjs[1], use_cuda)
    self.adjs = [adj0, adj1]

    self.weight_1 = Parameter(torch.FloatTensor(in_features, out_features))
    self.weight_2 = Parameter(torch.FloatTensor(in_features, out_features))

    if bias:
        self.bias = Parameter(torch.FloatTensor(out_features))
    else:
        self.register_parameter('bias', None)
    self.reset_parameters()

def reset_parameters(self):
    stdv = 1. / math.sqrt(self.weight.size(1))
    self.weight_1.data.uniform_(-stdv, stdv)
    self.weight_2.data.uniform_(-stdv, stdv)
    if self.bias is not None:
        self.bias.data.uniform_(-stdv, stdv)

def forward(self, input):
    support_1 = torch.matmul(input, self.weight_1)
    support_2 = torch.matmul(input, self.weight_2)
    #output = torch.spmm(adj, support)
    output1 = dot(self.adjs[0], support_1, True)
    output2 = dot(self.adjs[1], support_2, True)
    output = output1 + output2
    if self.bias is not None:
        return output + self.bias
    else:
        return output

def __repr__(self):
    return self.__class__.__name__ + ' (' \
           + str(self.in_features) + ' -> ' \
           + str(self.out_features) + ')'
xiayizhan2017 commented 5 years ago

@Tong-ZHAO hi, thanks for your reply. when I changed the code 'stdv = 1. / math.sqrt(self.weight.size(1))' to 'stdv = 1. / math.sqrt(self.weight1.size(1))' , it solved the problem of load the trained model.

I generated my own data to test, but the results were not good. Hope that the author can provide the code to generate the data. Have a good holiday! >.<