Loading a Keras Model with ML.NET

Introduction

ML.NET is a powerful machine learning framework for .NET applications. While ML.NET offers extensive functionality, it sometimes becomes necessary to leverage pre-trained models created with other frameworks like Keras. This article explores the process of loading a Keras model into an ML.NET application.

Prerequisites

  • Visual Studio with ML.NET workload installed
  • Keras model (saved as an HDF5 file, .h5)
  • ONNX runtime (NuGet package: Microsoft.ML.OnnxRuntime)

Conversion to ONNX

ML.NET utilizes ONNX (Open Neural Network Exchange) format for model loading. Therefore, the Keras model must be converted to ONNX before it can be used with ML.NET.

Code Example: Converting a Keras Model to ONNX


pip install onnx
pip install onnxruntime
pip install keras2onnx
import keras2onnx
import onnx

# Load your Keras model
model = keras.models.load_model('your_keras_model.h5')

# Convert the model to ONNX format
onnx_model = keras2onnx.convert_keras(model, 'your_keras_model.onnx')

# Save the ONNX model
onnx.save(onnx_model, 'your_keras_model.onnx')

Loading and Using the ONNX Model in ML.NET

Once the model is converted to ONNX, you can load and use it within your ML.NET application.

Code Example: Loading the ONNX Model in ML.NET


using Microsoft.ML;
using Microsoft.ML.OnnxRuntime;

// Load the ONNX model
var onnxModel = new OnnxModel(Path.Combine("your_keras_model.onnx"));

// Create an MLContext
var mlContext = new MLContext();

// Create an OnnxTransformer
var onnxTransformer = mlContext.Transforms.ApplyOnnxModel(
    onnxModel,
    new[] { "Input" }, // Input node name(s)
    new[] { "Output" } // Output node name(s)
);

// Create a pipeline 
var pipeline = mlContext.Transforms.Concatenate("Features", "Input")
    .Append(onnxTransformer);

// Train (actually load) the model 
var model = pipeline.Fit(data); 

// Use the loaded model to make predictions
var prediction = model.Transform(testData);

Example: MNIST Classification

Here’s an example demonstrating the process using the MNIST dataset for handwritten digit classification:

Keras Model ONNX Conversion ML.NET Loading

from keras.models import Sequential
from keras.layers import Dense, Flatten
from keras.datasets import mnist
import keras

# Load the MNIST dataset
(x_train, y_train), (x_test, y_test) = mnist.load_data()

# Preprocess the data
x_train = x_train.astype('float32') / 255.0
x_test = x_test.astype('float32') / 255.0
y_train = keras.utils.to_categorical(y_train, num_classes=10)
y_test = keras.utils.to_categorical(y_test, num_classes=10)

# Define the model
model = Sequential()
model.add(Flatten(input_shape=(28, 28)))
model.add(Dense(128, activation='relu'))
model.add(Dense(10, activation='softmax'))

# Compile and train the model
model.compile(loss='categorical_crossentropy', optimizer='adam', metrics=['accuracy'])
model.fit(x_train, y_train, epochs=5, batch_size=32)

# Save the model
model.save('mnist_model.h5')


pip install onnx
pip install onnxruntime
pip install keras2onnx
import keras2onnx
import onnx

model = keras.models.load_model('mnist_model.h5')
onnx_model = keras2onnx.convert_keras(model, 'mnist_model.onnx')
onnx.save(onnx_model, 'mnist_model.onnx')


using Microsoft.ML;
using Microsoft.ML.OnnxRuntime;

var onnxModel = new OnnxModel(Path.Combine("mnist_model.onnx"));

var mlContext = new MLContext();

var onnxTransformer = mlContext.Transforms.ApplyOnnxModel(
    onnxModel,
    new[] { "input_1" },
    new[] { "output_1" }
);

var pipeline = mlContext.Transforms.Concatenate("Features", "input_1")
    .Append(onnxTransformer);

var model = pipeline.Fit(data);

// Predict using loaded model
var prediction = model.Transform(testData); 

Conclusion

Loading pre-trained Keras models into ML.NET applications is achievable through the ONNX conversion process. This enables the integration of external model expertise while harnessing the power of ML.NET for .NET development.

Leave a Reply

Your email address will not be published. Required fields are marked *