-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest.py
91 lines (68 loc) · 2.25 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
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
import torch
from torch import nn
from torchvision import transforms
from torch.utils.data import DataLoader
import os
from sklearn.metrics import f1_score
from dataset import BlinkDataset
import model as m
CONFIG = {
'dataset': '/db/mEBAL/testdata.txt',
'ckpt': './ckpt',
'resize': (50, 50),
'workers': 8,
'batch_size': 256,
'model': 'ResNet20'
}
def main():
trainlst = CONFIG['dataset']
transform = transforms.Compose([
transforms.ToTensor(),
transforms.Resize(CONFIG['resize'])
])
dataset = BlinkDataset(trainlst, transform)
testloader = DataLoader(dataset,
batch_size=CONFIG['batch_size'],
shuffle=False,
num_workers=CONFIG['workers'],
pin_memory=True)
model = getattr(m, CONFIG['model'])(3, 2)
device = 'cpu'
if torch.cuda.is_available():
device = 'cuda:0'
if torch.cuda.device_count() > 1:
model = nn.DataParallel(model)
model.to(device)
criterion = nn.CrossEntropyLoss()
test_batches = len(testloader)
print(f'TEST BATCHES: {test_batches}')
best_ckpt = os.path.join(CONFIG['ckpt'], 'best.pth')
state_dict = torch.load(best_ckpt)
model.load_state_dict(state_dict)
model.eval()
testloss = 0.0
testf1 = 0.0
total = 0
correct = 0
for data in testloader:
with torch.no_grad():
left_eyes, right_eyes, labels = data
left_eyes = left_eyes.to(device)
right_eyes = right_eyes.to(device)
labels = labels.to(device)
outputs = model(left_eyes, right_eyes)
_, predicted = torch.max(outputs.data, 1)
total += labels.size(0)
correct += (predicted == labels).sum().item()
testf1 += f1_score(labels.cpu().numpy(),
predicted.cpu().numpy())
loss = criterion(outputs, labels)
testloss += loss.item()
testloss /= test_batches
acc = correct / total
testf1 /= test_batches
print_str = f'TEST LOSS: {testloss:.4f}, TEST ACCURACY: {acc:.3f} '
print_str += f'TEST F1: {testf1:.3f}'
print(print_str)
if __name__ == '__main__':
main()