我正在尝试使用以下代码将此链接中的 pytorch 模型转换为 onnx 模型:
device=t.device('cuda:0' if t.cuda.is_available() else 'cpu')
print(device)
faster_rcnn = FasterRCNNVGG16()
trainer = FasterRCNNTrainer(faster_rcnn).cuda()
#trainer = FasterRCNNTrainer(faster_rcnn).to(device)
trainer.load('./checkpoints/model.pth')
dummy_input = t.randn(1, 3, 300, 300, device = 'cuda')
#dummy_input = dummy_input.to(device)
t.onnx.export(faster_rcnn, dummy_input, "model.onnx", verbose = True)
但是我收到以下错误(抱歉,stackoverflow 下方的块引用不会让整个跟踪采用代码格式,也不会让问题以其他方式发布):
Traceback (most recent call last): small_object_detection_master_samirsen\onnxtest.py", line 44, in <module> t.onnx.export(faster_rcnn, dummy_input, "fasterrcnn_10120119_06025842847785781.onnx", verbose = True) File "C:\Users\HP\AppData\Local\Programs\Python\Python36\lib\site-packages\torch\onnx\__init__.py",
第 132 行,在导出 strip_doc_string,dynamic_axes) 文件“C:\Users\HP\AppData\Local\Programs\Python\Python36\lib\site-packages\torch\onnx\utils.py”,第 64 行,在导出 example_outputs= example_outputs、strip_doc_string=strip_doc_string、dynamic_axes=dynamic_axes) 文件“C:\Users\HP\AppData\Local\Programs\Python\Python36\lib\site-packages\torch\onnx\utils.py”,第 329 行,在 _export _retain_param_name , do_constant_folding) 文件“C:\Users\HP\AppData\Local\Programs\Python\Python36\lib\site-packages\torch\onnx\utils.py”,第 213 行,在 _model_to_graph 图中,torch_out = _trace_and_get_graph_from_model(model,参数,培训)文件“C:\Users\HP\AppData\Local\Programs\Python\Python36\lib\site-packages\torch\onnx\utils.py”,第 171 行,在 _trace_and_get_graph_from_model 跟踪中,torch_out = torch.jit.get_trace_graph(model, args, _force_outplace=True) 文件“C:\Users\HP\AppData\Local\Programs\Python\Python36\lib\site-packages\torch\ jit__init__.py”,第 256 行,在 get_trace_graph 返回 LegacyTracedModule(f, _force_outplace, return_inputs)(*args, **kwargs) 文件“C:\Users\HP\AppData\Local\Programs\Python\Python36\lib\site- packages\torch\nn\modules\module.py",第 547 行,在_force_outplace, return_inputs)(*args, **kwargs) 文件“C:\Users\HP\AppData\Local\Programs\Python\Python36\lib\site-packages\torch\nn\modules\module.py”,第 547 行, 在_force_outplace, return_inputs)(*args, **kwargs) 文件“C:\Users\HP\AppData\Local\Programs\Python\Python36\lib\site-packages\torch\nn\modules\module.py”,第 547 行, 在调用 结果 = self.forward(*input, **kwargs) 文件“C:\Users\HP\AppData\Local\Programs\Python\Python36\lib\site-packages\torch\jit__init__.py”,第 323 行,在forward out = self.inner(*trace_inputs) 文件“C:\Users\HP\AppData\Local\Programs\Python\Python36\lib\site-packages\torch\nn\modules\module.py”,第 545 行,在称呼 结果 = self._slow_forward(*input, **kwargs) 文件“C:\Users\HP\AppData\Local\Programs\Python\Python36\lib\site-packages\torch\nn\modules\module.py”,行531,在 _slow_forward 文件“C:\Users\HP\AppData\Local\Programs\Python\Python36\lib\site-packages\torch\nn\modules\module.py”中,第 531 行,在 _slow_forward 结果 = self.forward (*input, **kwargs) 文件“D:\smallobject2\export test s\small_object_detection_master_samirsen\model\faster_rcnn.py”,第 133 行,向前 h,rois,roi_indices) 文件“C:\Users\HP\AppData\ Local\Programs\Python\Python36\lib\site-packages\torch\nn\modules\module.py",第 545 行,正在调用中 结果 = self._slow_forward(*input, **kwargs) 文件“C:\Users\HP\AppData\Local\Programs\Python\Python36\lib\site-packages\torch\nn\modules\module.py”,行531,在 _slow_forward 结果 = self.forward(*input, **kwargs) 文件 "D:\smallobject2\export test s\small_object_detection_master_samirsen\model\faster_rcnn_vgg16.py",第 142 行,在 forward pool = self.roi(x, indices_and_rois)文件“C:\Users\HP\AppData\Local\Programs\Python\Python36\lib\site-packages\torch\nn\modules\module.py”,第 545 行,调用中 结果 = self._slow_forward(*input, **kwargs) 文件“C:\Users\HP\AppData\Local\Programs\Python\Python36\lib\site-packages\torch\nn\modules\module.py”,行531,在_slow_forward result = self.forward(*input, **kwargs) 文件“D:\smallobject2\export test s\small_object_detection_master_samirsen\model\roi_module.py”,第85行,在forward return self.RoI(x, rois ) RuntimeError: 试图跟踪 RoI,但不支持跟踪遗留函数