ultralytics 8.3.5__py3-none-any.whl → 8.3.7__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.
Potentially problematic release.
This version of ultralytics might be problematic. Click here for more details.
- tests/test_solutions.py +6 -8
- ultralytics/__init__.py +1 -1
- ultralytics/cfg/solutions/default.yaml +4 -0
- ultralytics/data/converter.py +64 -6
- ultralytics/data/explorer/gui/dash.py +4 -17
- ultralytics/engine/exporter.py +3 -4
- ultralytics/engine/model.py +2 -0
- ultralytics/engine/trainer.py +4 -4
- ultralytics/solutions/ai_gym.py +62 -110
- ultralytics/solutions/heatmap.py +63 -219
- ultralytics/solutions/object_counter.py +19 -17
- ultralytics/solutions/solutions.py +9 -4
- ultralytics/utils/__init__.py +47 -46
- ultralytics/utils/autobatch.py +3 -1
- ultralytics/utils/checks.py +36 -20
- ultralytics/utils/plotting.py +50 -70
- ultralytics/utils/torch_utils.py +12 -5
- {ultralytics-8.3.5.dist-info → ultralytics-8.3.7.dist-info}/METADATA +8 -9
- {ultralytics-8.3.5.dist-info → ultralytics-8.3.7.dist-info}/RECORD +23 -23
- {ultralytics-8.3.5.dist-info → ultralytics-8.3.7.dist-info}/LICENSE +0 -0
- {ultralytics-8.3.5.dist-info → ultralytics-8.3.7.dist-info}/WHEEL +0 -0
- {ultralytics-8.3.5.dist-info → ultralytics-8.3.7.dist-info}/entry_points.txt +0 -0
- {ultralytics-8.3.5.dist-info → ultralytics-8.3.7.dist-info}/top_level.txt +0 -0
ultralytics/solutions/heatmap.py
CHANGED
|
@@ -1,249 +1,93 @@
|
|
|
1
1
|
# Ultralytics YOLO 🚀, AGPL-3.0 license
|
|
2
2
|
|
|
3
|
-
from collections import defaultdict
|
|
4
|
-
|
|
5
3
|
import cv2
|
|
6
4
|
import numpy as np
|
|
7
5
|
|
|
8
|
-
from ultralytics.
|
|
6
|
+
from ultralytics.solutions.object_counter import ObjectCounter # Import object counter class
|
|
9
7
|
from ultralytics.utils.plotting import Annotator
|
|
10
8
|
|
|
11
|
-
check_requirements("shapely>=2.0.0")
|
|
12
|
-
|
|
13
|
-
from shapely.geometry import LineString, Point, Polygon
|
|
14
|
-
|
|
15
9
|
|
|
16
|
-
class Heatmap:
|
|
10
|
+
class Heatmap(ObjectCounter):
|
|
17
11
|
"""A class to draw heatmaps in real-time video stream based on their tracks."""
|
|
18
12
|
|
|
19
|
-
def __init__(
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
colormap=cv2.COLORMAP_JET,
|
|
23
|
-
view_img=False,
|
|
24
|
-
view_in_counts=True,
|
|
25
|
-
view_out_counts=True,
|
|
26
|
-
count_reg_pts=None,
|
|
27
|
-
count_txt_color=(0, 0, 0),
|
|
28
|
-
count_bg_color=(255, 255, 255),
|
|
29
|
-
count_reg_color=(255, 0, 255),
|
|
30
|
-
region_thickness=5,
|
|
31
|
-
line_dist_thresh=15,
|
|
32
|
-
line_thickness=2,
|
|
33
|
-
shape="circle",
|
|
34
|
-
):
|
|
35
|
-
"""Initializes the heatmap class with default values for Visual, Image, track, count and heatmap parameters."""
|
|
36
|
-
# Visual information
|
|
37
|
-
self.annotator = None
|
|
38
|
-
self.view_img = view_img
|
|
39
|
-
self.shape = shape
|
|
40
|
-
|
|
41
|
-
self.initialized = False
|
|
42
|
-
self.names = names # Classes names
|
|
43
|
-
|
|
44
|
-
# Image information
|
|
45
|
-
self.im0 = None
|
|
46
|
-
self.tf = line_thickness
|
|
47
|
-
self.view_in_counts = view_in_counts
|
|
48
|
-
self.view_out_counts = view_out_counts
|
|
49
|
-
|
|
50
|
-
# Heatmap colormap and heatmap np array
|
|
51
|
-
self.colormap = colormap
|
|
52
|
-
self.heatmap = None
|
|
53
|
-
|
|
54
|
-
# Predict/track information
|
|
55
|
-
self.boxes = []
|
|
56
|
-
self.track_ids = []
|
|
57
|
-
self.clss = []
|
|
58
|
-
self.track_history = defaultdict(list)
|
|
59
|
-
|
|
60
|
-
# Region & Line Information
|
|
61
|
-
self.counting_region = None
|
|
62
|
-
self.line_dist_thresh = line_dist_thresh
|
|
63
|
-
self.region_thickness = region_thickness
|
|
64
|
-
self.region_color = count_reg_color
|
|
65
|
-
|
|
66
|
-
# Object Counting Information
|
|
67
|
-
self.in_counts = 0
|
|
68
|
-
self.out_counts = 0
|
|
69
|
-
self.count_ids = []
|
|
70
|
-
self.class_wise_count = {}
|
|
71
|
-
self.count_txt_color = count_txt_color
|
|
72
|
-
self.count_bg_color = count_bg_color
|
|
73
|
-
self.cls_txtdisplay_gap = 50
|
|
74
|
-
|
|
75
|
-
# Check if environment supports imshow
|
|
76
|
-
self.env_check = check_imshow(warn=True)
|
|
77
|
-
|
|
78
|
-
# Region and line selection
|
|
79
|
-
self.count_reg_pts = count_reg_pts
|
|
80
|
-
print(self.count_reg_pts)
|
|
81
|
-
if self.count_reg_pts is not None:
|
|
82
|
-
if len(self.count_reg_pts) == 2:
|
|
83
|
-
print("Line Counter Initiated.")
|
|
84
|
-
self.counting_region = LineString(self.count_reg_pts)
|
|
85
|
-
elif len(self.count_reg_pts) >= 3:
|
|
86
|
-
print("Polygon Counter Initiated.")
|
|
87
|
-
self.counting_region = Polygon(self.count_reg_pts)
|
|
88
|
-
else:
|
|
89
|
-
print("Invalid Region points provided, region_points must be 2 for lines or >= 3 for polygons.")
|
|
90
|
-
print("Using Line Counter Now")
|
|
91
|
-
self.counting_region = LineString(self.count_reg_pts)
|
|
92
|
-
|
|
93
|
-
# Shape of heatmap, if not selected
|
|
94
|
-
if self.shape not in {"circle", "rect"}:
|
|
95
|
-
print("Unknown shape value provided, 'circle' & 'rect' supported")
|
|
96
|
-
print("Using Circular shape now")
|
|
97
|
-
self.shape = "circle"
|
|
98
|
-
|
|
99
|
-
def extract_results(self, tracks):
|
|
100
|
-
"""
|
|
101
|
-
Extracts results from the provided data.
|
|
13
|
+
def __init__(self, **kwargs):
|
|
14
|
+
"""Initializes function for heatmap class with default values."""
|
|
15
|
+
super().__init__(**kwargs)
|
|
102
16
|
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
self.track_ids = tracks[0].boxes.id.int().tolist()
|
|
17
|
+
self.initialized = False # bool variable for heatmap initialization
|
|
18
|
+
if self.region is not None: # check if user provided the region coordinates
|
|
19
|
+
self.initialize_region()
|
|
20
|
+
|
|
21
|
+
# store colormap
|
|
22
|
+
self.colormap = cv2.COLORMAP_PARULA if self.CFG["colormap"] is None else self.CFG["colormap"]
|
|
110
23
|
|
|
111
|
-
def
|
|
24
|
+
def heatmap_effect(self, box):
|
|
112
25
|
"""
|
|
113
|
-
|
|
26
|
+
Efficient calculation of heatmap area and effect location for applying colormap.
|
|
114
27
|
|
|
115
28
|
Args:
|
|
116
|
-
|
|
117
|
-
tracks (list): List of tracks obtained from the object tracking process.
|
|
29
|
+
box (list): Bounding Box coordinates data [x0, y0, x1, y1]
|
|
118
30
|
"""
|
|
119
|
-
|
|
31
|
+
x0, y0, x1, y1 = map(int, box)
|
|
32
|
+
radius_squared = (min(x1 - x0, y1 - y0) // 2) ** 2
|
|
120
33
|
|
|
121
|
-
#
|
|
122
|
-
|
|
123
|
-
self.heatmap = np.zeros((int(self.im0.shape[0]), int(self.im0.shape[1])), dtype=np.float32)
|
|
124
|
-
self.initialized = True
|
|
34
|
+
# Create a meshgrid with region of interest (ROI) for vectorized distance calculations
|
|
35
|
+
xv, yv = np.meshgrid(np.arange(x0, x1), np.arange(y0, y1))
|
|
125
36
|
|
|
126
|
-
|
|
37
|
+
# Calculate squared distances from the center
|
|
38
|
+
dist_squared = (xv - ((x0 + x1) // 2)) ** 2 + (yv - ((y0 + y1) // 2)) ** 2
|
|
127
39
|
|
|
128
|
-
|
|
129
|
-
|
|
40
|
+
# Create a mask of points within the radius
|
|
41
|
+
within_radius = dist_squared <= radius_squared
|
|
130
42
|
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
if self.count_reg_pts is not None:
|
|
134
|
-
self.annotator.draw_region(
|
|
135
|
-
reg_pts=self.count_reg_pts, color=self.region_color, thickness=self.region_thickness
|
|
136
|
-
)
|
|
43
|
+
# Update only the values within the bounding box in a single vectorized operation
|
|
44
|
+
self.heatmap[y0:y1, x0:x1][within_radius] += 2
|
|
137
45
|
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
self.class_wise_count[self.names[cls]] = {"IN": 0, "OUT": 0}
|
|
142
|
-
|
|
143
|
-
if self.shape == "circle":
|
|
144
|
-
center = (int((box[0] + box[2]) // 2), int((box[1] + box[3]) // 2))
|
|
145
|
-
radius = min(int(box[2]) - int(box[0]), int(box[3]) - int(box[1])) // 2
|
|
46
|
+
def generate_heatmap(self, im0):
|
|
47
|
+
"""
|
|
48
|
+
Generate heatmap for each frame using Ultralytics.
|
|
146
49
|
|
|
147
|
-
|
|
148
|
-
|
|
50
|
+
Args:
|
|
51
|
+
im0 (ndarray): Input image array for processing
|
|
52
|
+
Returns:
|
|
53
|
+
im0 (ndarray): Processed image for further usage
|
|
54
|
+
"""
|
|
55
|
+
self.heatmap = np.zeros_like(im0, dtype=np.float32) * 0.99 if not self.initialized else self.heatmap
|
|
56
|
+
self.initialized = True # Initialize heatmap only once
|
|
149
57
|
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
)
|
|
58
|
+
self.annotator = Annotator(im0, line_width=self.line_width) # Initialize annotator
|
|
59
|
+
self.extract_tracks(im0) # Extract tracks
|
|
153
60
|
|
|
154
|
-
|
|
155
|
-
|
|
61
|
+
# Iterate over bounding boxes, track ids and classes index
|
|
62
|
+
for box, track_id, cls in zip(self.boxes, self.track_ids, self.clss):
|
|
63
|
+
# Draw bounding box and counting region
|
|
64
|
+
self.heatmap_effect(box)
|
|
156
65
|
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
track_line.pop(0)
|
|
66
|
+
if self.region is not None:
|
|
67
|
+
self.annotator.draw_region(reg_pts=self.region, color=(104, 0, 123), thickness=self.line_width * 2)
|
|
68
|
+
self.store_tracking_history(track_id, box) # Store track history
|
|
69
|
+
self.store_classwise_counts(cls) # store classwise counts in dict
|
|
162
70
|
|
|
71
|
+
# Store tracking previous position and perform object counting
|
|
163
72
|
prev_position = self.track_history[track_id][-2] if len(self.track_history[track_id]) > 1 else None
|
|
73
|
+
self.count_objects(self.track_line, box, track_id, prev_position, cls) # Perform object counting
|
|
164
74
|
|
|
165
|
-
|
|
166
|
-
# Count objects in any polygon
|
|
167
|
-
if len(self.count_reg_pts) >= 3:
|
|
168
|
-
is_inside = self.counting_region.contains(Point(track_line[-1]))
|
|
169
|
-
|
|
170
|
-
if prev_position is not None and is_inside and track_id not in self.count_ids:
|
|
171
|
-
self.count_ids.append(track_id)
|
|
172
|
-
|
|
173
|
-
if (box[0] - prev_position[0]) * (self.counting_region.centroid.x - prev_position[0]) > 0:
|
|
174
|
-
self.in_counts += 1
|
|
175
|
-
self.class_wise_count[self.names[cls]]["IN"] += 1
|
|
176
|
-
else:
|
|
177
|
-
self.out_counts += 1
|
|
178
|
-
self.class_wise_count[self.names[cls]]["OUT"] += 1
|
|
179
|
-
|
|
180
|
-
# Count objects using line
|
|
181
|
-
elif len(self.count_reg_pts) == 2:
|
|
182
|
-
if prev_position is not None and track_id not in self.count_ids:
|
|
183
|
-
distance = Point(track_line[-1]).distance(self.counting_region)
|
|
184
|
-
if distance < self.line_dist_thresh and track_id not in self.count_ids:
|
|
185
|
-
self.count_ids.append(track_id)
|
|
186
|
-
|
|
187
|
-
if (box[0] - prev_position[0]) * (
|
|
188
|
-
self.counting_region.centroid.x - prev_position[0]
|
|
189
|
-
) > 0:
|
|
190
|
-
self.in_counts += 1
|
|
191
|
-
self.class_wise_count[self.names[cls]]["IN"] += 1
|
|
192
|
-
else:
|
|
193
|
-
self.out_counts += 1
|
|
194
|
-
self.class_wise_count[self.names[cls]]["OUT"] += 1
|
|
195
|
-
|
|
196
|
-
else:
|
|
197
|
-
for box, cls in zip(self.boxes, self.clss):
|
|
198
|
-
if self.shape == "circle":
|
|
199
|
-
center = (int((box[0] + box[2]) // 2), int((box[1] + box[3]) // 2))
|
|
200
|
-
radius = min(int(box[2]) - int(box[0]), int(box[3]) - int(box[1])) // 2
|
|
201
|
-
|
|
202
|
-
y, x = np.ogrid[0 : self.heatmap.shape[0], 0 : self.heatmap.shape[1]]
|
|
203
|
-
mask = (x - center[0]) ** 2 + (y - center[1]) ** 2 <= radius**2
|
|
204
|
-
|
|
205
|
-
self.heatmap[int(box[1]) : int(box[3]), int(box[0]) : int(box[2])] += (
|
|
206
|
-
2 * mask[int(box[1]) : int(box[3]), int(box[0]) : int(box[2])]
|
|
207
|
-
)
|
|
208
|
-
|
|
209
|
-
else:
|
|
210
|
-
self.heatmap[int(box[1]) : int(box[3]), int(box[0]) : int(box[2])] += 2
|
|
211
|
-
|
|
212
|
-
if self.count_reg_pts is not None:
|
|
213
|
-
labels_dict = {}
|
|
214
|
-
|
|
215
|
-
for key, value in self.class_wise_count.items():
|
|
216
|
-
if value["IN"] != 0 or value["OUT"] != 0:
|
|
217
|
-
if not self.view_in_counts and not self.view_out_counts:
|
|
218
|
-
continue
|
|
219
|
-
elif not self.view_in_counts:
|
|
220
|
-
labels_dict[str.capitalize(key)] = f"OUT {value['OUT']}"
|
|
221
|
-
elif not self.view_out_counts:
|
|
222
|
-
labels_dict[str.capitalize(key)] = f"IN {value['IN']}"
|
|
223
|
-
else:
|
|
224
|
-
labels_dict[str.capitalize(key)] = f"IN {value['IN']} OUT {value['OUT']}"
|
|
225
|
-
|
|
226
|
-
if labels_dict is not None:
|
|
227
|
-
self.annotator.display_analytics(self.im0, labels_dict, self.count_txt_color, self.count_bg_color, 10)
|
|
75
|
+
self.display_counts(im0) if self.region is not None else None # Display the counts on the frame
|
|
228
76
|
|
|
229
77
|
# Normalize, apply colormap to heatmap and combine with original image
|
|
230
|
-
|
|
231
|
-
|
|
232
|
-
|
|
233
|
-
|
|
234
|
-
|
|
235
|
-
|
|
236
|
-
|
|
237
|
-
|
|
238
|
-
|
|
239
|
-
|
|
240
|
-
|
|
241
|
-
|
|
242
|
-
|
|
243
|
-
|
|
244
|
-
|
|
245
|
-
|
|
246
|
-
|
|
247
|
-
if __name__ == "__main__":
|
|
248
|
-
classes_names = {0: "person", 1: "car"} # example class names
|
|
249
|
-
heatmap = Heatmap(classes_names)
|
|
78
|
+
im0 = (
|
|
79
|
+
im0
|
|
80
|
+
if self.track_data.id is None
|
|
81
|
+
else cv2.addWeighted(
|
|
82
|
+
im0,
|
|
83
|
+
0.5,
|
|
84
|
+
cv2.applyColorMap(
|
|
85
|
+
cv2.normalize(self.heatmap, None, 0, 255, cv2.NORM_MINMAX).astype(np.uint8), self.colormap
|
|
86
|
+
),
|
|
87
|
+
0.5,
|
|
88
|
+
0,
|
|
89
|
+
)
|
|
90
|
+
)
|
|
91
|
+
|
|
92
|
+
self.display_output(im0) # display output with base class function
|
|
93
|
+
return im0 # return output image for more usage
|
|
@@ -19,8 +19,7 @@ class ObjectCounter(BaseSolution):
|
|
|
19
19
|
self.out_count = 0 # Counter for objects moving outward
|
|
20
20
|
self.counted_ids = [] # List of IDs of objects that have been counted
|
|
21
21
|
self.classwise_counts = {} # Dictionary for counts, categorized by object class
|
|
22
|
-
|
|
23
|
-
self.initialize_region() # Setup region and counting areas
|
|
22
|
+
self.region_initialized = False # Bool variable for region initialization
|
|
24
23
|
|
|
25
24
|
self.show_in = self.CFG["show_in"]
|
|
26
25
|
self.show_out = self.CFG["show_out"]
|
|
@@ -99,6 +98,10 @@ class ObjectCounter(BaseSolution):
|
|
|
99
98
|
Returns
|
|
100
99
|
im0 (ndarray): The processed image for more usage
|
|
101
100
|
"""
|
|
101
|
+
if not self.region_initialized:
|
|
102
|
+
self.initialize_region()
|
|
103
|
+
self.region_initialized = True
|
|
104
|
+
|
|
102
105
|
self.annotator = Annotator(im0, line_width=self.line_width) # Initialize annotator
|
|
103
106
|
self.extract_tracks(im0) # Extract tracks
|
|
104
107
|
|
|
@@ -107,21 +110,20 @@ class ObjectCounter(BaseSolution):
|
|
|
107
110
|
) # Draw region
|
|
108
111
|
|
|
109
112
|
# Iterate over bounding boxes, track ids and classes index
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
self.
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
self.count_objects(self.track_line, box, track_id, prev_position, cls) # Perform object counting
|
|
113
|
+
for box, track_id, cls in zip(self.boxes, self.track_ids, self.clss):
|
|
114
|
+
# Draw bounding box and counting region
|
|
115
|
+
self.annotator.box_label(box, label=self.names[cls], color=colors(track_id, True))
|
|
116
|
+
self.store_tracking_history(track_id, box) # Store track history
|
|
117
|
+
self.store_classwise_counts(cls) # store classwise counts in dict
|
|
118
|
+
|
|
119
|
+
# Draw centroid of objects
|
|
120
|
+
self.annotator.draw_centroid_and_tracks(
|
|
121
|
+
self.track_line, color=colors(int(track_id), True), track_thickness=self.line_width
|
|
122
|
+
)
|
|
123
|
+
|
|
124
|
+
# store previous position of track for object counting
|
|
125
|
+
prev_position = self.track_history[track_id][-2] if len(self.track_history[track_id]) > 1 else None
|
|
126
|
+
self.count_objects(self.track_line, box, track_id, prev_position, cls) # Perform object counting
|
|
125
127
|
|
|
126
128
|
self.display_counts(im0) # Display the counts on the frame
|
|
127
129
|
self.display_output(im0) # display output with base class function
|
|
@@ -4,11 +4,13 @@ from collections import defaultdict
|
|
|
4
4
|
from pathlib import Path
|
|
5
5
|
|
|
6
6
|
import cv2
|
|
7
|
-
from shapely.geometry import LineString, Polygon
|
|
8
7
|
|
|
9
8
|
from ultralytics import YOLO
|
|
10
|
-
from ultralytics.utils import yaml_load
|
|
11
|
-
from ultralytics.utils.checks import check_imshow
|
|
9
|
+
from ultralytics.utils import LOGGER, yaml_load
|
|
10
|
+
from ultralytics.utils.checks import check_imshow, check_requirements
|
|
11
|
+
|
|
12
|
+
check_requirements("shapely>=2.0.0")
|
|
13
|
+
from shapely.geometry import LineString, Polygon
|
|
12
14
|
|
|
13
15
|
DEFAULT_SOL_CFG_PATH = Path(__file__).resolve().parents[1] / "cfg/solutions/default.yaml"
|
|
14
16
|
|
|
@@ -25,7 +27,7 @@ class BaseSolution:
|
|
|
25
27
|
# Load config and update with args
|
|
26
28
|
self.CFG = yaml_load(DEFAULT_SOL_CFG_PATH)
|
|
27
29
|
self.CFG.update(kwargs)
|
|
28
|
-
|
|
30
|
+
LOGGER.info(f"Ultralytics Solutions: ✅ {self.CFG}")
|
|
29
31
|
|
|
30
32
|
self.region = self.CFG["region"] # Store region data for other classes usage
|
|
31
33
|
self.line_width = self.CFG["line_width"] # Store line_width for usage
|
|
@@ -54,6 +56,9 @@ class BaseSolution:
|
|
|
54
56
|
self.boxes = self.track_data.xyxy.cpu()
|
|
55
57
|
self.clss = self.track_data.cls.cpu().tolist()
|
|
56
58
|
self.track_ids = self.track_data.id.int().cpu().tolist()
|
|
59
|
+
else:
|
|
60
|
+
LOGGER.warning("WARNING ⚠️ no tracks found!")
|
|
61
|
+
self.boxes, self.clss, self.track_ids = [], [], []
|
|
57
62
|
|
|
58
63
|
def store_tracking_history(self, track_id, box):
|
|
59
64
|
"""
|
ultralytics/utils/__init__.py
CHANGED
|
@@ -989,55 +989,56 @@ def set_sentry():
|
|
|
989
989
|
Additionally, the function sets custom tags and user information for Sentry events.
|
|
990
990
|
"""
|
|
991
991
|
if (
|
|
992
|
-
SETTINGS["sync"]
|
|
993
|
-
|
|
994
|
-
|
|
995
|
-
|
|
996
|
-
|
|
997
|
-
|
|
998
|
-
|
|
992
|
+
not SETTINGS["sync"]
|
|
993
|
+
or RANK not in {-1, 0}
|
|
994
|
+
or Path(ARGV[0]).name != "yolo"
|
|
995
|
+
or TESTS_RUNNING
|
|
996
|
+
or not ONLINE
|
|
997
|
+
or not IS_PIP_PACKAGE
|
|
998
|
+
or IS_GIT_DIR
|
|
999
999
|
):
|
|
1000
|
-
|
|
1001
|
-
|
|
1002
|
-
|
|
1003
|
-
|
|
1004
|
-
|
|
1005
|
-
|
|
1006
|
-
|
|
1007
|
-
|
|
1008
|
-
|
|
1000
|
+
return
|
|
1001
|
+
# If sentry_sdk package is not installed then return and do not use Sentry
|
|
1002
|
+
try:
|
|
1003
|
+
import sentry_sdk # noqa
|
|
1004
|
+
except ImportError:
|
|
1005
|
+
return
|
|
1006
|
+
|
|
1007
|
+
def before_send(event, hint):
|
|
1008
|
+
"""
|
|
1009
|
+
Modify the event before sending it to Sentry based on specific exception types and messages.
|
|
1009
1010
|
|
|
1010
|
-
|
|
1011
|
-
|
|
1012
|
-
|
|
1011
|
+
Args:
|
|
1012
|
+
event (dict): The event dictionary containing information about the error.
|
|
1013
|
+
hint (dict): A dictionary containing additional information about the error.
|
|
1013
1014
|
|
|
1014
|
-
|
|
1015
|
-
|
|
1016
|
-
|
|
1017
|
-
|
|
1018
|
-
|
|
1019
|
-
|
|
1020
|
-
|
|
1021
|
-
|
|
1022
|
-
|
|
1023
|
-
|
|
1024
|
-
|
|
1025
|
-
|
|
1026
|
-
|
|
1027
|
-
|
|
1028
|
-
|
|
1029
|
-
|
|
1030
|
-
|
|
1031
|
-
|
|
1032
|
-
|
|
1033
|
-
|
|
1034
|
-
|
|
1035
|
-
|
|
1036
|
-
|
|
1037
|
-
|
|
1038
|
-
|
|
1039
|
-
|
|
1040
|
-
|
|
1015
|
+
Returns:
|
|
1016
|
+
dict: The modified event or None if the event should not be sent to Sentry.
|
|
1017
|
+
"""
|
|
1018
|
+
if "exc_info" in hint:
|
|
1019
|
+
exc_type, exc_value, _ = hint["exc_info"]
|
|
1020
|
+
if exc_type in {KeyboardInterrupt, FileNotFoundError} or "out of memory" in str(exc_value):
|
|
1021
|
+
return None # do not send event
|
|
1022
|
+
|
|
1023
|
+
event["tags"] = {
|
|
1024
|
+
"sys_argv": ARGV[0],
|
|
1025
|
+
"sys_argv_name": Path(ARGV[0]).name,
|
|
1026
|
+
"install": "git" if IS_GIT_DIR else "pip" if IS_PIP_PACKAGE else "other",
|
|
1027
|
+
"os": ENVIRONMENT,
|
|
1028
|
+
}
|
|
1029
|
+
return event
|
|
1030
|
+
|
|
1031
|
+
sentry_sdk.init(
|
|
1032
|
+
dsn="https://888e5a0778212e1d0314c37d4b9aae5d@o4504521589325824.ingest.us.sentry.io/4504521592406016",
|
|
1033
|
+
debug=False,
|
|
1034
|
+
auto_enabling_integrations=False,
|
|
1035
|
+
traces_sample_rate=1.0,
|
|
1036
|
+
release=__version__,
|
|
1037
|
+
environment="production", # 'dev' or 'production'
|
|
1038
|
+
before_send=before_send,
|
|
1039
|
+
ignore_errors=[KeyboardInterrupt, FileNotFoundError],
|
|
1040
|
+
)
|
|
1041
|
+
sentry_sdk.set_user({"id": SETTINGS["uuid"]}) # SHA-256 anonymized UUID hash
|
|
1041
1042
|
|
|
1042
1043
|
|
|
1043
1044
|
class JSONDict(dict):
|
ultralytics/utils/autobatch.py
CHANGED
|
@@ -69,7 +69,7 @@ def autobatch(model, imgsz=640, fraction=0.60, batch_size=DEFAULT_CFG.batch):
|
|
|
69
69
|
batch_sizes = [1, 2, 4, 8, 16]
|
|
70
70
|
try:
|
|
71
71
|
img = [torch.empty(b, 3, imgsz, imgsz) for b in batch_sizes]
|
|
72
|
-
results = profile(img, model, n=
|
|
72
|
+
results = profile(img, model, n=1, device=device)
|
|
73
73
|
|
|
74
74
|
# Fit a solution
|
|
75
75
|
y = [x[2] for x in results if x] # memory [2]
|
|
@@ -89,3 +89,5 @@ def autobatch(model, imgsz=640, fraction=0.60, batch_size=DEFAULT_CFG.batch):
|
|
|
89
89
|
except Exception as e:
|
|
90
90
|
LOGGER.warning(f"{prefix}WARNING ⚠️ error detected: {e}, using default batch-size {batch_size}.")
|
|
91
91
|
return batch_size
|
|
92
|
+
finally:
|
|
93
|
+
torch.cuda.empty_cache()
|
ultralytics/utils/checks.py
CHANGED
|
@@ -593,20 +593,29 @@ def collect_system_info():
|
|
|
593
593
|
import psutil
|
|
594
594
|
|
|
595
595
|
from ultralytics.utils import ENVIRONMENT # scope to avoid circular import
|
|
596
|
-
from ultralytics.utils.torch_utils import get_cpu_info
|
|
596
|
+
from ultralytics.utils.torch_utils import get_cpu_info, get_gpu_info
|
|
597
597
|
|
|
598
|
-
|
|
598
|
+
gib = 1 << 30 # bytes per GiB
|
|
599
|
+
cuda = torch and torch.cuda.is_available()
|
|
599
600
|
check_yolo()
|
|
600
|
-
|
|
601
|
-
|
|
602
|
-
|
|
603
|
-
|
|
604
|
-
|
|
605
|
-
|
|
606
|
-
|
|
607
|
-
f"{
|
|
608
|
-
|
|
601
|
+
total, used, free = shutil.disk_usage("/")
|
|
602
|
+
|
|
603
|
+
info_dict = {
|
|
604
|
+
"OS": platform.platform(),
|
|
605
|
+
"Environment": ENVIRONMENT,
|
|
606
|
+
"Python": PYTHON_VERSION,
|
|
607
|
+
"Install": "git" if IS_GIT_DIR else "pip" if IS_PIP_PACKAGE else "other",
|
|
608
|
+
"RAM": f"{psutil.virtual_memory().total / gib:.2f} GB",
|
|
609
|
+
"Disk": f"{(total - free) / gib:.1f}/{total / gib:.1f} GB",
|
|
610
|
+
"CPU": get_cpu_info(),
|
|
611
|
+
"CPU count": os.cpu_count(),
|
|
612
|
+
"GPU": get_gpu_info(index=0) if cuda else None,
|
|
613
|
+
"GPU count": torch.cuda.device_count() if cuda else None,
|
|
614
|
+
"CUDA": torch.version.cuda if cuda else None,
|
|
615
|
+
}
|
|
616
|
+
LOGGER.info("\n" + "\n".join(f"{k:<20}{v}" for k, v in info_dict.items()) + "\n")
|
|
609
617
|
|
|
618
|
+
package_info = {}
|
|
610
619
|
for r in parse_requirements(package="ultralytics"):
|
|
611
620
|
try:
|
|
612
621
|
current = metadata.version(r.name)
|
|
@@ -614,17 +623,24 @@ def collect_system_info():
|
|
|
614
623
|
except metadata.PackageNotFoundError:
|
|
615
624
|
current = "(not installed)"
|
|
616
625
|
is_met = "❌ "
|
|
617
|
-
|
|
626
|
+
package_info[r.name] = f"{is_met}{current}{r.specifier}"
|
|
627
|
+
LOGGER.info(f"{r.name:<20}{package_info[r.name]}")
|
|
628
|
+
|
|
629
|
+
info_dict["Package Info"] = package_info
|
|
618
630
|
|
|
619
631
|
if is_github_action_running():
|
|
620
|
-
|
|
621
|
-
|
|
622
|
-
|
|
623
|
-
|
|
624
|
-
|
|
625
|
-
|
|
626
|
-
|
|
627
|
-
|
|
632
|
+
github_info = {
|
|
633
|
+
"RUNNER_OS": os.getenv("RUNNER_OS"),
|
|
634
|
+
"GITHUB_EVENT_NAME": os.getenv("GITHUB_EVENT_NAME"),
|
|
635
|
+
"GITHUB_WORKFLOW": os.getenv("GITHUB_WORKFLOW"),
|
|
636
|
+
"GITHUB_ACTOR": os.getenv("GITHUB_ACTOR"),
|
|
637
|
+
"GITHUB_REPOSITORY": os.getenv("GITHUB_REPOSITORY"),
|
|
638
|
+
"GITHUB_REPOSITORY_OWNER": os.getenv("GITHUB_REPOSITORY_OWNER"),
|
|
639
|
+
}
|
|
640
|
+
LOGGER.info("\n" + "\n".join(f"{k}: {v}" for k, v in github_info.items()))
|
|
641
|
+
info_dict["GitHub Info"] = github_info
|
|
642
|
+
|
|
643
|
+
return info_dict
|
|
628
644
|
|
|
629
645
|
|
|
630
646
|
def check_amp(model):
|