...
This serialization/deserialization module should have following methods supporting different formats:
Import model into mxnet.
sym, params = mx.serde.import(input_file, in_format=‘onnx’, out_format=’gluon’)
...
Note: Currently gluon does not provide an easy way to import a pre-trained model. (there is a workaround using which this can be done).
Export mxnet model to specified input format.
1) mx.serde.export(sym, params, format=‘onnx’, filename_prefix=”model_name”)
...
mx.mod.Module.export_to(format=‘onnx’, filename_prefix=”model_name”)
...
This function will directly save the file called “model_name.onnx” on disk.
Implementation Implementation approaches
There are two approaches which can be taken to import/export onnx model.
...
data_iter = data_process(mnist)
sym = construct_model_def(...)
# create module for training
mod = mx.mod.Module(symbol=sym, ...)
# train model
mod.fit(data_iter, optimizer='...', optimizer_params='...', ...)
# get parameters of trained model
params = mod.get_params()
# save into different format using sym/params
# This will internally call serde package API `serde.export`
mx.mod.serdeModule.export_to(sym, params, format=‘onnx’, filename=”model.onnx”)
# OR save into different format using module directly
mx.serdemod.Module.export_to(mod, format=‘onnx’, filename=”model.onnx”)
...
The general structure of onnx import would look something like this:
# Import model
# This will internally call serde package API `serde.import`
sym, params = mxnet.mod.serdeModule.import_from(input_file, in_format=‘onnx’)
# create module for inference
mod = mx.mod.Module(symbol=sym, data_names=..., context=mx.cpu(), label_names=None)
mod.bind(for_training=False, data_shapes=..., label_shapes=None)
# set parameters
mod.set_params(arg_params=params, aux_params=None, allow_missing=True)
# forward on the provided data batch
mod.forward(Batch([data_batch]))
...