-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest.py
32 lines (24 loc) · 893 Bytes
/
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
import torch
import torch.nn as nn
import torch.optim as optim
import cifar
import model
def test(model):
device = torch.device("cuda:0" if torch.cuda.is_available() else "cpu")
model = model.to(device)
testSet = cifar.loadCIFAR10(train=False)
dataloader = torch.utils.data.DataLoader(testSet, batch_size=128) # to avoid CUDA out of mem. errors
corrects = 0
for inputs, labels in dataloader:
inputs = inputs.to(device)
labels = labels.to(device)
outputs = model(inputs)
_, preds = torch.max(outputs, 1)
corrects += torch.sum(preds == labels.data)
print("Test set accuracy: {:.3f}%".format(100 * corrects / len(dataloader.dataset)))
if __name__ == '__main__':
model = model.VGG16()
model.load_state_dict(torch.load('checkpoints/checkpoint.pt'))
model.eval()
with torch.no_grad():
test(model)