Spaces:
Running
Running
Commit
·
c534101
1
Parent(s):
5cc72f5
Update src/detection.py
Browse files- src/detection.py +267 -140
src/detection.py
CHANGED
@@ -1,14 +1,52 @@
|
|
1 |
import numpy as np
|
2 |
-
from typing import List, Dict
|
3 |
import cv2
|
4 |
from pathlib import Path
|
|
|
5 |
import yaml
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
6 |
|
7 |
class YOLOv11Detector:
|
8 |
-
"""YOLOv11 detector for
|
9 |
|
10 |
def __init__(self, config_path: str = "config.yaml"):
|
11 |
-
"""Initialize YOLOv11 detector with
|
12 |
with open(config_path, 'r') as f:
|
13 |
self.config = yaml.safe_load(f)
|
14 |
|
@@ -36,8 +74,11 @@ class YOLOv11Detector:
|
|
36 |
self.iou_threshold = self.config['model']['iou_threshold']
|
37 |
self.classes = self.config['detection']['classes']
|
38 |
|
39 |
-
# Load model
|
40 |
-
self.
|
|
|
|
|
|
|
41 |
|
42 |
def _load_pytorch_model(self):
|
43 |
"""Load PyTorch model using Ultralytics"""
|
@@ -45,43 +86,146 @@ class YOLOv11Detector:
|
|
45 |
self.model = YOLO(self.model_path)
|
46 |
|
47 |
# Set model to appropriate device
|
48 |
-
if self.device == 'cuda:0':
|
49 |
self.model.to('cuda')
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
50 |
|
51 |
-
|
52 |
-
|
53 |
-
|
54 |
-
"""Load ONNX model using OpenCV DNN"""
|
55 |
-
self.net = cv2.dnn.readNet(self.model_path)
|
56 |
|
57 |
-
#
|
58 |
-
|
59 |
-
|
60 |
-
self.
|
61 |
-
|
62 |
-
|
63 |
-
|
64 |
|
65 |
-
|
|
|
|
|
|
|
|
|
66 |
|
67 |
def detect(self, image: np.ndarray) -> Dict:
|
68 |
"""
|
69 |
-
Perform detection on image
|
70 |
Args:
|
71 |
image: Input image as numpy array (BGR format)
|
72 |
Returns:
|
73 |
Dictionary containing detection results
|
74 |
"""
|
75 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
76 |
results = self.model(
|
77 |
image,
|
78 |
conf=self.confidence,
|
79 |
iou=self.iou_threshold,
|
80 |
-
device=self.device if
|
81 |
verbose=False
|
82 |
)
|
83 |
|
84 |
-
# Parse results
|
85 |
detections = {
|
86 |
'boxes': [],
|
87 |
'confidences': [],
|
@@ -91,21 +235,11 @@ class YOLOv11Detector:
|
|
91 |
|
92 |
if len(results) > 0 and results[0].boxes is not None:
|
93 |
boxes = results[0].boxes
|
94 |
-
|
95 |
for box in boxes:
|
96 |
-
# Get box coordinates (xyxy format)
|
97 |
x1, y1, x2, y2 = box.xyxy[0].cpu().numpy()
|
98 |
-
|
99 |
-
# Get confidence and class
|
100 |
conf = float(box.conf[0].cpu().numpy())
|
101 |
cls_id = int(box.cls[0].cpu().numpy())
|
102 |
-
|
103 |
-
# Map class ID to class name
|
104 |
-
if cls_id < len(self.classes):
|
105 |
-
cls_name = self.classes[cls_id]
|
106 |
-
else:
|
107 |
-
cls_name = f"class_{cls_id}"
|
108 |
-
|
109 |
detections['boxes'].append([int(x1), int(y1), int(x2), int(y2)])
|
110 |
detections['confidences'].append(conf)
|
111 |
detections['classes'].append(cls_name)
|
@@ -113,139 +247,132 @@ class YOLOv11Detector:
|
|
113 |
|
114 |
return detections
|
115 |
|
116 |
-
def
|
117 |
-
"""
|
118 |
-
# Run YOLO inference
|
119 |
-
results = self.model(
|
120 |
-
image,
|
121 |
-
conf=self.confidence,
|
122 |
-
iou=self.iou_threshold,
|
123 |
-
device=self.device,
|
124 |
-
verbose=False
|
125 |
-
)
|
126 |
|
127 |
-
#
|
128 |
-
|
129 |
-
'boxes': [],
|
130 |
-
'confidences': [],
|
131 |
-
'classes': [],
|
132 |
-
'class_ids': []
|
133 |
-
}
|
134 |
|
135 |
-
|
136 |
-
|
137 |
|
138 |
-
|
139 |
-
|
140 |
-
x1, y1, x2, y2 = box.xyxy[0].cpu().numpy()
|
141 |
|
142 |
-
|
143 |
-
conf = float(box.conf[0].cpu().numpy())
|
144 |
-
cls_id = int(box.cls[0].cpu().numpy())
|
145 |
|
146 |
-
|
147 |
-
|
148 |
-
cls_name = self.classes[cls_id]
|
149 |
-
else:
|
150 |
-
cls_name = f"class_{cls_id}"
|
151 |
|
152 |
-
|
153 |
-
|
154 |
-
detections['classes'].append(cls_name)
|
155 |
-
detections['class_ids'].append(cls_id)
|
156 |
|
157 |
-
|
|
|
|
|
158 |
|
159 |
-
|
160 |
-
|
161 |
-
|
162 |
|
163 |
-
#
|
164 |
-
|
165 |
-
image, 1/255.0, (640, 640),
|
166 |
-
swapRB=True, crop=False
|
167 |
-
)
|
168 |
|
169 |
-
|
170 |
-
|
171 |
-
preds = preds.transpose((0, 2, 1))
|
172 |
|
173 |
-
# Extract
|
174 |
-
|
175 |
-
|
176 |
-
|
177 |
-
input_shape=(640, 640)
|
178 |
-
)
|
179 |
|
180 |
-
|
|
|
|
|
|
|
|
|
181 |
|
182 |
-
|
183 |
-
|
184 |
-
|
185 |
-
|
186 |
-
|
187 |
-
|
188 |
-
|
189 |
-
x_factor = image_width / input_width
|
190 |
-
y_factor = image_height / input_height
|
191 |
-
|
192 |
-
rows = preds[0].shape[0]
|
193 |
-
for i in range(rows):
|
194 |
-
row = preds[0][i]
|
195 |
-
conf = row[4]
|
196 |
-
|
197 |
-
classes_score = row[4:]
|
198 |
-
_, _, _, max_idx = cv2.minMaxLoc(classes_score)
|
199 |
-
class_id = max_idx[1]
|
200 |
-
|
201 |
-
if classes_score[class_id] > self.confidence:
|
202 |
-
confs.append(float(conf))
|
203 |
-
label = self.classes[int(class_id)] if int(class_id) < len(self.classes) else f"class_{class_id}"
|
204 |
-
class_ids.append(label)
|
205 |
-
|
206 |
-
# Extract boxes
|
207 |
-
x, y, w, h = row[0].item(), row[1].item(), row[2].item(), row[3].item()
|
208 |
-
left = int((x - 0.5 * w) * x_factor)
|
209 |
-
top = int((y - 0.5 * h) * y_factor)
|
210 |
-
width = int(w * x_factor)
|
211 |
-
height = int(h * y_factor)
|
212 |
-
box = [left, top, left + width, top + height]
|
213 |
-
boxes.append(box)
|
214 |
|
215 |
# Apply NMS
|
216 |
-
if len(
|
217 |
indices = cv2.dnn.NMSBoxes(
|
218 |
-
|
219 |
-
|
|
|
|
|
220 |
)
|
221 |
|
222 |
if len(indices) > 0:
|
223 |
indices = indices.flatten()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
224 |
return {
|
225 |
-
'boxes':
|
226 |
-
'confidences':
|
227 |
-
'classes':
|
228 |
-
'class_ids':
|
229 |
}
|
230 |
|
231 |
return {'boxes': [], 'confidences': [], 'classes': [], 'class_ids': []}
|
232 |
|
233 |
def detect_batch(self, images: List[np.ndarray]) -> List[Dict]:
|
234 |
-
"""
|
235 |
-
|
|
|
|
|
|
|
236 |
|
237 |
-
def
|
238 |
-
"""
|
239 |
-
|
240 |
|
241 |
-
#
|
242 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
243 |
|
244 |
-
|
245 |
-
|
246 |
-
|
247 |
-
|
248 |
-
|
|
|
|
|
|
|
249 |
|
250 |
-
|
251 |
-
print(f"Loaded {model_type} model: {self.model_path}")
|
|
|
1 |
import numpy as np
|
2 |
+
from typing import List, Dict
|
3 |
import cv2
|
4 |
from pathlib import Path
|
5 |
+
import torch
|
6 |
import yaml
|
7 |
+
import onnxruntime as ort
|
8 |
+
import os
|
9 |
+
import psutil
|
10 |
+
|
11 |
+
|
12 |
+
def _get_optimal_threads():
|
13 |
+
"""Calculate optimal thread count for current system"""
|
14 |
+
physical_cores = psutil.cpu_count(logical=False)
|
15 |
+
logical_cores = psutil.cpu_count(logical=True)
|
16 |
+
|
17 |
+
# Optimal intra-op threads = physical cores
|
18 |
+
# For high-performance scenarios, use physical cores
|
19 |
+
intra_threads = physical_cores if physical_cores else 4
|
20 |
+
|
21 |
+
print(f"System info: {physical_cores} physical cores, {logical_cores} logical cores")
|
22 |
+
print(f"Using {intra_threads} intra-op threads for optimal performance")
|
23 |
+
|
24 |
+
return intra_threads
|
25 |
+
|
26 |
+
|
27 |
+
def _preprocess_image_optimized(image: np.ndarray) -> np.ndarray:
|
28 |
+
"""Optimized preprocessing for minimal overhead"""
|
29 |
+
height, width = image.shape[:2]
|
30 |
+
|
31 |
+
# Resize with optimal interpolation
|
32 |
+
img_resized = cv2.resize(image, (640, 640), interpolation=cv2.INTER_LINEAR)
|
33 |
+
|
34 |
+
# RGB conversion (most efficient method)
|
35 |
+
img_rgb = cv2.cvtColor(img_resized, cv2.COLOR_BGR2RGB)
|
36 |
+
|
37 |
+
# Normalize and transpose in one operation (memory efficient)
|
38 |
+
img_normalized = img_rgb.astype(np.float32, copy=False) / 255.0
|
39 |
+
img_transposed = np.transpose(img_normalized, (2, 0, 1))
|
40 |
+
img_batch = np.expand_dims(img_transposed, axis=0)
|
41 |
+
|
42 |
+
return img_batch, height, width
|
43 |
+
|
44 |
|
45 |
class YOLOv11Detector:
|
46 |
+
"""YOLOv11 detector optimized for ONNX Runtime v1.19 with opset 21"""
|
47 |
|
48 |
def __init__(self, config_path: str = "config.yaml"):
|
49 |
+
"""Initialize YOLOv11 detector with maximum ONNX Runtime optimizations"""
|
50 |
with open(config_path, 'r') as f:
|
51 |
self.config = yaml.safe_load(f)
|
52 |
|
|
|
74 |
self.iou_threshold = self.config['model']['iou_threshold']
|
75 |
self.classes = self.config['detection']['classes']
|
76 |
|
77 |
+
# Load model based on extension
|
78 |
+
if self.model_path.endswith('.onnx'):
|
79 |
+
self._load_onnx_model_optimized() # Will use optimizations for ONNX models
|
80 |
+
else:
|
81 |
+
self._load_pytorch_model() # Keep original PyTorch logic
|
82 |
|
83 |
def _load_pytorch_model(self):
|
84 |
"""Load PyTorch model using Ultralytics"""
|
|
|
86 |
self.model = YOLO(self.model_path)
|
87 |
|
88 |
# Set model to appropriate device
|
89 |
+
if self.device == 'cuda:0' and torch.cuda.is_available():
|
90 |
self.model.to('cuda')
|
91 |
+
else:
|
92 |
+
self.model.to('cpu')
|
93 |
+
|
94 |
+
print(f"Loaded PyTorch model: {self.model_path} on device: {self.device}")
|
95 |
+
|
96 |
+
def _load_onnx_model_optimized(self):
|
97 |
+
"""Load ONNX model with MAXIMUM optimizations for v1.19 + opset 21"""
|
98 |
+
|
99 |
+
# Get optimal thread count
|
100 |
+
intra_threads = _get_optimal_threads()
|
101 |
+
|
102 |
+
# Configure MAXIMUM performance session options
|
103 |
+
sess_options = ort.SessionOptions()
|
104 |
+
|
105 |
+
# === GRAPH OPTIMIZATIONS (Level: ALL) ===
|
106 |
+
# Enable ALL optimizations including layout optimizations
|
107 |
+
sess_options.graph_optimization_level = ort.GraphOptimizationLevel.ORT_ENABLE_ALL
|
108 |
+
|
109 |
+
# === THREADING OPTIMIZATIONS ===
|
110 |
+
# Intra-op parallelism (within operators) - use physical cores
|
111 |
+
sess_options.intra_op_num_threads = intra_threads
|
112 |
+
|
113 |
+
# Inter-op parallelism (between operators) - keep at 1 for sequential execution
|
114 |
+
# Sequential execution often performs better than parallel for single inference
|
115 |
+
sess_options.inter_op_num_threads = 1
|
116 |
+
sess_options.execution_mode = ort.ExecutionMode.ORT_SEQUENTIAL
|
117 |
+
|
118 |
+
# === MEMORY OPTIMIZATIONS ===
|
119 |
+
# Enable memory pattern optimization (reduces memory allocation overhead)
|
120 |
+
sess_options.enable_mem_pattern = True
|
121 |
+
|
122 |
+
# Enable memory arena optimization (better memory reuse)
|
123 |
+
sess_options.enable_cpu_mem_arena = True
|
124 |
+
|
125 |
+
# === CPU PERFORMANCE OPTIMIZATIONS ===
|
126 |
+
# Allow threads to spin waiting for work (trades CPU for latency)
|
127 |
+
sess_options.add_session_config_entry("session.intra_op.allow_spinning", "1")
|
128 |
+
sess_options.add_session_config_entry("session.inter_op.allow_spinning", "1")
|
129 |
+
|
130 |
+
# Dynamic cost model for better load balancing (reduces latency variance)
|
131 |
+
# Best value for dynamic_block_base is 4 according to docs
|
132 |
+
sess_options.add_session_config_entry("session.intra_op.dynamic_block_base", "4")
|
133 |
+
|
134 |
+
# For systems with >64 logical cores, use lock-free queues
|
135 |
+
logical_cores = psutil.cpu_count(logical=True)
|
136 |
+
if logical_cores and logical_cores > 64:
|
137 |
+
sess_options.add_session_config_entry("session.use_lock_free_queue", "1")
|
138 |
+
print("Enabled lock-free queues for high-core system")
|
139 |
+
|
140 |
+
# Disable profiling in production for best performance
|
141 |
+
sess_options.enable_profiling = False
|
142 |
+
|
143 |
+
# === EXECUTION PROVIDER CONFIGURATION ===
|
144 |
+
providers = []
|
145 |
+
provider_options = []
|
146 |
+
|
147 |
+
if self.device == 'cuda:0' and ort.get_device() == 'GPU':
|
148 |
+
# CUDA EP with optimizations
|
149 |
+
cuda_options = {
|
150 |
+
'device_id': 0,
|
151 |
+
'arena_extend_strategy': 'kNextPowerOfTwo', # Better memory allocation
|
152 |
+
'gpu_mem_limit': 2 * 1024 * 1024 * 1024, # 2GB limit
|
153 |
+
'cudnn_conv_algo_search': 'EXHAUSTIVE', # Find best conv algorithms
|
154 |
+
'do_copy_in_default_stream': True # Better stream utilization
|
155 |
+
}
|
156 |
+
providers.append('CUDAExecutionProvider')
|
157 |
+
provider_options.append(cuda_options)
|
158 |
+
print("CUDA EP configured with optimizations")
|
159 |
+
|
160 |
+
# CPU EP with OpenMP optimizations (always fallback)
|
161 |
+
cpu_options = {
|
162 |
+
'use_arena': True,
|
163 |
+
'arena_extend_strategy': 'kSameAsRequested'
|
164 |
+
}
|
165 |
+
providers.append('CPUExecutionProvider')
|
166 |
+
provider_options.append(cpu_options)
|
167 |
+
|
168 |
+
# === SET OPENMP ENVIRONMENT VARIABLES FOR OPTIMAL CPU PERFORMANCE ===
|
169 |
+
# These should ideally be set before importing onnxruntime, but we set them anyway
|
170 |
+
os.environ['OMP_NUM_THREADS'] = str(intra_threads)
|
171 |
+
os.environ['OMP_WAIT_POLICY'] = 'ACTIVE' # Don't yield CPU, faster inference
|
172 |
+
os.environ['OMP_NESTED'] = '0' # Disable nested parallelism
|
173 |
+
# For Intel CPUs: compact affinity for better cache usage
|
174 |
+
os.environ['KMP_AFFINITY'] = 'granularity=fine,compact,1,0'
|
175 |
+
|
176 |
+
print(f"OpenMP configuration: threads={intra_threads}, policy=ACTIVE")
|
177 |
+
|
178 |
+
# === CREATE OPTIMIZED SESSION ===
|
179 |
+
self.session = ort.InferenceSession(
|
180 |
+
self.model_path,
|
181 |
+
sess_options=sess_options,
|
182 |
+
providers=providers,
|
183 |
+
provider_options=provider_options
|
184 |
+
)
|
185 |
|
186 |
+
# Get input/output info
|
187 |
+
self.input_name = self.session.get_inputs()[0].name
|
188 |
+
self.output_name = self.session.get_outputs()[0].name
|
|
|
|
|
189 |
|
190 |
+
# Verify opset version (should be 21 for latest optimizations)
|
191 |
+
try:
|
192 |
+
# This might not always be available, but good to check
|
193 |
+
model_meta = self.session.get_modelmeta()
|
194 |
+
print(f"Model metadata - Domain: {getattr(model_meta, 'domain', 'N/A')}")
|
195 |
+
except:
|
196 |
+
pass
|
197 |
|
198 |
+
provider_used = self.session.get_providers()[0]
|
199 |
+
print(f"✅ ONNX Runtime v{ort.__version__} - Optimized session created")
|
200 |
+
print(f"📈 Provider: {provider_used}")
|
201 |
+
print(f"🧵 Threading: {intra_threads} intra-op threads, sequential execution")
|
202 |
+
print(f"🚀 Optimizations: Graph=ALL, Memory=Enabled, Spinning=Enabled, Dynamic=Enabled")
|
203 |
|
204 |
def detect(self, image: np.ndarray) -> Dict:
|
205 |
"""
|
206 |
+
Perform detection on image with maximum optimization
|
207 |
Args:
|
208 |
image: Input image as numpy array (BGR format)
|
209 |
Returns:
|
210 |
Dictionary containing detection results
|
211 |
"""
|
212 |
+
if self.model_path.endswith('.onnx'):
|
213 |
+
return self._detect_onnx_optimized(image)
|
214 |
+
else:
|
215 |
+
return self._detect_pytorch(image)
|
216 |
+
|
217 |
+
def _detect_pytorch(self, image: np.ndarray) -> Dict:
|
218 |
+
"""Detection using PyTorch model"""
|
219 |
+
from ultralytics import YOLO
|
220 |
results = self.model(
|
221 |
image,
|
222 |
conf=self.confidence,
|
223 |
iou=self.iou_threshold,
|
224 |
+
device=self.device if 'cuda' in self.device and torch.cuda.is_available() else 'cpu',
|
225 |
verbose=False
|
226 |
)
|
227 |
|
228 |
+
# Parse results
|
229 |
detections = {
|
230 |
'boxes': [],
|
231 |
'confidences': [],
|
|
|
235 |
|
236 |
if len(results) > 0 and results[0].boxes is not None:
|
237 |
boxes = results[0].boxes
|
|
|
238 |
for box in boxes:
|
|
|
239 |
x1, y1, x2, y2 = box.xyxy[0].cpu().numpy()
|
|
|
|
|
240 |
conf = float(box.conf[0].cpu().numpy())
|
241 |
cls_id = int(box.cls[0].cpu().numpy())
|
242 |
+
cls_name = self.classes[cls_id] if cls_id < len(self.classes) else f"class_{cls_id}"
|
|
|
|
|
|
|
|
|
|
|
|
|
243 |
detections['boxes'].append([int(x1), int(y1), int(x2), int(y2)])
|
244 |
detections['confidences'].append(conf)
|
245 |
detections['classes'].append(cls_name)
|
|
|
247 |
|
248 |
return detections
|
249 |
|
250 |
+
def _detect_onnx_optimized(self, image: np.ndarray) -> Dict:
|
251 |
+
"""Optimized ONNX detection with minimal overhead"""
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
252 |
|
253 |
+
# Optimized preprocessing
|
254 |
+
input_tensor, orig_height, orig_width = _preprocess_image_optimized(image)
|
|
|
|
|
|
|
|
|
|
|
255 |
|
256 |
+
# Run inference (optimized session handles the rest)
|
257 |
+
output = self.session.run([self.output_name], {self.input_name: input_tensor})[0]
|
258 |
|
259 |
+
# Optimized output parsing
|
260 |
+
detections = self._parse_yolo_output_optimized(output, orig_height, orig_width)
|
|
|
261 |
|
262 |
+
return detections
|
|
|
|
|
263 |
|
264 |
+
def _parse_yolo_output_optimized(self, output: np.ndarray, orig_height: int, orig_width: int) -> Dict:
|
265 |
+
"""Optimized YOLO output parsing for maximum performance"""
|
|
|
|
|
|
|
266 |
|
267 |
+
# Output shape: [1, 84, 8400] -> transpose to [8400, 84]
|
268 |
+
output = output[0].transpose(1, 0)
|
|
|
|
|
269 |
|
270 |
+
num_classes = len(self.classes)
|
271 |
+
x_factor = orig_width / 640.0
|
272 |
+
y_factor = orig_height / 640.0
|
273 |
|
274 |
+
# Vectorized operations for better performance
|
275 |
+
class_scores = output[:, 4:4 + num_classes]
|
276 |
+
max_confidences = np.max(class_scores, axis=1)
|
277 |
|
278 |
+
# Filter by confidence threshold (vectorized)
|
279 |
+
valid_indices = max_confidences > self.confidence
|
|
|
|
|
|
|
280 |
|
281 |
+
if not np.any(valid_indices):
|
282 |
+
return {'boxes': [], 'confidences': [], 'classes': [], 'class_ids': []}
|
|
|
283 |
|
284 |
+
# Extract valid detections
|
285 |
+
valid_output = output[valid_indices]
|
286 |
+
valid_confidences = max_confidences[valid_indices]
|
287 |
+
valid_class_ids = np.argmax(class_scores[valid_indices], axis=1)
|
|
|
|
|
288 |
|
289 |
+
# Convert bounding boxes (vectorized)
|
290 |
+
cx = valid_output[:, 0] * x_factor
|
291 |
+
cy = valid_output[:, 1] * y_factor
|
292 |
+
w = valid_output[:, 2] * x_factor
|
293 |
+
h = valid_output[:, 3] * y_factor
|
294 |
|
295 |
+
x1 = cx - w / 2
|
296 |
+
y1 = cy - h / 2
|
297 |
+
x2 = cx + w / 2
|
298 |
+
y2 = cy + h / 2
|
299 |
+
|
300 |
+
# Prepare for NMS
|
301 |
+
boxes_for_nms = np.column_stack([x1, y1, w, h])
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
302 |
|
303 |
# Apply NMS
|
304 |
+
if len(boxes_for_nms) > 0:
|
305 |
indices = cv2.dnn.NMSBoxes(
|
306 |
+
boxes_for_nms.tolist(),
|
307 |
+
valid_confidences.tolist(),
|
308 |
+
self.confidence,
|
309 |
+
self.iou_threshold
|
310 |
)
|
311 |
|
312 |
if len(indices) > 0:
|
313 |
indices = indices.flatten()
|
314 |
+
|
315 |
+
# Final results
|
316 |
+
final_boxes = [[int(x1[i]), int(y1[i]), int(x2[i]), int(y2[i])] for i in indices]
|
317 |
+
final_confs = [float(valid_confidences[i]) for i in indices]
|
318 |
+
final_class_ids = [int(valid_class_ids[i]) for i in indices]
|
319 |
+
final_classes = [
|
320 |
+
self.classes[cls_id] if cls_id < num_classes else f"class_{cls_id}"
|
321 |
+
for cls_id in final_class_ids
|
322 |
+
]
|
323 |
+
|
324 |
return {
|
325 |
+
'boxes': final_boxes,
|
326 |
+
'confidences': final_confs,
|
327 |
+
'classes': final_classes,
|
328 |
+
'class_ids': final_class_ids
|
329 |
}
|
330 |
|
331 |
return {'boxes': [], 'confidences': [], 'classes': [], 'class_ids': []}
|
332 |
|
333 |
def detect_batch(self, images: List[np.ndarray]) -> List[Dict]:
|
334 |
+
"""Optimized batch detection"""
|
335 |
+
if self.model_path.endswith('.onnx'):
|
336 |
+
return self._detect_batch_onnx_optimized(images)
|
337 |
+
else:
|
338 |
+
return [self.detect(img) for img in images]
|
339 |
|
340 |
+
def _detect_batch_onnx_optimized(self, images: List[np.ndarray]) -> List[Dict]:
|
341 |
+
"""Batch processing for ONNX with memory optimization"""
|
342 |
+
results = []
|
343 |
|
344 |
+
# Process images in optimal batch sizes to balance memory and performance
|
345 |
+
batch_size = min(4, len(images)) # Limit batch size for memory efficiency
|
346 |
+
|
347 |
+
for i in range(0, len(images), batch_size):
|
348 |
+
batch_images = images[i:i + batch_size]
|
349 |
+
batch_results = []
|
350 |
+
|
351 |
+
for img in batch_images:
|
352 |
+
batch_results.append(self.detect(img))
|
353 |
+
|
354 |
+
results.extend(batch_results)
|
355 |
+
|
356 |
+
return results
|
357 |
+
|
358 |
+
def get_performance_info(self) -> Dict:
|
359 |
+
"""Get current performance configuration info"""
|
360 |
+
info = {
|
361 |
+
"model_path": self.model_path,
|
362 |
+
"model_type": "ONNX" if self.model_path.endswith('.onnx') else "PyTorch",
|
363 |
+
"onnx_version": ort.__version__ if hasattr(self, 'session') else None,
|
364 |
+
"confidence_threshold": self.confidence,
|
365 |
+
"iou_threshold": self.iou_threshold,
|
366 |
+
"classes": self.classes
|
367 |
+
}
|
368 |
|
369 |
+
if hasattr(self, 'session'):
|
370 |
+
info.update({
|
371 |
+
"providers": self.session.get_providers(),
|
372 |
+
"optimization_level": "ORT_ENABLE_ALL",
|
373 |
+
"memory_optimizations": "Enabled",
|
374 |
+
"threading_optimizations": "Enabled",
|
375 |
+
"dynamic_cost_model": "Enabled"
|
376 |
+
})
|
377 |
|
378 |
+
return info
|
|