remote model
RemoteModel Module
This module provides a class to manage remote models in the Focoos ecosystem. It supports various functionalities including model training, deployment, inference, and monitoring.
Classes:
Name | Description |
---|---|
RemoteModel |
A class for interacting with remote models, managing their lifecycle, and performing inference. |
Modules:
Name | Description |
---|---|
HttpClient |
Handles HTTP requests. |
logger |
Logging utility. |
BoxAnnotator, LabelAnnotator, MaskAnnotator |
Annotation tools for visualizing detections and segmentation tasks. |
FocoosDet, FocoosDetections |
Classes for representing and managing detections. |
FocoosTask |
Enum for defining supported tasks (e.g., DETECTION, SEMSEG). |
Hyperparameters |
Structure for training configuration parameters. |
ModelMetadata |
Contains metadata for the model. |
ModelStatus |
Enum for representing the current status of the model. |
TrainInstance |
Enum for defining available training instances. |
image_loader |
Utility function for loading images. |
focoos_detections_to_supervision |
Converter for Focoos detections to supervision format. |
RemoteModel
#
Represents a remote model in the Focoos platform.
Attributes:
Name | Type | Description |
---|---|---|
model_ref |
str
|
Reference ID for the model. |
http_client |
HttpClient
|
Client for making HTTP requests. |
max_deploy_wait |
int
|
Maximum wait time for model deployment. |
metadata |
ModelMetadata
|
Metadata of the model. |
label_annotator |
LabelAnnotator
|
Annotator for adding labels to images. |
box_annotator |
BoxAnnotator
|
Annotator for drawing bounding boxes. |
mask_annotator |
MaskAnnotator
|
Annotator for drawing masks on images. |
Source code in focoos/remote_model.py
52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 |
|
__init__(model_ref, http_client)
#
Initialize the RemoteModel instance.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
model_ref |
str
|
Reference ID for the model. |
required |
http_client |
HttpClient
|
HTTP client instance for communication. |
required |
Raises:
Type | Description |
---|---|
ValueError
|
If model metadata retrieval fails. |
Source code in focoos/remote_model.py
66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 |
|
delete_model()
#
Delete the model from the system.
This method sends a request to delete the model identified by model_ref
.
If the request fails or the status code is not 204 (No Content), an error is logged
and a ValueError
is raised.
Raises:
Type | Description |
---|---|
ValueError
|
If the delete model request fails or does not return a 204 status code. |
Logs
- Error message if the request to delete the model fails, including the status code and response text.
Returns:
Name | Type | Description |
---|---|---|
None |
None
|
This method does not return any value. |
Source code in focoos/remote_model.py
450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 |
|
get_info()
#
Retrieve model metadata.
Returns:
Name | Type | Description |
---|---|---|
ModelMetadata |
ModelMetadata
|
Metadata of the model. |
Raises:
Type | Description |
---|---|
ValueError
|
If the request fails. |
Source code in focoos/remote_model.py
89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 |
|
infer(image, threshold=0.5, annotate=False)
#
Perform inference on the provided image using the remote model.
This method sends an image to the remote model for inference and retrieves the detection results. Optionally, it can annotate the image with the detection results.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
image |
Union[str, Path, bytes]
|
The image to infer on, which can be a file path, a string representing the path, or raw bytes. |
required |
threshold |
float
|
The confidence threshold for detections. Defaults to 0.5. |
0.5
|
annotate |
bool
|
Whether to annotate the image with the detection results. Defaults to False. |
False
|
Returns:
Type | Description |
---|---|
Tuple[FocoosDetections, Optional[ndarray]]
|
Tuple[FocoosDetections, Optional[np.ndarray]]:
- FocoosDetections: The detection results including class IDs, confidence scores, etc.
- Optional[np.ndarray]: The annotated image if |
Raises:
Type | Description |
---|---|
FileNotFoundError
|
If the provided image file path is invalid. |
ValueError
|
If the inference request fails. |
Source code in focoos/remote_model.py
236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 |
|
monitor_train(update_period=30)
#
Monitor the training process of the model and log its status periodically.
This method continuously checks the model's training status and logs updates based on the current state. It monitors the primary and secondary statuses of the model, and performs the following actions: - If the status is "Pending", it logs a waiting message and waits for resources. - If the status is "InProgress", it logs the current status and elapsed time, and logs the training metrics if the model is actively training. - If the status is "Completed", it logs the final metrics and exits. - If the training fails, is stopped, or any unexpected status occurs, it logs the status and exits.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
update_period |
int
|
The time (in seconds) to wait between status checks. Default is 30 seconds. |
30
|
Returns:
Name | Type | Description |
---|---|---|
None |
None
|
This method does not return any value but logs information about the training process. |
Logs
- The current training status, including elapsed time.
- Training metrics at regular intervals while the model is training.
Source code in focoos/remote_model.py
370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 |
|
stop_training()
#
Stop the training process of the model.
This method sends a request to stop the training of the model identified by model_ref
.
If the request fails, an error is logged and a ValueError
is raised.
Raises:
Type | Description |
---|---|
ValueError
|
If the stop training request fails. |
Logs
- Error message if the request to stop training fails, including the status code and response text.
Returns:
Name | Type | Description |
---|---|---|
None |
None
|
This method does not return any value. |
Source code in focoos/remote_model.py
427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 |
|
train(dataset_ref, hyperparameters, anyma_version='anyma-sagemaker-cu12-torch22-0111', instance_type=TrainInstance.ML_G4DN_XLARGE, volume_size=50, max_runtime_in_seconds=36000)
#
Initiate the training of a remote model on the Focoos platform.
This method sends a request to the Focoos platform to start the training process for the model
referenced by self.model_ref
. It requires a dataset reference and hyperparameters for training,
as well as optional configuration options for the instance type, volume size, and runtime.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
dataset_ref |
str
|
The reference ID of the dataset to be used for training. |
required |
hyperparameters |
Hyperparameters
|
A structure containing the hyperparameters for the training process. |
required |
anyma_version |
str
|
The version of Anyma to use for training. Defaults to "anyma-sagemaker-cu12-torch22-0111". |
'anyma-sagemaker-cu12-torch22-0111'
|
instance_type |
TrainInstance
|
The type of training instance to use. Defaults to TrainInstance.ML_G4DN_XLARGE. |
ML_G4DN_XLARGE
|
volume_size |
int
|
The size of the disk volume (in GB) for the training instance. Defaults to 50. |
50
|
max_runtime_in_seconds |
int
|
The maximum runtime for training in seconds. Defaults to 36000. |
36000
|
Returns:
Name | Type | Description |
---|---|---|
dict |
dict | None
|
A dictionary containing the response from the training initiation request. The content depends on the Focoos platform's response. |
Raises:
Type | Description |
---|---|
ValueError
|
If the request to start training fails (e.g., due to incorrect parameters or server issues). |
Source code in focoos/remote_model.py
106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 |
|
train_logs()
#
Retrieve the training logs for the model.
This method sends a request to fetch the logs of the model's training process. If the request is successful (status code 200), it returns the logs as a list of strings. If the request fails, it logs a warning and returns an empty list.
Returns:
Type | Description |
---|---|
list[str]
|
list[str]: A list of training logs as strings. |
Raises:
Type | Description |
---|---|
None
|
Returns an empty list if the request fails. |
Source code in focoos/remote_model.py
172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 |
|
train_metrics(period=60)
#
Retrieve training metrics for the model over a specified period.
This method fetches the training metrics for the remote model, including aggregated values, such as average performance metrics over the given period.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
period |
int
|
The period (in seconds) for which to fetch the metrics. Defaults to 60. |
60
|
Returns:
Type | Description |
---|---|
dict | None
|
Optional[dict]: A dictionary containing the training metrics if the request is successful, or None if the request fails. |
Source code in focoos/remote_model.py
298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 |
|
train_status()
#
Retrieve the current status of the model training.
Sends a request to check the training status of the model referenced by self.model_ref
.
Returns:
Name | Type | Description |
---|---|---|
dict |
dict | None
|
A dictionary containing the training status information. |
Raises:
Type | Description |
---|---|
ValueError
|
If the request to get training status fails. |
Source code in focoos/remote_model.py
152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 |
|