MCPcopy Create free account
hub / github.com/MiniMax-AI/VTP / evaluate

Function evaluate

tools/test_linear_probing_hf.py:303–345  ·  view source on GitHub ↗

Evaluate all classifiers and return accuracies.

(
    feature_model: nn.Module,
    linear_classifiers: AllClassifiers,
    val_loader: DataLoader,
    device: torch.device,
)

Source from the content-addressed store, hash-verified

301
302@torch.no_grad()
303def evaluate(
304 feature_model: nn.Module,
305 linear_classifiers: AllClassifiers,
306 val_loader: DataLoader,
307 device: torch.device,
308) -> Dict[str, float]:
309 """Evaluate all classifiers and return accuracies."""
310 linear_classifiers.eval()
311
312 classifiers_dict = linear_classifiers.module.classifiers_dict if hasattr(linear_classifiers, 'module') else linear_classifiers.classifiers_dict
313
314 correct = {k: 0 for k in classifiers_dict.keys()}
315 total = 0
316
317 progress_bar = tqdm(val_loader, desc="Evaluating") if is_main_process() else val_loader
318
319 for images, labels in progress_bar:
320 images = images.to(device, non_blocking=True)
321 labels = labels.to(device, non_blocking=True)
322
323 features = feature_model(images)
324 outputs = linear_classifiers(features)
325
326 for k, logits in outputs.items():
327 preds = logits.argmax(dim=1)
328 correct[k] += (preds == labels).sum().item()
329
330 total += labels.size(0)
331
332 # Aggregate across processes
333 if dist.is_initialized():
334 # Gather correct counts and total from all processes
335 total_tensor = torch.tensor([total], device=device, dtype=torch.long)
336 dist.all_reduce(total_tensor)
337 total = total_tensor.item()
338
339 for k in correct:
340 correct_tensor = torch.tensor([correct[k]], device=device, dtype=torch.long)
341 dist.all_reduce(correct_tensor)
342 correct[k] = correct_tensor.item()
343
344 accuracies = {k: 100.0 * v / total for k, v in correct.items()}
345 return accuracies
346
347
348# ============================================================================

Callers 1

test_linear_probingFunction · 0.70

Calls 1

is_main_processFunction · 0.85

Tested by

no test coverage detected