SDK ML Models

class Model

Class to abstract the management of an ML model and allowing inference on this instance, while allowing a general usage of any ML model.

Public Functions

Model(const MLParameters &ml_parameters)

Constructor.

Parameters

ml_parameters – The model parameters

virtual ~Model() = default

Destructor.

virtual void infer(const std::unordered_map<std::string, Value> &input, std::unordered_map<std::string, Value> &output) = 0

Runs the model inference.

Parameters
  • input – Map storing the input data to provide the model with

  • output – Map storing the output predicted by the model

virtual std::unordered_map<std::string, Value> get_input() = 0

Provides a structure with the appropriate architecture to feed the Model::infer This map should be acquired, filled with data, and provided to the infer method.

Returns

A map containing one or several Value to feed the model with, indexed by their key.

virtual std::unordered_map<std::string, Value> get_output() = 0

Retrieves the map of the data to be filled by the method Model::infer Getting the output from this method allows the user to provide the infer method directly with the stucture expected by the model.

Returns

A map containing the Value to be filled by the model, indexed by their key

virtual void log() const = 0

Provides log information on the current state of the model graph.

template<typename T>
inline T get_model_metadata(const std::string &name) const

Retrieves the metadata associated with the provided name.

Template Parameters

T – Type of the expected metadata

Parameters

name – The name of the metadata to retrieve

Throws

runtime – error if the requested name is not found in the metadata

Returns

The requested metadata

template<typename T>
inline T get_model_metadata(const std::string &name, const T &default_value) const

Retrieves the metadata associated with the provided name.

Template Parameters

T – Type of the expected metadata

Parameters
  • name – The name of the metadata to retrieve

  • default_value – A default value in case the parameter name is not found in the metadata

Returns

The requested metadata

const std::unordered_map<std::string, std::string> &get_model_metadata() const

Retrieves all the model metadata.

Returns

The model metadata in a map where metadatas are stored as strings and indexed by their name

int get_version() const

Retrieves the model version from the metadata.

Returns

The model version

Public Static Functions

static std::unique_ptr<Model> create_model(const std::unordered_map<std::string, std::any> &ml_parameters)

Produces a Model instance with the adequate backend depending on the MLParameters provided.

Parameters

ml_parameters – The parameters storing the model (and usage) information

Returns

The created model

struct MLParameters

Parameters for the generic Metavision::Model class.

Public Members

std::filesystem::path model_path

Path to the ML model to load.

bool use_cuda

Indicate ifyou want to use cuda support.

int gpu_id

In case of cuda support, indicates the gpu id to use.

Backend backend

Backend of the model.

struct Value

Structure to generalize the input to provide to the Model class and the output provided by the infer method of this class.

This way, the dictionnary provided to the infer method can contain either directly a Tensor or a list of values. This recursive pattern allows for a general input format. Similarly, any model output can be stored in a Value.

Public Functions

Value()

Default constructor.

Value(const Tensor &tensor)

Constructor for Tensor.

Parameters

tensor – The tensor to initialize with

Value(const std::vector<Value> &values)

Constructor for std::vector<Value>

Parameters

values – The vector to initialize with

Value(const Value &other)

Copy constructor.

Parameters

other – Instance to copy from

Value(Value &&other) noexcept

Move constructor.

Parameters

other – instance to move from

Value &operator=(const Value &other)

Copy assignment operator.

Parameters

other – Instance to copy from

Value &operator=(Value &&other) noexcept

Move assignment operator.

Parameters

other – instance to move from