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.
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.
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.
Use torch.onnx.export
.
Create and run convert.py
as follow.
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 has three type model, pb, h5(keras) and tflite.
$ pip install tf2onnx
Create and run convert.py
as follow.
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")
$ pip install keras2onnx
Create and run convert.py
as follow.
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')
$ pip install tflite2onnx
Create and run convert.py
as follow.
from tflite2onnx import convert tflite_path = './your_model.tflite' onnx_path = './your_model.onnx' convert(tflite_path, onnx_path)
$ 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
$ pip install caffe2onnx
Run command as follow.
$ python -m caffe2onnx.convert --prototxt your_model.prototxt \ --caffemodel your_model.caffemodel \ --onnx your_model.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.