-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathevaluate.py
43 lines (29 loc) · 852 Bytes
/
evaluate.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
import model
import torch
from torch import nn
from tqdm.auto import tqdm
from torch.utils.data import DataLoader
@torch.inference_mode()
def evaluate(
model: nn.Module,
dataloader: DataLoader,
extra_preprocess = None
) -> float:
model.eval()
num_samples = 0
num_correct = 0
for inputs, targets in tqdm(dataloader, desc="eval", leave=False):
# Move the data from CPU to GPU
inputs = inputs.cuda()
if extra_preprocess is not None:
for preprocess in extra_preprocess:
inputs = preprocess(inputs)
targets = targets.cuda()
# Inference
outputs = model(inputs)
# Convert logits to class indices
outputs = outputs.argmax(dim=1)
# Update metrics
num_samples += targets.size(0)
num_correct += (outputs == targets).sum()
return (num_correct / num_samples * 100).item()