derenlei / Unity_Detection2AR

Localize 2D image object detection in 3D Scene with Yolo in Unity Barracuda and ARFoundation.
https://derenlei.medium.com/object-detection-with-localization-using-unity-barracuda-and-arfoundation-794b4eff02f8
MIT License
223 stars 61 forks source link

How convert darknet model to onnx? #16

Closed michalek-marcin closed 3 years ago

michalek-marcin commented 3 years ago

Hi,

Can someone give me instructions how convert my custom darknet model (yolov2-tiny) to ONNX? I know I should convert my darknet model to tensorflow, then to onnx, but I struggle with this for several days.

What I've tried: Convert darknet model (weights + cfg) to tensorflow using darkflow -> I got .pb file (SUCCESS)

Then I wanted to convert pb file to onnx:

  1. Tried to convert pb file to onnx using mmconvert: mmconvert -sf tensorflow -iw path_to_my_pb_file -df onnx -om yolotfonnx --inNodeName input --dstNodeName output error: TypeError: can only concatenate list (not "NoneType") to list
  2. Tried to convert using tf2onnx: python3 -m tf2onnx.convert --graphdef path_to_my_pb_file --output yolov2-tiny.onnx --inputs input:0 --outputs output:0 error: AssertionError: output is not in graph

this also doesn't work: python3 -m tf2onnx.convert --saved-model path_to_my_pb_file --output yolov2-tiny.onnx error:

RuntimeError: MetaGraphDef associated with tags 'serve' could not be found in SavedModel. To inspect available tag-sets in the SavedModel, please use the SavedModel CLI: `saved_model_cli`
available_tags: []
  1. Tried to convert using onnx-tf: onnx-tf convert -i path_to_my_pb_file -o path_for_output_onnx_file error: ValidationError: The model does not have an ir_version set properly.

I think darkflow can be not compatible with yolov2-tiny. I also tried to convert original darknet yolov2 model and have same problems after conversion with darkflow.

derenlei commented 3 years ago

Hi @michalek-marcin ,

Did you freeze the tf graph to convert model parameters into constants? You may need tf weights (.ckpt, you can get it here: DW2TF) and the tf graph (.pb). Somthing like this: python3 -m tensorflow.python.tools.freeze_graph --input_graph=./checkpoint_dir/yolov2-tiny.pb --input_binary=true --output_node_names=yolov2-tiny/convolutional9/BiasAdd --input_checkpoint=./checkpoint_dir/yolov2-tiny.ckpt --output_graph=./checkpoint_dir/yolov2-tiny-freeze.pb

I then convert the frozen graph to onnx through: python3 -m tf2onnx.convert --graphdef ./yolov2-tiny-freeze.pb --output yolov2-tiny.onnx --inputs yolov2-tiny/net1:0 --outputs yolov2-tiny/convolutional9/BiasAdd:0

Make sure you use the correct graph inputs and outputs.

michalek-marcin commented 3 years ago

Hi @derenlei ,

I found different solution.

  1. Convert darknet weights and cfg to keras
  2. Convert kerasto coreml
  3. Convert coreml to onnx

Tools which I used: https://github.com/hollance/YOLO-CoreML-MPSNNGraph https://github.com/onnx/onnxmltools

I got onnx file and it looks good, but when i paste my model and change DetectorYolo2 component on scene it doesn't work: image

Here is my onnx file: https://drive.google.com/file/d/110foE_F0fDRtKByt-dFjyHb8EeVqDkOu/view?usp=sharing

My classes: emiratestower-left emiratestower-right emiratestower-back

App should detect this building: emirates

michalek-marcin commented 3 years ago

Ok, I tried your method @derenlei and I have new Onnx file, but I think I did something wrong. Maybe I wrote wrong output node name? I need to understand how read neural network graph in Netron.

What I did:

  1. https://github.com/jinyu121/DW2TF python3 main.py --cfg 'tiny/yolov2-tiny-custom.cfg' --weights 'tiny/yolov2-tiny-custom_final.weights' --output 'data/' --prefix 'yolov2-tiny/' --gpu 0
  1. Freeze graph python3 -m tensorflow.python.tools.freeze_graph --input_graph='/home/ubuntu/Downloads/DW2TF-master/data/yolov2-tiny-custom.pb' --input_binary=true --output_node_names=yolov2-tiny/convolutional9/BiasAdd --input_checkpoint='/home/ubuntu/Downloads/DW2TF-master/data/yolov2-tiny-custom.ckpt' --output_graph='/home/ubuntu/Downloads/DW2TF-master/data/yolov2-tiny-custom_freeze.pb'

  2. https://github.com/onnx/tensorflow-onnx python3 -m tf2onnx.convert --graphdef '/home/ubuntu/Documents/yolo/yolov2-tiny-custom_freeze.pb' --output yolov2-tiny.onnx --inputs yolov2-tiny/net1:0 --outputs yolov2-tiny/convolutional9/BiasAdd:0

Here are my files (cfg, weights and onnx): https://drive.google.com/drive/folders/1GXXe2ej5w8pKkM4XNPDrPArxFUTHa3YD?usp=sharing

My classes: emiratestower-left emiratestower-right emiratestower-back

BeiieB commented 3 years ago

Hi,

I tried your 'yolov2-tiny_freeze' model and I think it works well. Set the INPUT_NAME to yolov2-tiny/net1 and OUTPUT_NAME to yolov2-tiny/convolutional9/BiasAdd. Also, we use different scripts for yolov2 and v3 so make sure the Selected_detector under Camera Image is yolo 2_tiny. image

DaphiFluffi commented 3 years ago

@derenlei's first answer worked for me. Though for a noob like me it was quite difficult to figure out the parameters and tech stack versions I needed to use in order to get it working. I summarized all the steps I did in my Jupyter Notebook that I uploaded here: https://github.com/DaphiFluffi/Yolov3-To-Onnx-Unity. It converts a Darknet-trained yolov3-tiny model into a Tensorflow model, then into a frozen Tensorflow Graph and then into the Barracuda format. You can open it in Google Colab. Feel free to use it. I tried to describe everything as noob-friendly as I could. I hope it helps ✌

jjeongmin0308 commented 2 years ago

Hello, I'm having a hard time converting the darknet yolov2 model to onnx. It has been converted but does not detect anything when running Barracuda. I want to know in detail how you did it, is it possible?

https://github.com/derenlei/Unity_Detection2AR/issues/39

Here's what I tried.

DaphiFluffi commented 2 years ago

I remember that my converted model also didn't detect anything in Unity. Somebody wrote this under the repository I posted. Maybe it helps: _

For others coming here from Derenley's repo, i used @Kanyade 's answer from this issue https://github.com/derenlei/Unity_Detection2AR/issues/29 and my model is working now

_

Kanyade commented 2 years ago

Hello @jjeongmin0308 @DaphiFluffi This was the final colab snippet with which I converted the model successfully which can also be found at the end of the issue linked by Daphi:

https://gist.github.com/Kanyade/c8ee3b7eca6f6e470e418b018ea02e83

Please note that there may be redundant parts as I just copied it from a project of mine to share. Also note that the project the snippet is based on (https://github.com/jkjung-avt/tensorrt_demos) had been updated since then so it may or may not work with its latest version but you can always switch back to an earlier version.