Marcus Vinicius Zerbini Canhaço commited on
Commit
577120c
·
1 Parent(s): b181644

feat: atualização do detector com otimizações para GPU T4

Browse files
Files changed (1) hide show
  1. src/domain/detectors/gpu.py +7 -4
src/domain/detectors/gpu.py CHANGED
@@ -18,7 +18,7 @@ class WeaponDetectorGPU(BaseDetector):
18
  """Inicializa o detector."""
19
  super().__init__()
20
  self.default_resolution = 640
21
- self.device = self._get_best_device()
22
  self._initialize()
23
 
24
  def _initialize(self):
@@ -28,6 +28,9 @@ class WeaponDetectorGPU(BaseDetector):
28
  if not torch.cuda.is_available():
29
  raise RuntimeError("CUDA não está disponível!")
30
 
 
 
 
31
  # Carregar modelo e processador
32
  logger.info("Carregando modelo e processador...")
33
  model_name = "google/owlv2-base-patch16"
@@ -36,8 +39,8 @@ class WeaponDetectorGPU(BaseDetector):
36
  self.owlv2_model = Owlv2ForObjectDetection.from_pretrained(
37
  model_name,
38
  torch_dtype=torch.float16,
39
- device_map="auto"
40
- ).to(self.device)
41
 
42
  # Otimizar modelo
43
  self.owlv2_model.eval()
@@ -113,7 +116,7 @@ class WeaponDetectorGPU(BaseDetector):
113
 
114
  def _get_best_device(self):
115
  """Retorna o melhor dispositivo disponível."""
116
- return torch.device(0) # Usar primeira GPU
117
 
118
  def _clear_gpu_memory(self):
119
  """Limpa memória GPU."""
 
18
  """Inicializa o detector."""
19
  super().__init__()
20
  self.default_resolution = 640
21
+ self.device = None # Será configurado em _initialize
22
  self._initialize()
23
 
24
  def _initialize(self):
 
28
  if not torch.cuda.is_available():
29
  raise RuntimeError("CUDA não está disponível!")
30
 
31
+ # Configurar device corretamente
32
+ self.device = 0 # Usar índice inteiro para GPU
33
+
34
  # Carregar modelo e processador
35
  logger.info("Carregando modelo e processador...")
36
  model_name = "google/owlv2-base-patch16"
 
39
  self.owlv2_model = Owlv2ForObjectDetection.from_pretrained(
40
  model_name,
41
  torch_dtype=torch.float16,
42
+ device_map={"": self.device} # Mapear todo o modelo para GPU 0
43
+ )
44
 
45
  # Otimizar modelo
46
  self.owlv2_model.eval()
 
116
 
117
  def _get_best_device(self):
118
  """Retorna o melhor dispositivo disponível."""
119
+ return 0 # Usar índice inteiro para GPU
120
 
121
  def _clear_gpu_memory(self):
122
  """Limpa memória GPU."""