-
Notifications
You must be signed in to change notification settings - Fork 53
/
eval_torch.py
103 lines (82 loc) · 3.05 KB
/
eval_torch.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
92
93
94
95
96
97
98
99
100
101
102
103
import os
import os.path as osp
import numpy as np
import torch
import torch.nn as nn
from torch.nn import functional as F
from torch.nn import DataParallel
from torch.utils.data import DataLoader
from dataset import ValDataset
from metric import fast_hist, cal_scores
from network import EANet
import settings
from tensorboardX import SummaryWriter
logger = settings.logger
class Session:
def __init__(self, dt_split):
torch.cuda.set_device(settings.DEVICE)
self.log_dir = settings.LOG_DIR
self.model_dir = settings.MODEL_DIR
self.net = EANet(settings.N_CLASSES, settings.N_LAYERS).cuda()
self.net = DataParallel(self.net, device_ids=[settings.DEVICE])
dataset = ValDataset(split=dt_split)
self.dataloader = DataLoader(dataset, batch_size=1, shuffle=False,
num_workers=2, drop_last=False)
self.hist = 0
def load_checkpoints(self, name):
ckp_path = osp.join(self.model_dir, name)
try:
obj = torch.load(ckp_path,
map_location=lambda storage, loc: storage.cuda())
logger.info('Load checkpoint %s.' % ckp_path)
except FileNotFoundError:
logger.info('No checkpoint %s!' % ckp_path)
return
self.net.module.load_state_dict(obj['net'])
def inf_batch(self, image, label):
image = image.cuda()
label = label.cuda()
with torch.no_grad():
logit = self.net(image)
pred = logit.max(dim=1)[1]
self.hist += fast_hist(label, pred)
def main(ckp_name='step_24000.pth'):
sess = Session(dt_split='val')
sess.load_checkpoints(ckp_name)
dt_iter = sess.dataloader
sess.net.eval()
for i, [image, label] in enumerate(dt_iter):
sess.inf_batch(image, label)
if i % 10 == 0:
logger.info('num-%d' % i)
scores, cls_iu = cal_scores(sess.hist.cpu().numpy())
for k, v in scores.items():
logger.info('%s-%f' % (k, v))
scores, cls_iu = cal_scores(sess.hist.cpu().numpy())
for k, v in scores.items():
logger.info('%s-%f' % (k, v))
logger.info('')
for k, v in cls_iu.items():
logger.info('%s-%f' % (k, v))
def eval_epoch(writer, iterations):
sess = Session(dt_split='val')
sess.load_checkpoints('latest.pth')
dt_iter = sess.dataloader
sess.net.eval()
for i, [image, label] in enumerate(dt_iter):
sess.inf_batch(image, label)
if i % 10 == 0:
logger.info('num-%d' % i)
scores, cls_iu = cal_scores(sess.hist.cpu().numpy())
for k, v in scores.items():
logger.info('%s-%f' % (k, v))
scores, cls_iu = cal_scores(sess.hist.cpu().numpy())
for k, v in scores.items():
logger.info('%s-%f' % (k, v))
logger.info('')
for k, v in cls_iu.items():
logger.info('%s-%f' % (k, v))
writer.add_scalar('mIoU', scores['mIoU'], iterations)
writer.add_scalar('Acc', scores['pAcc'], iterations)
if __name__ == '__main__':
main()