Spaces:
Sleeping
Sleeping
Update src/detection.py
Browse files- src/detection.py +13 -19
src/detection.py
CHANGED
|
@@ -14,8 +14,12 @@ def _get_optimal_threads():
|
|
| 14 |
physical_cores = psutil.cpu_count(logical=False)
|
| 15 |
logical_cores = psutil.cpu_count(logical=True)
|
| 16 |
|
|
|
|
|
|
|
| 17 |
intra_threads = physical_cores if physical_cores else 4
|
| 18 |
|
|
|
|
|
|
|
| 19 |
|
| 20 |
return intra_threads
|
| 21 |
|
|
@@ -45,7 +49,7 @@ class YOLOv11Detector:
|
|
| 45 |
"""Initialize YOLOv11 detector with maximum ONNX Runtime optimizations"""
|
| 46 |
with open(config_path, 'r') as f:
|
| 47 |
self.config = yaml.safe_load(f)
|
| 48 |
-
|
| 49 |
model_path = self.config['model']['path']
|
| 50 |
|
| 51 |
# Check which model file exists
|
|
@@ -79,25 +83,8 @@ class YOLOv11Detector:
|
|
| 79 |
def _load_pytorch_model(self):
|
| 80 |
"""Load PyTorch model using Ultralytics"""
|
| 81 |
from ultralytics import YOLO
|
| 82 |
-
import ultralytics.nn.modules as u_modules
|
| 83 |
-
|
| 84 |
-
# Patch Conv để tránh lỗi 'bn' lần đầu
|
| 85 |
-
if hasattr(u_modules, "Conv"):
|
| 86 |
-
old_init = u_modules.Conv.__init__
|
| 87 |
-
|
| 88 |
-
def new_init(self_, *args, **kwargs):
|
| 89 |
-
old_init(self_, *args, **kwargs)
|
| 90 |
-
if not hasattr(self_, "bn"): # nếu thiếu bn thì gán None
|
| 91 |
-
self_.bn = None
|
| 92 |
-
|
| 93 |
-
u_modules.Conv.__init__ = new_init
|
| 94 |
-
|
| 95 |
-
# Load YOLO model
|
| 96 |
self.model = YOLO(self.model_path)
|
| 97 |
|
| 98 |
-
# Ngăn không cho gọi fuse() nữa
|
| 99 |
-
self.model.fuse = lambda *args, **kwargs: self.model
|
| 100 |
-
|
| 101 |
# Set model to appropriate device
|
| 102 |
if self.device == 'cuda:0' and torch.cuda.is_available():
|
| 103 |
self.model.to('cuda')
|
|
@@ -186,6 +173,7 @@ class YOLOv11Detector:
|
|
| 186 |
# For Intel CPUs: compact affinity for better cache usage
|
| 187 |
os.environ['KMP_AFFINITY'] = 'granularity=fine,compact,1,0'
|
| 188 |
|
|
|
|
| 189 |
|
| 190 |
# === CREATE OPTIMIZED SESSION ===
|
| 191 |
self.session = ort.InferenceSession(
|
|
@@ -203,10 +191,15 @@ class YOLOv11Detector:
|
|
| 203 |
try:
|
| 204 |
# This might not always be available, but good to check
|
| 205 |
model_meta = self.session.get_modelmeta()
|
|
|
|
| 206 |
except:
|
| 207 |
pass
|
| 208 |
|
| 209 |
provider_used = self.session.get_providers()[0]
|
|
|
|
|
|
|
|
|
|
|
|
|
| 210 |
|
| 211 |
def detect(self, image: np.ndarray) -> Dict:
|
| 212 |
"""
|
|
@@ -223,6 +216,7 @@ class YOLOv11Detector:
|
|
| 223 |
|
| 224 |
def _detect_pytorch(self, image: np.ndarray) -> Dict:
|
| 225 |
"""Detection using PyTorch model"""
|
|
|
|
| 226 |
results = self.model(
|
| 227 |
image,
|
| 228 |
conf=self.confidence,
|
|
@@ -316,7 +310,7 @@ class YOLOv11Detector:
|
|
| 316 |
)
|
| 317 |
|
| 318 |
if len(indices) > 0:
|
| 319 |
-
indices =
|
| 320 |
|
| 321 |
# Final results
|
| 322 |
final_boxes = [[int(x1[i]), int(y1[i]), int(x2[i]), int(y2[i])] for i in indices]
|
|
|
|
| 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 |
|
|
|
|
| 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 |
+
|
| 53 |
model_path = self.config['model']['path']
|
| 54 |
|
| 55 |
# Check which model file exists
|
|
|
|
| 83 |
def _load_pytorch_model(self):
|
| 84 |
"""Load PyTorch model using Ultralytics"""
|
| 85 |
from ultralytics import YOLO
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 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')
|
|
|
|
| 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(
|
|
|
|
| 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 |
"""
|
|
|
|
| 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,
|
|
|
|
| 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]
|