-
Notifications
You must be signed in to change notification settings - Fork 0
/
eval.py
34 lines (26 loc) · 1.03 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
import torch
from torch import nn
from model import *
from tqdm import tqdm
import warnings
warnings.filterwarnings('ignore')
def evaluate(args, test_iter, pretrained_dir):
# get net
device = f'cuda:{args.cuda}' if args.cuda >= 0 and torch.cuda.is_available() else 'cpu'
net = CNN().to(device)
net.load_state_dict(torch.load(pretrained_dir, map_location=device))
# set loss function
loss_func = nn.CrossEntropyLoss()
with torch.no_grad():
test_corr = 0
print('Start evaluation on test set...')
for _, (img, labels) in enumerate(tqdm(test_iter)):
net.eval() # test
img, labels = img.to(device), labels.to(device)
pred = net(img)
loss = loss_func(pred, labels.long()).mean()
_, pred = torch.max(pred.data, dim=1)
batch_corr = pred.eq(labels.data).cpu().sum().item()
test_corr += batch_corr
test_acc = 100.0 * test_corr / args.batch_size / len(test_iter)
print(f'test_acc = {test_acc:.4f}')