dgenerate-ultralytics-headless 8.3.134__py3-none-any.whl
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- dgenerate_ultralytics_headless-8.3.134.dist-info/METADATA +400 -0
- dgenerate_ultralytics_headless-8.3.134.dist-info/RECORD +272 -0
- dgenerate_ultralytics_headless-8.3.134.dist-info/WHEEL +5 -0
- dgenerate_ultralytics_headless-8.3.134.dist-info/entry_points.txt +3 -0
- dgenerate_ultralytics_headless-8.3.134.dist-info/licenses/LICENSE +661 -0
- dgenerate_ultralytics_headless-8.3.134.dist-info/top_level.txt +1 -0
- tests/__init__.py +22 -0
- tests/conftest.py +83 -0
- tests/test_cli.py +138 -0
- tests/test_cuda.py +215 -0
- tests/test_engine.py +131 -0
- tests/test_exports.py +236 -0
- tests/test_integrations.py +154 -0
- tests/test_python.py +694 -0
- tests/test_solutions.py +187 -0
- ultralytics/__init__.py +30 -0
- ultralytics/assets/bus.jpg +0 -0
- ultralytics/assets/zidane.jpg +0 -0
- ultralytics/cfg/__init__.py +1023 -0
- ultralytics/cfg/datasets/Argoverse.yaml +77 -0
- ultralytics/cfg/datasets/DOTAv1.5.yaml +37 -0
- ultralytics/cfg/datasets/DOTAv1.yaml +36 -0
- ultralytics/cfg/datasets/GlobalWheat2020.yaml +68 -0
- ultralytics/cfg/datasets/HomeObjects-3K.yaml +33 -0
- ultralytics/cfg/datasets/ImageNet.yaml +2025 -0
- ultralytics/cfg/datasets/Objects365.yaml +443 -0
- ultralytics/cfg/datasets/SKU-110K.yaml +58 -0
- ultralytics/cfg/datasets/VOC.yaml +106 -0
- ultralytics/cfg/datasets/VisDrone.yaml +77 -0
- ultralytics/cfg/datasets/african-wildlife.yaml +25 -0
- ultralytics/cfg/datasets/brain-tumor.yaml +23 -0
- ultralytics/cfg/datasets/carparts-seg.yaml +44 -0
- ultralytics/cfg/datasets/coco-pose.yaml +42 -0
- ultralytics/cfg/datasets/coco.yaml +118 -0
- ultralytics/cfg/datasets/coco128-seg.yaml +101 -0
- ultralytics/cfg/datasets/coco128.yaml +101 -0
- ultralytics/cfg/datasets/coco8-multispectral.yaml +104 -0
- ultralytics/cfg/datasets/coco8-pose.yaml +26 -0
- ultralytics/cfg/datasets/coco8-seg.yaml +101 -0
- ultralytics/cfg/datasets/coco8.yaml +101 -0
- ultralytics/cfg/datasets/crack-seg.yaml +22 -0
- ultralytics/cfg/datasets/dog-pose.yaml +24 -0
- ultralytics/cfg/datasets/dota8-multispectral.yaml +38 -0
- ultralytics/cfg/datasets/dota8.yaml +35 -0
- ultralytics/cfg/datasets/hand-keypoints.yaml +26 -0
- ultralytics/cfg/datasets/lvis.yaml +1240 -0
- ultralytics/cfg/datasets/medical-pills.yaml +22 -0
- ultralytics/cfg/datasets/open-images-v7.yaml +666 -0
- ultralytics/cfg/datasets/package-seg.yaml +22 -0
- ultralytics/cfg/datasets/signature.yaml +21 -0
- ultralytics/cfg/datasets/tiger-pose.yaml +25 -0
- ultralytics/cfg/datasets/xView.yaml +155 -0
- ultralytics/cfg/default.yaml +127 -0
- ultralytics/cfg/models/11/yolo11-cls-resnet18.yaml +17 -0
- ultralytics/cfg/models/11/yolo11-cls.yaml +33 -0
- ultralytics/cfg/models/11/yolo11-obb.yaml +50 -0
- ultralytics/cfg/models/11/yolo11-pose.yaml +51 -0
- ultralytics/cfg/models/11/yolo11-seg.yaml +50 -0
- ultralytics/cfg/models/11/yolo11.yaml +50 -0
- ultralytics/cfg/models/11/yoloe-11-seg.yaml +48 -0
- ultralytics/cfg/models/11/yoloe-11.yaml +48 -0
- ultralytics/cfg/models/12/yolo12-cls.yaml +32 -0
- ultralytics/cfg/models/12/yolo12-obb.yaml +48 -0
- ultralytics/cfg/models/12/yolo12-pose.yaml +49 -0
- ultralytics/cfg/models/12/yolo12-seg.yaml +48 -0
- ultralytics/cfg/models/12/yolo12.yaml +48 -0
- ultralytics/cfg/models/rt-detr/rtdetr-l.yaml +53 -0
- ultralytics/cfg/models/rt-detr/rtdetr-resnet101.yaml +45 -0
- ultralytics/cfg/models/rt-detr/rtdetr-resnet50.yaml +45 -0
- ultralytics/cfg/models/rt-detr/rtdetr-x.yaml +57 -0
- ultralytics/cfg/models/v10/yolov10b.yaml +45 -0
- ultralytics/cfg/models/v10/yolov10l.yaml +45 -0
- ultralytics/cfg/models/v10/yolov10m.yaml +45 -0
- ultralytics/cfg/models/v10/yolov10n.yaml +45 -0
- ultralytics/cfg/models/v10/yolov10s.yaml +45 -0
- ultralytics/cfg/models/v10/yolov10x.yaml +45 -0
- ultralytics/cfg/models/v3/yolov3-spp.yaml +49 -0
- ultralytics/cfg/models/v3/yolov3-tiny.yaml +40 -0
- ultralytics/cfg/models/v3/yolov3.yaml +49 -0
- ultralytics/cfg/models/v5/yolov5-p6.yaml +62 -0
- ultralytics/cfg/models/v5/yolov5.yaml +51 -0
- ultralytics/cfg/models/v6/yolov6.yaml +56 -0
- ultralytics/cfg/models/v8/yoloe-v8-seg.yaml +45 -0
- ultralytics/cfg/models/v8/yoloe-v8.yaml +45 -0
- ultralytics/cfg/models/v8/yolov8-cls-resnet101.yaml +28 -0
- ultralytics/cfg/models/v8/yolov8-cls-resnet50.yaml +28 -0
- ultralytics/cfg/models/v8/yolov8-cls.yaml +32 -0
- ultralytics/cfg/models/v8/yolov8-ghost-p2.yaml +58 -0
- ultralytics/cfg/models/v8/yolov8-ghost-p6.yaml +60 -0
- ultralytics/cfg/models/v8/yolov8-ghost.yaml +50 -0
- ultralytics/cfg/models/v8/yolov8-obb.yaml +49 -0
- ultralytics/cfg/models/v8/yolov8-p2.yaml +57 -0
- ultralytics/cfg/models/v8/yolov8-p6.yaml +59 -0
- ultralytics/cfg/models/v8/yolov8-pose-p6.yaml +60 -0
- ultralytics/cfg/models/v8/yolov8-pose.yaml +50 -0
- ultralytics/cfg/models/v8/yolov8-rtdetr.yaml +49 -0
- ultralytics/cfg/models/v8/yolov8-seg-p6.yaml +59 -0
- ultralytics/cfg/models/v8/yolov8-seg.yaml +49 -0
- ultralytics/cfg/models/v8/yolov8-world.yaml +51 -0
- ultralytics/cfg/models/v8/yolov8-worldv2.yaml +49 -0
- ultralytics/cfg/models/v8/yolov8.yaml +49 -0
- ultralytics/cfg/models/v9/yolov9c-seg.yaml +41 -0
- ultralytics/cfg/models/v9/yolov9c.yaml +41 -0
- ultralytics/cfg/models/v9/yolov9e-seg.yaml +64 -0
- ultralytics/cfg/models/v9/yolov9e.yaml +64 -0
- ultralytics/cfg/models/v9/yolov9m.yaml +41 -0
- ultralytics/cfg/models/v9/yolov9s.yaml +41 -0
- ultralytics/cfg/models/v9/yolov9t.yaml +41 -0
- ultralytics/cfg/trackers/botsort.yaml +22 -0
- ultralytics/cfg/trackers/bytetrack.yaml +14 -0
- ultralytics/data/__init__.py +26 -0
- ultralytics/data/annotator.py +66 -0
- ultralytics/data/augment.py +2945 -0
- ultralytics/data/base.py +438 -0
- ultralytics/data/build.py +258 -0
- ultralytics/data/converter.py +754 -0
- ultralytics/data/dataset.py +834 -0
- ultralytics/data/loaders.py +676 -0
- ultralytics/data/scripts/download_weights.sh +18 -0
- ultralytics/data/scripts/get_coco.sh +61 -0
- ultralytics/data/scripts/get_coco128.sh +18 -0
- ultralytics/data/scripts/get_imagenet.sh +52 -0
- ultralytics/data/split.py +125 -0
- ultralytics/data/split_dota.py +325 -0
- ultralytics/data/utils.py +777 -0
- ultralytics/engine/__init__.py +1 -0
- ultralytics/engine/exporter.py +1519 -0
- ultralytics/engine/model.py +1156 -0
- ultralytics/engine/predictor.py +502 -0
- ultralytics/engine/results.py +1840 -0
- ultralytics/engine/trainer.py +853 -0
- ultralytics/engine/tuner.py +243 -0
- ultralytics/engine/validator.py +377 -0
- ultralytics/hub/__init__.py +168 -0
- ultralytics/hub/auth.py +137 -0
- ultralytics/hub/google/__init__.py +176 -0
- ultralytics/hub/session.py +446 -0
- ultralytics/hub/utils.py +248 -0
- ultralytics/models/__init__.py +9 -0
- ultralytics/models/fastsam/__init__.py +7 -0
- ultralytics/models/fastsam/model.py +61 -0
- ultralytics/models/fastsam/predict.py +181 -0
- ultralytics/models/fastsam/utils.py +24 -0
- ultralytics/models/fastsam/val.py +40 -0
- ultralytics/models/nas/__init__.py +7 -0
- ultralytics/models/nas/model.py +102 -0
- ultralytics/models/nas/predict.py +58 -0
- ultralytics/models/nas/val.py +39 -0
- ultralytics/models/rtdetr/__init__.py +7 -0
- ultralytics/models/rtdetr/model.py +63 -0
- ultralytics/models/rtdetr/predict.py +84 -0
- ultralytics/models/rtdetr/train.py +85 -0
- ultralytics/models/rtdetr/val.py +191 -0
- ultralytics/models/sam/__init__.py +6 -0
- ultralytics/models/sam/amg.py +260 -0
- ultralytics/models/sam/build.py +358 -0
- ultralytics/models/sam/model.py +170 -0
- ultralytics/models/sam/modules/__init__.py +1 -0
- ultralytics/models/sam/modules/blocks.py +1129 -0
- ultralytics/models/sam/modules/decoders.py +515 -0
- ultralytics/models/sam/modules/encoders.py +854 -0
- ultralytics/models/sam/modules/memory_attention.py +299 -0
- ultralytics/models/sam/modules/sam.py +1006 -0
- ultralytics/models/sam/modules/tiny_encoder.py +1002 -0
- ultralytics/models/sam/modules/transformer.py +351 -0
- ultralytics/models/sam/modules/utils.py +394 -0
- ultralytics/models/sam/predict.py +1605 -0
- ultralytics/models/utils/__init__.py +1 -0
- ultralytics/models/utils/loss.py +455 -0
- ultralytics/models/utils/ops.py +268 -0
- ultralytics/models/yolo/__init__.py +7 -0
- ultralytics/models/yolo/classify/__init__.py +7 -0
- ultralytics/models/yolo/classify/predict.py +88 -0
- ultralytics/models/yolo/classify/train.py +233 -0
- ultralytics/models/yolo/classify/val.py +215 -0
- ultralytics/models/yolo/detect/__init__.py +7 -0
- ultralytics/models/yolo/detect/predict.py +124 -0
- ultralytics/models/yolo/detect/train.py +217 -0
- ultralytics/models/yolo/detect/val.py +451 -0
- ultralytics/models/yolo/model.py +354 -0
- ultralytics/models/yolo/obb/__init__.py +7 -0
- ultralytics/models/yolo/obb/predict.py +66 -0
- ultralytics/models/yolo/obb/train.py +81 -0
- ultralytics/models/yolo/obb/val.py +283 -0
- ultralytics/models/yolo/pose/__init__.py +7 -0
- ultralytics/models/yolo/pose/predict.py +79 -0
- ultralytics/models/yolo/pose/train.py +154 -0
- ultralytics/models/yolo/pose/val.py +394 -0
- ultralytics/models/yolo/segment/__init__.py +7 -0
- ultralytics/models/yolo/segment/predict.py +113 -0
- ultralytics/models/yolo/segment/train.py +123 -0
- ultralytics/models/yolo/segment/val.py +428 -0
- ultralytics/models/yolo/world/__init__.py +5 -0
- ultralytics/models/yolo/world/train.py +119 -0
- ultralytics/models/yolo/world/train_world.py +176 -0
- ultralytics/models/yolo/yoloe/__init__.py +22 -0
- ultralytics/models/yolo/yoloe/predict.py +169 -0
- ultralytics/models/yolo/yoloe/train.py +298 -0
- ultralytics/models/yolo/yoloe/train_seg.py +124 -0
- ultralytics/models/yolo/yoloe/val.py +191 -0
- ultralytics/nn/__init__.py +29 -0
- ultralytics/nn/autobackend.py +842 -0
- ultralytics/nn/modules/__init__.py +182 -0
- ultralytics/nn/modules/activation.py +53 -0
- ultralytics/nn/modules/block.py +1966 -0
- ultralytics/nn/modules/conv.py +712 -0
- ultralytics/nn/modules/head.py +880 -0
- ultralytics/nn/modules/transformer.py +713 -0
- ultralytics/nn/modules/utils.py +164 -0
- ultralytics/nn/tasks.py +1627 -0
- ultralytics/nn/text_model.py +351 -0
- ultralytics/solutions/__init__.py +41 -0
- ultralytics/solutions/ai_gym.py +116 -0
- ultralytics/solutions/analytics.py +252 -0
- ultralytics/solutions/config.py +106 -0
- ultralytics/solutions/distance_calculation.py +124 -0
- ultralytics/solutions/heatmap.py +127 -0
- ultralytics/solutions/instance_segmentation.py +84 -0
- ultralytics/solutions/object_blurrer.py +90 -0
- ultralytics/solutions/object_counter.py +195 -0
- ultralytics/solutions/object_cropper.py +84 -0
- ultralytics/solutions/parking_management.py +273 -0
- ultralytics/solutions/queue_management.py +93 -0
- ultralytics/solutions/region_counter.py +120 -0
- ultralytics/solutions/security_alarm.py +154 -0
- ultralytics/solutions/similarity_search.py +172 -0
- ultralytics/solutions/solutions.py +724 -0
- ultralytics/solutions/speed_estimation.py +110 -0
- ultralytics/solutions/streamlit_inference.py +196 -0
- ultralytics/solutions/templates/similarity-search.html +160 -0
- ultralytics/solutions/trackzone.py +88 -0
- ultralytics/solutions/vision_eye.py +68 -0
- ultralytics/trackers/__init__.py +7 -0
- ultralytics/trackers/basetrack.py +124 -0
- ultralytics/trackers/bot_sort.py +260 -0
- ultralytics/trackers/byte_tracker.py +480 -0
- ultralytics/trackers/track.py +125 -0
- ultralytics/trackers/utils/__init__.py +1 -0
- ultralytics/trackers/utils/gmc.py +376 -0
- ultralytics/trackers/utils/kalman_filter.py +493 -0
- ultralytics/trackers/utils/matching.py +157 -0
- ultralytics/utils/__init__.py +1435 -0
- ultralytics/utils/autobatch.py +106 -0
- ultralytics/utils/autodevice.py +174 -0
- ultralytics/utils/benchmarks.py +695 -0
- ultralytics/utils/callbacks/__init__.py +5 -0
- ultralytics/utils/callbacks/base.py +234 -0
- ultralytics/utils/callbacks/clearml.py +153 -0
- ultralytics/utils/callbacks/comet.py +552 -0
- ultralytics/utils/callbacks/dvc.py +205 -0
- ultralytics/utils/callbacks/hub.py +108 -0
- ultralytics/utils/callbacks/mlflow.py +138 -0
- ultralytics/utils/callbacks/neptune.py +140 -0
- ultralytics/utils/callbacks/raytune.py +43 -0
- ultralytics/utils/callbacks/tensorboard.py +132 -0
- ultralytics/utils/callbacks/wb.py +185 -0
- ultralytics/utils/checks.py +897 -0
- ultralytics/utils/dist.py +119 -0
- ultralytics/utils/downloads.py +499 -0
- ultralytics/utils/errors.py +43 -0
- ultralytics/utils/export.py +219 -0
- ultralytics/utils/files.py +221 -0
- ultralytics/utils/instance.py +499 -0
- ultralytics/utils/loss.py +813 -0
- ultralytics/utils/metrics.py +1356 -0
- ultralytics/utils/ops.py +885 -0
- ultralytics/utils/patches.py +143 -0
- ultralytics/utils/plotting.py +1011 -0
- ultralytics/utils/tal.py +416 -0
- ultralytics/utils/torch_utils.py +990 -0
- ultralytics/utils/triton.py +116 -0
- ultralytics/utils/tuner.py +159 -0
@@ -0,0 +1,446 @@
|
|
1
|
+
# Ultralytics 🚀 AGPL-3.0 License - https://ultralytics.com/license
|
2
|
+
|
3
|
+
import shutil
|
4
|
+
import threading
|
5
|
+
import time
|
6
|
+
from http import HTTPStatus
|
7
|
+
from pathlib import Path
|
8
|
+
from urllib.parse import parse_qs, urlparse
|
9
|
+
|
10
|
+
import requests
|
11
|
+
|
12
|
+
from ultralytics import __version__
|
13
|
+
from ultralytics.hub.utils import HELP_MSG, HUB_WEB_ROOT, PREFIX
|
14
|
+
from ultralytics.utils import IS_COLAB, LOGGER, SETTINGS, TQDM, checks, emojis
|
15
|
+
from ultralytics.utils.errors import HUBModelError
|
16
|
+
|
17
|
+
AGENT_NAME = f"python-{__version__}-colab" if IS_COLAB else f"python-{__version__}-local"
|
18
|
+
|
19
|
+
|
20
|
+
class HUBTrainingSession:
|
21
|
+
"""
|
22
|
+
HUB training session for Ultralytics HUB YOLO models. Handles model initialization, heartbeats, and checkpointing.
|
23
|
+
|
24
|
+
This class encapsulates the functionality for interacting with Ultralytics HUB during model training, including
|
25
|
+
model creation, metrics tracking, and checkpoint uploading.
|
26
|
+
|
27
|
+
Attributes:
|
28
|
+
model_id (str): Identifier for the YOLO model being trained.
|
29
|
+
model_url (str): URL for the model in Ultralytics HUB.
|
30
|
+
rate_limits (dict): Rate limits for different API calls (in seconds).
|
31
|
+
timers (dict): Timers for rate limiting.
|
32
|
+
metrics_queue (dict): Queue for the model's metrics.
|
33
|
+
metrics_upload_failed_queue (dict): Queue for metrics that failed to upload.
|
34
|
+
model (dict): Model data fetched from Ultralytics HUB.
|
35
|
+
model_file (str): Path to the model file.
|
36
|
+
train_args (dict): Arguments for training the model.
|
37
|
+
client (HUBClient): Client for interacting with Ultralytics HUB.
|
38
|
+
filename (str): Filename of the model.
|
39
|
+
|
40
|
+
Examples:
|
41
|
+
>>> session = HUBTrainingSession("https://hub.ultralytics.com/models/example-model")
|
42
|
+
>>> session.upload_metrics()
|
43
|
+
"""
|
44
|
+
|
45
|
+
def __init__(self, identifier):
|
46
|
+
"""
|
47
|
+
Initialize the HUBTrainingSession with the provided model identifier.
|
48
|
+
|
49
|
+
Args:
|
50
|
+
identifier (str): Model identifier used to initialize the HUB training session.
|
51
|
+
It can be a URL string or a model key with specific format.
|
52
|
+
|
53
|
+
Raises:
|
54
|
+
ValueError: If the provided model identifier is invalid.
|
55
|
+
ConnectionError: If connecting with global API key is not supported.
|
56
|
+
ModuleNotFoundError: If hub-sdk package is not installed.
|
57
|
+
"""
|
58
|
+
from hub_sdk import HUBClient
|
59
|
+
|
60
|
+
self.rate_limits = {"metrics": 3, "ckpt": 900, "heartbeat": 300} # rate limits (seconds)
|
61
|
+
self.metrics_queue = {} # holds metrics for each epoch until upload
|
62
|
+
self.metrics_upload_failed_queue = {} # holds metrics for each epoch if upload failed
|
63
|
+
self.timers = {} # holds timers in ultralytics/utils/callbacks/hub.py
|
64
|
+
self.model = None
|
65
|
+
self.model_url = None
|
66
|
+
self.model_file = None
|
67
|
+
self.train_args = None
|
68
|
+
|
69
|
+
# Parse input
|
70
|
+
api_key, model_id, self.filename = self._parse_identifier(identifier)
|
71
|
+
|
72
|
+
# Get credentials
|
73
|
+
active_key = api_key or SETTINGS.get("api_key")
|
74
|
+
credentials = {"api_key": active_key} if active_key else None # set credentials
|
75
|
+
|
76
|
+
# Initialize client
|
77
|
+
self.client = HUBClient(credentials)
|
78
|
+
|
79
|
+
# Load models
|
80
|
+
try:
|
81
|
+
if model_id:
|
82
|
+
self.load_model(model_id) # load existing model
|
83
|
+
else:
|
84
|
+
self.model = self.client.model() # load empty model
|
85
|
+
except Exception:
|
86
|
+
if identifier.startswith(f"{HUB_WEB_ROOT}/models/") and not self.client.authenticated:
|
87
|
+
LOGGER.warning(
|
88
|
+
f"{PREFIX}Please log in using 'yolo login API_KEY'. "
|
89
|
+
"You can find your API Key at: https://hub.ultralytics.com/settings?tab=api+keys."
|
90
|
+
)
|
91
|
+
|
92
|
+
@classmethod
|
93
|
+
def create_session(cls, identifier, args=None):
|
94
|
+
"""
|
95
|
+
Create an authenticated HUBTrainingSession or return None.
|
96
|
+
|
97
|
+
Args:
|
98
|
+
identifier (str): Model identifier used to initialize the HUB training session.
|
99
|
+
args (dict, optional): Arguments for creating a new model if identifier is not a HUB model URL.
|
100
|
+
|
101
|
+
Returns:
|
102
|
+
(HUBTrainingSession | None): An authenticated session or None if creation fails.
|
103
|
+
"""
|
104
|
+
try:
|
105
|
+
session = cls(identifier)
|
106
|
+
if args and not identifier.startswith(f"{HUB_WEB_ROOT}/models/"): # not a HUB model URL
|
107
|
+
session.create_model(args)
|
108
|
+
assert session.model.id, "HUB model not loaded correctly"
|
109
|
+
return session
|
110
|
+
# PermissionError and ModuleNotFoundError indicate hub-sdk not installed
|
111
|
+
except (PermissionError, ModuleNotFoundError, AssertionError):
|
112
|
+
return None
|
113
|
+
|
114
|
+
def load_model(self, model_id):
|
115
|
+
"""
|
116
|
+
Load an existing model from Ultralytics HUB using the provided model identifier.
|
117
|
+
|
118
|
+
Args:
|
119
|
+
model_id (str): The identifier of the model to load.
|
120
|
+
|
121
|
+
Raises:
|
122
|
+
ValueError: If the specified HUB model does not exist.
|
123
|
+
"""
|
124
|
+
self.model = self.client.model(model_id)
|
125
|
+
if not self.model.data: # then model does not exist
|
126
|
+
raise ValueError(emojis("❌ The specified HUB model does not exist")) # TODO: improve error handling
|
127
|
+
|
128
|
+
self.model_url = f"{HUB_WEB_ROOT}/models/{self.model.id}"
|
129
|
+
if self.model.is_trained():
|
130
|
+
LOGGER.info(f"Loading trained HUB model {self.model_url} 🚀")
|
131
|
+
url = self.model.get_weights_url("best") # download URL with auth
|
132
|
+
self.model_file = checks.check_file(url, download_dir=Path(SETTINGS["weights_dir"]) / "hub" / self.model.id)
|
133
|
+
return
|
134
|
+
|
135
|
+
# Set training args and start heartbeats for HUB to monitor agent
|
136
|
+
self._set_train_args()
|
137
|
+
self.model.start_heartbeat(self.rate_limits["heartbeat"])
|
138
|
+
LOGGER.info(f"{PREFIX}View model at {self.model_url} 🚀")
|
139
|
+
|
140
|
+
def create_model(self, model_args):
|
141
|
+
"""
|
142
|
+
Initialize a HUB training session with the specified model arguments.
|
143
|
+
|
144
|
+
Args:
|
145
|
+
model_args (dict): Arguments for creating the model, including batch size, epochs, image size, etc.
|
146
|
+
|
147
|
+
Returns:
|
148
|
+
(None): If the model could not be created.
|
149
|
+
"""
|
150
|
+
payload = {
|
151
|
+
"config": {
|
152
|
+
"batchSize": model_args.get("batch", -1),
|
153
|
+
"epochs": model_args.get("epochs", 300),
|
154
|
+
"imageSize": model_args.get("imgsz", 640),
|
155
|
+
"patience": model_args.get("patience", 100),
|
156
|
+
"device": str(model_args.get("device", "")), # convert None to string
|
157
|
+
"cache": str(model_args.get("cache", "ram")), # convert True, False, None to string
|
158
|
+
},
|
159
|
+
"dataset": {"name": model_args.get("data")},
|
160
|
+
"lineage": {
|
161
|
+
"architecture": {"name": self.filename.replace(".pt", "").replace(".yaml", "")},
|
162
|
+
"parent": {},
|
163
|
+
},
|
164
|
+
"meta": {"name": self.filename},
|
165
|
+
}
|
166
|
+
|
167
|
+
if self.filename.endswith(".pt"):
|
168
|
+
payload["lineage"]["parent"]["name"] = self.filename
|
169
|
+
|
170
|
+
self.model.create_model(payload)
|
171
|
+
|
172
|
+
# Model could not be created
|
173
|
+
# TODO: improve error handling
|
174
|
+
if not self.model.id:
|
175
|
+
return None
|
176
|
+
|
177
|
+
self.model_url = f"{HUB_WEB_ROOT}/models/{self.model.id}"
|
178
|
+
|
179
|
+
# Start heartbeats for HUB to monitor agent
|
180
|
+
self.model.start_heartbeat(self.rate_limits["heartbeat"])
|
181
|
+
|
182
|
+
LOGGER.info(f"{PREFIX}View model at {self.model_url} 🚀")
|
183
|
+
|
184
|
+
@staticmethod
|
185
|
+
def _parse_identifier(identifier):
|
186
|
+
"""
|
187
|
+
Parse the given identifier to determine the type and extract relevant components.
|
188
|
+
|
189
|
+
The method supports different identifier formats:
|
190
|
+
- A HUB model URL https://hub.ultralytics.com/models/MODEL
|
191
|
+
- A HUB model URL with API Key https://hub.ultralytics.com/models/MODEL?api_key=APIKEY
|
192
|
+
- A local filename that ends with '.pt' or '.yaml'
|
193
|
+
|
194
|
+
Args:
|
195
|
+
identifier (str): The identifier string to be parsed.
|
196
|
+
|
197
|
+
Returns:
|
198
|
+
(tuple): A tuple containing the API key, model ID, and filename as applicable.
|
199
|
+
|
200
|
+
Raises:
|
201
|
+
HUBModelError: If the identifier format is not recognized.
|
202
|
+
"""
|
203
|
+
api_key, model_id, filename = None, None, None
|
204
|
+
if str(identifier).endswith((".pt", ".yaml")):
|
205
|
+
filename = identifier
|
206
|
+
elif identifier.startswith(f"{HUB_WEB_ROOT}/models/"):
|
207
|
+
parsed_url = urlparse(identifier)
|
208
|
+
model_id = Path(parsed_url.path).stem # handle possible final backslash robustly
|
209
|
+
query_params = parse_qs(parsed_url.query) # dictionary, i.e. {"api_key": ["API_KEY_HERE"]}
|
210
|
+
api_key = query_params.get("api_key", [None])[0]
|
211
|
+
else:
|
212
|
+
raise HUBModelError(f"model='{identifier} invalid, correct format is {HUB_WEB_ROOT}/models/MODEL_ID")
|
213
|
+
return api_key, model_id, filename
|
214
|
+
|
215
|
+
def _set_train_args(self):
|
216
|
+
"""
|
217
|
+
Initialize training arguments and create a model entry on the Ultralytics HUB.
|
218
|
+
|
219
|
+
This method sets up training arguments based on the model's state and updates them with any additional
|
220
|
+
arguments provided. It handles different states of the model, such as whether it's resumable, pretrained,
|
221
|
+
or requires specific file setup.
|
222
|
+
|
223
|
+
Raises:
|
224
|
+
ValueError: If the model is already trained, if required dataset information is missing, or if there are
|
225
|
+
issues with the provided training arguments.
|
226
|
+
"""
|
227
|
+
if self.model.is_resumable():
|
228
|
+
# Model has saved weights
|
229
|
+
self.train_args = {"data": self.model.get_dataset_url(), "resume": True}
|
230
|
+
self.model_file = self.model.get_weights_url("last")
|
231
|
+
else:
|
232
|
+
# Model has no saved weights
|
233
|
+
self.train_args = self.model.data.get("train_args") # new response
|
234
|
+
|
235
|
+
# Set the model file as either a *.pt or *.yaml file
|
236
|
+
self.model_file = (
|
237
|
+
self.model.get_weights_url("parent") if self.model.is_pretrained() else self.model.get_architecture()
|
238
|
+
)
|
239
|
+
|
240
|
+
if "data" not in self.train_args:
|
241
|
+
# RF bug - datasets are sometimes not exported
|
242
|
+
raise ValueError("Dataset may still be processing. Please wait a minute and try again.")
|
243
|
+
|
244
|
+
self.model_file = checks.check_yolov5u_filename(self.model_file, verbose=False) # YOLOv5->YOLOv5u
|
245
|
+
self.model_id = self.model.id
|
246
|
+
|
247
|
+
def request_queue(
|
248
|
+
self,
|
249
|
+
request_func,
|
250
|
+
retry=3,
|
251
|
+
timeout=30,
|
252
|
+
thread=True,
|
253
|
+
verbose=True,
|
254
|
+
progress_total=None,
|
255
|
+
stream_response=None,
|
256
|
+
*args,
|
257
|
+
**kwargs,
|
258
|
+
):
|
259
|
+
"""
|
260
|
+
Attempt to execute `request_func` with retries, timeout handling, optional threading, and progress tracking.
|
261
|
+
|
262
|
+
Args:
|
263
|
+
request_func (callable): The function to execute.
|
264
|
+
retry (int): Number of retry attempts.
|
265
|
+
timeout (int): Maximum time to wait for the request to complete.
|
266
|
+
thread (bool): Whether to run the request in a separate thread.
|
267
|
+
verbose (bool): Whether to log detailed messages.
|
268
|
+
progress_total (int, optional): Total size for progress tracking.
|
269
|
+
stream_response (bool, optional): Whether to stream the response.
|
270
|
+
*args (Any): Additional positional arguments for request_func.
|
271
|
+
**kwargs (Any): Additional keyword arguments for request_func.
|
272
|
+
|
273
|
+
Returns:
|
274
|
+
(requests.Response | None): The response object if thread=False, otherwise None.
|
275
|
+
"""
|
276
|
+
|
277
|
+
def retry_request():
|
278
|
+
"""Attempt to call `request_func` with retries, timeout, and optional threading."""
|
279
|
+
t0 = time.time() # Record the start time for the timeout
|
280
|
+
response = None
|
281
|
+
for i in range(retry + 1):
|
282
|
+
if (time.time() - t0) > timeout:
|
283
|
+
LOGGER.warning(f"{PREFIX}Timeout for request reached. {HELP_MSG}")
|
284
|
+
break # Timeout reached, exit loop
|
285
|
+
|
286
|
+
response = request_func(*args, **kwargs)
|
287
|
+
if response is None:
|
288
|
+
LOGGER.warning(f"{PREFIX}Received no response from the request. {HELP_MSG}")
|
289
|
+
time.sleep(2**i) # Exponential backoff before retrying
|
290
|
+
continue # Skip further processing and retry
|
291
|
+
|
292
|
+
if progress_total:
|
293
|
+
self._show_upload_progress(progress_total, response)
|
294
|
+
elif stream_response:
|
295
|
+
self._iterate_content(response)
|
296
|
+
|
297
|
+
if HTTPStatus.OK <= response.status_code < HTTPStatus.MULTIPLE_CHOICES:
|
298
|
+
# if request related to metrics upload
|
299
|
+
if kwargs.get("metrics"):
|
300
|
+
self.metrics_upload_failed_queue = {}
|
301
|
+
return response # Success, no need to retry
|
302
|
+
|
303
|
+
if i == 0:
|
304
|
+
# Initial attempt, check status code and provide messages
|
305
|
+
message = self._get_failure_message(response, retry, timeout)
|
306
|
+
|
307
|
+
if verbose:
|
308
|
+
LOGGER.warning(f"{PREFIX}{message} {HELP_MSG} ({response.status_code})")
|
309
|
+
|
310
|
+
if not self._should_retry(response.status_code):
|
311
|
+
LOGGER.warning(f"{PREFIX}Request failed. {HELP_MSG} ({response.status_code}")
|
312
|
+
break # Not an error that should be retried, exit loop
|
313
|
+
|
314
|
+
time.sleep(2**i) # Exponential backoff for retries
|
315
|
+
|
316
|
+
# if request related to metrics upload and exceed retries
|
317
|
+
if response is None and kwargs.get("metrics"):
|
318
|
+
self.metrics_upload_failed_queue.update(kwargs.get("metrics"))
|
319
|
+
|
320
|
+
return response
|
321
|
+
|
322
|
+
if thread:
|
323
|
+
# Start a new thread to run the retry_request function
|
324
|
+
threading.Thread(target=retry_request, daemon=True).start()
|
325
|
+
else:
|
326
|
+
# If running in the main thread, call retry_request directly
|
327
|
+
return retry_request()
|
328
|
+
|
329
|
+
@staticmethod
|
330
|
+
def _should_retry(status_code):
|
331
|
+
"""
|
332
|
+
Determine if a request should be retried based on the HTTP status code.
|
333
|
+
|
334
|
+
Args:
|
335
|
+
status_code (int): The HTTP status code from the response.
|
336
|
+
|
337
|
+
Returns:
|
338
|
+
(bool): True if the request should be retried, False otherwise.
|
339
|
+
"""
|
340
|
+
retry_codes = {
|
341
|
+
HTTPStatus.REQUEST_TIMEOUT,
|
342
|
+
HTTPStatus.BAD_GATEWAY,
|
343
|
+
HTTPStatus.GATEWAY_TIMEOUT,
|
344
|
+
}
|
345
|
+
return status_code in retry_codes
|
346
|
+
|
347
|
+
def _get_failure_message(self, response: requests.Response, retry: int, timeout: int):
|
348
|
+
"""
|
349
|
+
Generate a retry message based on the response status code.
|
350
|
+
|
351
|
+
Args:
|
352
|
+
response (requests.Response): The HTTP response object.
|
353
|
+
retry (int): The number of retry attempts allowed.
|
354
|
+
timeout (int): The maximum timeout duration.
|
355
|
+
|
356
|
+
Returns:
|
357
|
+
(str): The retry message.
|
358
|
+
"""
|
359
|
+
if self._should_retry(response.status_code):
|
360
|
+
return f"Retrying {retry}x for {timeout}s." if retry else ""
|
361
|
+
elif response.status_code == HTTPStatus.TOO_MANY_REQUESTS: # rate limit
|
362
|
+
headers = response.headers
|
363
|
+
return (
|
364
|
+
f"Rate limit reached ({headers['X-RateLimit-Remaining']}/{headers['X-RateLimit-Limit']}). "
|
365
|
+
f"Please retry after {headers['Retry-After']}s."
|
366
|
+
)
|
367
|
+
else:
|
368
|
+
try:
|
369
|
+
return response.json().get("message", "No JSON message.")
|
370
|
+
except AttributeError:
|
371
|
+
return "Unable to read JSON."
|
372
|
+
|
373
|
+
def upload_metrics(self):
|
374
|
+
"""Upload model metrics to Ultralytics HUB."""
|
375
|
+
return self.request_queue(self.model.upload_metrics, metrics=self.metrics_queue.copy(), thread=True)
|
376
|
+
|
377
|
+
def upload_model(
|
378
|
+
self,
|
379
|
+
epoch: int,
|
380
|
+
weights: str,
|
381
|
+
is_best: bool = False,
|
382
|
+
map: float = 0.0,
|
383
|
+
final: bool = False,
|
384
|
+
) -> None:
|
385
|
+
"""
|
386
|
+
Upload a model checkpoint to Ultralytics HUB.
|
387
|
+
|
388
|
+
Args:
|
389
|
+
epoch (int): The current training epoch.
|
390
|
+
weights (str): Path to the model weights file.
|
391
|
+
is_best (bool): Indicates if the current model is the best one so far.
|
392
|
+
map (float): Mean average precision of the model.
|
393
|
+
final (bool): Indicates if the model is the final model after training.
|
394
|
+
"""
|
395
|
+
weights = Path(weights)
|
396
|
+
if not weights.is_file():
|
397
|
+
last = weights.with_name(f"last{weights.suffix}")
|
398
|
+
if final and last.is_file():
|
399
|
+
LOGGER.warning(
|
400
|
+
f"{PREFIX} Model 'best.pt' not found, copying 'last.pt' to 'best.pt' and uploading. "
|
401
|
+
"This often happens when resuming training in transient environments like Google Colab. "
|
402
|
+
"For more reliable training, consider using Ultralytics HUB Cloud. "
|
403
|
+
"Learn more at https://docs.ultralytics.com/hub/cloud-training."
|
404
|
+
)
|
405
|
+
shutil.copy(last, weights) # copy last.pt to best.pt
|
406
|
+
else:
|
407
|
+
LOGGER.warning(f"{PREFIX} Model upload issue. Missing model {weights}.")
|
408
|
+
return
|
409
|
+
|
410
|
+
self.request_queue(
|
411
|
+
self.model.upload_model,
|
412
|
+
epoch=epoch,
|
413
|
+
weights=str(weights),
|
414
|
+
is_best=is_best,
|
415
|
+
map=map,
|
416
|
+
final=final,
|
417
|
+
retry=10,
|
418
|
+
timeout=3600,
|
419
|
+
thread=not final,
|
420
|
+
progress_total=weights.stat().st_size if final else None, # only show progress if final
|
421
|
+
stream_response=True,
|
422
|
+
)
|
423
|
+
|
424
|
+
@staticmethod
|
425
|
+
def _show_upload_progress(content_length: int, response: requests.Response) -> None:
|
426
|
+
"""
|
427
|
+
Display a progress bar to track the upload progress of a file download.
|
428
|
+
|
429
|
+
Args:
|
430
|
+
content_length (int): The total size of the content to be downloaded in bytes.
|
431
|
+
response (requests.Response): The response object from the file download request.
|
432
|
+
"""
|
433
|
+
with TQDM(total=content_length, unit="B", unit_scale=True, unit_divisor=1024) as pbar:
|
434
|
+
for data in response.iter_content(chunk_size=1024):
|
435
|
+
pbar.update(len(data))
|
436
|
+
|
437
|
+
@staticmethod
|
438
|
+
def _iterate_content(response: requests.Response) -> None:
|
439
|
+
"""
|
440
|
+
Process the streamed HTTP response data.
|
441
|
+
|
442
|
+
Args:
|
443
|
+
response (requests.Response): The response object from the file download request.
|
444
|
+
"""
|
445
|
+
for _ in response.iter_content(chunk_size=1024):
|
446
|
+
pass # Do nothing with data chunks
|
ultralytics/hub/utils.py
ADDED
@@ -0,0 +1,248 @@
|
|
1
|
+
# Ultralytics 🚀 AGPL-3.0 License - https://ultralytics.com/license
|
2
|
+
|
3
|
+
import os
|
4
|
+
import platform
|
5
|
+
import random
|
6
|
+
import threading
|
7
|
+
import time
|
8
|
+
from pathlib import Path
|
9
|
+
|
10
|
+
import requests
|
11
|
+
|
12
|
+
from ultralytics import __version__
|
13
|
+
from ultralytics.utils import (
|
14
|
+
ARGV,
|
15
|
+
ENVIRONMENT,
|
16
|
+
IS_COLAB,
|
17
|
+
IS_GIT_DIR,
|
18
|
+
IS_PIP_PACKAGE,
|
19
|
+
LOGGER,
|
20
|
+
ONLINE,
|
21
|
+
RANK,
|
22
|
+
SETTINGS,
|
23
|
+
TESTS_RUNNING,
|
24
|
+
TQDM,
|
25
|
+
TryExcept,
|
26
|
+
colorstr,
|
27
|
+
get_git_origin_url,
|
28
|
+
)
|
29
|
+
from ultralytics.utils.downloads import GITHUB_ASSETS_NAMES
|
30
|
+
|
31
|
+
HUB_API_ROOT = os.environ.get("ULTRALYTICS_HUB_API", "https://api.ultralytics.com")
|
32
|
+
HUB_WEB_ROOT = os.environ.get("ULTRALYTICS_HUB_WEB", "https://hub.ultralytics.com")
|
33
|
+
|
34
|
+
PREFIX = colorstr("Ultralytics HUB: ")
|
35
|
+
HELP_MSG = "If this issue persists please visit https://github.com/ultralytics/hub/issues for assistance."
|
36
|
+
|
37
|
+
|
38
|
+
def request_with_credentials(url: str) -> any:
|
39
|
+
"""
|
40
|
+
Make an AJAX request with cookies attached in a Google Colab environment.
|
41
|
+
|
42
|
+
Args:
|
43
|
+
url (str): The URL to make the request to.
|
44
|
+
|
45
|
+
Returns:
|
46
|
+
(Any): The response data from the AJAX request.
|
47
|
+
|
48
|
+
Raises:
|
49
|
+
OSError: If the function is not run in a Google Colab environment.
|
50
|
+
"""
|
51
|
+
if not IS_COLAB:
|
52
|
+
raise OSError("request_with_credentials() must run in a Colab environment")
|
53
|
+
from google.colab import output # noqa
|
54
|
+
from IPython import display # noqa
|
55
|
+
|
56
|
+
display.display(
|
57
|
+
display.Javascript(
|
58
|
+
f"""
|
59
|
+
window._hub_tmp = new Promise((resolve, reject) => {{
|
60
|
+
const timeout = setTimeout(() => reject("Failed authenticating existing browser session"), 5000)
|
61
|
+
fetch("{url}", {{
|
62
|
+
method: 'POST',
|
63
|
+
credentials: 'include'
|
64
|
+
}})
|
65
|
+
.then((response) => resolve(response.json()))
|
66
|
+
.then((json) => {{
|
67
|
+
clearTimeout(timeout);
|
68
|
+
}}).catch((err) => {{
|
69
|
+
clearTimeout(timeout);
|
70
|
+
reject(err);
|
71
|
+
}});
|
72
|
+
}});
|
73
|
+
"""
|
74
|
+
)
|
75
|
+
)
|
76
|
+
return output.eval_js("_hub_tmp")
|
77
|
+
|
78
|
+
|
79
|
+
def requests_with_progress(method, url, **kwargs):
|
80
|
+
"""
|
81
|
+
Make an HTTP request using the specified method and URL, with an optional progress bar.
|
82
|
+
|
83
|
+
Args:
|
84
|
+
method (str): The HTTP method to use (e.g. 'GET', 'POST').
|
85
|
+
url (str): The URL to send the request to.
|
86
|
+
**kwargs (Any): Additional keyword arguments to pass to the underlying `requests.request` function.
|
87
|
+
|
88
|
+
Returns:
|
89
|
+
(requests.Response): The response object from the HTTP request.
|
90
|
+
|
91
|
+
Notes:
|
92
|
+
- If 'progress' is set to True, the progress bar will display the download progress for responses with a known
|
93
|
+
content length.
|
94
|
+
- If 'progress' is a number then progress bar will display assuming content length = progress.
|
95
|
+
"""
|
96
|
+
progress = kwargs.pop("progress", False)
|
97
|
+
if not progress:
|
98
|
+
return requests.request(method, url, **kwargs)
|
99
|
+
response = requests.request(method, url, stream=True, **kwargs)
|
100
|
+
total = int(response.headers.get("content-length", 0) if isinstance(progress, bool) else progress) # total size
|
101
|
+
try:
|
102
|
+
pbar = TQDM(total=total, unit="B", unit_scale=True, unit_divisor=1024)
|
103
|
+
for data in response.iter_content(chunk_size=1024):
|
104
|
+
pbar.update(len(data))
|
105
|
+
pbar.close()
|
106
|
+
except requests.exceptions.ChunkedEncodingError: # avoid 'Connection broken: IncompleteRead' warnings
|
107
|
+
response.close()
|
108
|
+
return response
|
109
|
+
|
110
|
+
|
111
|
+
def smart_request(method, url, retry=3, timeout=30, thread=True, code=-1, verbose=True, progress=False, **kwargs):
|
112
|
+
"""
|
113
|
+
Make an HTTP request using the 'requests' library, with exponential backoff retries up to a specified timeout.
|
114
|
+
|
115
|
+
Args:
|
116
|
+
method (str): The HTTP method to use for the request. Choices are 'post' and 'get'.
|
117
|
+
url (str): The URL to make the request to.
|
118
|
+
retry (int, optional): Number of retries to attempt before giving up.
|
119
|
+
timeout (int, optional): Timeout in seconds after which the function will give up retrying.
|
120
|
+
thread (bool, optional): Whether to execute the request in a separate daemon thread.
|
121
|
+
code (int, optional): An identifier for the request, used for logging purposes.
|
122
|
+
verbose (bool, optional): A flag to determine whether to print out to console or not.
|
123
|
+
progress (bool, optional): Whether to show a progress bar during the request.
|
124
|
+
**kwargs (Any): Keyword arguments to be passed to the requests function specified in method.
|
125
|
+
|
126
|
+
Returns:
|
127
|
+
(requests.Response): The HTTP response object. If the request is executed in a separate thread, returns None.
|
128
|
+
"""
|
129
|
+
retry_codes = (408, 500) # retry only these codes
|
130
|
+
|
131
|
+
@TryExcept(verbose=verbose)
|
132
|
+
def func(func_method, func_url, **func_kwargs):
|
133
|
+
"""Make HTTP requests with retries and timeouts, with optional progress tracking."""
|
134
|
+
r = None # response
|
135
|
+
t0 = time.time() # initial time for timer
|
136
|
+
for i in range(retry + 1):
|
137
|
+
if (time.time() - t0) > timeout:
|
138
|
+
break
|
139
|
+
r = requests_with_progress(func_method, func_url, **func_kwargs) # i.e. get(url, data, json, files)
|
140
|
+
if r.status_code < 300: # return codes in the 2xx range are generally considered "good" or "successful"
|
141
|
+
break
|
142
|
+
try:
|
143
|
+
m = r.json().get("message", "No JSON message.")
|
144
|
+
except AttributeError:
|
145
|
+
m = "Unable to read JSON."
|
146
|
+
if i == 0:
|
147
|
+
if r.status_code in retry_codes:
|
148
|
+
m += f" Retrying {retry}x for {timeout}s." if retry else ""
|
149
|
+
elif r.status_code == 429: # rate limit
|
150
|
+
h = r.headers # response headers
|
151
|
+
m = (
|
152
|
+
f"Rate limit reached ({h['X-RateLimit-Remaining']}/{h['X-RateLimit-Limit']}). "
|
153
|
+
f"Please retry after {h['Retry-After']}s."
|
154
|
+
)
|
155
|
+
if verbose:
|
156
|
+
LOGGER.warning(f"{PREFIX}{m} {HELP_MSG} ({r.status_code} #{code})")
|
157
|
+
if r.status_code not in retry_codes:
|
158
|
+
return r
|
159
|
+
time.sleep(2**i) # exponential standoff
|
160
|
+
return r
|
161
|
+
|
162
|
+
args = method, url
|
163
|
+
kwargs["progress"] = progress
|
164
|
+
if thread:
|
165
|
+
threading.Thread(target=func, args=args, kwargs=kwargs, daemon=True).start()
|
166
|
+
else:
|
167
|
+
return func(*args, **kwargs)
|
168
|
+
|
169
|
+
|
170
|
+
class Events:
|
171
|
+
"""
|
172
|
+
A class for collecting anonymous event analytics.
|
173
|
+
|
174
|
+
Event analytics are enabled when sync=True in settings and disabled when sync=False. Run 'yolo settings' to see and
|
175
|
+
update settings.
|
176
|
+
|
177
|
+
Attributes:
|
178
|
+
url (str): The URL to send anonymous events.
|
179
|
+
rate_limit (float): The rate limit in seconds for sending events.
|
180
|
+
metadata (dict): A dictionary containing metadata about the environment.
|
181
|
+
enabled (bool): A flag to enable or disable Events based on certain conditions.
|
182
|
+
"""
|
183
|
+
|
184
|
+
url = "https://www.google-analytics.com/mp/collect?measurement_id=G-X8NCJYTQXM&api_secret=QLQrATrNSwGRFRLE-cbHJw"
|
185
|
+
|
186
|
+
def __init__(self):
|
187
|
+
"""Initialize the Events object with default values for events, rate_limit, and metadata."""
|
188
|
+
self.events = [] # events list
|
189
|
+
self.rate_limit = 30.0 # rate limit (seconds)
|
190
|
+
self.t = 0.0 # rate limit timer (seconds)
|
191
|
+
self.metadata = {
|
192
|
+
"cli": Path(ARGV[0]).name == "yolo",
|
193
|
+
"install": "git" if IS_GIT_DIR else "pip" if IS_PIP_PACKAGE else "other",
|
194
|
+
"python": ".".join(platform.python_version_tuple()[:2]), # i.e. 3.10
|
195
|
+
"version": __version__,
|
196
|
+
"env": ENVIRONMENT,
|
197
|
+
"session_id": round(random.random() * 1e15),
|
198
|
+
"engagement_time_msec": 1000,
|
199
|
+
}
|
200
|
+
self.enabled = (
|
201
|
+
SETTINGS["sync"]
|
202
|
+
and RANK in {-1, 0}
|
203
|
+
and not TESTS_RUNNING
|
204
|
+
and ONLINE
|
205
|
+
and (IS_PIP_PACKAGE or get_git_origin_url() == "https://github.com/ultralytics/ultralytics.git")
|
206
|
+
)
|
207
|
+
|
208
|
+
def __call__(self, cfg):
|
209
|
+
"""
|
210
|
+
Attempt to add a new event to the events list and send events if the rate limit is reached.
|
211
|
+
|
212
|
+
Args:
|
213
|
+
cfg (IterableSimpleNamespace): The configuration object containing mode and task information.
|
214
|
+
"""
|
215
|
+
if not self.enabled:
|
216
|
+
# Events disabled, do nothing
|
217
|
+
return
|
218
|
+
|
219
|
+
# Attempt to add to events
|
220
|
+
if len(self.events) < 25: # Events list limited to 25 events (drop any events past this)
|
221
|
+
params = {
|
222
|
+
**self.metadata,
|
223
|
+
"task": cfg.task,
|
224
|
+
"model": cfg.model if cfg.model in GITHUB_ASSETS_NAMES else "custom",
|
225
|
+
}
|
226
|
+
if cfg.mode == "export":
|
227
|
+
params["format"] = cfg.format
|
228
|
+
self.events.append({"name": cfg.mode, "params": params})
|
229
|
+
|
230
|
+
# Check rate limit
|
231
|
+
t = time.time()
|
232
|
+
if (t - self.t) < self.rate_limit:
|
233
|
+
# Time is under rate limiter, wait to send
|
234
|
+
return
|
235
|
+
|
236
|
+
# Time is over rate limiter, send now
|
237
|
+
data = {"client_id": SETTINGS["uuid"], "events": self.events} # SHA-256 anonymized UUID hash and events list
|
238
|
+
|
239
|
+
# POST equivalent to requests.post(self.url, json=data)
|
240
|
+
smart_request("post", self.url, json=data, retry=0, verbose=False)
|
241
|
+
|
242
|
+
# Reset events and rate limit timer
|
243
|
+
self.events = []
|
244
|
+
self.t = t
|
245
|
+
|
246
|
+
|
247
|
+
# Run below code on hub/utils init -------------------------------------------------------------------------------------
|
248
|
+
events = Events()
|