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
218 stars 61 forks source link

darknet(yolov2) to onnx in barracuda #39

Open yyyubn opened 2 years ago

yyyubn commented 2 years ago

All of this was done by colab.

This is the file we used. https://drive.google.com/drive/folders/1CyP5i498dxbd5XLoM5qNS6nRNd6eZWHc?usp=sharing

Our ultimate goal is to get onnx for use in Barracuda.

Our weights file and cfg file are these.

With this, we did a darknet to keras/keras to onnx

The version we used is as follows.

keras==2.4.3 tensorflow==2.3.0 To match the onnxopset version to 8, onnx==1.3.0

When we convert darknet to keras, !conda create -n yad2k python=3.6.0 !pip install protobuf==3.6.1

You set up your virtual environment using the code above, and made other efforts.

As a result of running darknet to keras, I was able to get an h5 file like this. !python yad2k.py off-yolov2-tiny.cfg off-yolov2-tiny.weights yolov2.h5


2022-08-25 15:05:29.819139: I tensorflow/stream_executor/platform/default/dso_loader.cc:48] Successfully opened dynamic library libcudart.so.10.1 Loading weights. Weights Header: [ 0 2 0 26316800] Parsing Darknet config. Creating Keras model. Parsing section net_0 Parsing section convolutional_0 conv2d bn leaky (3, 3, 3, 16) 2022-08-25 15:05:31.036783: I tensorflow/stream_executor/platform/default/dso_loader.cc:48] Successfully opened dynamic library libcuda.so.1 2022-08-25 15:05:31.045699: E tensorflow/stream_executor/cuda/cuda_driver.cc:314] failed call to cuInit: CUDA_ERROR_NO_DEVICE: no CUDA-capable device is detected 2022-08-25 15:05:31.045752: I tensorflow/stream_executor/cuda/cuda_diagnostics.cc:156] kernel driver does not appear to be running on this host (065e3bdef5e0): /proc/driver/nvidia/version does not exist 2022-08-25 15:05:31.046078: I tensorflow/core/platform/cpu_feature_guard.cc:142] This TensorFlow binary is optimized with oneAPI Deep Neural Network Library (oneDNN)to use the following CPU instructions in performance-critical operations: AVX2 FMA To enable them in other operations, rebuild TensorFlow with the appropriate compiler flags. 2022-08-25 15:05:31.053293: I tensorflow/core/platform/profile_utils/cpu_utils.cc:104] CPU Frequency: 2199995000 Hz 2022-08-25 15:05:31.053580: I tensorflow/compiler/xla/service/service.cc:168] XLA service 0x564118db8840 initialized for platform Host (this does not guarantee that XLA will be used). Devices: 2022-08-25 15:05:31.053621: I tensorflow/compiler/xla/service/service.cc:176] StreamExecutor device (0): Host, Default Version Parsing section maxpool_0 Parsing section convolutional_1 conv2d bn leaky (3, 3, 16, 32) Parsing section maxpool_1 Parsing section convolutional_2 conv2d bn leaky (3, 3, 32, 64) Parsing section maxpool_2 Parsing section convolutional_3 conv2d bn leaky (3, 3, 64, 128) Parsing section maxpool_3 Parsing section convolutional_4 conv2d bn leaky (3, 3, 128, 256) Parsing section maxpool_4 Parsing section convolutional_5 conv2d bn leaky (3, 3, 256, 512) Parsing section maxpool_5 Parsing section convolutional_6 conv2d bn leaky (3, 3, 512, 1024) Parsing section convolutional_7 conv2d bn leaky (3, 3, 1024, 512) Parsing section convolutional_8 conv2d linear (1, 1, 512, 425) Parsing section region_0 Model: "functional_1"

Layer (type) Output Shape Param #

input_1 (InputLayer) [(None, 416, 416, 3)] 0

conv2d (Conv2D) (None, 416, 416, 16) 432

