ultralytics-opencv-headless 8.3.242__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.
Files changed (298) hide show
  1. tests/__init__.py +23 -0
  2. tests/conftest.py +59 -0
  3. tests/test_cli.py +131 -0
  4. tests/test_cuda.py +216 -0
  5. tests/test_engine.py +157 -0
  6. tests/test_exports.py +309 -0
  7. tests/test_integrations.py +151 -0
  8. tests/test_python.py +777 -0
  9. tests/test_solutions.py +371 -0
  10. ultralytics/__init__.py +48 -0
  11. ultralytics/assets/bus.jpg +0 -0
  12. ultralytics/assets/zidane.jpg +0 -0
  13. ultralytics/cfg/__init__.py +1026 -0
  14. ultralytics/cfg/datasets/Argoverse.yaml +78 -0
  15. ultralytics/cfg/datasets/DOTAv1.5.yaml +37 -0
  16. ultralytics/cfg/datasets/DOTAv1.yaml +36 -0
  17. ultralytics/cfg/datasets/GlobalWheat2020.yaml +68 -0
  18. ultralytics/cfg/datasets/HomeObjects-3K.yaml +32 -0
  19. ultralytics/cfg/datasets/ImageNet.yaml +2025 -0
  20. ultralytics/cfg/datasets/Objects365.yaml +447 -0
  21. ultralytics/cfg/datasets/SKU-110K.yaml +58 -0
  22. ultralytics/cfg/datasets/VOC.yaml +102 -0
  23. ultralytics/cfg/datasets/VisDrone.yaml +87 -0
  24. ultralytics/cfg/datasets/african-wildlife.yaml +25 -0
  25. ultralytics/cfg/datasets/brain-tumor.yaml +22 -0
  26. ultralytics/cfg/datasets/carparts-seg.yaml +44 -0
  27. ultralytics/cfg/datasets/coco-pose.yaml +64 -0
  28. ultralytics/cfg/datasets/coco.yaml +118 -0
  29. ultralytics/cfg/datasets/coco128-seg.yaml +101 -0
  30. ultralytics/cfg/datasets/coco128.yaml +101 -0
  31. ultralytics/cfg/datasets/coco8-grayscale.yaml +103 -0
  32. ultralytics/cfg/datasets/coco8-multispectral.yaml +104 -0
  33. ultralytics/cfg/datasets/coco8-pose.yaml +47 -0
  34. ultralytics/cfg/datasets/coco8-seg.yaml +101 -0
  35. ultralytics/cfg/datasets/coco8.yaml +101 -0
  36. ultralytics/cfg/datasets/construction-ppe.yaml +32 -0
  37. ultralytics/cfg/datasets/crack-seg.yaml +22 -0
  38. ultralytics/cfg/datasets/dog-pose.yaml +52 -0
  39. ultralytics/cfg/datasets/dota8-multispectral.yaml +38 -0
  40. ultralytics/cfg/datasets/dota8.yaml +35 -0
  41. ultralytics/cfg/datasets/hand-keypoints.yaml +50 -0
  42. ultralytics/cfg/datasets/kitti.yaml +27 -0
  43. ultralytics/cfg/datasets/lvis.yaml +1240 -0
  44. ultralytics/cfg/datasets/medical-pills.yaml +21 -0
  45. ultralytics/cfg/datasets/open-images-v7.yaml +663 -0
  46. ultralytics/cfg/datasets/package-seg.yaml +22 -0
  47. ultralytics/cfg/datasets/signature.yaml +21 -0
  48. ultralytics/cfg/datasets/tiger-pose.yaml +41 -0
  49. ultralytics/cfg/datasets/xView.yaml +155 -0
  50. ultralytics/cfg/default.yaml +130 -0
  51. ultralytics/cfg/models/11/yolo11-cls-resnet18.yaml +17 -0
  52. ultralytics/cfg/models/11/yolo11-cls.yaml +33 -0
  53. ultralytics/cfg/models/11/yolo11-obb.yaml +50 -0
  54. ultralytics/cfg/models/11/yolo11-pose.yaml +51 -0
  55. ultralytics/cfg/models/11/yolo11-seg.yaml +50 -0
  56. ultralytics/cfg/models/11/yolo11.yaml +50 -0
  57. ultralytics/cfg/models/11/yoloe-11-seg.yaml +48 -0
  58. ultralytics/cfg/models/11/yoloe-11.yaml +48 -0
  59. ultralytics/cfg/models/12/yolo12-cls.yaml +32 -0
  60. ultralytics/cfg/models/12/yolo12-obb.yaml +48 -0
  61. ultralytics/cfg/models/12/yolo12-pose.yaml +49 -0
  62. ultralytics/cfg/models/12/yolo12-seg.yaml +48 -0
  63. ultralytics/cfg/models/12/yolo12.yaml +48 -0
  64. ultralytics/cfg/models/rt-detr/rtdetr-l.yaml +53 -0
  65. ultralytics/cfg/models/rt-detr/rtdetr-resnet101.yaml +45 -0
  66. ultralytics/cfg/models/rt-detr/rtdetr-resnet50.yaml +45 -0
  67. ultralytics/cfg/models/rt-detr/rtdetr-x.yaml +57 -0
  68. ultralytics/cfg/models/v10/yolov10b.yaml +45 -0
  69. ultralytics/cfg/models/v10/yolov10l.yaml +45 -0
  70. ultralytics/cfg/models/v10/yolov10m.yaml +45 -0
  71. ultralytics/cfg/models/v10/yolov10n.yaml +45 -0
  72. ultralytics/cfg/models/v10/yolov10s.yaml +45 -0
  73. ultralytics/cfg/models/v10/yolov10x.yaml +45 -0
  74. ultralytics/cfg/models/v3/yolov3-spp.yaml +49 -0
  75. ultralytics/cfg/models/v3/yolov3-tiny.yaml +40 -0
  76. ultralytics/cfg/models/v3/yolov3.yaml +49 -0
  77. ultralytics/cfg/models/v5/yolov5-p6.yaml +62 -0
  78. ultralytics/cfg/models/v5/yolov5.yaml +51 -0
  79. ultralytics/cfg/models/v6/yolov6.yaml +56 -0
  80. ultralytics/cfg/models/v8/yoloe-v8-seg.yaml +48 -0
  81. ultralytics/cfg/models/v8/yoloe-v8.yaml +48 -0
  82. ultralytics/cfg/models/v8/yolov8-cls-resnet101.yaml +28 -0
  83. ultralytics/cfg/models/v8/yolov8-cls-resnet50.yaml +28 -0
  84. ultralytics/cfg/models/v8/yolov8-cls.yaml +32 -0
  85. ultralytics/cfg/models/v8/yolov8-ghost-p2.yaml +58 -0
  86. ultralytics/cfg/models/v8/yolov8-ghost-p6.yaml +60 -0
  87. ultralytics/cfg/models/v8/yolov8-ghost.yaml +50 -0
  88. ultralytics/cfg/models/v8/yolov8-obb.yaml +49 -0
  89. ultralytics/cfg/models/v8/yolov8-p2.yaml +57 -0
  90. ultralytics/cfg/models/v8/yolov8-p6.yaml +59 -0
  91. ultralytics/cfg/models/v8/yolov8-pose-p6.yaml +60 -0
  92. ultralytics/cfg/models/v8/yolov8-pose.yaml +50 -0
  93. ultralytics/cfg/models/v8/yolov8-rtdetr.yaml +49 -0
  94. ultralytics/cfg/models/v8/yolov8-seg-p6.yaml +59 -0
  95. ultralytics/cfg/models/v8/yolov8-seg.yaml +49 -0
  96. ultralytics/cfg/models/v8/yolov8-world.yaml +51 -0
  97. ultralytics/cfg/models/v8/yolov8-worldv2.yaml +49 -0
  98. ultralytics/cfg/models/v8/yolov8.yaml +49 -0
  99. ultralytics/cfg/models/v9/yolov9c-seg.yaml +41 -0
  100. ultralytics/cfg/models/v9/yolov9c.yaml +41 -0
  101. ultralytics/cfg/models/v9/yolov9e-seg.yaml +64 -0
  102. ultralytics/cfg/models/v9/yolov9e.yaml +64 -0
  103. ultralytics/cfg/models/v9/yolov9m.yaml +41 -0
  104. ultralytics/cfg/models/v9/yolov9s.yaml +41 -0
  105. ultralytics/cfg/models/v9/yolov9t.yaml +41 -0
  106. ultralytics/cfg/trackers/botsort.yaml +21 -0
  107. ultralytics/cfg/trackers/bytetrack.yaml +12 -0
  108. ultralytics/data/__init__.py +26 -0
  109. ultralytics/data/annotator.py +66 -0
  110. ultralytics/data/augment.py +2801 -0
  111. ultralytics/data/base.py +435 -0
  112. ultralytics/data/build.py +437 -0
  113. ultralytics/data/converter.py +855 -0
  114. ultralytics/data/dataset.py +834 -0
  115. ultralytics/data/loaders.py +704 -0
  116. ultralytics/data/scripts/download_weights.sh +18 -0
  117. ultralytics/data/scripts/get_coco.sh +61 -0
  118. ultralytics/data/scripts/get_coco128.sh +18 -0
  119. ultralytics/data/scripts/get_imagenet.sh +52 -0
  120. ultralytics/data/split.py +138 -0
  121. ultralytics/data/split_dota.py +344 -0
  122. ultralytics/data/utils.py +798 -0
  123. ultralytics/engine/__init__.py +1 -0
  124. ultralytics/engine/exporter.py +1574 -0
  125. ultralytics/engine/model.py +1124 -0
  126. ultralytics/engine/predictor.py +508 -0
  127. ultralytics/engine/results.py +1522 -0
  128. ultralytics/engine/trainer.py +974 -0
  129. ultralytics/engine/tuner.py +448 -0
  130. ultralytics/engine/validator.py +384 -0
  131. ultralytics/hub/__init__.py +166 -0
  132. ultralytics/hub/auth.py +151 -0
  133. ultralytics/hub/google/__init__.py +174 -0
  134. ultralytics/hub/session.py +422 -0
  135. ultralytics/hub/utils.py +162 -0
  136. ultralytics/models/__init__.py +9 -0
  137. ultralytics/models/fastsam/__init__.py +7 -0
  138. ultralytics/models/fastsam/model.py +79 -0
  139. ultralytics/models/fastsam/predict.py +169 -0
  140. ultralytics/models/fastsam/utils.py +23 -0
  141. ultralytics/models/fastsam/val.py +38 -0
  142. ultralytics/models/nas/__init__.py +7 -0
  143. ultralytics/models/nas/model.py +98 -0
  144. ultralytics/models/nas/predict.py +56 -0
  145. ultralytics/models/nas/val.py +38 -0
  146. ultralytics/models/rtdetr/__init__.py +7 -0
  147. ultralytics/models/rtdetr/model.py +63 -0
  148. ultralytics/models/rtdetr/predict.py +88 -0
  149. ultralytics/models/rtdetr/train.py +89 -0
  150. ultralytics/models/rtdetr/val.py +216 -0
  151. ultralytics/models/sam/__init__.py +25 -0
  152. ultralytics/models/sam/amg.py +275 -0
  153. ultralytics/models/sam/build.py +365 -0
  154. ultralytics/models/sam/build_sam3.py +377 -0
  155. ultralytics/models/sam/model.py +169 -0
  156. ultralytics/models/sam/modules/__init__.py +1 -0
  157. ultralytics/models/sam/modules/blocks.py +1067 -0
  158. ultralytics/models/sam/modules/decoders.py +495 -0
  159. ultralytics/models/sam/modules/encoders.py +794 -0
  160. ultralytics/models/sam/modules/memory_attention.py +298 -0
  161. ultralytics/models/sam/modules/sam.py +1160 -0
  162. ultralytics/models/sam/modules/tiny_encoder.py +979 -0
  163. ultralytics/models/sam/modules/transformer.py +344 -0
  164. ultralytics/models/sam/modules/utils.py +512 -0
  165. ultralytics/models/sam/predict.py +3940 -0
  166. ultralytics/models/sam/sam3/__init__.py +3 -0
  167. ultralytics/models/sam/sam3/decoder.py +546 -0
  168. ultralytics/models/sam/sam3/encoder.py +529 -0
  169. ultralytics/models/sam/sam3/geometry_encoders.py +415 -0
  170. ultralytics/models/sam/sam3/maskformer_segmentation.py +286 -0
  171. ultralytics/models/sam/sam3/model_misc.py +199 -0
  172. ultralytics/models/sam/sam3/necks.py +129 -0
  173. ultralytics/models/sam/sam3/sam3_image.py +339 -0
  174. ultralytics/models/sam/sam3/text_encoder_ve.py +307 -0
  175. ultralytics/models/sam/sam3/vitdet.py +547 -0
  176. ultralytics/models/sam/sam3/vl_combiner.py +160 -0
  177. ultralytics/models/utils/__init__.py +1 -0
  178. ultralytics/models/utils/loss.py +466 -0
  179. ultralytics/models/utils/ops.py +315 -0
  180. ultralytics/models/yolo/__init__.py +7 -0
  181. ultralytics/models/yolo/classify/__init__.py +7 -0
  182. ultralytics/models/yolo/classify/predict.py +90 -0
  183. ultralytics/models/yolo/classify/train.py +202 -0
  184. ultralytics/models/yolo/classify/val.py +216 -0
  185. ultralytics/models/yolo/detect/__init__.py +7 -0
  186. ultralytics/models/yolo/detect/predict.py +122 -0
  187. ultralytics/models/yolo/detect/train.py +227 -0
  188. ultralytics/models/yolo/detect/val.py +507 -0
  189. ultralytics/models/yolo/model.py +430 -0
  190. ultralytics/models/yolo/obb/__init__.py +7 -0
  191. ultralytics/models/yolo/obb/predict.py +56 -0
  192. ultralytics/models/yolo/obb/train.py +79 -0
  193. ultralytics/models/yolo/obb/val.py +302 -0
  194. ultralytics/models/yolo/pose/__init__.py +7 -0
  195. ultralytics/models/yolo/pose/predict.py +65 -0
  196. ultralytics/models/yolo/pose/train.py +110 -0
  197. ultralytics/models/yolo/pose/val.py +248 -0
  198. ultralytics/models/yolo/segment/__init__.py +7 -0
  199. ultralytics/models/yolo/segment/predict.py +109 -0
  200. ultralytics/models/yolo/segment/train.py +69 -0
  201. ultralytics/models/yolo/segment/val.py +307 -0
  202. ultralytics/models/yolo/world/__init__.py +5 -0
  203. ultralytics/models/yolo/world/train.py +173 -0
  204. ultralytics/models/yolo/world/train_world.py +178 -0
  205. ultralytics/models/yolo/yoloe/__init__.py +22 -0
  206. ultralytics/models/yolo/yoloe/predict.py +162 -0
  207. ultralytics/models/yolo/yoloe/train.py +287 -0
  208. ultralytics/models/yolo/yoloe/train_seg.py +122 -0
  209. ultralytics/models/yolo/yoloe/val.py +206 -0
  210. ultralytics/nn/__init__.py +27 -0
  211. ultralytics/nn/autobackend.py +958 -0
  212. ultralytics/nn/modules/__init__.py +182 -0
  213. ultralytics/nn/modules/activation.py +54 -0
  214. ultralytics/nn/modules/block.py +1947 -0
  215. ultralytics/nn/modules/conv.py +669 -0
  216. ultralytics/nn/modules/head.py +1183 -0
  217. ultralytics/nn/modules/transformer.py +793 -0
  218. ultralytics/nn/modules/utils.py +159 -0
  219. ultralytics/nn/tasks.py +1768 -0
  220. ultralytics/nn/text_model.py +356 -0
  221. ultralytics/py.typed +1 -0
  222. ultralytics/solutions/__init__.py +41 -0
  223. ultralytics/solutions/ai_gym.py +108 -0
  224. ultralytics/solutions/analytics.py +264 -0
  225. ultralytics/solutions/config.py +107 -0
  226. ultralytics/solutions/distance_calculation.py +123 -0
  227. ultralytics/solutions/heatmap.py +125 -0
  228. ultralytics/solutions/instance_segmentation.py +86 -0
  229. ultralytics/solutions/object_blurrer.py +89 -0
  230. ultralytics/solutions/object_counter.py +190 -0
  231. ultralytics/solutions/object_cropper.py +87 -0
  232. ultralytics/solutions/parking_management.py +280 -0
  233. ultralytics/solutions/queue_management.py +93 -0
  234. ultralytics/solutions/region_counter.py +133 -0
  235. ultralytics/solutions/security_alarm.py +151 -0
  236. ultralytics/solutions/similarity_search.py +219 -0
  237. ultralytics/solutions/solutions.py +828 -0
  238. ultralytics/solutions/speed_estimation.py +114 -0
  239. ultralytics/solutions/streamlit_inference.py +260 -0
  240. ultralytics/solutions/templates/similarity-search.html +156 -0
  241. ultralytics/solutions/trackzone.py +88 -0
  242. ultralytics/solutions/vision_eye.py +67 -0
  243. ultralytics/trackers/__init__.py +7 -0
  244. ultralytics/trackers/basetrack.py +115 -0
  245. ultralytics/trackers/bot_sort.py +257 -0
  246. ultralytics/trackers/byte_tracker.py +469 -0
  247. ultralytics/trackers/track.py +116 -0
  248. ultralytics/trackers/utils/__init__.py +1 -0
  249. ultralytics/trackers/utils/gmc.py +339 -0
  250. ultralytics/trackers/utils/kalman_filter.py +482 -0
  251. ultralytics/trackers/utils/matching.py +154 -0
  252. ultralytics/utils/__init__.py +1450 -0
  253. ultralytics/utils/autobatch.py +118 -0
  254. ultralytics/utils/autodevice.py +205 -0
  255. ultralytics/utils/benchmarks.py +728 -0
  256. ultralytics/utils/callbacks/__init__.py +5 -0
  257. ultralytics/utils/callbacks/base.py +233 -0
  258. ultralytics/utils/callbacks/clearml.py +146 -0
  259. ultralytics/utils/callbacks/comet.py +625 -0
  260. ultralytics/utils/callbacks/dvc.py +197 -0
  261. ultralytics/utils/callbacks/hub.py +110 -0
  262. ultralytics/utils/callbacks/mlflow.py +134 -0
  263. ultralytics/utils/callbacks/neptune.py +126 -0
  264. ultralytics/utils/callbacks/platform.py +73 -0
  265. ultralytics/utils/callbacks/raytune.py +42 -0
  266. ultralytics/utils/callbacks/tensorboard.py +123 -0
  267. ultralytics/utils/callbacks/wb.py +188 -0
  268. ultralytics/utils/checks.py +998 -0
  269. ultralytics/utils/cpu.py +85 -0
  270. ultralytics/utils/dist.py +123 -0
  271. ultralytics/utils/downloads.py +529 -0
  272. ultralytics/utils/errors.py +35 -0
  273. ultralytics/utils/events.py +113 -0
  274. ultralytics/utils/export/__init__.py +7 -0
  275. ultralytics/utils/export/engine.py +237 -0
  276. ultralytics/utils/export/imx.py +315 -0
  277. ultralytics/utils/export/tensorflow.py +231 -0
  278. ultralytics/utils/files.py +219 -0
  279. ultralytics/utils/git.py +137 -0
  280. ultralytics/utils/instance.py +484 -0
  281. ultralytics/utils/logger.py +444 -0
  282. ultralytics/utils/loss.py +849 -0
  283. ultralytics/utils/metrics.py +1560 -0
  284. ultralytics/utils/nms.py +337 -0
  285. ultralytics/utils/ops.py +664 -0
  286. ultralytics/utils/patches.py +201 -0
  287. ultralytics/utils/plotting.py +1045 -0
  288. ultralytics/utils/tal.py +403 -0
  289. ultralytics/utils/torch_utils.py +984 -0
  290. ultralytics/utils/tqdm.py +440 -0
  291. ultralytics/utils/triton.py +112 -0
  292. ultralytics/utils/tuner.py +160 -0
  293. ultralytics_opencv_headless-8.3.242.dist-info/METADATA +374 -0
  294. ultralytics_opencv_headless-8.3.242.dist-info/RECORD +298 -0
  295. ultralytics_opencv_headless-8.3.242.dist-info/WHEEL +5 -0
  296. ultralytics_opencv_headless-8.3.242.dist-info/entry_points.txt +3 -0
  297. ultralytics_opencv_headless-8.3.242.dist-info/licenses/LICENSE +661 -0
  298. ultralytics_opencv_headless-8.3.242.dist-info/top_level.txt +1 -0
