同时加载tensorflow和pytorch模型时的运行时错误

时间:2019-07-03 01:07:21

标签: python tensorflow keras pytorch

这是设置

Project A
    |-- Module A
           |--->step1 - Loads and run tensorflow pretrained model to detect 
                           texts 
           |
           |--->step2 - import and Instantiates Module-B from Project B
                             --> Runtime Failure here.   

Project B
    |-- Module B
           |--> step 1 - Loads pretrained Pytorch model 
           |--> step 2 - Runs model to classify text.  

这是实际的运行时错误

self.trmodel.load_state_dict(torch.load(self.opts.saved_model, map_location='cpu'))
File/root/anaconda3/envs/py3/lib/python3.6/site-packages/torch/nn/modules/module.py", line 777, in load_state_dict
self.__class__.__name__,\n\t".join(error_msgs)))
RuntimeError: Error(s) in loading state_dict for DataParallel:
Unexpected key(s) in state_dict:module.Transformation.LocalizationNetwork.conv.0.weight",module.Transformation.LocalizationNetwork.conv.1.weight",module.Transformation.LocalizationNetwork.conv.1.bias",module.Transformation.LocalizationNetwork.conv.1.running_mean",module.Transformation.LocalizationNetwork.conv.1.running_var",module.Transformation.LocalizationNetwork.conv.1.num_batches_tracked",module.Transformation.LocalizationNetwork.conv.4.weight",module.Transformation.LocalizationNetwork.conv.5.weight",module.Transformation.LocalizationNetwork.conv.5.bias",module.Transformation.LocalizationNetwork.conv.5.running_mean",module.Transformation.LocalizationNetwork.conv.5.running_var",module.Transformation.LocalizationNetwork.conv.5.num_batches_tracked",module.Transformation.LocalizationNetwork.conv.8.weight",module.Transformation.LocalizationNetwork.conv.9.weight",module.Transformation.LocalizationNetwork.conv.9.bias",module.Transformation.LocalizationNetwork.conv.9.running_mean",module.Transformation.LocalizationNetwork.conv.9.running_var",module.Transformation.LocalizationNetwork.conv.9.num_batches_tracked",module.Transformation.LocalizationNetwork.conv.12.weight",module.Transformation.LocalizationNetwork.conv.13.weight",module.Transformation.LocalizationNetwork.conv.13.bias",module.Transformation.LocalizationNetwork.conv.13.running_mean",module.Transformation.LocalizationNetwork.conv.13.running_var",module.Transformation.LocalizationNetwork.conv.13.num_batches_tracked",module.Transformation.LocalizationNetwork.localization_fc1.0.weight",module.Transformation.LocalizationNetwork.localization_fc1.0.bias",module.Transformation.LocalizationNetwork.localization_fc2.weight",module.Transformation.LocalizationNetwork.localization_fc2.bias",module.Transformation.GridGenerator.inv_delta_C",module.Transformation.GridGenerator.P_hat",module.FeatureExtraction.ConvNet.conv0_1.weight",module.FeatureExtraction.ConvNet.bn0_1.weight",module.FeatureExtraction.ConvNet.bn0_1.bias",module.FeatureExtraction.ConvNet.bn0_1.running_mean",module.FeatureExtraction.ConvNet.bn0_1.running_var",module.FeatureExtraction.ConvNet.bn0_1.num_batches_tracked",module.FeatureExtraction.ConvNet.conv0_2.weight",module.FeatureExtraction.ConvNet.bn0_2.weight",module.FeatureExtraction.ConvNet.bn0_2.bias",module.FeatureExtraction.ConvNet.bn0_2.running_mean",module.FeatureExtraction.ConvNet.bn0_2.running_var",module.FeatureExtraction.ConvNet.bn0_2.num_batches_tracked",module.FeatureExtraction.ConvNet.layer1.0.conv1.weight",module.FeatureExtraction.ConvNet.layer1.0.bn1.weight",module.FeatureExtraction.ConvNet.layer1.0.bn1.bias",module.FeatureExtraction.ConvNet.layer1.0.bn1.running_mean",module.FeatureExtraction.ConvNet.layer1.0.bn1.running_var",module.FeatureExtraction.ConvNet.layer1.0.bn1.num_batches_tracked",module.FeatureExtraction.ConvNet.layer1.0.conv2.weight",module.FeatureExtraction.ConvNet.layer1.0.bn2.weight",module.FeatureExtraction.ConvNet.layer1.0.bn2.bias",module.FeatureExtraction.ConvNet.layer1.0.bn2.running_mean",module.FeatureExtraction.ConvNet.layer1.0.bn2.running_var",module.FeatureExtraction.ConvNet.layer1.0.bn2.num_batches_tracked",module.FeatureExtraction.ConvNet.layer1.0.downsample.0.weight",module.FeatureExtraction.ConvNet.layer1.0.downsample.1.weight",module.FeatureExtraction.ConvNet.layer1.0.downsample.1.bias",module.FeatureExtraction.ConvNet.layer1.0.downsample.1.running_mean",module.FeatureExtraction.ConvNet.layer1.0.downsample.1.running_var",module.FeatureExtraction.ConvNet.layer1.0.downsample.1.num_batches_tracked",module.FeatureExtraction.ConvNet.conv1.weight",module.FeatureExtraction.ConvNet.bn1.weight",module.FeatureExtraction.ConvNet.bn1.bias",module.FeatureExtraction.ConvNet.bn1.running_mean",module.FeatureExtraction.ConvNet.bn1.running_var",module.FeatureExtraction.ConvNet.bn1.num_batches_tracked",module.FeatureExtraction.ConvNet.layer2.0.conv1.weight",module.FeatureExtraction.ConvNet.layer2.0.bn1.weight",module.FeatureExtraction.ConvNet.layer2.0.bn1.bias",module.FeatureExtraction.ConvNet.layer2.0.bn1.running_mean",module.FeatureExtraction.ConvNet.layer2.0.bn1.running_var",module.FeatureExtraction.ConvNet.layer2.0.bn1.num_batches_tracked",module.FeatureExtraction.ConvNet.layer2.0.conv2.weight",module.FeatureExtraction.ConvNet.layer2.0.bn2.weight",module.FeatureExtraction.ConvNet.layer2.0.bn2.bias",module.FeatureExtraction.ConvNet.layer2.0.bn2.running_mean",module.FeatureExtraction.ConvNet.layer2.0.bn2.running_var",module.FeatureExtraction.ConvNet.layer2.0.bn2.num_batches_tracked",module.FeatureExtraction.ConvNet.layer2.0.downsample.0.weight",module.FeatureExtraction.ConvNet.layer2.0.downsample.1.weight",module.FeatureExtraction.ConvNet.layer2.0.downsample.1.bias",module.FeatureExtraction.ConvNet.layer2.0.downsample.1.running_mean",module.FeatureExtraction.ConvNet.layer2.0.downsample.1.running_var",module.FeatureExtraction.ConvNet.layer2.0.downsample.1.num_batches_tracked",module.FeatureExtraction.ConvNet.layer2.1.conv1.weight",module.FeatureExtraction.ConvNet.layer2.1.bn1.weight",module.FeatureExtraction.ConvNet.layer2.1.bn1.bias",module.FeatureExtraction.ConvNet.layer2.1.bn1.running_mean",module.FeatureExtraction.ConvNet.layer2.1.bn1.running_var",module.FeatureExtraction.ConvNet.layer2.1.bn1.num_batches_tracked",module.FeatureExtraction.ConvNet.layer2.1.conv2.weight",module.FeatureExtraction.ConvNet.layer2.1.bn2.weight",module.FeatureExtraction.ConvNet.layer2.1.bn2.bias",module.FeatureExtraction.ConvNet.layer2.1.bn2.running_mean",module.FeatureExtraction.ConvNet.layer2.1.bn2.running_var",module.FeatureExtraction.ConvNet.layer2.1.bn2.num_batches_tracked",module.FeatureExtraction.ConvNet.conv2.weight",module.FeatureExtraction.ConvNet.bn2.weight",module.FeatureExtraction.ConvNet.bn2.bias",module.FeatureExtraction.ConvNet.bn2.running_mean",module.FeatureExtraction.ConvNet.bn2.running_var",module.FeatureExtraction.ConvNet.bn2.num_batches_tracked",module.FeatureExtraction.ConvNet.layer3.0.conv1.weight",module.FeatureExtraction.ConvNet.layer3.0.bn1.weight",module.FeatureExtraction.ConvNet.layer3.0.bn1.bias",module.FeatureExtraction.ConvNet.layer3.0.bn1.running_mean",module.FeatureExtraction.ConvNet.layer3.0.bn1.running_var",module.FeatureExtraction.ConvNet.layer3.0.bn1.num_batches_tracked",module.FeatureExtraction.ConvNet.layer3.0.conv2.weight",module.FeatureExtraction.ConvNet.layer3.0.bn2.weight",module.FeatureExtraction.ConvNet.layer3.0.bn2.bias",module.FeatureExtraction.ConvNet.layer3.0.bn2.running_mean",module.FeatureExtraction.ConvNet.layer3.0.bn2.running_var",module.FeatureExtraction.ConvNet.layer3.0.bn2.num_batches_tracked",module.FeatureExtraction.ConvNet.layer3.0.downsample.0.weight",module.FeatureExtraction.ConvNet.layer3.0.downsample.1.weight",module.FeatureExtraction.ConvNet.layer3.0.downsample.1.bias",module.FeatureExtraction.ConvNet.layer3.0.downsample.1.running_mean",module.FeatureExtraction.ConvNet.layer3.0.downsample.1.running_var",module.FeatureExtraction.ConvNet.layer3.0.downsample.1.num_batches_tracked",module.FeatureExtraction.ConvNet.layer3.1.conv1.weight",module.FeatureExtraction.ConvNet.layer3.1.bn1.weight",module.FeatureExtraction.ConvNet.layer3.1.bn1.bias",module.FeatureExtraction.ConvNet.layer3.1.bn1.running_mean",module.FeatureExtraction.ConvNet.layer3.1.bn1.running_var",module.FeatureExtraction.ConvNet.layer3.1.bn1.num_batches_tracked",module.FeatureExtraction.ConvNet.layer3.1.conv2.weight",module.FeatureExtraction.ConvNet.layer3.1.bn2.weight",module.FeatureExtraction.ConvNet.layer3.1.bn2.bias",module.FeatureExtraction.ConvNet.layer3.1.bn2.running_mean",module.FeatureExtraction.ConvNet.layer3.1.bn2.running_var",module.FeatureExtraction.ConvNet.layer3.1.bn2.num_batches_tracked",module.FeatureExtraction.ConvNet.layer3.2.conv1.weight",module.FeatureExtraction.ConvNet.layer3.2.bn1.weight",module.FeatureExtraction.ConvNet.layer3.2.bn1.bias",module.FeatureExtraction.ConvNet.layer3.2.bn1.running_mean",module.FeatureExtraction.ConvNet.layer3.2.bn1.running_var",module.FeatureExtraction.ConvNet.layer3.2.bn1.num_batches_tracked",module.FeatureExtraction.ConvNet.layer3.2.conv2.weight",module.FeatureExtraction.ConvNet.layer3.2.bn2.weight",module.FeatureExtraction.ConvNet.layer3.2.bn2.bias",module.FeatureExtraction.ConvNet.layer3.2.bn2.running_mean",module.FeatureExtraction.ConvNet.layer3.2.bn2.running_var",module.FeatureExtraction.ConvNet.layer3.2.bn2.num_batches_tracked",module.FeatureExtraction.ConvNet.layer3.3.conv1.weight",module.FeatureExtraction.ConvNet.layer3.3.bn1.weight",module.FeatureExtraction.ConvNet.layer3.3.bn1.bias",module.FeatureExtraction.ConvNet.layer3.3.bn1.running_mean",module.FeatureExtraction.ConvNet.layer3.3.bn1.running_var",module.FeatureExtraction.ConvNet.layer3.3.bn1.num_batches_tracked",module.FeatureExtraction.ConvNet.layer3.3.conv2.weight",module.FeatureExtraction.ConvNet.layer3.3.bn2.weight",module.FeatureExtraction.ConvNet.layer3.3.bn2.bias",module.FeatureExtraction.ConvNet.layer3.3.bn2.running_mean",module.FeatureExtraction.ConvNet.layer3.3.bn2.running_var",module.FeatureExtraction.ConvNet.layer3.3.bn2.num_batches_tracked",module.FeatureExtraction.ConvNet.layer3.4.conv1.weight",module.FeatureExtraction.ConvNet.layer3.4.bn1.weight",module.FeatureExtraction.ConvNet.layer3.4.bn1.bias",module.FeatureExtraction.ConvNet.layer3.4.bn1.running_mean",module.FeatureExtraction.ConvNet.layer3.4.bn1.running_var",module.FeatureExtraction.ConvNet.layer3.4.bn1.num_batches_tracked",module.FeatureExtraction.ConvNet.layer3.4.conv2.weight",module.FeatureExtraction.ConvNet.layer3.4.bn2.weight",module.FeatureExtraction.ConvNet.layer3.4.bn2.bias",module.FeatureExtraction.ConvNet.layer3.4.bn2.running_mean",module.FeatureExtraction.ConvNet.layer3.4.bn2.running_var",module.FeatureExtraction.ConvNet.layer3.4.bn2.num_batches_tracked",module.FeatureExtraction.ConvNet.conv3.weight",module.FeatureExtraction.ConvNet.bn3.weight",module.FeatureExtraction.ConvNet.bn3.bias",module.FeatureExtraction.ConvNet.bn3.running_mean",module.FeatureExtraction.ConvNet.bn3.running_var",module.FeatureExtraction.ConvNet.bn3.num_batches_tracked",module.FeatureExtraction.ConvNet.layer4.0.conv1.weight",module.FeatureExtraction.ConvNet.layer4.0.bn1.weight",module.FeatureExtraction.ConvNet.layer4.0.bn1.bias",module.FeatureExtraction.ConvNet.layer4.0.bn1.running_mean",module.FeatureExtraction.ConvNet.layer4.0.bn1.running_var",module.FeatureExtraction.ConvNet.layer4.0.bn1.num_batches_tracked",module.FeatureExtraction.ConvNet.layer4.0.conv2.weight",module.FeatureExtraction.ConvNet.layer4.0.bn2.weight",module.FeatureExtraction.ConvNet.layer4.0.bn2.bias",module.FeatureExtraction.ConvNet.layer4.0.bn2.running_mean",module.FeatureExtraction.ConvNet.layer4.0.bn2.running_var",module.FeatureExtraction.ConvNet.layer4.0.bn2.num_batches_tracked",module.FeatureExtraction.ConvNet.layer4.1.conv1.weight",module.FeatureExtraction.ConvNet.layer4.1.bn1.weight",module.FeatureExtraction.ConvNet.layer4.1.bn1.bias",module.FeatureExtraction.ConvNet.layer4.1.bn1.running_mean",module.FeatureExtraction.ConvNet.layer4.1.bn1.running_var",module.FeatureExtraction.ConvNet.layer4.1.bn1.num_batches_tracked",module.FeatureExtraction.ConvNet.layer4.1.conv2.weight",module.FeatureExtraction.ConvNet.layer4.1.bn2.weight",module.FeatureExtraction.ConvNet.layer4.1.bn2.bias",module.FeatureExtraction.ConvNet.layer4.1.bn2.running_mean",module.FeatureExtraction.ConvNet.layer4.1.bn2.running_var",module.FeatureExtraction.ConvNet.layer4.1.bn2.num_batches_tracked",module.FeatureExtraction.ConvNet.layer4.2.conv1.weight",module.FeatureExtraction.ConvNet.layer4.2.bn1.weight",module.FeatureExtraction.ConvNet.layer4.2.bn1.bias",module.FeatureExtraction.ConvNet.layer4.2.bn1.running_mean",module.FeatureExtraction.ConvNet.layer4.2.bn1.running_var",module.FeatureExtraction.ConvNet.layer4.2.bn1.num_batches_tracked",module.FeatureExtraction.ConvNet.layer4.2.conv2.weight",module.FeatureExtraction.ConvNet.layer4.2.bn2.weight",module.FeatureExtraction.ConvNet.layer4.2.bn2.bias",module.FeatureExtraction.ConvNet.layer4.2.bn2.running_mean",module.FeatureExtraction.ConvNet.layer4.2.bn2.running_var",module.FeatureExtraction.ConvNet.layer4.2.bn2.num_batches_tracked",module.FeatureExtraction.ConvNet.conv4_1.weight",module.FeatureExtraction.ConvNet.bn4_1.weight",module.FeatureExtraction.ConvNet.bn4_1.bias",module.FeatureExtraction.ConvNet.bn4_1.running_mean",module.FeatureExtraction.ConvNet.bn4_1.running_var",module.FeatureExtraction.ConvNet.bn4_1.num_batches_tracked",module.FeatureExtraction.ConvNet.conv4_2.weight",module.FeatureExtraction.ConvNet.bn4_2.weight",module.FeatureExtraction.ConvNet.bn4_2.bias",module.FeatureExtraction.ConvNet.bn4_2.running_mean",module.FeatureExtraction.ConvNet.bn4_2.running_var",module.FeatureExtraction.ConvNet.bn4_2.num_batches_tracked",module.SequenceModeling.0.rnn.weight_ih_l0",module.SequenceModeling.0.rnn.weight_hh_l0",module.SequenceModeling.0.rnn.bias_ih_l0",module.SequenceModeling.0.rnn.bias_hh_l0",module.SequenceModeling.0.rnn.weight_ih_l0_reverse",module.SequenceModeling.0.rnn.weight_hh_l0_reverse",module.SequenceModeling.0.rnn.bias_ih_l0_reverse",module.SequenceModeling.0.rnn.bias_hh_l0_reverse",module.SequenceModeling.0.linear.weight",module.SequenceModeling.0.linear.bias",module.SequenceModeling.1.rnn.weight_ih_l0",module.SequenceModeling.1.rnn.weight_hh_l0",module.SequenceModeling.1.rnn.bias_ih_l0",module.SequenceModeling.1.rnn.bias_hh_l0",module.SequenceModeling.1.rnn.weight_ih_l0_reverse",module.SequenceModeling.1.rnn.weight_hh_l0_reverse",module.SequenceModeling.1.rnn.bias_ih_l0_reverse",module.SequenceModeling.1.rnn.bias_hh_l0_reverse",module.SequenceModeling.1.linear.weight",module.SequenceModeling.1.linear.bias",module.Prediction.attention_cell.i2h.weight",module.Prediction.attention_cell.h2h.weight",module.Prediction.attention_cell.h2h.bias",module.Prediction.attention_cell.score.weight",module.Prediction.attention_cell.rnn.weight_ih",module.Prediction.attention_cell.rnn.weight_hh",module.Prediction.attention_cell.rnn.bias_ih",module.Prediction.attention_cell.rnn.bias_hh",module.Prediction.generator.weight",module.Prediction.generator.bias".

这是陷阱

  1. 模块A和模块B在同一个Py3 env中独立运行,没有问题
  2. 我在加载模块A后使用了Python shell并导入了模块B,并且加载也很好。

有什么线索吗?

供参考

模块B(失败的代码)

    def pre_load_model(self):

            print("preloading the model with opts "+str(self.opts))

            self.trmodel = Model(self.opts)        

            self.trmodel = torch.nn.DataParallel(self.trmodel)    
            if torch.cuda.is_available():
                self.trmodel = self.trmodel.cuda()
                self.device = torch.device('cuda:0')
            else:
                self.device = torch.device('cpu')       

            # load model
            print('loading pretrained model from %s' % self.opts.saved_model)
            if torch.cuda.is_available():
                self.trmodel.load_state_dict(torch.load(self.opts.saved_model))
            else:
                self.trmodel.load_state_dict(torch.load(self.opts.saved_model, map_location='cpu'))

1 个答案:

答案 0 :(得分:0)

我认为您遇到了Python全局解释器锁定或GIL问题。