Converting a Keras-trained model to ONNX format for Image Classification: A Step-by-Step Example

Posted by

Converting Keras Trained Model to ONNX Format

Converting Keras Trained Model to ONNX Format

Keras is a popular open-source neural network library written in Python. It is capable of running on top of other popular deep learning frameworks such as TensorFlow and Theano. ONNX (Open Neural Network Exchange) is an open-source format for representing deep learning models. It allows for seamless interoperability between different deep learning frameworks. In this article, we will discuss how to convert a Keras trained model to ONNX format, with an image classification example.

Image Classification Example

For this example, let’s consider a simple image classification model trained using Keras. We will use a pre-trained model such as VGG16 and fine-tune it for a specific image classification task. Once the model is trained and ready, we can convert it to ONNX format using the following steps.

Step 1: Install Required Libraries

First, we need to install the required libraries for converting the Keras model to ONNX format. This can be done using the following Python code:

    
    pip install keras2onnx
    
    

Step 2: Convert the Model to ONNX Format

Once the required libraries are installed, we can convert the Keras trained model to ONNX format using the following code:

    
    import keras
    import keras2onnx

    model = keras.models.load_model('trained_model.h5')
    onnx_model = keras2onnx.convert_keras(model, model.name)
    keras2onnx.save_model(onnx_model, 'model.onnx')
    
    

Step 3: Verify the Conversion

Finally, we can verify the conversion by loading the ONNX model using an ONNX-compatible framework such as PyTorch or TensorFlow. We can then evaluate the model using test images to ensure that the conversion was successful.

Conclusion

Converting a Keras trained model to ONNX format allows for interoperability between different deep learning frameworks. This can be beneficial when deploying models in production environments or when collaborating with teams using different deep learning frameworks. By following the steps outlined in this article, you can easily convert your Keras trained model to ONNX format and leverage the benefits of interoperability.

0 0 votes
Article Rating
8 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
@aajas
6 months ago

15:05 is there a reason you are using python 3.7? Version 3.8 introduced pickle protocol version 5 implementing PEP 574, that I think might help with this specific error you see in reduce().

Thank you for this video series! I did not know anything about ONNX before but it's a wonderful concept— providing not only interchangeable formats but also an optimized runtime.

As always with binary data there are some elusive versioning gotchas

@HoaNguyen-dw3hm
6 months ago

I have the prediction result from loading *.ONNX file has different with result from AutoML C#. Could you please help me this case ?

@neoblackcyptron
6 months ago

Unfortunately, this implementation from the git repository only saves the weights of the MaskRCNN, and not the entire model. So if anyone wants to take the .h5 file change to ONNX to port into an edge device like a Camera it will not work.

@randomworld6787
6 months ago

Hello sir,
I would like to extract data from an image which has tabular data with colours in background.can you please provide your suggestions like which procedure I can take

@puranjitsingh1782
6 months ago

Hello, I would recommend you that you should make a series of tutorial on performing semantic segmentation using transformers, and explaining various mechanisms such as attention, self-attention, positional encoding based mechanisms etc.

@rashahaggag4910
6 months ago

dear Dr. can you make a video about siamese network?

@FredrikOLSSON-sw6sn
6 months ago

Hi, excellent topic. Look forward to next. I am interested in step after onnx, the actual deployment when acquiring microscope images and making the analyses. My missing step is from onnx to make a dll in C# which can be imported by IronPython. Any help/guide would be highly appreciated. Thanks.

@Test-vj4ee
6 months ago

¡Gracias!