~~tag> NPU Model ONNX PyTorch TensorFlow Paddle Caffe Darknet~~ ====== Convert Your Model to ONNX ====== {{indexmenu_n>1}} ===== Why use ONNX ===== There are many deep learning frameworks available. But these frameworks cannot be converted to each other. ONNX acts as a bridge between them. One framework can convert its model to ONNX and then convert ONNX model to the other framework model. ===== Weights Only Model and Full Model ===== There are usually two ways to save models. One is that only saves weights, and the other is that saves weights and model structures. ONNX model is a type of full model. If you want to convert a full model to ONNX, you only need to load model and then convert. But if you want to convert a weights only model, you need to load both model and model structure and then convert. ==== How to know your model ==== Open your model by netron. If get like this, model is full model. {{:products:sbc:vim4:npu:full-model.webp?200|}} If get like this without model structure, model is weights only. {{:products:sbc:vim4:npu:weights-only-model.webp?200|}} Here mainly introduct how to convert full model to ONNX. ===== PyTorch to ONNX ===== Use ''torch.onnx.export''. Create and run ''convert.py'' as follow. ```python covnert.py import torch model = torch.jit.load("your_model.pt") x = torch.randn(1, 3, 640, 640, requires_grad=True) torch.onnx.export(model, x, "model.onnx", verbose=True, input_names=["input"], output_names=["output"]) ``` ===== TensorFlow to ONNX ===== TensorFlow has three type model, pb, h5(keras) and tflite. ==== pb to ONNX ==== ```shell $ pip install tf2onnx ``` Create and run ''convert.py'' as follow. ```python convert.py import tensorflow as tf import tf2onnx import onnx input_signature = [tf.TensorSpec([1, 3, 640, 640], tf.float32, name="data")] frozen_path = 'your_model.pb' model = MobilenetV2(frozen_path) onnx_model, _ = tf2onnx.convert.from_keras(model, input_signature, opset=13) onnx.save(onnx_model, "your_model.onnx") ``` ==== Keras to ONNX ==== ```shell $ pip install keras2onnx ``` Create and run ''convert.py'' as follow. ```python convert.py import keras2onnx from keras.models import load_model model = load_model('your_model.h5') onnx_model = keras2onnx.convert_keras(model, model.name) keras2onnx.save_model(onnx_model, 'your_model.onnx') ``` ==== TFlite to ONNX ==== ```shell $ pip install tflite2onnx ``` Create and run ''convert.py'' as follow. ```python convert.py from tflite2onnx import convert tflite_path = './your_model.tflite' onnx_path = './your_model.onnx' convert(tflite_path, onnx_path) ``` ===== Paddle to ONNX ===== ```shell $ pip install paddle2onnx ``` Run command as follow. ```bash $ paddle2onnx --model_dir your_model_folder \ --model_filename your_model.pdmodel \ --params_filename your_model.pdiparams \ --save_file your_model.onnx ``` ===== Caffe to ONNX ===== ```shell $ pip install caffe2onnx ``` Run command as follow. ```bash $ python -m caffe2onnx.convert --prototxt your_model.prototxt \ --caffemodel your_model.caffemodel \ --onnx your_model.onnx ``` ===== DarkNet to ONNX ===== Now there is not a tool that convert DarkNet model to ONNX directly. In general method, convert DarkNet model to PyTorch model and then convert PyTorch model to ONNX. If you need to covert, suggest you refer this codes. [[gh>Tianxiaomo/pytorch-YOLOv4/blob/master/tool/darknet2onnx.py|darknet2onnx.py]] Caffe and DarkNet are very old platform. They have not update for a long time. If you want to convert your Caffe model or DarkNet model to nb model. Suggest you using convert tool to convert directly first.