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.
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. |
model |
dict
|
Model data fetched from Ultralytics HUB. |
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
_get_failure_message
Generate a retry message based on the response status code.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
response
|
Response
|
The HTTP response object. |
required |
retry
|
int
|
The number of retry attempts allowed. |
required |
timeout
|
int
|
The maximum timeout duration. |
required |
Returns:
Type | Description |
---|---|
str
|
The retry message. |
Source code in ultralytics/hub/session.py
_iterate_content
staticmethod
Process the streamed HTTP response data.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
response
|
Response
|
The response object from the file download request. |
required |
Returns:
Type | Description |
---|---|
None
|
None |
Source code in ultralytics/hub/session.py
_parse_identifier
staticmethod
Parses the given identifier to determine the type of identifier and extract relevant components.
The method supports different identifier formats
- A HUB model URL https://hub.ultralytics.com/models/MODEL
- A HUB model URL with API Key https://hub.ultralytics.com/models/MODEL?api_key=APIKEY
- A local filename that ends with '.pt' or '.yaml'
Parameters:
Name | Type | Description | Default |
---|---|---|---|
identifier
|
str
|
The identifier string to be parsed. |
required |
Returns:
Type | Description |
---|---|
tuple
|
A tuple containing the API key, model ID, and filename as applicable. |
Raises:
Type | Description |
---|---|
HUBModelError
|
If the identifier format is not recognized. |
Source code in ultralytics/hub/session.py
_set_train_args
Initializes training arguments and creates a model entry on the Ultralytics HUB.
This method sets up training arguments based on the model's state and updates them with any additional arguments provided. It handles different states of the model, such as whether it's resumable, pretrained, or requires specific file setup.
Raises:
Type | Description |
---|---|
ValueError
|
If the model is already trained, if required dataset information is missing, or if there are issues with the provided training arguments. |
Source code in ultralytics/hub/session.py
_should_retry
staticmethod
Determines if a request should be retried based on the HTTP status code.
Source code in ultralytics/hub/session.py
_show_upload_progress
staticmethod
Display a progress bar to track the upload progress of a file download.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
content_length
|
int
|
The total size of the content to be downloaded in bytes. |
required |
response
|
Response
|
The response object from the file download request. |
required |
Returns:
Type | Description |
---|---|
None
|
None |
Source code in ultralytics/hub/session.py
create_model
Initializes a HUB training session with the specified model identifier.
Source code in ultralytics/hub/session.py
create_session
classmethod
Class method to create an authenticated HUBTrainingSession or return None.
Source code in ultralytics/hub/session.py
load_model
Loads an existing model from Ultralytics HUB using the provided model identifier.
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
)
Attempts to execute request_func
with retries, timeout handling, optional threading, and progress.
Source code in ultralytics/hub/session.py
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
|