Skip to main content

MlModel

Namespace: MLLIB  ·  Library: Machine Learning Library

#include <ml/ml_model.h>

class MLLIB::MlModel

Backend-agnostic inference interface: load, predict, save plus model/task descriptors.

src/examples/ex_ml/main.cpp
std::unique_ptr<MlModel> model = std::make_unique<MlOnnxModel>();
const bool loaded = model->load(MNE_EX_ML_DATA_DIR "/affine_softmax.onnx");
const MlTensor probabilities = model->predict(batch); // shape {2, 2}, one row per sample
const QString classes = static_cast<MlOnnxModel&>(*model).metadata("classes");

Inheritance​


Public Methods​

~MlModel()​

Virtual destructor.


predict(input)​

Run inference on the given input tensor.

Parameters:

  • input : const MlTensor & The input data.

Returns:


save(path)​

Serialise the model to disk.

Parameters:

  • path : const QString & File path.

Returns:

  • bool — True if successful.

load(path)​

Load a model from disk.

Parameters:

  • path : const QString & File path.

Returns:

  • bool — True if successful.

modelType()​

Returns:

  • QString — Human-readable model type name.

taskType()​

Returns:

  • MlTaskType — The task type this model is configured for.

Example​

Used in examples​

ex_ml​

ML library: tensors, ONNX inference through the MlModel interface, and the Python trainer.

src/examples/ex_ml/main.cpp, line 85
#ifdef MNE_USE_ONNXRUNTIME
std::unique_ptr<MlModel> model = std::make_unique<MlOnnxModel>();
const bool loaded = model->load(MNE_EX_ML_DATA_DIR "/affine_softmax.onnx");
const MlTensor probabilities = model->predict(batch); // shape {2, 2}, one row per sample
const QString classes = static_cast<MlOnnxModel&>(*model).metadata("classes");
// NumPy: softmax(x @ W + b) = [[0.6681878, 0.33181223], [0.00235232, 0.9976477]]
Matrix2f expected;
expected << 0.6681878f, 0.33181223f, 0.00235232f, 0.9976477f;
ok &= expect(loaded && model->modelType() == "onnx" && probabilities.shape() == std::vector<int64_t>({2, 2}) && (probabilities.toMatrixXf() - expected).cwiseAbs().maxCoeff() < 1e-6f && classes == "rest,task",
QString("MlOnnxModel: p(task) = %1, %2 match NumPy; classes '%3'").arg(probabilities.matrix()(0, 1)).arg(probabilities.matrix()(1, 1)).arg(classes));
#else
qInfo().noquote() << " skip MlOnnxModel (built without ONNX Runtime)";

Authors of this file​