-
Notifications
You must be signed in to change notification settings - Fork 0
/
eval.py
36 lines (27 loc) · 1.08 KB
/
eval.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
import torch
class Eval:
"""Class to evaluate the model on the testloader"""
def __init__(self, testloader: torch.utils.data.dataloader.DataLoader, device: torch.device) -> None:
"""Method to initialize the class
:param testloader: the testloader
:type testloader: torch.utils.data.dataloader.DataLoader
:param device: the device to use
:type device: torch.device
"""
self._testloader = testloader
self.device = device
def eval(self, model) -> int:
correct = 0
total = 0
with torch.no_grad():
for data in self._testloader:
images, labels = data
images = images.to(self.device)
labels = labels.to(self.device)
outputs = model(images)
_, predicted = torch.max(outputs.data, 1)
total += labels.size(0)
for i in range(len(labels)):
if torch.max(labels[i], 0)[1] == predicted[i]:
correct += 1
return 100 * correct / total