环境: ubuntu 16.04 python 3.5 torch 1.0.1 GTX1080 torchvision0.2.2
拿到同事训练好的torch模型,想要保存为onnx格式,为以后转TensorRT做准备。
代码就几行,torch.onnx定义的方法:
import torch
from torch.autograd import Variable
from efficientnet import efficientnet_b0b
import torch.nn as nn
from collections import OrderedDict
model_path = '0net_params.pkl'
filepath='efficientnet.trt'
num_classes=num_classes=1852
net=efficientnet_b0b((224,224),num_classes=num_classes)
if torch.cuda.is_available():
net=net.cuda()
state_dict=torch.load(model_path)
new_state_dict=OrderedDict()
for k,v in state_dict.items():
name=k[7:]
new_state_dict[name]=v
net.load_state_dict(new_state_dict)
net.eval()
print(net)
dummy_input=Variable(torch.randn(64,3,224,224,device='cuda'))
with torch.no_grad():
output=torch.onnx.export(net,dummy_input,filepath,verbose=True)
以下是出现的问题,以此记录:
问题目录
问题1:内存超出问题
最初在没加"with torch.no_grad()"之前,会报错(pytorch新手,顺便积累其他情况):
RuntimeError: CUDA out of memory. Tried to allocate 34.00 MiB (GPU 0; 7.93 GiB total capacity; 6.24 GiB already allocated; 35.19 MiB free; 113.92 MiB cached)
当我把batchsize设置为64的时候会报错,即GPU内存不足。
问题2:nn.DataParallel问题
由于同事给我的torch的模型文件在训练的时候就用到了多GPU,所以最初我的代码,在加载的时候,也要用nn.DataParallel加载,如下所示:
if torch.cuda.is_available():
net = nn.DataParallel(net)
net= net.cuda()
net.load_state_dict(torch.load(model_path))
但是,发现,如果用以下方式加载,在最后导出onnx文件时,会报错:
RuntimeError: Error(s) in loading state_dict for EfficientNet:
Missing key(s) in state_dict: "features.init_block.conv.conv.weight", "features.init_block.conv.bn.bias", "features.init_block.conv.bn.running_var", "features.init_block.conv.bn.weight", "features.init_block.conv.bn.running_mean", "features.stage1.unit1.dw_conv.conv.weight", "features.stage1.unit1.dw_conv.bn.bias", "features.stage1.unit1.dw_conv.bn.running_var", "features.stage1.unit1.dw_conv.bn.weight", "features.stage1.unit1.dw_conv.bn.running_mean", "features.stage1.unit1.se.conv1.bias", "features.stage1.unit1.se.conv1.weight", "features.stage1.unit1.se.conv2.bias", "features.stage1.unit1.se.conv2.weight", "features.stage1.unit1.pw_conv.conv.weight", "features.stage1.unit1.pw_conv.bn.bias", "features.stage1.unit1.pw_conv.bn.running_var", "features.stage1.unit1.pw_conv.bn.weight", "features.stage1.unit1.pw_conv.bn.running_mean", "features.stage2.unit1.conv1.conv.weight", "features.stage2.unit1.conv1.bn.bias", "features.stage2.unit1.conv1.bn.running_var", "features.stage2.unit1.conv1.bn.weight", "features.stage2.unit1.conv1.bn.running_mean", "features.stage2.unit1.conv2.conv.weight", "features.stage2.unit1.conv2.bn.bias", "features.stage2.unit1.conv2.bn.running_var", "features.stage2.unit1.conv2.bn.weight", "features.stage2.unit1.conv2.bn.running_mean", "features.stage2.unit1.se.conv1.bias", "features.stage2.unit1.se.conv1.weight", "features.stage2.unit1.se.conv2.bias", "features.stage2.unit1.se.conv2.weight", "features.stage2.unit1.conv3.conv.weight", "features.stage2.unit1.conv3.bn.bias", "features.stage2.unit1.conv3.bn.running_var", "features.stage2.unit1.conv3.bn.weight", "features.stage2.unit1.conv3.bn.running_mean", "features.stage2.unit2.conv1.conv.weight", "features.stage2.unit2.conv1.bn.bias", "features.stage2.unit2.conv1.bn.running_var", "features.stage2.unit2.conv1.bn.weight", "features.stage2.unit2.conv1.bn.running_mean", "features.stage2.unit2.conv2.conv.weight", "features.stage2.unit2.conv2.bn.bias", "features.stage2.unit2.conv2.bn.running_var", "features.stage2.unit2.conv2.bn.weight", "features.stage2.unit2.conv2.bn.running_mean", "features.stage2.unit2.se.conv1.bias", "features.stage2.unit2.se.conv1.weight", "features.stage2.unit2.se.conv2.bias", "features.stage2.unit2.se.conv2.weight", "features.stage2.unit2.conv3.conv.weight", "features.stage2.unit2.conv3.bn.bias", "features.stage2.unit2.conv3.bn.running_var", "features.stage2.unit2.conv3.bn.weight", "features.stage2.unit2.conv3.bn.running_mean", "features.stage3.unit1.conv1.conv.weight", "features.stage3.unit1.conv1.bn.bias", "features.stage3.unit1.conv1.bn.running_var", "features.stage3.unit1.conv1.bn.weight", "features.stage3.unit1.conv1.bn.running_mean", "features.stage3.unit1.conv2.conv.weight", "features.stage3.unit1.conv2.bn.bias", "features.stage3.unit1.conv2.bn.running_var", "features.stage3.unit1.conv2.bn.weight", "features.stage3.unit1.conv2.bn.running_mean", "features.stage3.unit1.se.conv1.bias", "features.stage3.unit1.se.conv1.weight", "features.stage3.unit1.se.conv2.bias", "features.stage3.unit1.se.conv2.weight", "features.stage3.unit1.conv3.conv.weight", "features.stage3.unit1.conv3.bn.bias", "features.stage3.unit1.conv3.bn.running_var", "features.stage3.unit1.conv3.bn.weight", "features.stage3.unit1.conv3.bn.running_mean", "features.stage3.unit2.conv1.conv.weight", "features.stage3.unit2.conv1.bn.bias", "features.stage3.unit2.conv1.bn.running_var", "features.stage3.unit2.conv1.bn.weight", "features.stage3.unit2.conv1.bn.running_mean", "features.stage3.unit2.conv2.conv.weight", "features.stage3.unit2.conv2.bn.bias", "features.stage3.unit2.conv2.bn.running_var", "features.stage3.unit2.conv2.bn.weight", "features.stage3.unit2.conv2.bn.running_mean", "features.stage3.unit2.se.conv1.bias", "features.stage3.unit2.se.conv1.weight", "features.stage3.unit2.se.conv2.bias", "features.stage3.unit2.se.conv2.weight", "features.stage3.unit2.conv3.conv.weight", "features.stage3.unit2.conv3.bn.bias", "features.stage3.unit2.conv3.bn.running_var", "features.stage3.unit2.conv3.bn.weight", "features.stage3.unit2.conv3.bn.running_mean", "features.stage4.unit1.conv1.conv.weight", "features.stage4.unit1.conv1.bn.bias", "features.stage4.unit1.conv1.bn.running_var", "features.stage4.unit1.conv1.bn.weight", "features.stage4.unit1.conv1.bn.running_mean", "features.stage4.unit1.conv2.conv.weight", "features.stage4.unit1.conv2.bn.bias", "features.stage4.unit1.conv2.bn.running_var", "features.stage4.unit1.conv2.bn.weight", "features.stage4.unit1.conv2.bn.running_mean", "features.stage4.unit1.se.conv1.bias", "features.stage4.unit1.se.conv1.weight", "features.stage4.unit1.se.conv2.bias", "features.stage4.unit1.se.conv2.weight", "features.stage4.unit1.conv3.conv.weight", "features.stage4.unit1.conv3.bn.bias", "features.stage4.unit1.conv3.bn.running_var", "features.stage4.unit1.conv3.bn.weight", "features.stage4.unit1.conv3.bn.running_mean", "features.stage4.unit2.conv1.conv.weight", "features.stage4.unit2.conv1.bn.bias", "features.stage4.unit2.conv1.bn.running_var", "features.stage4.unit2.conv1.bn.weight", "features.stage4.unit2.conv1.bn.running_mean", "features.stage4.unit2.conv2.conv.weight", "features.stage4.unit2.conv2.bn.bias", "features.stage4.unit2.conv2.bn.running_var", "features.stage4.unit2.conv2.bn.weight", "features.stage4.unit2.conv2.bn.running_mean", "features.stage4.unit2.se.conv1.bias", "features.stage4.unit2.se.conv1.weight", "features.stage4.unit2.se.conv2.bias", "features.stage4.unit2.se.conv2.weight", "features.stage4.unit2.conv3.conv.weight", "features.stage4.unit2.conv3.bn.bias", "features.stage4.unit2.conv3.bn.running_var", "features.stage4.unit2.conv3.bn.weight", "features.stage4.unit2.conv3.bn.running_mean", "features.stage4.unit3.conv1.conv.weight", "features.stage4.unit3.conv1.bn.bias", "features.stage4.unit3.conv1.bn.running_var", "features.stage4.unit3.conv1.bn.weight", "features.stage4.unit3.conv1.bn.running_mean", "features.stage4.unit3.conv2.conv.weight", "features.stage4.unit3.conv2.bn.bias", "features.stage4.unit3.conv2.bn.running_var", "features.stage4.unit3.conv2.bn.weight", "features.stage4.unit3.conv2.bn.running_mean", "features.stage4.unit3.se.conv1.bias", "features.stage4.unit3.se.conv1.weight", "features.stage4.unit3.se.conv2.bias", "features.stage4.unit3.se.conv2.weight", "features.stage4.unit3.conv3.conv.weight", "features.stage4.unit3.conv3.bn.bias", "features.stage4.unit3.conv3.bn.running_var", "features.stage4.unit3.conv3.bn.weight", "features.stage4.unit3.conv3.bn.running_mean", "features.stage4.unit4.conv1.conv.weight", "features.stage4.unit4.conv1.bn.bias", "features.stage4.unit4.conv1.bn.running_var", "features.stage4.unit4.conv1.bn.weight", "features.stage4.unit4.conv1.bn.running_mean", "features.stage4.unit4.conv2.conv.weight", "features.stage4.unit4.conv2.bn.bias", "features.stage4.unit4.conv2.bn.running_var", "features.stage4.unit4.conv2.bn.weight", "features.stage4.unit4.conv2.bn.running_mean", "features.stage4.unit4.se.conv1.bias", "features.stage4.unit4.se.conv1.weight", "features.stage4.unit4.se.conv2.bias", "features.stage4.unit4.se.conv2.weight", "features.stage4.unit4.conv3.conv.weight", "features.stage4.unit4.conv3.bn.bias", "features.stage4.unit4.conv3.bn.running_var", "features.stage4.unit4.conv3.bn.weight", "features.stage4.unit4.conv3.bn.running_mean", "features.stage4.unit5.conv1.conv.weight", "features.stage4.unit5.conv1.bn.bias", "features.stage4.unit5.conv1.bn.running_var", "features.stage4.unit5.conv1.bn.weight", "features.stage4.unit5.conv1.bn.running_mean", "features.stage4.unit5.conv2.conv.weight", "features.stage4.unit5.conv2.bn.bias", "features.stage4.unit5.conv2.bn.running_var", "features.stage4.unit5.conv2.bn.weight", "features.stage4.unit5.conv2.bn.running_mean", "features.stage4.unit5.se.conv1.bias", "features.stage4.unit5.se.conv1.weight", "features.stage4.unit5.se.conv2.bias", "features.stage4.unit5.se.conv2.weight", "features.stage4.unit5.conv3.conv.weight", "features.stage4.unit5.conv3.bn.bias", "features.stage4.unit5.conv3.bn.running_var", "features.stage4.unit5.conv3.bn.weight", "features.stage4.unit5.conv3.bn.running_mean", "features.stage4.unit6.conv1.conv.weight", "features.stage4.unit6.conv1.bn.bias", "features.stage4.unit6.conv1.bn.running_var", "features.stage4.unit6.conv1.bn.weight", "features.stage4.unit6.conv1.bn.running_mean", "features.stage4.unit6.conv2.conv.weight", "features.stage4.unit6.conv2.bn.bias", "features.stage4.unit6.conv2.bn.running_var", "features.stage4.unit6.conv2.bn.weight", "features.stage4.unit6.conv2.bn.running_mean", "features.stage4.unit6.se.conv1.bias", "features.stage4.unit6.se.conv1.weight", "features.stage4.unit6.se.conv2.bias", "features.stage4.unit6.se.conv2.weight", "features.stage4.unit6.conv3.conv.weight", "features.stage4.unit6.conv3.bn.bias", "features.stage4.unit6.conv3.bn.running_var", "features.stage4.unit6.conv3.bn.weight", "features.stage4.unit6.conv3.bn.running_mean", "features.stage5.unit1.conv1.conv.weight", "features.stage5.unit1.conv1.bn.bias", "features.stage5.unit1.conv1.bn.running_var", "features.stage5.unit1.conv1.bn.weight", "features.stage5.unit1.conv1.bn.running_mean", "features.stage5.unit1.conv2.conv.weight", "features.stage5.unit1.conv2.bn.bias", "features.stage5.unit1.conv2.bn.running_var", "features.stage5.unit1.conv2.bn.weight", "features.stage5.unit1.conv2.bn.running_mean", "features.stage5.unit1.se.conv1.bias", "features.stage5.unit1.se.conv1.weight", "features.stage5.unit1.se.conv2.bias", "features.stage5.unit1.se.conv2.weight", "features.stage5.unit1.conv3.conv.weight", "features.stage5.unit1.conv3.bn.bias", "features.stage5.unit1.conv3.bn.running_var", "features.stage5.unit1.conv3.bn.weight", "features.stage5.unit1.conv3.bn.running_mean", "features.stage5.unit2.conv1.conv.weight", "features.stage5.unit2.conv1.bn.bias", "features.stage5.unit2.conv1.bn.running_var", "features.stage5.unit2.conv1.bn.weight", "features.stage5.unit2.conv1.bn.running_mean", "features.stage5.unit2.conv2.conv.weight", "features.stage5.unit2.conv2.bn.bias", "features.stage5.unit2.conv2.bn.running_var", "features.stage5.unit2.conv2.bn.weight", "features.stage5.unit2.conv2.bn.running_mean", "features.stage5.unit2.se.conv1.bias", "features.stage5.unit2.se.conv1.weight", "features.stage5.unit2.se.conv2.bias", "features.stage5.unit2.se.conv2.weight", "features.stage5.unit2.conv3.conv.weight", "features.stage5.unit2.conv3.bn.bias", "features.stage5.unit2.conv3.bn.running_var", "features.stage5.unit2.conv3.bn.weight", "features.stage5.unit2.conv3.bn.running_mean", "features.stage5.unit3.conv1.conv.weight", "features.stage5.unit3.conv1.bn.bias", "features.stage5.unit3.conv1.bn.running_var", "features.stage5.unit3.conv1.bn.weight", "features.stage5.unit3.conv1.bn.running_mean", "features.stage5.unit3.conv2.conv.weight", "features.stage5.unit3.conv2.bn.bias", "features.stage5.unit3.conv2.bn.running_var", "features.stage5.unit3.conv2.bn.weight", "features.stage5.unit3.conv2.bn.running_mean", "features.stage5.unit3.se.conv1.bias", "features.stage5.unit3.se.conv1.weight", "features.stage5.unit3.se.conv2.bias", "features.stage5.unit3.se.conv2.weight", "features.stage5.unit3.conv3.conv.weight", "features.stage5.unit3.conv3.bn.bias", "features.stage5.unit3.conv3.bn.running_var", "features.stage5.unit3.conv3.bn.weight", "features.stage5.unit3.conv3.bn.running_mean", "features.stage5.unit4.conv1.conv.weight", "features.stage5.unit4.conv1.bn.bias", "features.stage5.unit4.conv1.bn.running_var", "features.stage5.unit4.conv1.bn.weight", "features.stage5.unit4.conv1.bn.running_mean", "features.stage5.unit4.conv2.conv.weight", "features.stage5.unit4.conv2.bn.bias", "features.stage5.unit4.conv2.bn.running_var", "features.stage5.unit4.conv2.bn.weight", "features.stage5.unit4.conv2.bn.running_mean", "features.stage5.unit4.se.conv1.bias", "features.stage5.unit4.se.conv1.weight", "features.stage5.unit4.se.conv2.bias", "features.stage5.unit4.se.conv2.weight", "features.stage5.unit4.conv3.conv.weight", "features.stage5.unit4.conv3.bn.bias", "features.stage5.unit4.conv3.bn.running_var", "features.stage5.unit4.conv3.bn.weight", "features.stage5.unit4.conv3.bn.running_mean", "features.stage5.unit5.conv1.conv.weight", "features.stage5.unit5.conv1.bn.bias", "features.stage5.unit5.conv1.bn.running_var", "features.stage5.unit5.conv1.bn.weight", "features.stage5.unit5.conv1.bn.running_mean", "features.stage5.unit5.conv2.conv.weight", "features.stage5.unit5.conv2.bn.bias", "features.stage5.unit5.conv2.bn.running_var", "features.stage5.unit5.conv2.bn.weight", "features.stage5.unit5.conv2.bn.running_mean", "features.stage5.unit5.se.conv1.bias", "features.stage5.unit5.se.conv1.weight", "features.stage5.unit5.se.conv2.bias", "features.stage5.unit5.se.conv2.weight", "features.stage5.unit5.conv3.conv.weight", "features.stage5.unit5.conv3.bn.bias", "features.stage5.unit5.conv3.bn.running_var", "features.stage5.unit5.conv3.bn.weight", "features.stage5.unit5.conv3.bn.running_mean", "features.final_block.conv.weight", "features.final_block.bn.bias", "features.final_block.bn.running_var", "features.final_block.bn.weight", "features.final_block.bn.running_mean", "output.fc.bias", "output.fc.weight".
Unexpected key(s) in state_dict: "module.features.init_block.conv.conv.weight", "module.features.init_block.conv.bn.weight", "module.features.init_block.conv.bn.bias", "module.features.init_block.conv.bn.running_mean", "module.features.init_block.conv.bn.running_var", "module.features.init_block.conv.bn.num_batches_tracked", "module.features.stage1.unit1.dw_conv.conv.weight", "module.features.stage1.unit1.dw_conv.bn.weight", "module.features.stage1.unit1.dw_conv.bn.bias", "module.features.stage1.unit1.dw_conv.bn.running_mean", "module.features.stage1.unit1.dw_conv.bn.running_var", "module.features.stage1.unit1.dw_conv.bn.num_batches_tracked", "module.features.stage1.unit1.se.conv1.weight", "module.features.stage1.unit1.se.conv1.bias", "module.features.stage1.unit1.se.conv2.weight", "module.features.stage1.unit1.se.conv2.bias", "module.features.stage1.unit1.pw_conv.conv.weight", "module.features.stage1.unit1.pw_conv.bn.weight", "module.features.stage1.unit1.pw_conv.bn.bias", "module.features.stage1.unit1.pw_conv.bn.running_mean", "module.features.stage1.unit1.pw_conv.bn.running_var", "module.features.stage1.unit1.pw_conv.bn.num_batches_tracked", "module.features.stage2.unit1.conv1.conv.weight", "module.features.stage2.unit1.conv1.bn.weight", "module.features.stage2.unit1.conv1.bn.bias", "module.features.stage2.unit1.conv1.bn.running_mean", "module.features.stage2.unit1.conv1.bn.running_var", "module.features.stage2.unit1.conv1.bn.num_batches_tracked", "module.features.stage2.unit1.conv2.conv.weight", "module.features.stage2.unit1.conv2.bn.weight", "module.features.stage2.unit1.conv2.bn.bias", "module.features.stage2.unit1.conv2.bn.running_mean", "module.features.stage2.unit1.conv2.bn.running_var", "module.features.stage2.unit1.conv2.bn.num_batches_tracked", "module.features.stage2.unit1.se.conv1.weight", "module.features.stage2.unit1.se.conv1.bias", "module.features.stage2.unit1.se.conv2.weight", "module.features.stage2.unit1.se.conv2.bias", "module.features.stage2.unit1.conv3.conv.weight", "module.features.stage2.unit1.conv3.bn.weight", "module.features.stage2.unit1.conv3.bn.bias", "module.features.stage2.unit1.conv3.bn.running_mean", "module.features.stage2.unit1.conv3.bn.running_var", "module.features.stage2.unit1.conv3.bn.num_batches_tracked", "module.features.stage2.unit2.conv1.conv.weight", "module.features.stage2.unit2.conv1.bn.weight", "module.features.stage2.unit2.conv1.bn.bias", "module.features.stage2.unit2.conv1.bn.running_mean", "module.features.stage2.unit2.conv1.bn.running_var", "module.features.stage2.unit2.conv1.bn.num_batches_tracked", "module.features.stage2.unit2.conv2.conv.weight", "module.features.stage2.unit2.conv2.bn.weight", "module.features.stage2.unit2.conv2.bn.bias", "module.features.stage2.unit2.conv2.bn.running_mean", "module.features.stage2.unit2.conv2.bn.running_var", "module.features.stage2.unit2.conv2.bn.num_batches_tracked", "module.features.stage2.unit2.se.conv1.weight", "module.features.stage2.unit2.se.conv1.bias", "module.features.stage2.unit2.se.conv2.weight", "module.features.stage2.unit2.se.conv2.bias", "module.features.stage2.unit2.conv3.conv.weight", "module.features.stage2.unit2.conv3.bn.weight", "module.features.stage2.unit2.conv3.bn.bias", "module.features.stage2.unit2.conv3.bn.running_mean", "module.features.stage2.unit2.conv3.bn.running_var", "module.features.stage2.unit2.conv3.bn.num_batches_tracked", "module.features.stage3.unit1.conv1.conv.weight", "module.features.stage3.unit1.conv1.bn.weight", "module.features.stage3.unit1.conv1.bn.bias", "module.features.stage3.unit1.conv1.bn.running_mean", "module.features.stage3.unit1.conv1.bn.running_var", "module.features.stage3.unit1.conv1.bn.num_batches_tracked", "module.features.stage3.unit1.conv2.conv.weight", "module.features.stage3.unit1.conv2.bn.weight", "module.features.stage3.unit1.conv2.bn.bias", "module.features.stage3.unit1.conv2.bn.running_mean", "module.features.stage3.unit1.conv2.bn.running_var", "module.features.stage3.unit1.conv2.bn.num_batches_tracked", "module.features.stage3.unit1.se.conv1.weight", "module.features.stage3.unit1.se.conv1.bias", "module.features.stage3.unit1.se.conv2.weight", "module.features.stage3.unit1.se.conv2.bias", "module.features.stage3.unit1.conv3.conv.weight", "module.features.stage3.unit1.conv3.bn.weight", "module.features.stage3.unit1.conv3.bn.bias", "module.features.stage3.unit1.conv3.bn.running_mean", "module.features.stage3.unit1.conv3.bn.running_var", "module.features.stage3.unit1.conv3.bn.num_batches_tracked", "module.features.stage3.unit2.conv1.conv.weight", "module.features.stage3.unit2.conv1.bn.weight", "module.features.stage3.unit2.conv1.bn.bias", "module.features.stage3.unit2.conv1.bn.running_mean", "module.features.stage3.unit2.conv1.bn.running_var", "module.features.stage3.unit2.conv1.bn.num_batches_tracked", "module.features.stage3.unit2.conv2.conv.weight", "module.features.stage3.unit2.conv2.bn.weight", "module.features.stage3.unit2.conv2.bn.bias", "module.features.stage3.unit2.conv2.bn.running_mean", "module.features.stage3.unit2.conv2.bn.running_var", "module.features.stage3.unit2.conv2.bn.num_batches_tracked", "module.features.stage3.unit2.se.conv1.weight", "module.features.stage3.unit2.se.conv1.bias", "module.features.stage3.unit2.se.conv2.weight", "module.features.stage3.unit2.se.conv2.bias", "module.features.stage3.unit2.conv3.conv.weight", "module.features.stage3.unit2.conv3.bn.weight", "module.features.stage3.unit2.conv3.bn.bias", "module.features.stage3.unit2.conv3.bn.running_mean", "module.features.stage3.unit2.conv3.bn.running_var", "module.features.stage3.unit2.conv3.bn.num_batches_tracked", "module.features.stage4.unit1.conv1.conv.weight", "module.features.stage4.unit1.conv1.bn.weight", "module.features.stage4.unit1.conv1.bn.bias", "module.features.stage4.unit1.conv1.bn.running_mean", "module.features.stage4.unit1.conv1.bn.running_var", "module.features.stage4.unit1.conv1.bn.num_batches_tracked", "module.features.stage4.unit1.conv2.conv.weight", "module.features.stage4.unit1.conv2.bn.weight", "module.features.stage4.unit1.conv2.bn.bias", "module.features.stage4.unit1.conv2.bn.running_mean", "module.features.stage4.unit1.conv2.bn.running_var", "module.features.stage4.unit1.conv2.bn.num_batches_tracked", "module.features.stage4.unit1.se.conv1.weight", "module.features.stage4.unit1.se.conv1.bias", "module.features.stage4.unit1.se.conv2.weight", "module.features.stage4.unit1.se.conv2.bias", "module.features.stage4.unit1.conv3.conv.weight", "module.features.stage4.unit1.conv3.bn.weight", "module.features.stage4.unit1.conv3.bn.bias", "module.features.stage4.unit1.conv3.bn.running_mean", "module.features.stage4.unit1.conv3.bn.running_var", "module.features.stage4.unit1.conv3.bn.num_batches_tracked", "module.features.stage4.unit2.conv1.conv.weight", "module.features.stage4.unit2.conv1.bn.weight", "module.features.stage4.unit2.conv1.bn.bias", "module.features.stage4.unit2.conv1.bn.running_mean", "module.features.stage4.unit2.conv1.bn.running_var", "module.features.stage4.unit2.conv1.bn.num_batches_tracked", "module.features.stage4.unit2.conv2.conv.weight", "module.features.stage4.unit2.conv2.bn.weight", "module.features.stage4.unit2.conv2.bn.bias", "module.features.stage4.unit2.conv2.bn.running_mean", "module.features.stage4.unit2.conv2.bn.running_var", "module.features.stage4.unit2.conv2.bn.num_batches_tracked", "module.features.stage4.unit2.se.conv1.weight", "module.features.stage4.unit2.se.conv1.bias", "module.features.stage4.unit2.se.conv2.weight", "module.features.stage4.unit2.se.conv2.bias", "module.features.stage4.unit2.conv3.conv.weight", "module.features.stage4.unit2.conv3.bn.weight", "module.features.stage4.unit2.conv3.bn.bias", "module.features.stage4.unit2.conv3.bn.running_mean", "module.features.stage4.unit2.conv3.bn.running_var", "module.features.stage4.unit2.conv3.bn.num_batches_tracked", "module.features.stage4.unit3.conv1.conv.weight", "module.features.stage4.unit3.conv1.bn.weight", "module.features.stage4.unit3.conv1.bn.bias", "module.features.stage4.unit3.conv1.bn.running_mean", "module.features.stage4.unit3.conv1.bn.running_var", "module.features.stage4.unit3.conv1.bn.num_batches_tracked", "module.features.stage4.unit3.conv2.conv.weight", "module.features.stage4.unit3.conv2.bn.weight", "module.features.stage4.unit3.conv2.bn.bias", "module.features.stage4.unit3.conv2.bn.running_mean", "module.features.stage4.unit3.conv2.bn.running_var", "module.features.stage4.unit3.conv2.bn.num_batches_tracked", "module.features.stage4.unit3.se.conv1.weight", "module.features.stage4.unit3.se.conv1.bias", "module.features.stage4.unit3.se.conv2.weight", "module.features.stage4.unit3.se.conv2.bias", "module.features.stage4.unit3.conv3.conv.weight", "module.features.stage4.unit3.conv3.bn.weight", "module.features.stage4.unit3.conv3.bn.bias", "module.features.stage4.unit3.conv3.bn.running_mean", "module.features.stage4.unit3.conv3.bn.running_var", "module.features.stage4.unit3.conv3.bn.num_batches_tracked", "module.features.stage4.unit4.conv1.conv.weight", "module.features.stage4.unit4.conv1.bn.weight", "module.features.stage4.unit4.conv1.bn.bias", "module.features.stage4.unit4.conv1.bn.running_mean", "module.features.stage4.unit4.conv1.bn.running_var", "module.features.stage4.unit4.conv1.bn.num_batches_tracked", "module.features.stage4.unit4.conv2.conv.weight", "module.features.stage4.unit4.conv2.bn.weight", "module.features.stage4.unit4.conv2.bn.bias", "module.features.stage4.unit4.conv2.bn.running_mean", "module.features.stage4.unit4.conv2.bn.running_var", "module.features.stage4.unit4.conv2.bn.num_batches_tracked", "module.features.stage4.unit4.se.conv1.weight", "module.features.stage4.unit4.se.conv1.bias", "module.features.stage4.unit4.se.conv2.weight", "module.features.stage4.unit4.se.conv2.bias", "module.features.stage4.unit4.conv3.conv.weight", "module.features.stage4.unit4.conv3.bn.weight", "module.features.stage4.unit4.conv3.bn.bias", "module.features.stage4.unit4.conv3.bn.running_mean", "module.features.stage4.unit4.conv3.bn.running_var", "module.features.stage4.unit4.conv3.bn.num_batches_tracked", "module.features.stage4.unit5.conv1.conv.weight", "module.features.stage4.unit5.conv1.bn.weight", "module.features.stage4.unit5.conv1.bn.bias", "module.features.stage4.unit5.conv1.bn.running_mean", "module.features.stage4.unit5.conv1.bn.running_var", "module.features.stage4.unit5.conv1.bn.num_batches_tracked", "module.features.stage4.unit5.conv2.conv.weight", "module.features.stage4.unit5.conv2.bn.weight", "module.features.stage4.unit5.conv2.bn.bias", "module.features.stage4.unit5.conv2.bn.running_mean", "module.features.stage4.unit5.conv2.bn.running_var", "module.features.stage4.unit5.conv2.bn.num_batches_tracked", "module.features.stage4.unit5.se.conv1.weight", "module.features.stage4.unit5.se.conv1.bias", "module.features.stage4.unit5.se.conv2.weight", "module.features.stage4.unit5.se.conv2.bias", "module.features.stage4.unit5.conv3.conv.weight", "module.features.stage4.unit5.conv3.bn.weight", "module.features.stage4.unit5.conv3.bn.bias", "module.features.stage4.unit5.conv3.bn.running_mean", "module.features.stage4.unit5.conv3.bn.running_var", "module.features.stage4.unit5.conv3.bn.num_batches_tracked", "module.features.stage4.unit6.conv1.conv.weight", "module.features.stage4.unit6.conv1.bn.weight", "module.features.stage4.unit6.conv1.bn.bias", "module.features.stage4.unit6.conv1.bn.running_mean", "module.features.stage4.unit6.conv1.bn.running_var", "module.features.stage4.unit6.conv1.bn.num_batches_tracked", "module.features.stage4.unit6.conv2.conv.weight", "module.features.stage4.unit6.conv2.bn.weight", "module.features.stage4.unit6.conv2.bn.bias", "module.features.stage4.unit6.conv2.bn.running_mean", "module.features.stage4.unit6.conv2.bn.running_var", "module.features.stage4.unit6.conv2.bn.num_batches_tracked", "module.features.stage4.unit6.se.conv1.weight", "module.features.stage4.unit6.se.conv1.bias", "module.features.stage4.unit6.se.conv2.weight", "module.features.stage4.unit6.se.conv2.bias", "module.features.stage4.unit6.conv3.conv.weight", "module.features.stage4.unit6.conv3.bn.weight", "module.features.stage4.unit6.conv3.bn.bias", "module.features.stage4.unit6.conv3.bn.running_mean", "module.features.stage4.unit6.conv3.bn.running_var", "module.features.stage4.unit6.conv3.bn.num_batches_tracked", "module.features.stage5.unit1.conv1.conv.weight", "module.features.stage5.unit1.conv1.bn.weight", "module.features.stage5.unit1.conv1.bn.bias", "module.features.stage5.unit1.conv1.bn.running_mean", "module.features.stage5.unit1.conv1.bn.running_var", "module.features.stage5.unit1.conv1.bn.num_batches_tracked", "module.features.stage5.unit1.conv2.conv.weight", "module.features.stage5.unit1.conv2.bn.weight", "module.features.stage5.unit1.conv2.bn.bias", "module.features.stage5.unit1.conv2.bn.running_mean", "module.features.stage5.unit1.conv2.bn.running_var", "module.features.stage5.unit1.conv2.bn.num_batches_tracked", "module.features.stage5.unit1.se.conv1.weight", "module.features.stage5.unit1.se.conv1.bias", "module.features.stage5.unit1.se.conv2.weight", "module.features.stage5.unit1.se.conv2.bias", "module.features.stage5.unit1.conv3.conv.weight", "module.features.stage5.unit1.conv3.bn.weight", "module.features.stage5.unit1.conv3.bn.bias", "module.features.stage5.unit1.conv3.bn.running_mean", "module.features.stage5.unit1.conv3.bn.running_var", "module.features.stage5.unit1.conv3.bn.num_batches_tracked", "module.features.stage5.unit2.conv1.conv.weight", "module.features.stage5.unit2.conv1.bn.weight", "module.features.stage5.unit2.conv1.bn.bias", "module.features.stage5.unit2.conv1.bn.running_mean", "module.features.stage5.unit2.conv1.bn.running_var", "module.features.stage5.unit2.conv1.bn.num_batches_tracked", "module.features.stage5.unit2.conv2.conv.weight", "module.features.stage5.unit2.conv2.bn.weight", "module.features.stage5.unit2.conv2.bn.bias", "module.features.stage5.unit2.conv2.bn.running_mean", "module.features.stage5.unit2.conv2.bn.running_var", "module.features.stage5.unit2.conv2.bn.num_batches_tracked", "module.features.stage5.unit2.se.conv1.weight", "module.features.stage5.unit2.se.conv1.bias", "module.features.stage5.unit2.se.conv2.weight", "module.features.stage5.unit2.se.conv2.bias", "module.features.stage5.unit2.conv3.conv.weight", "module.features.stage5.unit2.conv3.bn.weight", "module.features.stage5.unit2.conv3.bn.bias", "module.features.stage5.unit2.conv3.bn.running_mean", "module.features.stage5.unit2.conv3.bn.running_var", "module.features.stage5.unit2.conv3.bn.num_batches_tracked", "module.features.stage5.unit3.conv1.conv.weight", "module.features.stage5.unit3.conv1.bn.weight", "module.features.stage5.unit3.conv1.bn.bias", "module.features.stage5.unit3.conv1.bn.running_mean", "module.features.stage5.unit3.conv1.bn.running_var", "module.features.stage5.unit3.conv1.bn.num_batches_tracked", "module.features.stage5.unit3.conv2.conv.weight", "module.features.stage5.unit3.conv2.bn.weight", "module.features.stage5.unit3.conv2.bn.bias", "module.features.stage5.unit3.conv2.bn.running_mean", "module.features.stage5.unit3.conv2.bn.running_var", "module.features.stage5.unit3.conv2.bn.num_batches_tracked", "module.features.stage5.unit3.se.conv1.weight", "module.features.stage5.unit3.se.conv1.bias", "module.features.stage5.unit3.se.conv2.weight", "module.features.stage5.unit3.se.conv2.bias", "module.features.stage5.unit3.conv3.conv.weight", "module.features.stage5.unit3.conv3.bn.weight", "module.features.stage5.unit3.conv3.bn.bias", "module.features.stage5.unit3.conv3.bn.running_mean", "module.features.stage5.unit3.conv3.bn.running_var", "module.features.stage5.unit3.conv3.bn.num_batches_tracked", "module.features.stage5.unit4.conv1.conv.weight", "module.features.stage5.unit4.conv1.bn.weight", "module.features.stage5.unit4.conv1.bn.bias", "module.features.stage5.unit4.conv1.bn.running_mean", "module.features.stage5.unit4.conv1.bn.running_var", "module.features.stage5.unit4.conv1.bn.num_batches_tracked", "module.features.stage5.unit4.conv2.conv.weight", "module.features.stage5.unit4.conv2.bn.weight", "module.features.stage5.unit4.conv2.bn.bias", "module.features.stage5.unit4.conv2.bn.running_mean", "module.features.stage5.unit4.conv2.bn.running_var", "module.features.stage5.unit4.conv2.bn.num_batches_tracked", "module.features.stage5.unit4.se.conv1.weight", "module.features.stage5.unit4.se.conv1.bias", "module.features.stage5.unit4.se.conv2.weight", "module.features.stage5.unit4.se.conv2.bias", "module.features.stage5.unit4.conv3.conv.weight", "module.features.stage5.unit4.conv3.bn.weight", "module.features.stage5.unit4.conv3.bn.bias", "module.features.stage5.unit4.conv3.bn.running_mean", "module.features.stage5.unit4.conv3.bn.running_var", "module.features.stage5.unit4.conv3.bn.num_batches_tracked", "module.features.stage5.unit5.conv1.conv.weight", "module.features.stage5.unit5.conv1.bn.weight", "module.features.stage5.unit5.conv1.bn.bias", "module.features.stage5.unit5.conv1.bn.running_mean", "module.features.stage5.unit5.conv1.bn.running_var", "module.features.stage5.unit5.conv1.bn.num_batches_tracked", "module.features.stage5.unit5.conv2.conv.weight", "module.features.stage5.unit5.conv2.bn.weight", "module.features.stage5.unit5.conv2.bn.bias", "module.features.stage5.unit5.conv2.bn.running_mean", "module.features.stage5.unit5.conv2.bn.running_var", "module.features.stage5.unit5.conv2.bn.num_batches_tracked", "module.features.stage5.unit5.se.conv1.weight", "module.features.stage5.unit5.se.conv1.bias", "module.features.stage5.unit5.se.conv2.weight", "module.features.stage5.unit5.se.conv2.bias", "module.features.stage5.unit5.conv3.conv.weight", "module.features.stage5.unit5.conv3.bn.weight", "module.features.stage5.unit5.conv3.bn.bias", "module.features.stage5.unit5.conv3.bn.running_mean", "module.features.stage5.unit5.conv3.bn.running_var", "module.features.stage5.unit5.conv3.bn.num_batches_tracked", "module.features.final_block.conv.weight", "module.features.final_block.bn.weight", "module.features.final_block.bn.bias", "module.features.final_block.bn.running_mean", "module.features.final_block.bn.running_var", "module.features.final_block.bn.num_batches_tracked", "module.output.fc.weight", "module.output.fc.bias".
仔细看可以看到,用DataParallel保存的模型key值前面会多处’modules.’,而我们需要的不带,就导致生成onnx的方法无法读到对应的属性,以此,我参考了这篇博文的方法:解决pytorch中DataParallel后模型参数出现问题的方法
即自己设置一个有序字典,有选择的读取模型数据,然后保存成torch.onnx可以读取的格式,即可,具体正确的代码如问题一上面写的那样。
问题3:有些onnx的属性无法export(Failed to export an ONNX attribute)
RuntimeError: Failed to export an ONNX attribute, since it's not constant, please try to make things (e.g., kernel size) static if possible
网上说,是pytorch版本问题,应该从1.1.0降到1.0.1。(使用清华源出现错误详见问题4)
问题4:pip外链报错:Cannot determine archive format of /tmp/pip-fd2724ty-build
用清华源的时候,出现错误
查询资料发现,外链(清华源)下载软件是要使用新命令的:
sudo pip3 install --index-url https://pypi.tuna.tsinghua.edu.cn/simple/ torch==1.0.1
就ok了。
问题5:安装torch1.0.1之后,发现自动变成1.1.0
发现如问题4的图,明明装的torch1.0.1,装上的却是1.1.0
>>> import torch
>>> torch.__version__
'1.1.0'
>>>
于是怀疑是在torch1.0.1之后安装torchvision的问题,安装torchvision的时候没有控制版本。查了一下,果然torchvision更新torch
然后重装了torch1.0.1,然后指定版本装了torchvision0.2.2。
问题6:RuntimeError: ONNX symbolic expected a constant value in the trace
在百度上找不到解答,暂时翻不出去。
绝望中用debug看了一下运行的源码,发现在我错的地方,value的值如下
value={Value} 391 defined in (%391 : int[] = prim::ListConstruct(%377, %382, %385, %390), scope: EfficientNet/Sequential[features]/EffiInitBlock[init_block])
可知391层用到了prim::ListConstruct这个结构,但是很明显,torch的意思因为它不是onnx::Constant格式的,就要报错了。