![]() |
ImFusion C++ SDK 4.5.0
|
#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...
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.
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< ModelInfo > | getLocalModels () |
| Lists all cached models that are contained in the given directory. | |
| std::vector< ModelInfo > | getRemoteModels () |
| 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< ModelInfo > | findLocalModels (const std::function< bool(const ModelInfo &)> &predicate) |
| Helper function to get a filtered collection of local ModelInfo objects. | |
| std::unique_ptr< ML::MachineLearningModel > | createModel (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 () |
| 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.
| 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!
| errorMessage | If not nullptr, filled with a short English description when this function returns false. |
| onDownloadProgress | Optional 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. |
| shouldCancel | Optional: return true to abort the current HTTP transfer (e.g. user canceled the progress dialog). |
| std::vector< ModelInfo > ImFusion::ML::ModelManager::findLocalModels | ( | const std::function< bool(const ModelInfo &)> & | predicate | ) |
| 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).
nullptr when creation fails. Failure cases include: