How to save CNTK model as ONNX in C#

367 Views Asked by At

The only function I know to save a trained model is trainer.SaveCheckpoint which can save the CNTK model, but I cannot find how to save the model to ONNX format in C#

On the documentation site here https://learn.microsoft.com/en-us/cognitive-toolkit/serialization

I can find only the python method to save it as ONNX

z.save("myModel.onnx", format=C.ModelFormat.ONNX)

But this does not work in C#

2

There are 2 best solutions below

0
Ruslan Gilmutdinov On BEST ANSWER

Function object of .NET Managed library has a private method _Save with the following signature:

private void _Save(string filepath, ModelFormat format)

You can execute it via Reflection:

string cntkFilePath = "myModel.model";
string onnxFilePath = "myModel.onnx";

Function model = Function.Load(cntkFilePath, DeviceDescriptor.CPUDevice, ModelFormat.CNTKv2);
MethodInfo saveMethod = typeof(Function).GetMethod(
    "_Save",
    BindingFlags.NonPublic | BindingFlags.Instance,
    null, 
    new[] { typeof(string), typeof(ModelFormat) },
    null);

saveMethod?.Invoke(model, new object[] { onnxFilePath, ModelFormat.ONNX });
2
snowflake On

You can consider using MMdnn from microsoft to convert your CNTK model to onnx.

MMdnn is a comprehensive and cross-framework tool to convert, visualize and diagnose deep learning (DL) models. The "MM" stands for model management, and "dnn" is the acronym of deep neural network.