Run zero-shot evaluation. Args: model: VTPModel instance. classifier: Zero-shot classifier weights, shape (embed_dim, num_classes). dataloader: ImageNet validation dataloader. device: Device to use. precision: Precision for inference. Returns:
(
model: VTPModel,
classifier: torch.Tensor,
dataloader: DataLoader,
device: torch.device,
precision: str = 'fp32',
)
| 399 | # ============================================================================ |
| 400 | |
| 401 | def evaluate( |
| 402 | model: VTPModel, |
| 403 | classifier: torch.Tensor, |
| 404 | dataloader: DataLoader, |
| 405 | device: torch.device, |
| 406 | precision: str = 'fp32', |
| 407 | ) -> Tuple[float, float]: |
| 408 | """Run zero-shot evaluation. |
| 409 | |
| 410 | Args: |
| 411 | model: VTPModel instance. |
| 412 | classifier: Zero-shot classifier weights, shape (embed_dim, num_classes). |
| 413 | dataloader: ImageNet validation dataloader. |
| 414 | device: Device to use. |
| 415 | precision: Precision for inference. |
| 416 | |
| 417 | Returns: |
| 418 | Tuple of (top1_accuracy, top5_accuracy). |
| 419 | """ |
| 420 | autocast_ctx = get_autocast_context(precision, device_type=device.type) |
| 421 | input_dtype = get_input_dtype(precision) |
| 422 | |
| 423 | top1, top5, n = 0.0, 0.0, 0 |
| 424 | |
| 425 | with torch.inference_mode(): |
| 426 | for images, targets in tqdm(dataloader, desc="Evaluating"): |
| 427 | images = images.to(device=device, dtype=input_dtype) |
| 428 | targets = targets.to(device) |
| 429 | |
| 430 | with autocast_ctx(): |
| 431 | image_features = model.get_clip_image_feature(images, normalize=True) |
| 432 | logits = 100.0 * image_features @ classifier |
| 433 | |
| 434 | acc1, acc5 = accuracy(logits, targets, topk=(1, 5)) |
| 435 | top1 += acc1 |
| 436 | top5 += acc5 |
| 437 | n += images.size(0) |
| 438 | |
| 439 | top1 = top1 / n * 100 |
| 440 | top5 = top5 / n * 100 |
| 441 | return top1, top5 |
| 442 | |
| 443 | |
| 444 | # ============================================================================ |
no test coverage detected