我尝试将我的 pytorch Resnet50 模型转换为 ONNX 并进行推理。转换程序没有错误,但是onnxruntime的onnx模型的最终结果与pytorch的origin模型的结果有很大差距。
可能的解决方案是什么?
ONNX版本:1.5.0
pytorch版本:1.1.0
CUDA:9.0
系统:Ubuntu 18.06
Python:3.5
这是转换代码
import torch
import models
from collections import OrderedDict
state_dict = "/home/yx-wan/newhome/workspace/filter-pruning-geometric-median/scripts/snapshots/resnet50-rate-0.7/best.resnet50.GM_0.7_76.82.pth.tar"
arch = 'resnet50'
def import_sparse(model,state_dict):
new_state_dict = OrderedDict()
for k, v in state_dict.items():
name = k[7:] # remove `module.`
new_state_dict[name] = v
model.load_state_dict(new_state_dict)
print("sparse_model_loaded")
return model
# initialize model
model = models.__dict__[arch](pretrained=False).cuda()
checkpoint = torch.load(state_dict)
model = import_sparse(model, checkpoint['state_dict'])
print("Top 1 precise of model: {}".format(checkpoint['best_prec1']))
dummy_input =torch.randn(1, 3, 224, 224).cuda()
torch.onnx.export(model, dummy_input, "{}.onnx".format(arch), …Run Code Online (Sandbox Code Playgroud)