sovit-123 / fasterrcnn-pytorch-training-pipeline

PyTorch Faster R-CNN Object Detection on Custom Dataset
MIT License
187 stars 64 forks source link

Export non-dynamic onnx #148

Closed knoppmyth closed 8 hours ago

knoppmyth commented 5 days ago

Hi, is it possible to export an ONNX model without dynamic axes? I'd like to use OpenCV's DNN to read the model and process it. However OpenCV currently doesn't support dynamic shapes.

Thanks,

km

sovit-123 commented 5 days ago

Can you try the following? In the export.py file, this is the export method:

# Export the model
    torch.onnx.export(
        model,
        x,
        os.path.join(OUT_DIR, args['out']),
        export_params=True,
        opset_version=11,
        do_constant_folding=True,
        input_names=['input'],
        output_names = ['output'],
        dynamic_axes={
            'input': {0: 'batch_size', 2: 'height', 3: 'width'},
            'output' : {0 : 'batch_size'}
        }
    )

Can you try commenting out the dynamic_axes argument? This part:

dynamic_axes={
            'input': {0: 'batch_size', 2: 'height', 3: 'width'},
            'output' : {0 : 'batch_size'}
        }
knoppmyth commented 1 day ago

Thanks for the response. I'm getting this error:

[ERROR:0@0.584] global onnx_importer.cpp:1035 handleNode DNN/ONNX: ERROR during processing node with 2 inputs and 1 outputs: [Concat]:(onnx_node!/transform/Concat_1) from domain='ai.onnx'
Traceback (most recent call last):
  File "/uni_obj_detector.py", line 201, in <module>
    main()
  File "/uni_obj_detector.py", line 183, in main
    detector_to_run(args.detector, args.device, args.model_fam, args.weights)
  File "/uni_obj_detector.py", line 46, in detector_to_run
    run_inference_cv2_onnx(weights_path, model_fam)
  File "/uni_obj_detector.py", line 54, in run_inference_cv2_onnx
    net = cv2.dnn.readNet(weights)
          ^^^^^^^^^^^^^^^^^^^^^^^^
cv2.error: OpenCV(4.10.0) /usr/src/debug/opencv/opencv/modules/dnn/src/onnx/onnx_importer.cpp:1057: error: (-2:Unspecified error) in function 'handleNode'
> Node [Concat@ai.onnx]:(onnx_node!/transform/Concat_1) parse error: OpenCV(4.10.0) /usr/src/debug/opencv/opencv/modules/dnn/src/layers/concat_layer.cpp:104: error: (-215:Assertion failed) curShape.size() == outputs[0].size() in function 'getMemoryShapes'
>
sovit-123 commented 21 hours ago

@knoppmyth I need to check this. I have never tried the models exported from this library with OpenCV DNN. Will you be able to provide me with a reproducible code base?

knoppmyth commented 8 hours ago

@sovit-123 I'm going to close this as the issue is with OpenCV and won't be resolved until OpenCV 5.0. Thanks for the responses and the code!

sovit-123 commented 4 hours ago

Got it. Thanks for the update.