MxNet模型導出ONNX模型
Open Neural Network Exchange (ONNX)爲AI模型提供了一種開源的數據模型格式。它定義了一個可擴展的計算圖模型,以及內置運算符和標準數據類型的定義。它可以作爲各種AI模型之間進行轉換的媒介,例如,市面上沒有現成的Caffe模型到MxNet模型的轉換工具,我們可以藉助於ONNX,首先將Caffe轉換爲Onnx,然後再將Onnx轉換爲MxNet,更爲神奇的是,這之間的轉換過程不過丟失原有模型的精度。
在本教程中,我們將展示如何將MXNet模型保存爲ONNX格式。MXNet-ONNX操作符的覆蓋範圍和特性定期更新。請訪問 ONNX operator coverage以獲取最新信息。在本教程中,我們將學習如何使用MXNet到ONNX的模型導出工具對預先訓練的模型進行導出。
預備知識
要運行本教程,你需要安裝以下python模塊:
-
MXNet >= 1.3.0。注意,經測試使用下面的命令進行安裝MXNET可用:PIP INSTALL MXNET==1.4.0 --USER
-
onnx。注意,經測試使用如下命令進行安裝onnx可用:pip install onnx==1.2.1 --user
**注意:**MXNet-ONNX導入、導出工具遵循ONNX操作符集的第7版,該操作符集附帶ONNX v1.2.1。
import mxnet as mx
import numpy as np
from mxnet.contrib import onnx as onnx_mxnet
import logging
logging.basicConfig(level=logging.INFO)
從MXNet Model Zoo下載一個模型
我們從MXNet Model Zoo.下載預訓練的ResNet-18 ImageNet 模型。我們還將下載synset文件來匹配標籤
# Download pre-trained resnet model - json and params by running following code.
path='http://data.mxnet.io/models/imagenet/'
[mx.test_utils.download(path+'resnet/18-layers/resnet-18-0000.params'),
mx.test_utils.download(path+'resnet/18-layers/resnet-18-symbol.json'),
mx.test_utils.download(path+'synset.txt')]
現在,我們已經在磁盤上下載了ResNet-18、params和synset文件。
MXNet到ONNX導出器API
讓我們來描述MXNet的’ export_model ’ API。
help(onnx_mxnet.export_model)
Help on function export_model in module mxnet.contrib.onnx.mx2onnx.export_model:
export_model(sym, params, input_shape, input_type=<type 'numpy.float32'>, onnx_file_path=u'model.onnx', verbose=False)
Exports the MXNet model file, passed as a parameter, into ONNX model.
Accepts both symbol,parameter objects as well as json and params filepaths as input.
Operator support and coverage - https://cwiki.apache.org/confluence/display/MXNET/MXNet-ONNX+Integration
Parameters
----------
sym : str or symbol object
Path to the json file or Symbol object
params : str or symbol object
Path to the params file or params dictionary. (Including both arg_params and aux_params)
input_shape : List of tuple
Input shape of the model e.g [(1,3,224,224)]
input_type : data type
Input data type e.g. np.float32
onnx_file_path : str
Path where to save the generated onnx file
verbose : Boolean
If true will print logs of the model conversion
Returns
-------
onnx_file_path : str
Onnx file path
’ export_model ’ API可以通過以下兩種方式之一接受MXNet模型。
- MXNet sym, params對象:
- 如果我們正在訓練一個模型,這是有用的。在訓練結束時,我們只需要調用’ export_model '函數,並提供sym和params對象作爲輸入和其他屬性,以將模型保存爲ONNX格式。
- MXNet導出的json和params文件:
- 如果我們有預先訓練過的模型,並且希望將它們轉換爲ONNX格式,那麼這是非常有用的。
由於我們已經下載了預訓練的模型文件,我們將通過傳遞符號和params文件的路徑來使用’ export_model ’ API。
如何使用MXNet到ONNXA導入、導出工具PI
我們將使用下載的預訓練的模型文件(sym、params)並定義輸入變量。
# 下載的輸入符號和參數文件
sym = './resnet-18-symbol.json'
params = './resnet-18-0000.params'
# 標準Imagenet輸入- 3通道,224*224
input_shape = (1,3,224,224)
# 輸出文件的路徑
onnx_file = './mxnet_exported_resnet50.onnx'
我們已經定義了’ export_model ’ API所需的輸入參數。現在,我們準備將MXNet模型轉換爲ONNX格式
# 調用導出模型API。它返回轉換後的onnx模型的路徑
converted_model_path = onnx_mxnet.export_model(sym, params, [input_shape], np.float32, onnx_file)
這個API返回轉換後的模型的路徑,您稍後可以使用該路徑將模型導入其他框架。
檢驗ONNX模型的有效性
現在我們可以使用ONNX檢查工具來檢查轉換後的ONNX模型的有效性。該工具將通過檢查內容是否包含有效的protobuf來驗證模型:
from onnx import checker
import onnx
# Load onnx model
model_proto = onnx.load_model(converted_model_path)
# Check if converted ONNX protobuf is valid
checker.check_graph(model_proto.graph)
如果轉換後的protobuf格式不符合ONNX proto規範,檢查器將拋出錯誤,但在本例中成功通過。
該方法驗證了導出模型原buf的有效性。現在,模型可以導入到其他框架中進行推理了!