Reference for ultralytics/hub/session.py
Note
This file is available at https://github.com/ultralytics/ultralytics/blob/main/ultralytics/hub/session.py. If you spot a problem please help fix it by contributing a Pull Request 🛠️. Thank you 🙏!
ultralytics.hub.session.HUBTrainingSession
HUB training session for Ultralytics HUB YOLO models. Handles model initialization, heartbeats, and checkpointing.
This class encapsulates the functionality for interacting with Ultralytics HUB during model training, including model creation, metrics tracking, and checkpoint uploading.
Attributes:
Name | Type | Description |
---|---|---|
model_id |
str
|
Identifier for the YOLO model being trained. |
model_url |
str
|
URL for the model in Ultralytics HUB. |
rate_limits |
dict
|
Rate limits for different API calls (in seconds). |
timers |
dict
|
Timers for rate limiting. |
metrics_queue |
dict
|
Queue for the model's metrics. |
metrics_upload_failed_queue |
dict
|
Queue for metrics that failed to upload. |
model |
dict
|
Model data fetched from Ultralytics HUB. |
model_file |
str
|
Path to the model file. |
train_args |
dict
|
Arguments for training the model. |
client |
HUBClient
|
Client for interacting with Ultralytics HUB. |
filename |
str
|
Filename of the model. |
Examples:
>>> session = HUBTrainingSession("https://hub.ultralytics.com/models/example-model")
>>> session.upload_metrics()
Parameters:
Name | Type | Description | Default |
---|---|---|---|
identifier
|
str
|
Model identifier used to initialize the HUB training session. It can be a URL string or a model key with specific format. |
required |
Raises:
Type | Description |
---|---|
ValueError
|
If the provided model identifier is invalid. |
ConnectionError
|
If connecting with global API key is not supported. |
ModuleNotFoundError
|
If hub-sdk package is not installed. |
Source code in ultralytics/hub/session.py
create_model
Initialize a HUB training session with the specified model arguments.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
model_args
|
dict
|
Arguments for creating the model, including batch size, epochs, image size, etc. |
required |
Returns:
Type | Description |
---|---|
None
|
If the model could not be created. |
Source code in ultralytics/hub/session.py
create_session
classmethod
Create an authenticated HUBTrainingSession or return None.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
identifier
|
str
|
Model identifier used to initialize the HUB training session. |
required |
args
|
dict
|
Arguments for creating a new model if identifier is not a HUB model URL. |
None
|
Returns:
Type | Description |
---|---|
HUBTrainingSession | None
|
An authenticated session or None if creation fails. |
Source code in ultralytics/hub/session.py
load_model
Load an existing model from Ultralytics HUB using the provided model identifier.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
model_id
|
str
|
The identifier of the model to load. |
required |
Raises:
Type | Description |
---|---|
ValueError
|
If the specified HUB model does not exist. |
Source code in ultralytics/hub/session.py
request_queue
request_queue(
request_func,
retry=3,
timeout=30,
thread=True,
verbose=True,
progress_total=None,
stream_response=None,
*args,
**kwargs
)
Attempt to execute request_func
with retries, timeout handling, optional threading, and progress tracking.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
request_func
|
callable
|
The function to execute. |
required |
retry
|
int
|
Number of retry attempts. |
3
|
timeout
|
int
|
Maximum time to wait for the request to complete. |
30
|
thread
|
bool
|
Whether to run the request in a separate thread. |
True
|
verbose
|
bool
|
Whether to log detailed messages. |
True
|
progress_total
|
int
|
Total size for progress tracking. |
None
|
stream_response
|
bool
|
Whether to stream the response. |
None
|
*args
|
Any
|
Additional positional arguments for request_func. |
()
|
**kwargs
|
Any
|
Additional keyword arguments for request_func. |
{}
|
Returns:
Type | Description |
---|---|
Response | None
|
The response object if thread=False, otherwise None. |
Source code in ultralytics/hub/session.py
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 |
|
upload_metrics
upload_model
upload_model(
epoch: int,
weights: str,
is_best: bool = False,
map: float = 0.0,
final: bool = False,
) -> None
Upload a model checkpoint to Ultralytics HUB.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
epoch
|
int
|
The current training epoch. |
required |
weights
|
str
|
Path to the model weights file. |
required |
is_best
|
bool
|
Indicates if the current model is the best one so far. |
False
|
map
|
float
|
Mean average precision of the model. |
0.0
|
final
|
bool
|
Indicates if the model is the final model after training. |
False
|