forked from dk-liang/FIDTM
-
Notifications
You must be signed in to change notification settings - Fork 0
/
train_baseline.py
390 lines (299 loc) · 12.5 KB
/
train_baseline.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
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
from __future__ import division
import warnings
from Networks.HR_Net.seg_hrnet import get_seg_model
import torch.nn as nn
import torch.nn.functional as F
from torchvision import datasets, transforms
import dataset
import math
from image import *
from utils import *
import logging
import nni
from nni.utils import merge_parameter
from config import return_args, args
import time
warnings.filterwarnings('ignore')
'''fixed random seed '''
setup_seed(args.seed)
logger = logging.getLogger('mnist_AutoML')
def main(args):
if args['dataset'] == 'ShanghaiA':
train_file = './npydata/ShanghaiA_train.npy'
test_file = './npydata/ShanghaiA_test.npy'
elif args['dataset'] == 'ShanghaiB':
train_file = './npydata/ShanghaiB_train.npy'
test_file = './npydata/ShanghaiB_test.npy'
elif args['dataset'] == 'UCF_QNRF':
train_file = './npydata/qnrf_train.npy'
test_file = './npydata/qnrf_test.npy'
elif args['dataset'] == 'JHU':
train_file = './npydata/jhu_train.npy'
test_file = './npydata/jhu_val.npy'
elif args['dataset'] == 'NWPU':
train_file = './npydata/nwpu_train.npy'
test_file = './npydata/nwpu_val.npy'
with open(train_file, 'rb') as outfile:
train_list = np.load(outfile).tolist()
with open(test_file, 'rb') as outfile:
test_list = np.load(outfile).tolist()
os.environ['CUDA_VISIBLE_DEVICES'] = args['gpu_id']
model = get_seg_model(train=True)
model = nn.DataParallel(model, device_ids=[0])
model = model.cuda()
optimizer = torch.optim.Adam(
[ #
{'params': model.parameters(), 'lr': args['lr']},
], lr=args['lr'], weight_decay=args['weight_decay'])
criterion = nn.MSELoss(size_average=False).cuda()
print(args['pre'])
if not os.path.exists(args['save_path']):
os.makedirs(args['save_path'])
if args['pre']:
if os.path.isfile(args['pre']):
print("=> loading checkpoint '{}'".format(args['pre']))
checkpoint = torch.load(args['pre'])
model.load_state_dict(checkpoint['state_dict'], strict=False)
args['start_epoch'] = checkpoint['epoch']
args['best_pred'] = checkpoint['best_prec1']
else:
print("=> no checkpoint found at '{}'".format(args['pre']))
torch.set_num_threads(args['workers'])
print(args['best_pred'], args['start_epoch'])
if args['preload_data'] == True:
train_data = pre_data(train_list, args, train=True)
test_data = pre_data(test_list, args, train=False)
else:
train_data = train_list
test_data = test_list
for epoch in range(args['start_epoch'], args['epochs']):
start = time.time()
train(train_data, model, criterion, optimizer, epoch, args)
end1 = time.time()
'''inference '''
if epoch % 10 == 0 and epoch >= 200:
prec1, visi = validate(test_data, model, args)
end2 = time.time()
is_best = prec1 < args['best_pred']
args['best_pred'] = min(prec1, args['best_pred'])
print(' * best MAE {mae:.3f} '.format(mae=args['best_pred']), args['save_path'], end1 - start, end2 - end1)
save_checkpoint({
'epoch': epoch + 1,
'arch': args['pre'],
'state_dict': model.state_dict(),
'best_prec1': args['best_pred'],
'optimizer': optimizer.state_dict(),
}, visi, is_best, args['save_path'])
def pre_data(train_list, args, train):
print("Pre_load dataset ......")
data_keys = {}
count = 0
for j in range(len(train_list)):
Img_path = train_list[j]
fname = os.path.basename(Img_path)
# print(fname)
img, fidt_map, kpoint = load_data_fidt(Img_path, args, train)
if min(fidt_map.shape[0], fidt_map.shape[1]) < 256 and train == True:
# ignore some small resolution images
continue
# print(img.size, fidt_map.shape)
blob = {}
blob['img'] = img
blob['kpoint'] = np.array(kpoint)
blob['fidt_map'] = fidt_map
blob['fname'] = fname
data_keys[count] = blob
count += 1
return data_keys
def train(Pre_data, model, criterion, optimizer, epoch, args):
losses = AverageMeter()
batch_time = AverageMeter()
data_time = AverageMeter()
train_loader = torch.utils.data.DataLoader(
dataset.listDataset(Pre_data, args['save_path'],
shuffle=True,
transform=transforms.Compose([
transforms.ToTensor(),
transforms.Normalize(mean=[0.485, 0.456, 0.406],
std=[0.229, 0.224, 0.225]),
]),
train=True,
batch_size=args['batch_size'],
num_workers=args['workers'],
args=args),
batch_size=args['batch_size'], drop_last=False)
args['lr'] = optimizer.param_groups[0]['lr']
print('epoch %d, processed %d samples, lr %.10f' % (epoch, epoch * len(train_loader.dataset), args['lr']))
model.train()
end = time.time()
for i, (fname, img, fidt_map, kpoint) in enumerate(train_loader):
data_time.update(time.time() - end)
img = img.cuda()
fidt_map = fidt_map.type(torch.FloatTensor).unsqueeze(1).cuda()
d6 = model(img)
if d6.shape != fidt_map.shape:
print("the shape is wrong, please check. Both of prediction and GT should be [B, C, H, W].")
exit()
loss = criterion(d6, fidt_map)
losses.update(loss.item(), img.size(0))
optimizer.zero_grad()
loss.backward()
optimizer.step()
batch_time.update(time.time() - end)
end = time.time()
if i % args['print_freq'] == 0:
print('4_Epoch: [{0}][{1}/{2}]\t'
'Time {batch_time.val:.3f} ({batch_time.avg:.3f})\t'
'Data {data_time.val:.3f} ({data_time.avg:.3f})\t'
'Loss {loss.val:.4f} ({loss.avg:.4f})\t'
.format(
epoch, i, len(train_loader), batch_time=batch_time,
data_time=data_time, loss=losses))
def validate(Pre_data, model, args):
print('begin test')
batch_size = 1
test_loader = torch.utils.data.DataLoader(
dataset.listDataset(Pre_data, args['save_path'],
shuffle=False,
transform=transforms.Compose([
transforms.ToTensor(), transforms.Normalize(mean=[0.485, 0.456, 0.406],
std=[0.229, 0.224, 0.225]),
]),
args=args, train=False),
batch_size=1)
model.eval()
mae = 0.0
mse = 0.0
visi = []
index = 0
if not os.path.exists('./local_eval/loc_file'):
os.makedirs('./local_eval/loc_file')
'''output coordinates'''
f_loc = open("./local_eval/A_localization.txt", "w+")
for i, (fname, img, fidt_map, kpoint) in enumerate(test_loader):
count = 0
img = img.cuda()
if len(img.shape) == 5:
img = img.squeeze(0)
if len(fidt_map.shape) == 5:
fidt_map = fidt_map.squeeze(0)
if len(img.shape) == 3:
img = img.unsqueeze(0)
if len(fidt_map.shape) == 3:
fidt_map = fidt_map.unsqueeze(0)
with torch.no_grad():
d6 = model(img)
'''return counting and coordinates'''
count, pred_kpoint, f_loc = LMDS_counting(d6, i + 1, f_loc, args)
point_map = generate_point_map(pred_kpoint, f_loc, rate=1)
if args['visual'] == True:
if not os.path.exists(args['save_path'] + '_box/'):
os.makedirs(args['save_path'] + '_box/')
ori_img, box_img = generate_bounding_boxes(pred_kpoint, fname)
show_fidt = show_map(d6.data.cpu().numpy())
gt_show = show_map(fidt_map.data.cpu().numpy())
res = np.hstack((ori_img, gt_show, show_fidt, point_map, box_img))
cv2.imwrite(args['save_path'] + '_box/' + fname[0], res)
gt_count = torch.sum(kpoint).item()
mae += abs(gt_count - count)
mse += abs(gt_count - count) * abs(gt_count - count)
if i % 15 == 0:
print('{fname} Gt {gt:.2f} Pred {pred}'.format(fname=fname[0], gt=gt_count, pred=count))
visi.append(
[img.data.cpu().numpy(), d6.data.cpu().numpy(), fidt_map.data.cpu().numpy(),
fname])
index += 1
mae = mae * 1.0 / (len(test_loader) * batch_size)
mse = math.sqrt(mse / (len(test_loader)) * batch_size)
nni.report_intermediate_result(mae)
print(' \n* MAE {mae:.3f}\n'.format(mae=mae), '* MSE {mse:.3f}'.format(mse=mse))
return mae, visi
def LMDS_counting(input, w_fname, f_loc, args):
input_max = torch.max(input).item()
''' find local maxima'''
if args['dataset'] == 'UCF_QNRF' :
input = nn.functional.avg_pool2d(input, (3, 3), stride=1, padding=1)
keep = nn.functional.max_pool2d(input, (3, 3), stride=1, padding=1)
else:
keep = nn.functional.max_pool2d(input, (3, 3), stride=1, padding=1)
keep = (keep == input).float()
input = keep * input
'''set the pixel valur of local maxima as 1 for counting'''
input[input < 100.0 / 255.0 * input_max] = 0
input[input > 0] = 1
''' negative sample'''
if input_max < 0.1:
input = input * 0
count = int(torch.sum(input).item())
kpoint = input.data.squeeze(0).squeeze(0).cpu().numpy()
f_loc.write('{} {} '.format(w_fname, count))
return count, kpoint, f_loc
def generate_point_map(kpoint, f_loc, rate=1):
'''obtain the location coordinates'''
pred_coor = np.nonzero(kpoint)
point_map = np.zeros((int(kpoint.shape[0] * rate), int(kpoint.shape[1] * rate), 3), dtype="uint8") + 255 # 22
# count = len(pred_coor[0])
coord_list = []
for i in range(0, len(pred_coor[0])):
h = int(pred_coor[0][i] * rate)
w = int(pred_coor[1][i] * rate)
coord_list.append([w, h])
cv2.circle(point_map, (w, h), 2, (0, 0, 0), -1)
for data in coord_list:
f_loc.write('{} {} '.format(math.floor(data[0]), math.floor(data[1])))
f_loc.write('\n')
return point_map
def generate_bounding_boxes(kpoint, fname):
'''change the data path'''
Img_data = cv2.imread(
'/home/dkliang/projects/synchronous/datasets/ShanghaiTech/part_A_final/test_data/images/' + fname[0])
ori_Img_data = Img_data.copy()
'''generate sigma'''
pts = np.array(list(zip(np.nonzero(kpoint)[1], np.nonzero(kpoint)[0])))
leafsize = 2048
# build kdtree
tree = scipy.spatial.KDTree(pts.copy(), leafsize=leafsize)
distances, locations = tree.query(pts, k=4)
for index, pt in enumerate(pts):
pt2d = np.zeros(kpoint.shape, dtype=np.float32)
pt2d[pt[1], pt[0]] = 1.
if np.sum(kpoint) > 1:
sigma = (distances[index][1] + distances[index][2] + distances[index][3]) * 0.1
else:
sigma = np.average(np.array(kpoint.shape)) / 2. / 2. # case: 1 point
sigma = min(sigma, min(Img_data.shape[0], Img_data.shape[1]) * 0.05)
if sigma < 6:
t = 2
else:
t = 2
Img_data = cv2.rectangle(Img_data, (int(pt[0] - sigma), int(pt[1] - sigma)),
(int(pt[0] + sigma), int(pt[1] + sigma)), (0, 255, 0), t)
return ori_Img_data, Img_data
def show_map(input):
input[input < 0] = 0
input = input[0][0]
fidt_map1 = input
fidt_map1 = fidt_map1 / np.max(fidt_map1) * 255
fidt_map1 = fidt_map1.astype(np.uint8)
fidt_map1 = cv2.applyColorMap(fidt_map1, 2)
return fidt_map1
class AverageMeter(object):
"""Computes and stores the average and current value"""
def __init__(self):
self.reset()
def reset(self):
self.val = 0
self.avg = 0
self.sum = 0
self.count = 0
def update(self, val, n=1):
self.val = val
self.sum += val * n
self.count += n
self.avg = self.sum / self.count
if __name__ == '__main__':
tuner_params = nni.get_next_parameter()
logger.debug(tuner_params)
params = vars(merge_parameter(return_args, tuner_params))
print(params)
main(params)