Deepfake Authenticator commited on
Commit
578b5d6
·
1 Parent(s): fa1d723

fix: revert float16 (breaks CPU inference), fix micro-batch indentation bug — restore accuracy

Browse files
Files changed (1) hide show
  1. backend/detector.py +5 -22
backend/detector.py CHANGED
@@ -444,15 +444,7 @@ class DecisionAgent:
444
  logger.info(f"Loading model: {cfg['id']}")
445
  proc = ViTImageProcessor.from_pretrained(cfg["id"])
446
  model = ViTForImageClassification.from_pretrained(cfg["id"])
447
-
448
- # ── Float16: 2× faster inference, negligible accuracy loss ──
449
- try:
450
- model = model.half()
451
- logger.info(f"Model {cfg['id']} converted to float16")
452
- except Exception:
453
- pass
454
-
455
- model.eval()
456
 
457
  fake_idx = None
458
  for idx, lbl in model.config.id2label.items():
@@ -502,22 +494,13 @@ class DecisionAgent:
502
  for model_idx, (proc, model, fake_idx) in enumerate(self.models):
503
  try:
504
  model_scores = []
505
- # Process in micro-batches
506
  for i in range(0, len(pil_imgs), MICRO_BATCH):
507
- batch = pil_imgs[i:i + MICRO_BATCH]
508
  inputs = proc(images=batch, return_tensors="pt")
509
-
510
- # Match model dtype
511
- model_dtype = next(model.parameters()).dtype
512
- if model_dtype == torch.float16:
513
- inputs = {
514
- k: v.half() if v.dtype == torch.float32 else v
515
- for k, v in inputs.items()
516
- }
517
-
518
  with torch.no_grad():
519
- logits = model(**inputs).logits
520
- probs = torch.softmax(logits.float(), dim=-1)
521
  scores = probs[:, fake_idx].tolist()
522
  model_scores.extend(scores)
523
 
 
444
  logger.info(f"Loading model: {cfg['id']}")
445
  proc = ViTImageProcessor.from_pretrained(cfg["id"])
446
  model = ViTForImageClassification.from_pretrained(cfg["id"])
447
+ model.eval() # Keep float32 — float16 on CPU produces incorrect results
 
 
 
 
 
 
 
 
448
 
449
  fake_idx = None
450
  for idx, lbl in model.config.id2label.items():
 
494
  for model_idx, (proc, model, fake_idx) in enumerate(self.models):
495
  try:
496
  model_scores = []
497
+ # Process in micro-batches — avoids OOM on CPU
498
  for i in range(0, len(pil_imgs), MICRO_BATCH):
499
+ batch = pil_imgs[i:i + MICRO_BATCH]
500
  inputs = proc(images=batch, return_tensors="pt")
 
 
 
 
 
 
 
 
 
501
  with torch.no_grad():
502
+ logits = model(**inputs).logits # float32
503
+ probs = torch.softmax(logits, dim=-1) # [batch, classes]
504
  scores = probs[:, fake_idx].tolist()
505
  model_scores.extend(scores)
506