Khadas Docs

Amazing Khadas, always amazes you!

User Tools

Site Tools


Sidebar

products:sbc:vim3:npu:convert-onnx

Convert Your Model to ONNX

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.

If get like this without model structure, model is weights only.

Here mainly introduct how to convert full model to ONNX.

PyTorch to ONNX

Use torch.onnx.export.

Create and run convert.py as follow.

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

$ pip install tf2onnx

Create and run convert.py as follow.

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

$ pip install keras2onnx

Create and run convert.py as follow.

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

$ pip install tflite2onnx

Create and run convert.py as follow.

convert.py
from tflite2onnx import convert
 
tflite_path = './your_model.tflite'
onnx_path = './your_model.onnx'
 
convert(tflite_path, onnx_path)

Paddle to ONNX

$ pip install paddle2onnx

Run command as follow.

$ paddle2onnx --model_dir your_model_folder \
              --model_filename your_model.pdmodel \
              --params_filename your_model.pdiparams \
              --save_file your_model.onnx

Caffe to ONNX

$ pip install caffe2onnx

Run command as follow.

$ 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. 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.

Last modified: 2025/01/02 04:45 by louis