eval.py
1.85 KB
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
import os
import fire
import json
from pprint import pprint
import torch
import torch.nn as nn
from torch.utils.tensorboard import SummaryWriter
from utils import *
# command
# python eval.py --model_path='logs/April_16_00:26:10__resnet50__None/'
def eval(model_path):
print('\n[+] Parse arguments')
kwargs_path = os.path.join(model_path, 'kwargs.json')
kwargs = json.loads(open(kwargs_path).read())
args, kwargs = parse_args(kwargs)
pprint(args)
device = torch.device('cuda' if args.use_cuda else 'cpu')
print('\n[+] Create network')
model = select_model(args)
optimizer = select_optimizer(args, model)
criterion = nn.CrossEntropyLoss()
if args.use_cuda:
model = model.cuda()
criterion = criterion.cuda()
print('\n[+] Load model')
weight_path = os.path.join(model_path, 'model', 'model.pt')
model.load_state_dict(torch.load(weight_path))
print('\n[+] Load dataset')
test_transform = get_valid_transform(args, model)
#print('\nTEST Transform\n', test_transform)
test_dataset = get_dataset(args, test_transform, 'test')
"""
test_transform
Compose(
Resize(size=[224, 224], interpolation=PIL.Image.BILINEAR)
ToTensor()
)
"""
test_loader = iter(get_dataloader(args, test_dataset)) ###
print('\n[+] Start testing')
writer = SummaryWriter(log_dir=model_path)
_test_res = validate(args, model, criterion, test_loader, step=0, writer=writer)
print('\n[+] Valid results')
print(' Acc@1 : {:.3f}%'.format(_test_res[0].data.cpu().numpy()[0]*100))
print(' Acc@5 : {:.3f}%'.format(_test_res[1].data.cpu().numpy()[0]*100))
print(' Loss : {:.3f}'.format(_test_res[2].data))
print(' Infer Time(per image) : {:.3f}ms'.format(_test_res[3]*1000 / len(test_dataset)))
writer.close()
if __name__ == '__main__':
fire.Fire(eval)