@@ -0,0 +1,444 @@
1
+ # Ultralytics 🚀 AGPL-3.0 License - https://ultralytics.com/license
2
+
3
+ import logging
4
+ import queue
5
+ import shutil
6
+ import sys
7
+ import threading
8
+ import time
9
+ from datetime import datetime
10
+ from pathlib import Path
11
+
12
+ from ultralytics.utils import MACOS, RANK
13
+ from ultralytics.utils.checks import check_requirements
14
+
15
+ # Initialize default log file
16
+ DEFAULT_LOG_PATH = Path("train.log")
17
+ if RANK in {-1, 0} and DEFAULT_LOG_PATH.exists():
18
+ DEFAULT_LOG_PATH.unlink(missing_ok=True)
19
+
20
+
21
+ class ConsoleLogger:
22
+ """Console output capture with API/file streaming and deduplication.
23
+
24
+ Captures stdout/stderr output and streams it to either an API endpoint or local file, with intelligent deduplication
25
+ to reduce noise from repetitive console output.
26
+
27
+ Attributes:
28
+ destination (str | Path): Target destination for streaming (URL or Path object).
29
+ is_api (bool): Whether destination is an API endpoint (True) or local file (False).
30
+ original_stdout: Reference to original sys.stdout for restoration.
31
+ original_stderr: Reference to original sys.stderr for restoration.
32
+ log_queue (queue.Queue): Thread-safe queue for buffering log messages.
33
+ active (bool): Whether console capture is currently active.
34
+ worker_thread (threading.Thread): Background thread for processing log queue.
35
+ last_line (str): Last processed line for deduplication.
36
+ last_time (float): Timestamp of last processed line.
37
+ last_progress_line (str): Last progress bar line for progress deduplication.
38
+ last_was_progress (bool): Whether the last line was a progress bar.
39
+
40
+ Examples:
41
+ Basic file logging:
42
+ >>> logger = ConsoleLogger("training.log")
43
+ >>> logger.start_capture()
44
+ >>> print("This will be logged")
45
+ >>> logger.stop_capture()
46
+
47
+ API streaming:
48
+ >>> logger = ConsoleLogger("https://api.example.com/logs")
49
+ >>> logger.start_capture()
50
+ >>> # All output streams to API
51
+ >>> logger.stop_capture()
52
+ """
53
+
54
+ def __init__(self, destination):
55
+ """Initialize with API endpoint or local file path.
56
+
57
+ Args:
58
+ destination (str | Path): API endpoint URL (http/https) or local file path for streaming output.
59
+ """
60
+ self.destination = destination
61
+ self.is_api = isinstance(destination, str) and destination.startswith(("http://", "https://"))
62
+ if not self.is_api:
63
+ self.destination = Path(destination)
64
+
65
+ # Console capture
66
+ self.original_stdout = sys.stdout
67
+ self.original_stderr = sys.stderr
68
+ self.log_queue = queue.Queue(maxsize=1000)
69
+ self.active = False
70
+ self.worker_thread = None
71
+
72
+ # State tracking
73
+ self.last_line = ""
74
+ self.last_time = 0.0
75
+ self.last_progress_line = "" # Track last progress line for deduplication
76
+ self.last_was_progress = False # Track if last line was a progress bar
77
+
78
+ def start_capture(self):
79
+ """Start capturing console output and redirect stdout/stderr to custom capture objects."""
80
+ if self.active:
81
+ return
82
+
83
+ self.active = True
84
+ sys.stdout = self._ConsoleCapture(self.original_stdout, self._queue_log)
85
+ sys.stderr = self._ConsoleCapture(self.original_stderr, self._queue_log)
86
+
87
+ # Hook Ultralytics logger
88
+ try:
89
+ handler = self._LogHandler(self._queue_log)
90
+ logging.getLogger("ultralytics").addHandler(handler)
91
+ except Exception:
92
+ pass
93
+
94
+ self.worker_thread = threading.Thread(target=self._stream_worker, daemon=True)
95
+ self.worker_thread.start()
96
+
97
+ def stop_capture(self):
98
+ """Stop capturing console output and restore original stdout/stderr."""
99
+ if not self.active:
100
+ return
101
+
102
+ self.active = False
103
+ sys.stdout = self.original_stdout
104
+ sys.stderr = self.original_stderr
105
+ self.log_queue.put(None)
106
+
107
+ def _queue_log(self, text):
108
+ """Queue console text with deduplication and timestamp processing."""
109
+ if not self.active:
110
+ return
111
+
112
+ current_time = time.time()
113
+
114
+ # Handle carriage returns and process lines
115
+ if "\r" in text:
116
+ text = text.split("\r")[-1]
117
+
118
+ lines = text.split("\n")
119
+ if lines and lines[-1] == "":
120
+ lines.pop()
121
+
122
+ for line in lines:
123
+ line = line.rstrip()
124
+
125
+ # Skip lines with only thin progress bars (partial progress)
126
+ if "─" in line: # Has thin lines but no thick lines
127
+ continue
128
+
129
+ # Deduplicate completed progress bars only if they match the previous progress line
130
+ if " ━━" in line:
131
+ progress_core = line.split(" ━━")[0].strip()
132
+ if progress_core == self.last_progress_line and self.last_was_progress:
133
+ continue
134
+ self.last_progress_line = progress_core
135
+ self.last_was_progress = True
136
+ else:
137
+ # Skip empty line after progress bar
138
+ if not line and self.last_was_progress:
139
+ self.last_was_progress = False
140
+ continue
141
+ self.last_was_progress = False
142
+
143
+ # General deduplication
144
+ if line == self.last_line and current_time - self.last_time < 0.1:
145
+ continue
146
+
147
+ self.last_line = line
148
+ self.last_time = current_time
149
+
150
+ # Add timestamp if needed
151
+ if not line.startswith("[20"):
152
+ timestamp = datetime.now().strftime("%Y-%m-%d %H:%M:%S")
153
+ line = f"[{timestamp}] {line}"
154
+
155
+ # Queue with overflow protection
156
+ if not self._safe_put(f"{line}\n"):
157
+ continue # Skip if queue handling fails
158
+
159
+ def _safe_put(self, item):
160
+ """Safely put item in queue with overflow handling."""
161
+ try:
162
+ self.log_queue.put_nowait(item)
163
+ return True
164
+ except queue.Full:
165
+ try:
166
+ self.log_queue.get_nowait() # Drop oldest
167
+ self.log_queue.put_nowait(item)
168
+ return True
169
+ except queue.Empty:
170
+ return False
171
+
172
+ def _stream_worker(self):
173
+ """Background worker for streaming logs to destination."""
174
+ while self.active:
175
+ try:
176
+ log_text = self.log_queue.get(timeout=1)
177
+ if log_text is None:
178
+ break
179
+ self._write_log(log_text)
180
+ except queue.Empty:
181
+ continue
182
+
183
+ def _write_log(self, text):
184
+ """Write log to API endpoint or local file destination."""
185
+ try:
186
+ if self.is_api:
187
+ import requests # scoped as slow import
188
+
189
+ payload = {"timestamp": datetime.now().isoformat(), "message": text.strip()}
190
+ requests.post(str(self.destination), json=payload, timeout=5)
191
+ else:
192
+ self.destination.parent.mkdir(parents=True, exist_ok=True)
193
+ with self.destination.open("a", encoding="utf-8") as f:
194
+ f.write(text)
195
+ except Exception as e:
196
+ print(f"Platform logging error: {e}", file=self.original_stderr)
197
+
198
+ class _ConsoleCapture:
199
+ """Lightweight stdout/stderr capture."""
200
+
201
+ __slots__ = ("callback", "original")
202
+
203
+ def __init__(self, original, callback):
204
+ """Initialize a stream wrapper that redirects writes to a callback while preserving the original."""
205
+ self.original = original
206
+ self.callback = callback
207
+
208
+ def write(self, text):
209
+ """Forward text to the wrapped original stream, preserving default stdout/stderr semantics."""
210
+ self.original.write(text)
211
+ self.callback(text)
212
+
213
+ def flush(self):
214
+ """Flush the wrapped stream to propagate buffered output promptly during console capture."""
215
+ self.original.flush()
216
+
217
+ class _LogHandler(logging.Handler):
218
+ """Lightweight logging handler."""
219
+
220
+ __slots__ = ("callback",)
221
+
222
+ def __init__(self, callback):
223
+ """Initialize a lightweight logging.Handler that forwards log records to the provided callback."""
224
+ super().__init__()
225
+ self.callback = callback
226
+
227
+ def emit(self, record):
228
+ """Format and forward LogRecord messages to the capture callback for unified log streaming."""
229
+ self.callback(self.format(record) + "\n")
230
+
231
+
232
+ class SystemLogger:
233
+ """Log dynamic system metrics for training monitoring.
234
+
235
+ Captures real-time system metrics including CPU, RAM, disk I/O, network I/O, and NVIDIA GPU statistics for training
236
+ performance monitoring and analysis.
237
+
238
+ Attributes:
239
+ pynvml: NVIDIA pynvml module instance if successfully imported, None otherwise.
240
+ nvidia_initialized (bool): Whether NVIDIA GPU monitoring is available and initialized.
241
+ net_start: Initial network I/O counters for calculating cumulative usage.
242
+ disk_start: Initial disk I/O counters for calculating cumulative usage.
243
+
244
+ Examples:
245
+ Basic usage:
246
+ >>> logger = SystemLogger()
247
+ >>> metrics = logger.get_metrics()
248
+ >>> print(f"CPU: {metrics['cpu']}%, RAM: {metrics['ram']}%")
249
+ >>> if metrics["gpus"]:
250
+ ... gpu0 = metrics["gpus"]["0"]
251
+ ... print(f"GPU0: {gpu0['usage']}% usage, {gpu0['temp']}°C")
252
+
253
+ Training loop integration:
254
+ >>> system_logger = SystemLogger()
255
+ >>> for epoch in range(epochs):
256
+ ... # Training code here
257
+ ... metrics = system_logger.get_metrics()
258
+ ... # Log to database/file
259
+ """
260
+
261
+ def __init__(self):
262
+ """Initialize the system logger."""
263
+ import psutil # scoped as slow import
264
+
265
+ self.pynvml = None
266
+ self.nvidia_initialized = self._init_nvidia()
267
+ self.net_start = psutil.net_io_counters()
268
+ self.disk_start = psutil.disk_io_counters()
269
+
270
+ # For rate calculation
271
+ self._prev_net = self.net_start
272
+ self._prev_disk = self.disk_start
273
+ self._prev_time = time.time()
274
+
275
+ def _init_nvidia(self):
276
+ """Initialize NVIDIA GPU monitoring with pynvml."""
277
+ try:
278
+ assert not MACOS
279
+ check_requirements("nvidia-ml-py>=12.0.0")
280
+ self.pynvml = __import__("pynvml")
281
+ self.pynvml.nvmlInit()
282
+ return True
283
+ except Exception:
284
+ return False
285
+
286
+ def get_metrics(self, rates=False):
287
+ """Get current system metrics including CPU, RAM, disk, network, and GPU usage.
288
+
289
+ Collects comprehensive system metrics including CPU usage, RAM usage, disk I/O statistics, network I/O
290
+ statistics, and GPU metrics (if available).
291
+
292
+ Example output (rates=False, default):
293
+ ```python
294
+ {
295
+ "cpu": 45.2,
296
+ "ram": 78.9,
297
+ "disk": {"read_mb": 156.7, "write_mb": 89.3, "used_gb": 256.8},
298
+ "network": {"recv_mb": 157.2, "sent_mb": 89.1},
299
+ "gpus": {
300
+ "0": {"usage": 95.6, "memory": 85.4, "temp": 72, "power": 285},
301
+ "1": {"usage": 94.1, "memory": 82.7, "temp": 70, "power": 278},
302
+ },
303
+ }
304
+ ```
305
+
306
+ Example output (rates=True):
307
+ ```python
308
+ {
309
+ "cpu": 45.2,
310
+ "ram": 78.9,
311
+ "disk": {"read_mbs": 12.5, "write_mbs": 8.3, "used_gb": 256.8},
312
+ "network": {"recv_mbs": 5.2, "sent_mbs": 1.1},
313
+ "gpus": {
314
+ "0": {"usage": 95.6, "memory": 85.4, "temp": 72, "power": 285},
315
+ },
316
+ }
317
+ ```
318
+
319
+ Args:
320
+ rates (bool): If True, return disk/network as MB/s rates instead of cumulative MB.
321
+
322
+ Returns:
323
+ (dict): Metrics dictionary with cpu, ram, disk, network, and gpus keys.
324
+
325
+ Examples:
326
+ >>> logger = SystemLogger()
327
+ >>> logger.get_metrics()["cpu"] # CPU percentage
328
+ >>> logger.get_metrics(rates=True)["network"]["recv_mbs"] # MB/s download rate
329
+ """
330
+ import psutil # scoped as slow import
331
+
332
+ net = psutil.net_io_counters()
333
+ disk = psutil.disk_io_counters()
334
+ memory = psutil.virtual_memory()
335
+ disk_usage = shutil.disk_usage("/")
336
+ now = time.time()
337
+
338
+ metrics = {
339
+ "cpu": round(psutil.cpu_percent(), 3),
340
+ "ram": round(memory.percent, 3),
341
+ "gpus": {},
342
+ }
343
+
344
+ # Calculate elapsed time since last call
345
+ elapsed = max(0.1, now - self._prev_time) # Avoid division by zero
346
+
347
+ if rates:
348
+ # Calculate MB/s rates from delta since last call
349
+ metrics["disk"] = {
350
+ "read_mbs": round(max(0, (disk.read_bytes - self._prev_disk.read_bytes) / (1 << 20) / elapsed), 3),
351
+ "write_mbs": round(max(0, (disk.write_bytes - self._prev_disk.write_bytes) / (1 << 20) / elapsed), 3),
352
+ "used_gb": round(disk_usage.used / (1 << 30), 3),
353
+ }
354
+ metrics["network"] = {
355
+ "recv_mbs": round(max(0, (net.bytes_recv - self._prev_net.bytes_recv) / (1 << 20) / elapsed), 3),
356
+ "sent_mbs": round(max(0, (net.bytes_sent - self._prev_net.bytes_sent) / (1 << 20) / elapsed), 3),
357
+ }
358
+ else:
359
+ # Cumulative MB since initialization (original behavior)
360
+ metrics["disk"] = {
361
+ "read_mb": round((disk.read_bytes - self.disk_start.read_bytes) / (1 << 20), 3),
362
+ "write_mb": round((disk.write_bytes - self.disk_start.write_bytes) / (1 << 20), 3),
363
+ "used_gb": round(disk_usage.used / (1 << 30), 3),
364
+ }
365
+ metrics["network"] = {
366
+ "recv_mb": round((net.bytes_recv - self.net_start.bytes_recv) / (1 << 20), 3),
367
+ "sent_mb": round((net.bytes_sent - self.net_start.bytes_sent) / (1 << 20), 3),
368
+ }
369
+
370
+ # Always update previous values for accurate rate calculation on next call
371
+ self._prev_net = net
372
+ self._prev_disk = disk
373
+ self._prev_time = now
374
+
375
+ # Add GPU metrics (NVIDIA only)
376
+ if self.nvidia_initialized:
377
+ metrics["gpus"].update(self._get_nvidia_metrics())
378
+
379
+ return metrics
380
+
381
+ def _get_nvidia_metrics(self):
382
+ """Get NVIDIA GPU metrics including utilization, memory, temperature, and power."""
383
+ gpus = {}
384
+ if not self.nvidia_initialized or not self.pynvml:
385
+ return gpus
386
+ try:
387
+ device_count = self.pynvml.nvmlDeviceGetCount()
388
+ for i in range(device_count):
389
+ handle = self.pynvml.nvmlDeviceGetHandleByIndex(i)
390
+ util = self.pynvml.nvmlDeviceGetUtilizationRates(handle)
391
+ memory = self.pynvml.nvmlDeviceGetMemoryInfo(handle)
392
+ temp = self.pynvml.nvmlDeviceGetTemperature(handle, self.pynvml.NVML_TEMPERATURE_GPU)
393
+ power = self.pynvml.nvmlDeviceGetPowerUsage(handle) // 1000
394
+
395
+ gpus[str(i)] = {
396
+ "usage": round(util.gpu, 3),
397
+ "memory": round((memory.used / memory.total) * 100, 3),
398
+ "temp": temp,
399
+ "power": power,
400
+ }
401
+ except Exception:
402
+ pass
403
+ return gpus
404
+
405
+
406
+ if __name__ == "__main__":
407
+ print("SystemLogger Real-time Metrics Monitor")
408
+ print("Press Ctrl+C to stop\n")
409
+
410
+ logger = SystemLogger()
411
+
412
+ try:
413
+ while True:
414
+ metrics = logger.get_metrics()
415
+
416
+ # Clear screen (works on most terminals)
417
+ print("\033[H\033[J", end="")
418
+
419
+ # Display system metrics
420
+ print(f"CPU: {metrics['cpu']:5.1f}%")
421
+ print(f"RAM: {metrics['ram']:5.1f}%")
422
+ print(f"Disk Read: {metrics['disk']['read_mb']:8.1f} MB")
423
+ print(f"Disk Write: {metrics['disk']['write_mb']:7.1f} MB")
424
+ print(f"Disk Used: {metrics['disk']['used_gb']:8.1f} GB")
425
+ print(f"Net Recv: {metrics['network']['recv_mb']:9.1f} MB")
426
+ print(f"Net Sent: {metrics['network']['sent_mb']:9.1f} MB")
427
+
428
+ # Display GPU metrics if available
429
+ if metrics["gpus"]:
430
+ print("\nGPU Metrics:")
431
+ for gpu_id, gpu_data in metrics["gpus"].items():
432
+ print(
433
+ f" GPU {gpu_id}: {gpu_data['usage']:3}% | "
434
+ f"Mem: {gpu_data['memory']:5.1f}% | "
435
+ f"Temp: {gpu_data['temp']:2}°C | "
436
+ f"Power: {gpu_data['power']:3}W"
437
+ )
438
+ else:
439
+ print("\nGPU: No NVIDIA GPUs detected")
440
+
441
+ time.sleep(1)
442
+
443
+ except KeyboardInterrupt:
444
+ print("\n\nStopped monitoring.")