-
Notifications
You must be signed in to change notification settings - Fork 0
/
test.py
35 lines (28 loc) · 1.12 KB
/
test.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
import torch
import torch.nn as nn
import torch.nn.functional as F
from _config import MODEL_PATH
from WeedNet import WeedNet
from prepare_data import create_loader, test_transforms, test_loader
weed_net = WeedNet()
device = torch.device("cpu")
# test_loader = create_loader('test/', test_transforms)
def test(test_loader, model, device = device):
"""
Loads the test data set and tests the model on it.
Returns achieved loss, correctly guessed samples and accuracy.
"""
model.eval()
test_loss = 0
correct = 0
with torch.no_grad():
for data, target in test_loader:
data, target = data.to(device), target.to(device)
output = model(data)
test_loss += F.nll_loss(output, target, reduction = 'sum').item()
pred = output.argmax(dim = 1, keepdim = True)
correct += pred.eq(target.view_as(pred)).sum().item()
test_loss /= len(test_loader.dataset)
accuracy_percent = 100 * correct/len(test_loader.dataset)
print('\nTest set: Average loss: {:.4f}, Accuracy: {}/{} ({:.0f}%)\n'.format(test_loss, correct, len(test_loader.dataset), accuracy_percent))
return test_loss, correct, len(test_loader.dataset), accuracy_percent