Search code examples
c#cntkonnx

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#


Solution

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