-
Notifications
You must be signed in to change notification settings - Fork 0
/
utils.py
35 lines (26 loc) · 869 Bytes
/
utils.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
import re
import os
import torch
def compute_accuracy(model, loader):
if torch.cuda.is_available():
device = 'cuda'
else:
device = 'cpu'
model.eval() # Evaluation mode
predictions = []
raw_predictions = []
ground_truth = []
total_samples = 0
correct_samples = 0
for i_step, (x, y) in enumerate(loader):
x_gpu = x.to(device)
y_gpu = y.to(device)
prediction = model(x_gpu)
indices = torch.argmax(prediction, 1)
correct_samples += torch.sum(indices == y_gpu)
total_samples += y.shape[0]
# store result
raw_predictions.extend(prediction.tolist())
predictions.extend(indices.tolist())
ground_truth.extend(y.tolist())
return float(correct_samples) * 100 / total_samples, predictions, ground_truth, raw_predictions