ModelAPI¶
-
class ModelAPI(api=
None, task_id=None, url=None)[source]¶ Bases:
objectClient for interacting with a deployed model (load settings/metadata, run inference).
The instance can be created either from a Supervisely Task ID (to resolve the deployment URL automatically) or from a direct deployment URL.
- Parameters:
- Raises:
AssertionError – If both
task_idandurlare provided, or neither is provided.ValueError – If
task_idis provided but the task is not found.
- Task based usage:
import os from dotenv import load_dotenv import supervisely as sly from supervisely.nn.model.model_api import ModelAPI # Load secrets and create API object from .env file (recommended) # Learn more here: https://developer.supervisely.com/getting-started/basics-of-authentication if sly.is_development(): load_dotenv(os.path.expanduser("~/supervisely.env")) api = sly.Api.from_env() model = ModelAPI(api=api, task_id=12345) meta = model.get_model_meta() classes = model.get_classes() predictions = model.predict(image_id=100500, classes=classes)- Direct URL usage:
from supervisely.nn.model.model_api import ModelAPI model = ModelAPI(url='https://app.supervisely.com/net/<sessionToken>') predictions = model.predict(input='/path/to/image.jpg')
Methods
Freeze the model to free up resources.
Convenience wrapper to return output class names from
get_model_meta().Return deployment info for the current model.
Return output
ProjectMetafor the deployed model.Return custom inference settings for the deployed model.
Return tracking settings for the deployed model.
Check whether the deployment is ready.
Return a list of training experiments in Supervisely.
Return a list of pretrained model infos with full information about each model.
Return a list of pretrained model names available for deployment.
Load a model into the deployment.
Run inference and return predictions as a list.
Create a prediction session (lazy iterator).
Stop the deployment task (task-based mode) or request shutdown (URL-based mode).
Return deployment status JSON.
- freeze_model()[source]¶
Freeze the model to free up resources.
- Returns:
Backend response.
- Return type:
- get_classes()[source]¶
Convenience wrapper to return output class names from
get_model_meta().- Returns:
List of class names.
- Return type:
List[str]
- get_info()[source]¶
Return deployment info for the current model.
For task-based mode this calls internal deploy API, for URL-based mode it calls
get_deploy_infoendpoint of the deployment.- Returns:
Deployment info (raw JSON returned by the backend).
- Return type:
- get_model_meta()[source]¶
Return output
ProjectMetafor the deployed model.The meta typically includes object classes and tags that the model predicts.
- Returns:
Model output meta.
- Return type:
- get_settings()[source]¶
Return custom inference settings for the deployed model.
- Returns:
Settings dict.
- Return type:
- get_tracking_settings()[source]¶
Return tracking settings for the deployed model.
Currently returns settings for the
botsorttracker.- Returns:
Tracking settings dict.
- Return type:
- list_experiments()[source]¶
Return a list of training experiments in Supervisely.
Note
This method is not implemented.
- Returns:
Experiments list.
- Return type:
List[
ExperimentInfo]- Raises:
NotImplementedError – Always.
- list_pretrained_model_infos()[source]¶
Return a list of pretrained model infos with full information about each model.
- Returns:
List of model info dicts.
- Return type:
List[dict]
- list_pretrained_models()[source]¶
Return a list of pretrained model names available for deployment.
- Returns:
Pretrained model names.
- Return type:
List[str]
-
load(model, device=
None, runtime=None)[source]¶ Load a model into the deployment.
Behavior depends on the connection mode:
URL-based Mode (
task_idis None): ifmodelpoints to an existing local file, it is treated as a custom checkpoint; otherwise it is treated as a pretrained model name.Task-based Mode: if
modelstarts with/it is treated as a path to a custom checkpoint in team files; otherwise it is treated as a pretrained model name.
- Parameters:
- Returns:
Backend response (URL-based mode) or None (task-based mode).
- Return type:
dict or None
- Raises:
ValueError – If pretrained model name is not found (URL-based mode).
-
predict(input=
None, image_id=None, video_id=None, dataset_id=None, project_id=None, batch_size=None, conf=None, img_size=None, classes=None, upload_mode=None, recursive=False, tracking=None, tracking_config=None, **kwargs)[source]¶ Run inference and return predictions as a list.
This is a convenience wrapper over
predict_detached()that consumes the session and returnslist(session).Parameters are forwarded to
PredictionSession.- Returns:
Predictions list.
- Return type:
List[
Prediction]
-
predict_detached(input=
None, image_id=None, video_id=None, dataset_id=None, project_id=None, batch_size=None, conf=None, img_size=None, classes=None, upload_mode=None, recursive=False, tracking=None, tracking_config=None, **kwargs)[source]¶ Create a prediction session (lazy iterator).
Use this method when you want to iterate predictions as they are produced, or if you need direct access to
PredictionSession.Parameters are forwarded to
PredictionSession.- Returns:
Prediction session.
- Return type: