[PYTHON] ONNX conversion / confirmation library, application summary

TL;DR

--Library and application summary to convert to ONNX or check the contents of ONNX (will be updated at any time)

table of contents

-[1. ONNX Version and Opset](# 1-onnx version and opset) -[2. Conversion between machine learning frameworks](# 2-Conversion between machine learning frameworks) - 2.1. MMdnn - 2.2. ONNXMLTools - 2.3. keras2onnx - 2.4. WinMLTools -[3. ONNX version conversion](# 3-onnx version conversion) - 3.1. ONNX Version Converter -[4. Confirmation](# 4-Confirmation) - 4.1. Netron

Premise

――Basically, proceed to work after creating a virtual environment

Example in Anaconda


conda create -n mmdnn python=3.6

1. ONNX Version and Opset

1.1. Version

(investigating...)

1.2. Opset

(investigating...)

1.3. Reference

ONNX versioning https://github.com/onnx/onnx/blob/master/docs/Versioning.md ONNX Version Converter https://github.com/onnx/onnx/blob/master/docs/VersionConverter.md ONNX Runtime version compatibility documentation https://github.com/microsoft/onnxruntime/blob/master/docs/Versioning.md ONNX version and Windows build https://docs.microsoft.com/ja-jp/windows/ai/windows-ml/onnx-versions

2. Conversion between machine learning frameworks

2.1. MMdnn

2.1.1. Official website

https://github.com/Microsoft/MMdnn

2.1.2. Overview

MMdnn is a comprehensive, cross-framework tool for transforming, visualizing and diagnosing deep learning (DL) models. "MM" is an abbreviation for model management, and "dnn" is an acronym for deep neural network.

Reference: https://github.com/Microsoft/MMdnn

2.1.3. Corresponding machine learning framework

Reprinted from (as of July 7, 2020): https://github.com/Microsoft/MMdnn

2.1.4. Installation

pip install mmdnn

--Other modules are required according to the conversion format. If you want to convert keras format (* .karas, * .h5), keras, tensorflow, etc.

2.1.5. How to use

mmconvert -sf keras -iw <keras HDF5 file(*.h5)> -df onnx -om <Output ONNX file(*.onnx)>

Reference: https://arakan-pgm-ai.hatenablog.com/entry/2019/03/01/090000

2.1.6. Tips

-** When raise ValueError ('No model found in config file.') Is displayed in the load_model_from_hdf5 method **

It seems that it is displayed when the saved keras format is only weighting.

There is information that the workaround was improved by using model.save () instead of model.save_weights () when saving.

reference: - AssertionError · Issue #324 · microsoft/MMdnn -[About weight / model storage and loading in Keras](https://intellectual-curiosity.tokyo/2019/07/04/keras%E3%81%AB%E3%81%8A%E3%81%91% E3% 82% 8B% E9% 87% 8D% E3% 81% BF% EF% BC% 8F% E3% 83% A2% E3% 83% 87% E3% 83% AB% E3% 81% AE% E4% BF% 9D% E5% AD% 98% E3% 80% 81% E8% AA% AD% E3% 81% BF% E8% BE% BC% E3% 81% BF% E3% 81% AB% E3% 81% A4% E3% 81% 84 /)

2.2. ONNXMLTools

2.2.1. Official website

https://github.com/onnx/onnxmltools https://pypi.org/project/onnxmltools/

2.2.2. Overview

ONNXMLTools allows you to convert models from various machine learning toolkits to ONNX.

Source: https://github.com/onnx/onnxmltools

2.2.3. Compatible machine learning framework

--Keras (wrapper of keras2onnx converter) --Tensorflow (wrapper of tf2onnx converter) --scikit-learn (wrapper of skl2onnx converter)

Reprinted from (as of July 7, 2020): https://github.com/onnx/onnxmltools

2.3. keras2onnx

2.3.1. Official site

https://github.com/onnx/keras-onnx

2.3.2. Overview

The keras2onnx model converter allows you to convert Keras models to ONNX model format. Initially, the Keras converter was developed in the onnxmltools project, but the development of the Keras2onnx converter has been moved to a separate repository to support more types of Keras models and reduce the complexity of mixing multiple converters. ..

Source: https://github.com/onnx/keras-onnx

Also, [WinMLTool](# 12-winmltools) wraps the call to keras2onnx.

2.4. WinMLTools

Convert various machine learning models to ONNX and use them in Windows ML (fast inference environment for Windows) A tool to enable you.

2.4.1. Official website

https://docs.microsoft.com/ja-jp/windows/ai/windows-ml/convert-model-winmltools

2.4.2. Overview

WinMLTools allows you to convert machine learning models created by various training frameworks to ONNX. This is an extension of ONNXMLTools and TF2ONNX to convert the model to ONNX for use in Windows ML.

Source: https://docs.microsoft.com/ja-jp/windows/ai/windows-ml/convert-model-winmltools

2.4.3. Supported machine learning frameworks

Reprinted from (as of July 7, 2020): https://docs.microsoft.com/ja-jp/windows/ai/windows-ml/convert-model-winmltools

3. ONNX version conversion

3.1. ONNX Version Converter

3.1.1. Official website

https://github.com/onnx/onnx/blob/master/docs/VersionConverter.md

Tutorial showing an example of how to upgrade and downgrade an ONNX model to a new target opset https://github.com/onnx/tutorials/blob/master/tutorials/ExportModelFromPyTorchForWinML.md

3.1.2. Overview

The ONNX API provides a library for converting ONNX models between different opset versions. This allows developers and data scientists to upgrade their existing ONNX models to newer versions or downgrade their models to older versions of the ONNX specification. Version converters can be called using the C ++ or Python API. A tutorial is also provided that shows an example of how to upgrade and downgrade an ONNX model to a new target opset.

Source: https://docs.microsoft.com/ja-jp/windows/ai/windows-ml/onnx-versions

4. Confirmation

4.1. Netron

4.1.1. Official website

https://github.com/lutzroeder/netron

4.1.2. Installation

Install with Winget

winget install -e --id LutzRoeder.Netron

4.1.3. Information confirmation

You can check the ONNX version from the Model Properties button on the upper left.

image.png

** Confirmation example 1 **

image.png

--ONNX file format version is v6 --Two INPUTS, three OUTPUTS

** Confirmation example 2 **

image.png

--ONNX file format version is v7 --Two INPUTS and three OUTPUTS (no difference from Confirmation Example 1)

Recommended Posts

ONNX conversion / confirmation library, application summary
LINQ library summary