Spaces:
Running
Running
File size: 1,531 Bytes
e07dafb fa700aa e07dafb fa700aa e07dafb fa700aa e07dafb fa700aa e07dafb fa700aa | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 | import torch
import os
import numpy as np
# ANSI color codes
class Colors:
RESET = "\033[0m"
BOLD = "\033[1m"
GREEN = "\033[92m"
CYAN = "\033[96m"
YELLOW = "\033[93m"
def get_yolov5(confident_val):
"""Initialize YOLOv5 model with optimized thread settings"""
# Force CPU mode
os.environ['CUDA_VISIBLE_DEVICES'] = ''
# Use multiple threads for better throughput (auto-scale based on CPU)
num_threads = min(os.cpu_count() or 2, 4) # Cap at 4 threads
torch.set_num_threads(num_threads)
torch.set_num_interop_threads(num_threads)
print(f"{Colors.CYAN}[MODEL]{Colors.RESET} π Loading YOLOv5 with {Colors.BOLD}{num_threads} threads{Colors.RESET}")
print(f"{Colors.CYAN}[MODEL]{Colors.RESET} π Confidence threshold: {confident_val}")
# Load model with optimization flags
model = torch.hub.load('./yolov5', 'custom', path='./countmodel.pt', source='local', force_reload=False)
# Configure model settings
model.conf = confident_val # Confidence threshold
model.max_det = 4000 # Maximum detections
model.cpu() # Ensure CPU mode
# Enable inference optimizations
model.eval() # Set to evaluation mode
# Enable inference mode for better performance
torch.inference_mode()
print(f"{Colors.GREEN}[MODEL]{Colors.RESET} β
Model loaded successfully!")
print(f"{Colors.CYAN}[MODEL]{Colors.RESET} π Max detections: {model.max_det}")
return model
|