YonghaoHe / LFD-A-Light-and-Fast-Detector

LFD is a big update upon LFFD. Generally, LFD is a multi-class object detector characterized by lightweight, low inference latency and superior precision. It is for real-world appilcations.
418 stars 82 forks source link

请问模型如何导出onnx? #40

Open ChungTak opened 2 years ago

ChungTak commented 2 years ago

只看到有TensorRT deployment 部署,请问onnx导出要运行那个脚本?

YonghaoHe commented 2 years ago

@ChungTak 在TRT部署的脚本里面进行了ONNX的导出,你可以稍微看几行代码就知道了。

ChungTak commented 2 years ago

我抽取了里面的导出代码来运行,但转换不成功,运行代码:

from _WIDERFACE_LFD_L_work_dir_20210116_153509.WIDERFACE_LFD_L import config_dict, prepare_model
prepare_model()
model = config_dict['model']
input_shapes = [[1, 3, 720, 1280]]
input_names = ['input_data']
output_names = ['classification_output', 'regression_output'],

temp_onnx_file_path = os.path.join(
    os.path.dirname(__file__), 'temp', 'temp.onnx')
if not os.path.exists(os.path.dirname(temp_onnx_file_path)):
    os.makedirs(os.path.dirname(temp_onnx_file_path))

input_tensors = [torch.rand(input_shape) for input_shape in input_shapes]

print('Start to convert pytorch model to onnx format------------------')
torch.onnx.export(model,
                  args=tuple(input_tensors),
                  f=temp_onnx_file_path,
                  verbose=True,
                  input_names=input_names,
                  output_names=output_names,
                  opset_version=10
                  )
print('Converting successfully---------------')

错误:

Start to convert pytorch model to onnx format------------------
Traceback (most recent call last):
  File "export.py", line 26, in <module>
    torch.onnx.export(model,
  File "/home/.local/lib/python3.8/site-packages/torch/onnx/__init__.py", line 225, in export
    return utils.export(model, args, f, export_params, verbose, training,
  File "/home/.local/lib/python3.8/site-packages/torch/onnx/utils.py", line 85, in export
    _export(model, args, f, export_params, verbose, training, input_names, output_names,
  File "/home/.local/lib/python3.8/site-packages/torch/onnx/utils.py", line 632, in _export
    _model_to_graph(model, args, verbose, input_names,
  File "/home/.local/lib/python3.8/site-packages/torch/onnx/utils.py", line 435, in _model_to_graph
    _set_input_and_output_names(graph, input_names, output_names)
  File "/home/.local/lib/python3.8/site-packages/torch/onnx/utils.py", line 712, in _set_input_and_output_names
    set_names(list(graph.outputs()), output_names, 'output')
  File "/home/.local/lib/python3.8/site-packages/torch/onnx/utils.py", line 710, in set_names
    node.setDebugName(name)
TypeError: setDebugName(): incompatible function arguments. The following argument types are supported:
    1. (self: torch._C.Value, arg0: str) -> torch._C.Value

Invoked with: 644 defined in (%644 : Float(1:76760, 76760:1, 1:1, requires_grad=1, device=cpu) = onnx::Concat[axis=1](%472, %510, %548, %586, %624) # /home/LFD-A-Light-and-Fast-Detector/lfd/model/lfd.py:539:0
), ['classification_output', 'regression_output']

请问是那里问题?运行推理没问题,用的是官方 WIDERFACE_LFD_L 模型 WIDERFACE_LFD_L.py 试过github上和网盘上的都是一样的错误

运行环境: python3.8 pytourch 1.7.1

YonghaoHe commented 2 years ago

@ChungTak ONNX的版本是不是有问题?这个报错和我那个代码应该没有关系。