ImFusion C++ SDK 4.5.0
ImFusion::ML::ModelManager Class Reference

#include <ImFusion/ML/ModelManager.h>

The ModelManager is used for centralized management of the ML models available in ImFusion, including features such as downloading and generating models for algorithms. More...

Detailed Description

The ModelManager is used for centralized management of the ML models available in ImFusion, including features such as downloading and generating models for algorithms.

It is a singleton that exists in the MLPlugin, and any plugins that use the MLPlugin can access it. New plugins can call addPlugin() during the initialization process.

The ModelManager requires a readable and writable directory to store all models. It can retrieve JSON documents from the ImFusion remote server and list all models available for download for the added plugins. At the same time, ModelManager also maintains a local JSON document that lists all locally stored models. The local JSON is a subset of the remote one.

Note
It should be noted that sometimes, even if the model files exist locally, they may not be listed in the local JSON file, and the user will need to download them again.
This is because it cannot be guaranteed that the local files have not been modified.

Users can search for the required model files in the ModelManager and use the create function to obtain the model.

Public Member Functions

void setModelDirectory (const std::optional< Filesystem::Directory > &modelsDirectory)
 Sets the filesystem directory used to cache and enumerate local ML models.
Filesystem::Directory getModelDirectory () const
void addPlugin (const std::string &plugin)
void removePlugin (const std::string &plugin)
std::vector< ModelInfogetLocalModels ()
 Lists all cached models that are contained in the given directory.
std::vector< ModelInfogetRemoteModels ()
 Lists all models that are available on the server.
bool downloadModel (const ModelInfo &modelInfo, std::string *errorMessage=nullptr, const std::function< void(int filePart, std::uint64_t downloadNow, std::uint64_t downloadTotal)> &onDownloadProgress={}, const std::function< bool()> &shouldCancel={})
 Downloads a model from the server and caches it in the manager's model directory.
std::vector< ModelInfofindLocalModels (const std::function< bool(const ModelInfo &)> &predicate)
 Helper function to get a filtered collection of local ModelInfo objects.
std::unique_ptr< ML::MachineLearningModelcreateModel (const ModelInfo &model)
 Creates a MachineLearningModel from the cached config and weight files for model.

Static Public Member Functions

static std::shared_ptr< ModelManager > getModelManager ()

Member Function Documentation

◆ setModelDirectory()

void ImFusion::ML::ModelManager::setModelDirectory ( const std::optional< Filesystem::Directory > & modelsDirectory)

Sets the filesystem directory used to cache and enumerate local ML models.

When modelsDirectory contains a path, any missing directories are created recursively (same policy as downloads). The path must not already exist as a non-directory file. After creation or if it already exists, the directory must be writable. If creation fails or the directory is not usable, a warning is logged and the previous directory is kept unchanged. When modelsDirectory is empty (std::nullopt), the current directory is left unchanged.

◆ downloadModel()

bool ImFusion::ML::ModelManager::downloadModel ( const ModelInfo & modelInfo,
std::string * errorMessage = nullptr,
const std::function< void(int filePart, std::uint64_t downloadNow, std::uint64_t downloadTotal)> & onDownloadProgress = {},
const std::function< bool()> & shouldCancel = {} )

Downloads a model from the server and caches it in the manager's model directory.

The model directory is created if missing (same policy as setModelDirectory). It must be writable; otherwise the download fails and false is returned. Downloads both the model config file and the model binary file. Both files are named according to their filenames in modelInfo. Any existing files with those names will be overwritten!

Parameters
errorMessageIf not nullptr, filled with a short English description when this function returns false.
onDownloadProgressOptional hook (no model mutex): called during each transfer with filePart 0 for weights (modelFileName) and 1 for config (configFileName). downloadNow / downloadTotal are from libcurl progress (bytes); downloadTotal is 0 when unknown.
shouldCancelOptional: return true to abort the current HTTP transfer (e.g. user canceled the progress dialog).

◆ findLocalModels()

std::vector< ModelInfo > ImFusion::ML::ModelManager::findLocalModels ( const std::function< bool(const ModelInfo &)> & predicate)

Helper function to get a filtered collection of local ModelInfo objects.

example auto models = findLocalModels([](const ModelInfo& m) { return m.modelName.find("monai-seg-tool") != std::string::npos;});

◆ createModel()

std::unique_ptr< ML::MachineLearningModel > ImFusion::ML::ModelManager::createModel ( const ModelInfo & model)

Creates a MachineLearningModel from the cached config and weight files for model.

Resolves paths under the current model directory as <modelDir>/<plugin>/<setName>/<configFileName> and the same for modelFileName. Inference settings such as PredictionOutput must be defined in that YAML configuration (see MachineLearningModel::create).

Returns
A ready-to-use model, or nullptr when creation fails. Failure cases include:
  • The model directory does not exist or is not writable (error logged during lookup).
  • Missing config or weights on disk (informational message during lookup; error logged when creation aborts).
  • The YAML or engine initialization fails inside MachineLearningModel::create (errors are logged there).

The documentation for this class was generated from the following file:
  • ImFusion/ML/ModelManager.h
Search Tab / S to search, Esc to close