ImFusion SDK 4.3
ModelFactory Class Reference

#include <AnatomyPlugin/include/ImFusion/AnatomyPlugin/ModelFactory.h>

Factory for managing different versions of ML model sets globally. More...

Detailed Description

Factory for managing different versions of ML model sets globally.

Such a model set is a structured collection of (e.g. per-anatomy) ML models. Can be used for customers to override default models in anatomy-specific segmentation and registration algorithms.

Example usages:

Implement modelFactoryInstance() to share a global collection of model sets across instances of a particular class:

static ModelFactory& modelFactoryInstance() {
auto initModelFactory = [](){
"ImFusion Standard",
"2024.v1",
{
{
ModelIdentifier::KIDNEY_SEGMENTATION_MODEL,
{ Keys::RightKidney::key, Keys::LeftKidney::key },
{
{ModelFactory::MLEngine::Torch, "<torchPath>"},
{ModelFactory::MLEngine::ONNX, "<onnxPath>"},
{ModelFactory::MLEngine::TensorRT, "<tensorRtPath>"},
}
}
}
);
defaultModelSet.metadata.setParam("Description", "This is a description.");
defaultModelSet.metadata.setParam("KeypointThreshold", 0.4);
auto modelFactory = std::make_unique<ModelFactory>();
modelFactory->registerModelSet(defaultModelSet, true);
return modelFactory;
};
static std::unique_ptr<ModelFactory> instance = initModelFactory();
return *instance;
}
std::shared_ptr< const ModelSet > defaultModelSet() const
Returns the default ModelSet. nullptr if no model set was registered as default yet.
Definition ModelFactory.h:180
ModelFactory()=default
Create a model factory.
T make_unique(T... args)
List of models.
Definition ModelFactory.h:118

Create a customized model set from the default model set by copying and modifying it:

if (auto defaultModelSet = MyAlgorithmBase::modelFactoryInstance().defaultModelSet())
{
// Copy the default ModelSet
// ... modify the modelSet while keeping some models from the default model set and register it.
// Note: Registering the new ModelSet without changing the name or version replaces the default ModelSet.
MyAlgorithmBase::modelFactoryInstance().registerModelSet(modelSet);
}
std::shared_ptr< const ModelSet > modelSet(const ModelSet::Name &modelSetName, const ModelSet::Version &modelSetVersion) const
Returns the ModelSet with the given ModelSet::Name and ModelSet::Version if it exists,...

Convenience function for getting the appropriate model path for the ML::Engine passed:

ModelInfo myModel = modelSet.modelIdentifierToModelInfo.at(ModelIdentifier::KIDNEY_SEGMENTATION_MODEL);
std::optional< std::string > modelPath(const ModelSet::Name &modelSetName, const ModelSet::Version &modelSetVersion, const ModelIdentifier &modelIdentifier, const MLEngineName &engineName) const
Returns true if the model set contains a model with the given identifier for the given engine.
Holds the meta information about a single model.
Definition ModelFactory.h:89
static std::optional< std::string > modelPath(const ModelInfo &modelInfo, const MLEngineName &engineName)
Depending on the available engine returns the model's path for that engine if available,...

Classes

struct  MLEngine
 Currently supported ML Engines (cf. ML::Engine) More...
 
struct  ModelInfo
 Holds the meta information about a single model. More...
 
struct  ModelSet
 List of models. More...
 

Public Types

using ModelIdentifier = std::string
 
using MLEngineName = std::string
 

Public Member Functions

 ModelFactory ()=default
 Create a model factory.
 
ModelFactoryoperator= (const ModelFactory &)=delete
 
ModelFactoryoperator= (ModelFactory &&)=default
 
 ModelFactory (ModelFactory &&)=default
 
ModelFactory clone () const
 Create a copy of the current model factory, can be used to assign a model factory (via operator=(ModelFactory&&)) even though the usual assignment operator has been deleted.
 
void registerModelSet (const ModelSet &modelSetIn, bool asNewDefault=false)
 Registers the given ModelSet in the factory.
 
std::vector< ModelSet::NamemodelSetNames () const
 Returns the names of all registered ModelSets.
 
std::vector< ModelSet::VersionmodelSetVersions (const ModelSet::Name &modelSetName) const
 Returns the versions of all registered ModelSets with the given ModelSet::Name.
 
std::shared_ptr< const ModelSetmodelSet (const ModelSet::Name &modelSetName, const ModelSet::Version &modelSetVersion) const
 Returns the ModelSet with the given ModelSet::Name and ModelSet::Version if it exists, nullptr otherwise.
 
std::shared_ptr< const ModelSetdefaultModelSet () const
 Returns the default ModelSet. nullptr if no model set was registered as default yet.
 
std::shared_ptr< const ModelSetlastRegisteredModelSet () const
 Returns the last registered ModelSet. nullptr if no model set was registered yet.
 
bool anyModelAvailable (const ModelIdentifier &modelIdentifier) const
 Returns true if any model set contains a model with the given identifier.
 
std::optional< std::stringmodelPath (const ModelSet::Name &modelSetName, const ModelSet::Version &modelSetVersion, const ModelIdentifier &modelIdentifier, const MLEngineName &engineName) const
 Returns true if the model set contains a model with the given identifier for the given engine.
 
bool hasModel (const ModelSet::Name &modelSetName, const ModelSet::Version &modelSetVersion, const ModelIdentifier &modelIdentifier, const MLEngineName &engineName) const
 Returns true if the model set contains a model with the given identifier for the given engine.
 
std::unique_ptr< ML::MachineLearningModelcreateModel (const ModelSet::Name &modelSetName, const ModelSet::Version &modelSetVersion, const ModelIdentifier &modelIdentifier, const MLEngineName &engineName, bool verbose=true) const
 Convenience function to fetch model information from the factory and directly instantiate it using the ML plugin.
 

Member Function Documentation

◆ registerModelSet()

void registerModelSet ( const ModelSet & modelSetIn,
bool asNewDefault = false )

Registers the given ModelSet in the factory.

A potentially existing ModelSet with the same ModelSet::Name and ModelSet::Version combination is overridden. To set the model set as the new default (cf. defaultModelSet()) pass true for asNewDefault.

◆ createModel()

std::unique_ptr< ML::MachineLearningModel > createModel ( const ModelSet::Name & modelSetName,
const ModelSet::Version & modelSetVersion,
const ModelIdentifier & modelIdentifier,
const MLEngineName & engineName,
bool verbose = true ) const
nodiscard

Convenience function to fetch model information from the factory and directly instantiate it using the ML plugin.

Returns nullptr if the model is not stored in the ModelFactory or the ML engine is not configured correctly.


The documentation for this class was generated from the following file:
Search Tab / S to search, Esc to close