monai.deploy.core.models.Model¶
- class monai.deploy.core.models.Model(path, name='')[source]¶
Bases:
object
Represents a model or a model repository.
This encapsulates model’s name and path.
If this presents a model repository, repository’s name and path are accessed via ‘name’ and ‘path’ attributes.
If this presents a model, the model’s name and path are accessed via ‘name’ and ‘path’ attributes.
If the model’s path is not specified(Model(“”)), the model is considered as a null model and bool(Model(“”)) == False.
All models that this class represents can be retrieved by using items() method and a model with specific name can be retrieved by get() method with a model name argument (If only one model is available, you can skip specifying the model name).
Loaded model object can be accessed via ‘predictor’ attribute and the predictor can be called using __call__ method.
In the Operator class, A model is accessible via context.models attribute inside compute method. Some subclasses (such as TorchModel) loads model file when predictor attribute is accessed so you can call(__call__) the model directly.
>>> class MyOperator(Operator): >>> def compute(self, op_input: InputContext, op_output: OutputContext, context: ExecutionContext): >>> model = context.models.get() >>> result = model(op_input.get().asnumpy())
If you want to load a model file manually, please set ‘predictor’ attribute to a loaded model object.
>>> class MyOperator(Operator): >>> def compute(self, op_input: InputContext, op_output: OutputContext, context: ExecutionContext): >>> import torch >>> model = context.models.get() >>> model.predictor = torch.jit.load(model.path, map_location="cpu").eval() >>> result = model(op_input.get().asnumpy())
Supported model types can be registered using static ‘register’ method.
Constructor of a model.
If name is not provided, the model name is taken from the path. _predicator is set to None and it is expected to be set by the child class when needed. _items is set to an dictionary having itself ({self.name: self}) and it is expected to be cleared by the child class if the path presents a model repository.
- Parameters
path (str) – A path to a model.
name (str) – A name of the model.
Methods
__init__
(path[, name])Constructor of a model.
accept
(path)Check if the path is a type of this model class.
Return a name of the model class.
get
([name])Return a model object by name.
Return a list of models in the repository.
items
()Return an ItemsView of models that this Model instance has.
register
(cls_list)Register a list of model classes.
Return a list of registered model classes.
Attributes
model_type
Return a name of the model.
Return a path to the model.
Return a predictor of the model.
- __init__(path, name='')[source]¶
Constructor of a model.
If name is not provided, the model name is taken from the path. _predicator is set to None and it is expected to be set by the child class when needed. _items is set to an dictionary having itself ({self.name: self}) and it is expected to be cleared by the child class if the path presents a model repository.
- Parameters
path (str) – A path to a model.
name (str) – A name of the model.
- classmethod accept(path)[source]¶
Check if the path is a type of this model class.
- Parameters
path (str) – A path to a model.
- Return type
Tuple
[bool
,str
]- Returns
(True, <model_type>) if the path is a type of this model class, (False, “”) otherwise.
- get(name='')[source]¶
Return a model object by name.
If there is only one model in the repository or the model path, model object can be returned without specifying name.
If there are more than one models in the repository, the model object can be returned by name whose name matches the provided name.
- Parameters
name (str) – A name of the model.
- Return type
- Returns
A model object is returned, matching the provided name if given.
- get_model_list()[source]¶
Return a list of models in the repository.
If this model represents a model repository, then a list of model objects (name and path) is returned. Otherwise, a single model object list is returned.
- Return type
List
[Dict
[str
,str
]]- Returns
A list of models (name, path dictionary) in the repository.
- items()[source]¶
Return an ItemsView of models that this Model instance has.
If this model represents a model repository, then an ItemsView of submodel objects is returned. Otherwise, an ItemsView of a single model object (self) is returned.
- Returns
<model name>: <model object>.
- Return type
An ItemView of models
- property name¶
Return a name of the model.
- property path¶
Return a path to the model.
- property predictor¶
Return a predictor of the model.
- Returns
A predictor of the model.