batch_normalization (BatchNo (None, 416, 416, 16) 64

leaky_re_lu (LeakyReLU) (None, 416, 416, 16) 0

max_pooling2d (MaxPooling2D) (None, 208, 208, 16) 0

conv2d_1 (Conv2D) (None, 208, 208, 32) 4608

batch_normalization_1 (Batch (None, 208, 208, 32) 128

leaky_re_lu_1 (LeakyReLU) (None, 208, 208, 32) 0

max_pooling2d_1 (MaxPooling2 (None, 104, 104, 32) 0

conv2d_2 (Conv2D) (None, 104, 104, 64) 18432

batch_normalization_2 (Batch (None, 104, 104, 64) 256

leaky_re_lu_2 (LeakyReLU) (None, 104, 104, 64) 0

max_pooling2d_2 (MaxPooling2 (None, 52, 52, 64) 0

conv2d_3 (Conv2D) (None, 52, 52, 128) 73728

batch_normalization_3 (Batch (None, 52, 52, 128) 512

leaky_re_lu_3 (LeakyReLU) (None, 52, 52, 128) 0

max_pooling2d_3 (MaxPooling2 (None, 26, 26, 128) 0

conv2d_4 (Conv2D) (None, 26, 26, 256) 294912

batch_normalization_4 (Batch (None, 26, 26, 256) 1024

leaky_re_lu_4 (LeakyReLU) (None, 26, 26, 256) 0

max_pooling2d_4 (MaxPooling2 (None, 13, 13, 256) 0

conv2d_5 (Conv2D) (None, 13, 13, 512) 1179648

batch_normalization_5 (Batch (None, 13, 13, 512) 2048

leaky_re_lu_5 (LeakyReLU) (None, 13, 13, 512) 0

max_pooling2d_5 (MaxPooling2 (None, 13, 13, 512) 0

conv2d_6 (Conv2D) (None, 13, 13, 1024) 4718592

batch_normalization_6 (Batch (None, 13, 13, 1024) 4096

leaky_re_lu_6 (LeakyReLU) (None, 13, 13, 1024) 0

conv2d_7 (Conv2D) (None, 13, 13, 512) 4718592

batch_normalization_7 (Batch (None, 13, 13, 512) 2048

leaky_re_lu_7 (LeakyReLU) (None, 13, 13, 512) 0

conv2d_8 (Conv2D) (None, 13, 13, 425) 218025

Total params: 11,237,145 Trainable params: 11,232,057 Non-trainable params: 5,088

None Saved Keras model to yolov2.h5 Read 11237145 of 11237146.0 from Darknet weights. Warning: 1.0 unused weights


!pip install keras2onnx

As a result of the installation of this method and the execution of Keras to onnx, I was able to get an onnx file like this. from tensorflow.python.keras import backend as K from tensorflow.python.keras.models import load_model import onnx import keras2onnx

onnx_model_name = 'yolov2-tiny_825.onnx'

model = load_model('yolov2-tiny_825.h5') onnx_model = keras2onnx.convert_keras(model, model.name) onnx.save_model(onnx_model, onnx_model_name)


WARNING:tensorflow:No training configuration found in the save file, so the model was not compiled. Compile it manually. tf executing eager_mode: True tf.keras model eager_mode: False /usr/local/lib/python3.7/site-packages/keras2onnx/ke2onnx/batch_norm.py:45: RuntimeWarning: invalid value encountered in sqrt gamma = params[0] / np.sqrt(params[3] + op.epsilon) /usr/local/lib/python3.7/site-packages/keras2onnx/ke2onnx/batch_norm.py:46: RuntimeWarning: invalid value encountered in sqrt beta = params[1] - params[0] * params[2] / np.sqrt(params[3] + op.epsilon) The ONNX operator number change on the optimization: 96 -> 33


After inserting the onnx file into the barracuda in unity, image image image

Put this txt file in Conversion of the number of classes / There is an error that is not recognized properly when setting yolov2-tiny in camera image.

I want to know why h5 and onnx files are not recognized when they are obtained normally. also, Errors do not appear.