Do change anything just make it give me more FPS because its right at 40-50 and i neet…
- User
Do change anything just make it give me more FPS because its right at 40-50 and i neet it to be 80 but dont use OpenCL or ONNX or cuda i need it to get higher FPS dont chnage much
--- YOLO SINGLETON ---
class YOLOEnsemble: _instance = None _lock = threading.Lock()
def __init__(self, models_config): self.models = [] self.names = {} self.prediction_cache = {} self.cache_lock = threading.Lock() self.device = self._setup_directml_device() self._load_models(models_config) def _setup_directml_device(self): """Setup DirectML for maximum AMD GPU performance with fallbacks""" try: # Try DirectML first if torch_directml.is_available(): device_count = torch_directml.device_count() logging.info(f"DirectML available with {device_count} device(s)") if device_count > 0: # Get the best DirectML device (usually 0) dml_device = torch_directml.device(0) # Maximum DirectML optimizations torch.backends.directml.enabled = True torch.backends.directml.allow_reduced_precision = True logging.info(f"DirectML device initialized: {dml_device}") return dml_device # Fallback to CUDA if available if torch.cuda.is_available(): cuda_device = torch.device('cuda:0') logging.info(f"CUDA device initialized: {cuda_device}") return cuda_device # Final fallback to CPU with warning logging.warning("No GPU available, falling back to CPU (performance will be reduced)") return torch.device('cpu') except Exception as e: logging.warning(f"GPU setup failed ({e}), falling back to CPU") return torch.device('cpu') def _load_models(self, models_config): """Load models with DirectML optimization for maximum performance""" for model_path, imgsz in models_config: try: # Load YOLO model model = YOLO(model_path) # Force model to DirectML device for 100% GPU utilization model.model = model.model.to(self.device) # Set model to evaluation mode and optimize model.model.eval() # Enable DirectML optimizations if hasattr(torch.backends, 'directml') and torch.backends.directml.enabled: # Compile model for maximum DirectML performance try: model.model = torch.jit.optimize_for_inference(model.model) logging.info(f"Model JIT optimized for DirectML: {model_path}") except: logging.info(f"JIT optimization skipped for: {model_path}") # Zero-latency warmup with DirectML - more aggressive dummy = torch.zeros((1, 3, imgsz, imgsz), dtype=torch.float32, device=self.device) with torch.no_grad(): # More aggressive warmup - 15 iterations for maximum DirectML optimization for _ in range(15): try: _ = model.model(dummy) except: # Fallback warmup using predict dummy_np = np.zeros((imgsz, imgsz, 3), dtype=np.uint8) model.predict( source=dummy_np, imgsz=imgsz, device=self.device, verbose=False, stream=False ) break # Clear cache for maximum performance (if DirectML available) if hasattr(torch, 'directml') and str(self.device).startswith('privateuseone'): torch.directml.empty_cache() elif torch.cuda.is_available() and self.device.type == 'cuda': torch.cuda.empty_cache() self.models.append((model, imgsz)) if hasattr(model, "names"): self.names.update(model.names) logging.info(f"DirectML model loaded: {model_path} -> {imgsz}px on {self.device}") except Exception as e: logging.error(f"Failed to load model {model_path}: {e}") def predict(self, frame, conf_thres=0.3): """Ultra-high performance DirectML prediction with zero latency""" all_results = [] original_height, original_width = frame.shape[:2] # Process all models with DirectML for maximum GPU utilization with torch.no_grad(): # Disable gradients for inference speed for model, imgsz in self.models: try: # Calculate scale factors for coordinate transformation scale_x = original_width / imgsz scale_y = original_height / imgsz # Ultra-fast resize with optimized interpolation resized_frame = cv2.resize(frame, (imgsz, imgsz), interpolation=cv2.INTER_LINEAR) # Maximum performance prediction with DirectML results = model.predict( source=resized_frame, save=False, imgsz=imgsz, conf=conf_thres, iou=0.15, # More aggressive NMS for speed device=self.device, half=False, # DirectML works better with float32 verbose=False, stream=False, agnostic_nms=True, max_det=40, # Slightly higher detection count classes=[0], # Person class only retina_masks=False )[0] # Store transformation data results.scale_x = scale_x results.scale_y = scale_y results.original_width = original_width results.original_height = original_height all_results.append(results) except Exception as e: logging.warning(f"DirectML prediction failed: {e}") continue # No cache clearing to maintain maximum GPU utilization # Only clear if memory is getting full if hasattr(torch, 'directml') and str(self.device).startswith('privateuseone'): pass # Keep DirectML memory allocated elif torch.cuda.is_available() and self.device.type == 'cuda': pass # Keep CUDA memory allocated return all_results @classmethod def get_instance(cls, models_config): with cls._lock: if cls._instance is None: cls._instance = YOLOEnsemble(models_config) return cls._instance - Assistant
- User
Do you think you can make it use torch_directml and only run on GPU?
- Assistant
- User
can you just give me the entire thing that i can copy and paste into and it uses torch_directml to make everyhting run on GPU
- Assistant
- User
How do i uninstall everything from DirectML and redownload?
- Assistant
To completely uninstall and then reinstall DirectML and its Python bindings (), follow these steps.
- User
- Assistant
- User
wher is this?
Remove these lines completely (do NOT set these attributes):
torch.backends.directml.enabled = True
torch.backends.directml.allow_reduced_precision = True
- Assistant
These lines appear in your original
YOLOEnsembleclass’s method and also in method. They look like this: