onnx / onnx-tensorflow

Tensorflow Backend for ONNX
Other
1.29k stars 296 forks source link

Yolov5 onnx weights to tensorflow error #678

Open tdurand opened 4 years ago

tdurand commented 4 years ago

Hi,

Describe the bug

Error while converting Yolov5s onnx weights to tensorflow

Traceback (most recent call last):
  File "/home/tdurand/.local/bin/onnx-tf", line 8, in <module>
    sys.exit(main())
  File "/home/tdurand/.local/lib/python3.6/site-packages/onnx_tf/cli.py", line 20, in main
    return onnx_tf.converter.main(args[1:])
  File "/home/tdurand/.local/lib/python3.6/site-packages/onnx_tf/converter.py", line 20, in main
    convert(**{k: v for k, v in vars(args).items() if v is not None})
  File "/home/tdurand/.local/lib/python3.6/site-packages/onnx_tf/converter.py", line 133, in convert
    tf_rep = backend.prepare(onnx_model, **kwargs)
  File "/home/tdurand/.local/lib/python3.6/site-packages/onnx_tf/backend.py", line 65, in prepare
    return cls.onnx_model_to_tensorflow_rep(model, strict)
  File "/home/tdurand/.local/lib/python3.6/site-packages/onnx_tf/backend.py", line 85, in onnx_model_to_tensorflow_rep
    return cls._onnx_graph_to_tensorflow_rep(model.graph, opset_import, strict)
  File "/home/tdurand/.local/lib/python3.6/site-packages/onnx_tf/backend.py", line 143, in _onnx_graph_to_tensorflow_rep
    onnx_node, tensor_dict, handlers, opset=opset, strict=strict)
  File "/home/tdurand/.local/lib/python3.6/site-packages/onnx_tf/backend.py", line 238, in _onnx_node_to_tensorflow_op
    return handler.handle(node, tensor_dict=tensor_dict, strict=strict)
  File "/home/tdurand/.local/lib/python3.6/site-packages/onnx_tf/handlers/handler.py", line 61, in handle
    return ver_handle(node, **kwargs)
  File "/home/tdurand/.local/lib/python3.6/site-packages/onnx_tf/handlers/backend/slice.py", line 126, in version_11
    return cls.version_10(node, **kwargs)
  File "/home/tdurand/.local/lib/python3.6/site-packages/onnx_tf/handlers/backend/slice.py", line 56, in version_10
    input_tensor_shape = tf.constant(input_tensor.shape.dims, ends.dtype)
  File "/home/tdurand/.local/lib/python3.6/site-packages/tensorflow/python/framework/constant_op.py", line 180, in constant_v1
    allow_broadcast=False)
  File "/home/tdurand/.local/lib/python3.6/site-packages/tensorflow/python/framework/constant_op.py", line 284, in _constant_impl
    allow_broadcast=allow_broadcast))
  File "/home/tdurand/.local/lib/python3.6/site-packages/tensorflow/python/framework/tensor_util.py", line 454, in make_tensor_proto
    raise ValueError("None values not supported.")
ValueError: None values not supported.

To Reproduce

onnx-tf convert -i yolov5s.onnx -o yolov5s.pb

ONNX model file

https://u.pcloud.link/publink/show?code=XZkXEUkZeHMBgszcW2SMXgmuxnp5rmbLyj9V

Python, ONNX, ONNX-TF, Tensorflow version

Python version: 3.6.9 (default, Apr 18 2020, 01:56:04) [GCC 8.4.0] ONNX version: 1.7.0 ONNX-TF version: 1.5.0 Tensorflow version: 1.14.0

PS: installed ONNX-TF with pip install onnx-tf

Additional context

I create the onnx weights file from https://github.com/ultralytics/yolov5

Using onnx_export.py

With the following command

export PYTHONPATH="$PWD" && python3 models/onnx_export.py --weights ./weights/yolov5s.pt

Thanks for you work !

chinhuang007 commented 4 years ago

I tried your onnx file and found a couple of issues:

  1. a slice issue, which is fixed in this merged PR, https://github.com/onnx/onnx-tensorflow/pull/543
  2. a resize issue, which is fixed in this open PR, https://github.com/onnx/onnx-tensorflow/pull/612

We are working on a release this week and both fixes will be included. If you want to try it now, please checkout PR 612 and let us know if works for you.

tdurand commented 4 years ago

ok ! great, will wait for the release then

Chilicy commented 4 years ago

@tdurand hi, I used the same method to convert yolov5 model from pytorch weights --> onnx weights --> tensorflow weights, and it worked successfully now. But it seems there're some differences on the model output (feature maps in the model predictitions). Have you encountered this problem? Or should I make some modifications on the converting code in onnx-tensorflow? @chinhuang007

hope for your help !

attacting the predictions from pytorch and tensorflow weights. 1) pytorch [ 4.47754e-01, 1.94092e-01, -1.46484e-01, ..., -8.39062e+00, -8.77344e+00, -8.06250e+00], [-7.99561e-02, 4.58496e-01, 9.13086e-01, ..., -8.55469e+00, -8.94531e+00, -8.40625e+00], [-9.25293e-02, 3.60840e-01, 1.17383e+00, ..., -8.60938e+00, -9.16406e+00, -8.60156e+00],....

2) tensorflow [ 4.45807695e-01, 1.93201512e-01, -1.47372961e-01, ..., -8.40334892e+00, -8.77904129e+00, -8.06796741e+00], [-7.97356218e-02, 4.55422223e-01, 9.14129257e-01, ..., -8.55984402e+00, -8.95124817e+00, -8.40561104e+00], [-9.17178839e-02, 3.59077632e-01, 1.17550290e+00, ..., -8.61985493e+00, -9.16726494e+00, -8.60023499e+00],...

tdurand commented 4 years ago

@Chilicy no sorry, I didn't work further on this

Chilicy commented 4 years ago

@tdurand thanks anyway. @chinhuang007 can you provide me some advices?

albertoisorna commented 4 years ago

@tdurand hi, I used the same method to convert yolov5 model from pytorch weights --> onnx weights --> tensorflow weights, and it worked successfully now. But it seems there're some differences on the model output (feature maps in the model predictitions). Have you encountered this problem? Or should I make some modifications on the converting code in onnx-tensorflow? @chinhuang007

hope for your help !

attacting the predictions from pytorch and tensorflow weights.

  1. pytorch [ 4.47754e-01, 1.94092e-01, -1.46484e-01, ..., -8.39062e+00, -8.77344e+00, -8.06250e+00], [-7.99561e-02, 4.58496e-01, 9.13086e-01, ..., -8.55469e+00, -8.94531e+00, -8.40625e+00], [-9.25293e-02, 3.60840e-01, 1.17383e+00, ..., -8.60938e+00, -9.16406e+00, -8.60156e+00],....
  2. tensorflow [ 4.45807695e-01, 1.93201512e-01, -1.47372961e-01, ..., -8.40334892e+00, -8.77904129e+00, -8.06796741e+00], [-7.97356218e-02, 4.55422223e-01, 9.14129257e-01, ..., -8.55984402e+00, -8.95124817e+00, -8.40561104e+00], [-9.17178839e-02, 3.59077632e-01, 1.17550290e+00, ..., -8.61985493e+00, -9.16726494e+00, -8.60023499e+00],...

Hello!! Could you detail a bit more the steps to do the conversion please?? @Chilicy