Unexpected key(s) in state_dict: "module.backbone.bn1.num_batches_tracked"

pytorch预测的时候报异常了:
{RuntimeError}Error(s) in loading state_dict for DataParallel:
    Unexpected key(s) in state_dict: "module.backbone.bn1.num_batches_tracked", "module.backbone.shuffles.ShuffleConvs_0.DownsampleUnit.bn1r.num_batches_tracked", "module.backbone.shuffles.ShuffleConvs_0.DownsampleUnit.bn2r.num_batches_tracked", "module.backbone.shuffles.ShuffleConvs_0.DownsampleUnit.bn3r.num_batches_tracked", "module.backbone.shuffles.ShuffleConvs_0.DownsampleUnit.bn1l.num_batches_tracked", "module.backbone.shuffles.ShuffleConvs_0.DownsampleUnit.bn2l.num_batches_tracked", "module.backbone.shuffles.ShuffleConvs_0.ShuffleUnit0_0.bn1.num_batches_tracked", "module.backbone.shuffles.ShuffleConvs_0.ShuffleUnit0_0.bn2.num_batches_tracked", "module.backbone.shuffles.ShuffleConvs_0.ShuffleUnit0_0.bn3.num_batches_tracked", "module.backbone.shuffles.ShuffleConvs_0.ShuffleUnit0_1.bn1.num_batches_tracked", "module.backbone.shuffles.ShuffleConvs_0.ShuffleUnit0_1.bn2.num_batches_tracked", "module.backbone.shuffles.ShuffleCo...

经过分析是用torch0.4.1版本训练,用0.4.0版本预测的时候报的异常

 

这里提供一种改key的方法:

model.load_state_dict({k.replace('module.',''):v for k,v in torch.load(checkpoint_path)['state_dict'].items()})

你可能感兴趣的:(torch)