-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy patheval.py
290 lines (228 loc) · 10.8 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
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
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
from torch.utils.data import Dataset
from torch.utils.data import DataLoader
import torch.backends.cudnn as cudnn
from torch.autograd import Variable
import torch
from torch.utils.tensorboard import SummaryWriter
import torch.optim as optim
import torch.cuda.amp as amp
import torchvision
from torchvision.transforms import InterpolationMode
from torch.utils import data
import torch.nn.functional as F
from utils.utils import reverse_one_hot, compute_global_accuracy, fast_hist, per_class_iu, colour_code_segmentation,poly_lr_scheduler,cal_miou
from utils.loss import CrossEntropy2d,DiceLoss
import numpy as np
import os
import os.path as osp
import random
import matplotlib.pyplot as plt
import collections
from PIL import Image
import json
import argparse
from tqdm import tqdm
# Dataset class:
from dataset.cityscapesDataSet import cityscapesDataSet
# Network
from model.build_BiSeNet import BiSeNet
## -- VALIDATION --
def val(args, model, dataloader, save=False, batch_size=1, save_path=""):
palette = [[128,64,128],[244,35,232], [70,70,70],[102,102,156],[190,153,153],[153,153,153],[250,170,30],[220,220,0],[107,142,35],[152,251,152],[70,130,180],[220,20,60],[255,0,0],[0,0,142],[0,0,70],[0,60,100],[0,80,100],[0,0,230],[119,11,32],[0,0,0]]
num = list(range(0, len(palette)-1))
num.append(255)
dictionary = dict(zip(num, palette))
if (save):
folder_predict =os.path.join(save_path, "predict")
folder_labels =os.path.join(save_path, "labels")
if not os.path.isdir(folder_predict):
os.mkdir(folder_predict)
if not os.path.isdir(folder_labels):
os.mkdir(folder_labels)
with torch.no_grad():
model.eval()
precision_record = []
hist = np.zeros((args.num_classes, args.num_classes))
tq =tqdm(total=len(dataloader) * batch_size)
tq.set_description('val')
for i, (data, label) in enumerate(dataloader):
tq.update(batch_size)
label = label.type(torch.LongTensor)
if torch.cuda.is_available() and args.use_gpu:
data = data.cuda()
label = label.cuda()
# get RGB predict image
predict = model(data).squeeze()
predict = reverse_one_hot(predict)
predict = np.array(predict.cpu())
# get RGB label image
label = label.squeeze()
label = np.array(label.cpu())
# compute per pixel accuracy
precision = compute_global_accuracy(predict, label)
hist += fast_hist(label.flatten(), predict.flatten(), args.num_classes)
precision_record.append(precision)
if save and i < 20:
# save some images
predict = colour_code_segmentation(np.array(predict), dictionary)
label = colour_code_segmentation(np.array(label), dictionary)
predictImage = Image.fromarray(predict.astype('uint8'), "RGB")
predictImage.save(os.path.join(folder_predict, str(i) + ".png"))
labelImage = Image.fromarray(label.astype('uint8'), "RGB")
labelImage.save(os.path.join(folder_labels, str(i) + ".png"))
precision = np.mean(precision_record)
# miou = np.mean(per_class_iu(hist))
miou_list = per_class_iu(hist) #[:-1]
# miou_dict, miou = cal_miou(miou_list, csv_path)
miou = np.mean(miou_list)
print("")
print('precision per pixel for test: %.3f' % precision)
print('mIoU for validation: %.3f' % miou)
# miou_str = ''
# for key in miou_dict:
# miou_str += '{}:{},\n'.format(key, miou_dict[key])
# print('mIoU for each class:')
# print(miou_str)
return precision, miou
def test(args,model,dataloader, info_json, save_path="", save=False, batch_size=1):
palette = info_json['palette']
num = list(range(0, len(palette)-1))
num.append(255)
dictionary = dict(zip(num, palette))
print('start test!')
if (save):
folder_predict =os.path.join(save_path, "predict")
folder_labels =os.path.join(save_path, "labels")
if not os.path.isdir(folder_predict):
os.mkdir(folder_predict)
if not os.path.isdir(folder_labels):
os.mkdir(folder_labels)
with torch.no_grad():
model.eval()
precision_record = []
tq = tqdm(total=len(dataloader) * batch_size)
tq.set_description('test')
hist = np.zeros((args.num_classes, args.num_classes))
for i, (data, label) in enumerate(dataloader):
tq.update(batch_size)
label = label.type(torch.LongTensor)
if torch.cuda.is_available() and args.use_gpu:
data = data.cuda()
label = label.cuda()
# get RGB predict image
predict = model(data).squeeze()
predict = reverse_one_hot(predict)
predict = np.array(predict.cpu())
# get RGB label image
label = label.squeeze()
label = np.array(label.cpu())
# compute per pixel accuracy
precision = compute_global_accuracy(predict, label)
hist += fast_hist(label.flatten(), predict.flatten(), args.num_classes)
precision_record.append(precision)
if save and i < 20:
# save some images
predict = colour_code_segmentation(np.array(predict), dictionary)
label = colour_code_segmentation(np.array(label), dictionary)
predictImage = Image.fromarray(predict.astype('uint8'), "RGB")
predictImage.save(os.path.join(folder_predict, str(i) + ".png"))
labelImage = Image.fromarray(label.astype('uint8'), "RGB")
labelImage.save(os.path.join(folder_labels, str(i) + ".png"))
precision = np.mean(precision_record)
miou_list = per_class_iu(hist) #[:-1]
miou_dict, miou = cal_miou(miou_list, info_json)
print('')
print('IoU for each class:')
for key in miou_dict:
print('{}:{},'.format(key, miou_dict[key]))
tq.close()
print('---------------------------')
print('precision for test: %.3f' % precision)
print('mIoU for validation: %.3f' % miou)
return precision
def get_arguments(params=[]):
"""Parse all the arguments provided from the CLI.
Returns:
A list of parsed arguments.
"""
parser = argparse.ArgumentParser()
parser.add_argument('--dataset', type=str, default="Cityscapes", help='Dataset you are using.')
parser.add_argument('--crop_width', type=int, default=1024, help='Width of cropped/resized input image to network')
parser.add_argument('--crop_height', type=int, default=512, help='Height of cropped/resized input image to network')
parser.add_argument('--context_path', type=str, default="resnet18", help='The context path model you are using, resnet18, resnet101.')
parser.add_argument('--data', type=str, default='content/data', help='path of training data')
parser.add_argument('--num_workers', type=int, default=8, help='num of workers')
parser.add_argument('--num_classes', type=int, default=19, help='num of object classes')
parser.add_argument('--cuda', type=str, default='0', help='GPU ids used for training')
parser.add_argument('--use_gpu', type=bool, default=True, help='whether to user gpu for training')
parser.add_argument('--pretrained_model_path', type=str, default=None, help='path to pretrained model')
parser.add_argument("--random-seed", type=int, default=42, help="Random seed to have reproducible results.")
parser.add_argument('--save_img_path', type=str, default=None, help='path to folder where to save imgs')
args = parser.parse_args(params)
return args
def main(params):
args = get_arguments(params)
# Set random seed
torch.manual_seed(args.random_seed)
torch.cuda.manual_seed(args.random_seed)
np.random.seed(args.random_seed)
random.seed(args.random_seed)
# create dataset and dataloader
data_root_path = os.path.join(args.data, args.dataset) # /content/data/Cityscapes
val_path = os.path.join(data_root_path, "val.txt") # /content/data/Cityscapes/val.txt
info_path = os.path.join(args.data, args.dataset, "info.json") # /content/data/Cityscapes/info.json
# preprocessing informations:
input_size = (int(args.crop_width), int(args.crop_height))
f = open(info_path)
info = json.load(f)
# mean
img_mean = np.array((104.00698793, 116.66876762, 122.67891434), dtype=np.float32)
img_mean = np.array(img_mean, dtype=np.float32)
# dataset
test_dataset = cityscapesDataSet(root=data_root_path,
list_path = val_path,
info_json = info,
crop_size=input_size,
mean=img_mean)
print(f'test_dataset: {len(test_dataset)}')
image, label = test_dataset[0]
print(f'images shape: {image.shape}')
print(f'label shape: {label.shape}')
# Define dataloaders
dataloader_test = DataLoader(test_dataset, batch_size=1, shuffle=False, num_workers=args.num_workers, pin_memory=True)
# build model
os.environ['CUDA_VISIBLE_DEVICES'] = args.cuda
model = BiSeNet(args.num_classes, args.context_path)
if torch.cuda.is_available() and args.use_gpu:
model = torch.nn.DataParallel(model).cuda()
# load pretrained model
print('load model from %s ...' % args.pretrained_model_path)
checkpoint= torch.load(args.pretrained_model_path)
try: # model comes from step 3
model.module.load_state_dict(checkpoint['segNet_state_dict'])
epoch_start_i = int(checkpoint['iter'])
except: # model comes from step 2
model.module.load_state_dict(checkpoint['model_state_dict'])
epoch_start_i = int(checkpoint['epoch'])
miou_init = float(checkpoint['max_miou'])
print('Done!')
print('Trained until Epoch:', epoch_start_i)
print('- Best miou:', miou_init)
if (args.save_img_path is not None):
if not os.path.isdir(args.save_img_path):
os.mkdir(args.save_img_path)
# test
test(args,model, dataloader_test, info, save=True, batch_size=1, save_path=args.save_img_path)
else:
# test
test(args,model, dataloader_test, info, save=False, batch_size=1)
if __name__ == '__main__':
params = [
'--pretrained_model_path', '/gdrive/MyDrive/Project_AML/Models/segmentation/checkpoints-m_resnet18-sgd-e_50-b_4-c_1024_512/latest_CE_loss.pth',
'--data', '/content/data',
'--cuda', '0',
'--context_path', 'resnet18',
'--num_classes', '19',
'--save_img_path', '/gdrive/MyDrive/Project_AML/Output/segmentation/noaug'
]
main(params)