Home > Net >  how to convert an .h5 file into ONNX
how to convert an .h5 file into ONNX

Time:06-28

I am working on a real time object detection project, I have trained the data and saved the model into .h5 file and then I have red in an article that to load that file to detect object in opecCV you need to convert it in onnx format , but when ever am installing it either using pip or Conda its not importing, and when I downgraded tensor to 2.1.0 it worked but this time my .h5 file is showing error due to older version on TensorFlow.

currently am using tensorflow=2.9 python = 3.8

can anyone please help me out with this, or any other suggestions to make this work.

to summarise, I want to use opencv for real time object detection

CodePudding user response:

You should write, as first thing, which model you're using. For example, with Yolov5 there is a custom function to convert the model to .ONNX format

CodePudding user response:

Install deps:

numpy
tensorflow==1.13.1
keras==2.1.6
pillow
keras2onnx==1.7.0
onnxruntime
h5py==2.10.0

Run this script for your install of TensorFlow v2.9:

from tensorflow.python.keras import backend as K
from tensorflow.python.keras.models import load_model
import onnx
import os
os.environ['TF_KERAS'] = '1'
import keras2onnx

onnx_model_name = 'my-model-resnet75.onnx'

model = load_model('model-resnet75-final.h5')
onnx_model = keras2onnx.convert_keras(model, model.name)
onnx.save_model(onnx_model, onnx_model_name)
  • Related