Mario
Mario

Reputation: 14780

How to save CNTK model as ONNX in C#

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#

Upvotes: 1

Views: 426

Answers (2)

Ruslan Gilmutdinov
Ruslan Gilmutdinov

Reputation: 1417

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 });

Upvotes: 2

snowflake
snowflake

Reputation: 951

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.

Upvotes: 1

Related Questions