-
Notifications
You must be signed in to change notification settings - Fork 1
/
utils.py
29 lines (22 loc) · 757 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
import re
import os
import torch
def compute_accuracy(model, loader):
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
y_gpu = y
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) / total_samples, predictions, ground_truth, raw_predictions