forked from Reagan1311/DABNet
-
Notifications
You must be signed in to change notification settings - Fork 0
/
test.py
177 lines (153 loc) · 7.01 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
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
import os
import time
import torch
import numpy as np
import torch.backends.cudnn as cudnn
from torch.autograd import Variable
from argparse import ArgumentParser
# user
from builders.model_builder import build_model
from builders.dataset_builder import build_dataset_test
from utils.utils import save_predict
from utils.metric import get_iou
from utils.convert_state import convert_state_dict
def test(args, test_loader, model):
"""
args:
test_loader: loaded for test dataset
model: model
return: class IoU and mean IoU
"""
# evaluation or test mode
model.eval()
total_batches = len(test_loader)
data_list = []
for i, (input, label, size, name) in enumerate(test_loader):
with torch.no_grad():
input_var = Variable(input).cuda()
start_time = time.time()
output = model(input_var)
torch.cuda.synchronize()
time_taken = time.time() - start_time
print('[%d/%d] time: %.2f' % (i + 1, total_batches, time_taken))
output = output.cpu().data[0].numpy()
gt = np.asarray(label[0].numpy(), dtype=np.uint8)
output = output.transpose(1, 2, 0)
output = np.asarray(np.argmax(output, axis=2), dtype=np.uint8)
data_list.append([gt.flatten(), output.flatten()])
# save the predicted image
if args.save:
save_predict(output, gt, name[0], args.dataset, args.save_seg_dir,
output_grey=False, output_color=True, gt_color=True)
meanIoU, per_class_iu = get_iou(data_list, args.classes)
return meanIoU, per_class_iu
def test_model(args):
"""
main function for testing
param args: global arguments
return: None
"""
print(args)
if args.cuda:
print("=====> use gpu id: '{}'".format(args.gpus))
os.environ["CUDA_VISIBLE_DEVICES"] = args.gpus
if not torch.cuda.is_available():
raise Exception("no GPU found or wrong gpu id, please run without --cuda")
# build the model
model = build_model(args.model, num_classes=args.classes)
if args.cuda:
model = model.cuda() # using GPU for inference
cudnn.benchmark = True
if args.save:
if not os.path.exists(args.save_seg_dir):
os.makedirs(args.save_seg_dir)
# load the test set
datas, testLoader = build_dataset_test(args.dataset, args.num_workers)
if not args.best:
if args.checkpoint:
if os.path.isfile(args.checkpoint):
print("=====> loading checkpoint '{}'".format(args.checkpoint))
checkpoint = torch.load(args.checkpoint)
model.load_state_dict(checkpoint['model'])
# model.load_state_dict(convert_state_dict(checkpoint['model']))
else:
print("=====> no checkpoint found at '{}'".format(args.checkpoint))
raise FileNotFoundError("no checkpoint found at '{}'".format(args.checkpoint))
print("=====> beginning validation")
print("validation set length: ", len(testLoader))
mIOU_val, per_class_iu = test(args, testLoader, model)
print(mIOU_val)
print(per_class_iu)
# Get the best test result among the last 10 model records.
else:
if args.checkpoint:
if os.path.isfile(args.checkpoint):
dirname, basename = os.path.split(args.checkpoint)
epoch = int(os.path.splitext(basename)[0].split('_')[1])
mIOU_val = []
per_class_iu = []
for i in range(epoch - 9, epoch + 1):
basename = 'model_' + str(i) + '.pth'
resume = os.path.join(dirname, basename)
checkpoint = torch.load(resume)
model.load_state_dict(checkpoint['model'])
print("=====> beginning test the" + basename)
print("validation set length: ", len(testLoader))
mIOU_val_0, per_class_iu_0 = test(args, testLoader, model)
mIOU_val.append(mIOU_val_0)
per_class_iu.append(per_class_iu_0)
index = list(range(epoch - 9, epoch + 1))[np.argmax(mIOU_val)]
print("The best mIoU among the last 10 models is", index)
print(mIOU_val)
per_class_iu = per_class_iu[np.argmax(mIOU_val)]
mIOU_val = np.max(mIOU_val)
print(mIOU_val)
print(per_class_iu)
else:
print("=====> no checkpoint found at '{}'".format(args.checkpoint))
raise FileNotFoundError("no checkpoint found at '{}'".format(args.checkpoint))
# Save the result
if not args.best:
model_path = os.path.splitext(os.path.basename(args.checkpoint))
args.logFile = 'test_' + model_path[0] + '.txt'
logFileLoc = os.path.join(os.path.dirname(args.checkpoint), args.logFile)
else:
args.logFile = 'test_' + 'best' + str(index) + '.txt'
logFileLoc = os.path.join(os.path.dirname(args.checkpoint), args.logFile)
# Save the result
if os.path.isfile(logFileLoc):
logger = open(logFileLoc, 'a')
else:
logger = open(logFileLoc, 'w')
logger.write("Mean IoU: %.4f" % mIOU_val)
logger.write("\nPer class IoU: ")
for i in range(len(per_class_iu)):
logger.write("%.4f\t" % per_class_iu[i])
logger.flush()
logger.close()
if __name__ == '__main__':
parser = ArgumentParser()
parser.add_argument('--model', default="DABNet", help="model name: Context Guided Network (CGNet)")
parser.add_argument('--dataset', default="cityscapes", help="dataset: cityscapes or camvid")
parser.add_argument('--num_workers', type=int, default=1, help="the number of parallel threads")
parser.add_argument('--batch_size', type=int, default=1,
help=" the batch_size is set to 1 when evaluating or testing")
parser.add_argument('--checkpoint', type=str,
default="./checkpoint/camvid/DABNetbs16gpu1_trainval/model_1000.pth",
help="use the file to load the checkpoint for evaluating or testing ")
parser.add_argument('--save_seg_dir', type=str, default="./result/",
help="saving path of prediction result")
parser.add_argument('--best', action='store_true', help="Get the best result among last few checkpoints")
parser.add_argument('--save', action='store_true', help="Save the predicted image")
parser.add_argument('--cuda', default=True, help="run on CPU or GPU")
parser.add_argument("--gpus", default="0", type=str, help="gpu ids (default: 0)")
args = parser.parse_args()
args.save_seg_dir = os.path.join(args.save_seg_dir, args.dataset, args.model)
if args.dataset == 'cityscapes':
args.classes = 19
elif args.dataset == 'camvid':
args.classes = 11
else:
raise NotImplementedError(
"This repository now supports two datasets: cityscapes and camvid, %s is not included" % args.dataset)
test_model(args)