Spaces:
Paused
Paused
Fix AMP deprecation warnings
Browse files
src/engines/coherence/engine.py
CHANGED
|
@@ -7,6 +7,7 @@ import tempfile
|
|
| 7 |
import threading
|
| 8 |
import time
|
| 9 |
import urllib.request
|
|
|
|
| 10 |
from pathlib import Path
|
| 11 |
from typing import Optional
|
| 12 |
|
|
@@ -194,6 +195,15 @@ def _prepare_runtime(device: str) -> None:
|
|
| 194 |
_resnet_fallback.to(device)
|
| 195 |
|
| 196 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 197 |
def _release_runtime(device: str) -> None:
|
| 198 |
global _device
|
| 199 |
_device = "cpu"
|
|
@@ -479,7 +489,7 @@ class CoherenceEngine:
|
|
| 479 |
if face is not None:
|
| 480 |
face_gpu = face.unsqueeze(0).to(_device)
|
| 481 |
with _torch.no_grad():
|
| 482 |
-
with
|
| 483 |
emb = _resnet(face_gpu).detach().float().cpu().numpy()[0]
|
| 484 |
embeddings.append(emb)
|
| 485 |
except Exception:
|
|
@@ -515,7 +525,7 @@ class CoherenceEngine:
|
|
| 515 |
crop = Image.fromarray(frame[y1:y2, x1:x2]).convert("RGB")
|
| 516 |
tensor = _transform_fallback(crop).unsqueeze(0).to(_device)
|
| 517 |
with _torch.no_grad():
|
| 518 |
-
with
|
| 519 |
emb = _resnet_fallback(tensor).detach().float().cpu().numpy()[0]
|
| 520 |
embeddings_fb.append(emb)
|
| 521 |
except Exception:
|
|
|
|
| 7 |
import threading
|
| 8 |
import time
|
| 9 |
import urllib.request
|
| 10 |
+
from contextlib import nullcontext
|
| 11 |
from pathlib import Path
|
| 12 |
from typing import Optional
|
| 13 |
|
|
|
|
| 195 |
_resnet_fallback.to(device)
|
| 196 |
|
| 197 |
|
| 198 |
+
def _autocast_context(device: str):
|
| 199 |
+
if device != "cuda" or _torch is None:
|
| 200 |
+
return nullcontext()
|
| 201 |
+
try:
|
| 202 |
+
return _torch.amp.autocast("cuda")
|
| 203 |
+
except AttributeError:
|
| 204 |
+
return _torch.cuda.amp.autocast()
|
| 205 |
+
|
| 206 |
+
|
| 207 |
def _release_runtime(device: str) -> None:
|
| 208 |
global _device
|
| 209 |
_device = "cpu"
|
|
|
|
| 489 |
if face is not None:
|
| 490 |
face_gpu = face.unsqueeze(0).to(_device)
|
| 491 |
with _torch.no_grad():
|
| 492 |
+
with _autocast_context(_device):
|
| 493 |
emb = _resnet(face_gpu).detach().float().cpu().numpy()[0]
|
| 494 |
embeddings.append(emb)
|
| 495 |
except Exception:
|
|
|
|
| 525 |
crop = Image.fromarray(frame[y1:y2, x1:x2]).convert("RGB")
|
| 526 |
tensor = _transform_fallback(crop).unsqueeze(0).to(_device)
|
| 527 |
with _torch.no_grad():
|
| 528 |
+
with _autocast_context(_device):
|
| 529 |
emb = _resnet_fallback(tensor).detach().float().cpu().numpy()[0]
|
| 530 |
embeddings_fb.append(emb)
|
| 531 |
except Exception:
|
src/engines/fingerprint/engine.py
CHANGED
|
@@ -10,6 +10,7 @@ import logging
|
|
| 10 |
import os
|
| 11 |
import threading
|
| 12 |
import time
|
|
|
|
| 13 |
from typing import Any, Optional
|
| 14 |
|
| 15 |
import numpy as np
|
|
@@ -175,6 +176,15 @@ def _prepare_runtime(device: str) -> None:
|
|
| 175 |
_clip_model.to(device)
|
| 176 |
|
| 177 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 178 |
def _release_runtime(device: str) -> None:
|
| 179 |
if device != "cuda":
|
| 180 |
return
|
|
@@ -285,7 +295,7 @@ class FingerprintEngine:
|
|
| 285 |
inputs = {k: v.to(device) for k, v in inputs.items()}
|
| 286 |
|
| 287 |
with torch.no_grad():
|
| 288 |
-
with
|
| 289 |
outputs = _clip_model(**inputs)
|
| 290 |
logits = outputs.logits_per_image[0].float()
|
| 291 |
image_embeds = outputs.image_embeds.detach().float().cpu().numpy()[0]
|
|
@@ -331,7 +341,7 @@ class FingerprintEngine:
|
|
| 331 |
inputs = {k: v.to(device) for k, v in inputs.items()}
|
| 332 |
|
| 333 |
with torch.no_grad():
|
| 334 |
-
with
|
| 335 |
logits = _clip_model(**inputs).logits_per_image.float()
|
| 336 |
|
| 337 |
probs_batch = logits.softmax(dim=-1).cpu().numpy()
|
|
|
|
| 10 |
import os
|
| 11 |
import threading
|
| 12 |
import time
|
| 13 |
+
from contextlib import nullcontext
|
| 14 |
from typing import Any, Optional
|
| 15 |
|
| 16 |
import numpy as np
|
|
|
|
| 176 |
_clip_model.to(device)
|
| 177 |
|
| 178 |
|
| 179 |
+
def _autocast_context(device: str):
|
| 180 |
+
if device != "cuda":
|
| 181 |
+
return nullcontext()
|
| 182 |
+
try:
|
| 183 |
+
return torch.amp.autocast("cuda")
|
| 184 |
+
except AttributeError:
|
| 185 |
+
return torch.cuda.amp.autocast()
|
| 186 |
+
|
| 187 |
+
|
| 188 |
def _release_runtime(device: str) -> None:
|
| 189 |
if device != "cuda":
|
| 190 |
return
|
|
|
|
| 295 |
inputs = {k: v.to(device) for k, v in inputs.items()}
|
| 296 |
|
| 297 |
with torch.no_grad():
|
| 298 |
+
with _autocast_context(device):
|
| 299 |
outputs = _clip_model(**inputs)
|
| 300 |
logits = outputs.logits_per_image[0].float()
|
| 301 |
image_embeds = outputs.image_embeds.detach().float().cpu().numpy()[0]
|
|
|
|
| 341 |
inputs = {k: v.to(device) for k, v in inputs.items()}
|
| 342 |
|
| 343 |
with torch.no_grad():
|
| 344 |
+
with _autocast_context(device):
|
| 345 |
logits = _clip_model(**inputs).logits_per_image.float()
|
| 346 |
|
| 347 |
probs_batch = logits.softmax(dim=-1).cpu().numpy()
|