monai.deploy.core.models.TorchScriptModel

class monai.deploy.core.models.TorchScriptModel(path, name='')[source]

Bases: monai.deploy.core.models.model.Model

Represents TorchScript model.

TorchScript serialization format (TorchScript model file) is created by torch.jit.save() method and the serialized model (which usually has .pt or .pth extension) is a ZIP archive containing many files. (https://github.com/pytorch/pytorch/blob/master/torch/csrc/jit/docs/serialization.md)

We consider that the model is a torchscript model if its unzipped archive contains files named ‘data.pkl’ and ‘constants.pkl’, and folders named ‘code’ and ‘data’.

When predictor property is accessed or the object is called (__call__), the model is loaded in evaluation mode from the serialized model file (if it is not loaded yet) and the model is ready to be used.

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.

class_name()

Return a name of the model class.

eval()

Set the model in evaluation model.

get([name])

Return a model object by name.

get_model_list()

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.

registered_models()

Return a list of registered model classes.

train([mode])

Set the model in training mode.

Attributes

model_type

name

Return a name of the model.

path

Return a path to the model.

predictor

Get the model’s predictor (torch.nn.Module)

__init__(path, name='')

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.

Returns

(True, <model_type>) if the path is a type of this model class, (False, “”) otherwise.

classmethod class_name()

Return a name of the model class.

eval()[source]

Set the model in evaluation model.

This is a proxy method for torch.jit.ScriptModule.eval(). See https://pytorch.org/docs/stable/generated/torch.jit.ScriptModule.html?highlight=eval#torch.jit.ScriptModule.eval

Return type

TorchScriptModel

Returns

self

get(name='')

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

Model

Returns

A model object is returned, matching the provided name if given.

get_model_list()

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()

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: torch.nn.modules.module.Module

Get the model’s predictor (torch.nn.Module)

If the predictor is not loaded, load it from the model file in evaluation mode. (https://pytorch.org/docs/stable/generated/torch.jit.ScriptModule.html?highlight=eval#torch.jit.ScriptModule.eval)

Returns

the model’s predictor

Return type

torch.nn.Module

static register(cls_list)

Register a list of model classes.

static registered_models()

Return a list of registered model classes.

train(mode=True)[source]

Set the model in training mode.

This is a proxy method for torch.jit.ScriptModule.train(). See https://pytorch.org/docs/stable/generated/torch.jit.ScriptModule.html?highlight=train#torch.jit.ScriptModule.train

Parameters

mode (bool) – whether the model is in training mode

Return type

TorchScriptModel

Returns

self