-
Notifications
You must be signed in to change notification settings - Fork 0
/
train_yolo.py
520 lines (450 loc) · 23.5 KB
/
train_yolo.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
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
import os
import sys
import argparse
import shutil
import time
import random
import gc
import json
from distutils.version import LooseVersion
import scipy.misc
import logging
import matplotlib as mpl
mpl.use('Agg')
from matplotlib import pyplot as plt
import pickle
from PIL import Image
import numpy as np
import torch
import torch.nn as nn
import torch.nn.parallel
import torch.backends.cudnn as cudnn
import torch.distributed as dist
import torch.optim
import torch.utils.data as data
import torch.utils.data.distributed
import torchvision
import torchvision.transforms as transforms
import torchvision.datasets as datasets
import torchvision.models as models
import torch.nn.functional as F
from torch.autograd import Variable
from torch.utils.data import DataLoader
from torchvision.transforms import Compose, ToTensor, Normalize
from utils.transforms import ResizeImage, ResizeAnnotation
from dataset.talk2car_loader import *
from model.grounding_model import *
from utils.parsing_metrics import *
from utils.utils import *
def yolo_loss(input, target, gi, gj, best_n_list, w_coord=5., w_neg=1./5, size_average=True):
mseloss = torch.nn.MSELoss(size_average=True)
celoss = torch.nn.CrossEntropyLoss(size_average=True)
batch = input[0].size(0)
pred_bbox = Variable(torch.zeros(batch,4).cuda())
gt_bbox = Variable(torch.zeros(batch,4).cuda())
for ii in range(batch):
pred_bbox[ii, 0:2] = F.sigmoid(input[best_n_list[ii]//3][ii,best_n_list[ii]%3,0:2,gj[ii],gi[ii]])
pred_bbox[ii, 2:4] = input[best_n_list[ii]//3][ii,best_n_list[ii]%3,2:4,gj[ii],gi[ii]]
gt_bbox[ii, :] = target[best_n_list[ii]//3][ii,best_n_list[ii]%3,:4,gj[ii],gi[ii]]
loss_x = mseloss(pred_bbox[:,0], gt_bbox[:,0])
loss_y = mseloss(pred_bbox[:,1], gt_bbox[:,1])
loss_w = mseloss(pred_bbox[:,2], gt_bbox[:,2])
loss_h = mseloss(pred_bbox[:,3], gt_bbox[:,3])
pred_conf_list, gt_conf_list = [], []
for scale_ii in range(len(input)):
pred_conf_list.append(input[scale_ii][:,:,4,:,:].contiguous().view(batch,-1))
gt_conf_list.append(target[scale_ii][:,:,4,:,:].contiguous().view(batch,-1))
pred_conf = torch.cat(pred_conf_list, dim=1)
gt_conf = torch.cat(gt_conf_list, dim=1)
loss_conf = celoss(pred_conf, gt_conf.max(1)[1])
return (loss_x+loss_y+loss_w+loss_h)*w_coord + loss_conf
def lr_poly(base_lr, iter, max_iter, power):
return base_lr * ((1 - float(iter) / max_iter) ** (power))
def adjust_learning_rate(optimizer, i_iter):
# print(optimizer.param_groups[0]['lr'], optimizer.param_groups[1]['lr'])
if args.power!=0.:
lr = lr_poly(args.lr, i_iter, args.nb_epoch, args.power)
optimizer.param_groups[0]['lr'] = lr
if len(optimizer.param_groups) > 1:
optimizer.param_groups[1]['lr'] = lr / 10
def save_checkpoint(state, is_best, filename='default'):
if filename=='default':
filename = 'model_%s_batch%d'%(args.dataset,args.batch_size)
checkpoint_name = './saved_models/%s_checkpoint_continued.pth.tar'%(filename)
best_name = './saved_models/%s_model_best_continued.pth.tar'%(filename)
torch.save(state, checkpoint_name)
if is_best:
shutil.copyfile(checkpoint_name, best_name)
def build_target(raw_coord, pred):
coord_list, bbox_list = [],[]
for scale_ii in range(len(pred)):
coord = Variable(torch.zeros(raw_coord.size(0), raw_coord.size(1)).cuda())
batch, grid = raw_coord.size(0), args.size//(32//(2**scale_ii))
coord[:,0] = (raw_coord[:,0] + raw_coord[:,2])/(2*args.size)
coord[:,1] = (raw_coord[:,1] + raw_coord[:,3])/(2*args.size)
coord[:,2] = (raw_coord[:,2] - raw_coord[:,0])/(args.size)
coord[:,3] = (raw_coord[:,3] - raw_coord[:,1])/(args.size)
coord = coord * grid
coord_list.append(coord)
bbox_list.append(torch.zeros(coord.size(0),3,5,grid, grid))
best_n_list, best_gi, best_gj = [],[],[]
for ii in range(batch):
anch_ious = []
for scale_ii in range(len(pred)):
batch, grid = raw_coord.size(0), args.size//(32//(2**scale_ii))
gi = coord_list[scale_ii][ii,0].long()
gj = coord_list[scale_ii][ii,1].long()
tx = coord_list[scale_ii][ii,0] - gi.float()
ty = coord_list[scale_ii][ii,1] - gj.float()
gw = coord_list[scale_ii][ii,2]
gh = coord_list[scale_ii][ii,3]
anchor_idxs = [x + 3*scale_ii for x in [0,1,2]]
anchors = [anchors_full[i] for i in anchor_idxs]
scaled_anchors = [ (x[0] / (args.anchor_imsize/grid), \
x[1] / (args.anchor_imsize/grid)) for x in anchors]
## Get shape of gt box
gt_box = torch.FloatTensor(np.array([0, 0, gw, gh], dtype=np.float)).unsqueeze(0)
## Get shape of anchor box
anchor_shapes = torch.FloatTensor(np.concatenate((np.zeros((len(scaled_anchors), 2)), np.array(scaled_anchors)), 1))
## Calculate iou between gt and anchor shapes
anch_ious += list(bbox_iou(gt_box, anchor_shapes))
## Find the best matching anchor box
best_n = np.argmax(np.array(anch_ious))
best_scale = best_n//3
batch, grid = raw_coord.size(0), args.size//(32/(2**best_scale))
anchor_idxs = [x + 3*best_scale for x in [0,1,2]]
anchors = [anchors_full[i] for i in anchor_idxs]
scaled_anchors = [ (x[0] / (args.anchor_imsize/grid), \
x[1] / (args.anchor_imsize/grid)) for x in anchors]
gi = coord_list[best_scale][ii,0].long()
gj = coord_list[best_scale][ii,1].long()
tx = coord_list[best_scale][ii,0] - gi.float()
ty = coord_list[best_scale][ii,1] - gj.float()
gw = coord_list[best_scale][ii,2]
gh = coord_list[best_scale][ii,3]
tw = torch.log(gw / scaled_anchors[best_n%3][0] + 1e-16)
th = torch.log(gh / scaled_anchors[best_n%3][1] + 1e-16)
bbox_list[best_scale][ii, best_n%3, :, gj, gi] = torch.stack([tx, ty, tw, th, torch.ones(1).cuda().squeeze()])
best_n_list.append(int(best_n))
best_gi.append(gi)
best_gj.append(gj)
for ii in range(len(bbox_list)):
bbox_list[ii] = Variable(bbox_list[ii].cuda())
return bbox_list, best_gi, best_gj, best_n_list
def main():
parser = argparse.ArgumentParser(
description='Dataloader test')
parser.add_argument('--gpu', default='0', help='gpu id')
parser.add_argument('--workers', default=16, type=int, help='num workers for data loading')
parser.add_argument('--nb_epoch', default=60, type=int, help='training epoch')
parser.add_argument('--lr', default=1e-4, type=float, help='learning rate')
parser.add_argument('--power', default=1.0, type=float, help='lr poly power')
parser.add_argument('--batch_size', default=14, type=int, help='batch size')
parser.add_argument('--size_average', dest='size_average',
default=False, action='store_true', help='size_average')
parser.add_argument('--size', default=416, type=int, help='image size')
parser.add_argument('--anchor_imsize', default=416, type=int,
help='scale used to calculate anchors defined in model cfg file')
parser.add_argument('--data_root', type=str, default='./ln_data/',
help='path to ReferIt splits data folder')
parser.add_argument('--time', default=40, type=int,
help='maximum time steps (lang length) per batch')
parser.add_argument('--emb_size', default=512, type=int,
help='fusion module embedding dimensions')
parser.add_argument('--resume', default='', type=str, metavar='PATH',
help='path to latest checkpoint (default: none)')
parser.add_argument('--optimizer', default='adam', help='optimizer: sgd, adam, RMSprop')
parser.add_argument('--print_freq', '-p', default=100, type=int,
metavar='N', help='print frequency (default: 1e3)')
parser.add_argument('--savename', default='one_stage_lstm', type=str, help='Name head for saved model')
parser.add_argument('--save_plot', dest='save_plot', default=False, action='store_true', help='save visulization plots')
parser.add_argument('--seed', default=42, type=int, help='random seed')
global args, anchors_full
args = parser.parse_args()
print('----------------------------------------------------------------------')
print(sys.argv[0])
print(args)
print('----------------------------------------------------------------------')
os.environ["CUDA_DEVICE_ORDER"] = "PCI_BUS_ID"
os.environ["CUDA_VISIBLE_DEVICES"] = args.gpu
## fix seed
cudnn.benchmark = False
cudnn.deterministic = True
random.seed(args.seed)
np.random.seed(args.seed+1)
torch.manual_seed(args.seed+2)
torch.cuda.manual_seed_all(args.seed+3)
eps=1e-10
anchors = '10,13, 16,30, 33,23, 30,61, 62,45, 59,119, 116,90, 156,198, 373,326'
anchors = [float(x) for x in anchors.split(',')]
anchors_full = [(anchors[i], anchors[i + 1]) for i in range(0, len(anchors), 2)][::-1]
## save logs
if args.savename=='default':
args.savename = 'model_%s_batch%d'%(args.dataset,args.batch_size)
if not os.path.exists('./logs'):
os.mkdir('logs')
logging.basicConfig(level=logging.DEBUG, filename="./logs/%s"%args.savename, filemode="a+",
format="%(asctime)-15s %(levelname)-8s %(message)s")
input_transform = Compose([
ToTensor(),
Normalize(
mean=[0.485, 0.456, 0.406],
std=[0.229, 0.224, 0.225])
])
train_dataset = Talk2CarDataset(data_root=args.data_root,
split='train',
imsize = args.size,
transform=input_transform,
max_query_len=args.time,
augment=True)
val_dataset = Talk2CarDataset(data_root=args.data_root,
split='val',
imsize = args.size,
transform=input_transform,
max_query_len=args.time,
)
train_loader = DataLoader(train_dataset, batch_size=args.batch_size, shuffle=True,
pin_memory=True, drop_last=True, num_workers=args.workers)
val_loader = DataLoader(val_dataset, batch_size=args.batch_size, shuffle=False,
pin_memory=True, drop_last=True, num_workers=args.workers)
## Model
model = grounding_model(corpus=train_dataset.corpus, emb_size=args.emb_size)
model = model.cuda()
args.start_epoch = 0
print('Num of parameters:', sum([param.nelement() for param in model.parameters()]))
logging.info('Num of parameters:%d'%int(sum([param.nelement() for param in model.parameters()])))
visu_param = model.visumodel.parameters()
rest_param = [param for param in model.parameters() if param not in visu_param]
visu_param = list(model.visumodel.parameters())
sum_visu = sum([param.nelement() for param in visu_param])
sum_text = sum([param.nelement() for param in model.textmodel.parameters()])
sum_fusion = sum([param.nelement() for param in rest_param]) - sum_text
print('visu, text, fusion module parameters:', sum_visu, sum_text, sum_fusion)
## optimizer; rmsprop default
if args.optimizer=='adam':
optimizer = torch.optim.Adam([{'params': rest_param},
{'params': visu_param, 'lr': args.lr/10.}], lr=args.lr, weight_decay=0.0005)
elif args.optimizer=='sgd':
optimizer = torch.optim.SGD([{'params': rest_param},
{'params': visu_param, 'lr': args.lr/10.}], lr=args.lr, momentum=0.99)
else:
optimizer = torch.optim.RMSprop([{'params': rest_param},
{'params': visu_param, 'lr': args.lr/10.}], lr=args.lr, weight_decay=0.0005)
if args.resume:
if os.path.isfile(args.resume):
print(("=> loading checkpoint '{}'".format(args.resume)))
logging.info("=> loading checkpoint '{}'".format(args.resume))
checkpoint = torch.load(args.resume)
args.start_epoch = checkpoint['epoch']
best_accu = checkpoint['best_accu']
model.load_state_dict(checkpoint['model_state_dict'])
optimizer.load_state_dict(checkpoint['optimizer_state_dict'])
print(("=> loaded checkpoint (epoch {}) Accu. {}"
.format(checkpoint['epoch'], best_accu)))
logging.info("=> loaded checkpoint (epoch {}) Accu. {}"
.format(checkpoint['epoch'], best_accu))
del checkpoint
else:
print(("=> no checkpoint found at '{}'".format(args.resume)))
logging.info(("=> no checkpoint found at '{}'".format(args.resume)))
## training and testing
best_accu = -float('Inf')
for epoch in range(args.start_epoch, args.nb_epoch):
adjust_learning_rate(optimizer, epoch)
train_epoch(train_loader, model, optimizer, epoch, args.size_average, 0.1)
accu_new = validate_epoch(val_loader, model, args.size_average)
## remember best accu and save checkpoint
is_best = accu_new > best_accu
best_accu = max(accu_new, best_accu)
print(f"Accu New {accu_new}, BEST Accu{best_accu}")
save_checkpoint({
'epoch': epoch + 1,
'model_state_dict': model.state_dict(),
'best_accu': best_accu,
'optimizer_state_dict' : optimizer.state_dict(),
}, is_best, filename=args.savename)
print('\nBest Accu: %f\n'%best_accu)
logging.info('\nBest Accu: %f\n'%best_accu)
def train_epoch(train_loader, model, optimizer, epoch, size_average, lambda_map_loss):
batch_time = AverageMeter()
data_time = AverageMeter()
losses = AverageMeter()
acc = AverageMeter()
acc_center = AverageMeter()
miou = AverageMeter()
map_loss_fxn = nn.BCELoss()
model.train()
end = time.time()
for batch_idx, (imgs, ob8, ob16, ob32, word_id, bbox) in enumerate(train_loader):
imgs = imgs.cuda()
ob8 = ob8.cuda().float()
ob16 = ob16.cuda().float()
ob32 = ob32.cuda().float()
word_id = word_id.cuda()
bbox = bbox.cuda()
image = Variable(imgs)
ob8 = Variable(ob8)
ob16 = Variable(ob16)
ob32 = Variable(ob32)
word_id = Variable(word_id)
bbox = Variable(bbox)
bbox = torch.clamp(bbox,min=0,max=args.size-1)
pred_anchor, attn_map = model(image, word_id)
## convert gt box to center+offset format
gt_param, gi, gj, best_n_list = build_target(bbox, pred_anchor)
## flatten anchor dim at each scale
for ii in range(len(pred_anchor)):
pred_anchor[ii] = pred_anchor[ii].view( \
pred_anchor[ii].size(0),3,5,pred_anchor[ii].size(2),pred_anchor[ii].size(3))
## loss
batch_size = imgs.size(0)
map_loss = 0.0
obmap = [ ob8, ob16, ob32]
for ii in range(len(attn_map)):
# print(attn_map[ii].shape, obmap[ii].shape)
map_loss += map_loss_fxn(attn_map[ii].view(batch_size, -1), obmap[ii].view(batch_size, -1))
loss = yolo_loss(pred_anchor, gt_param, gi, gj, best_n_list) + lambda_map_loss*map_loss
optimizer.zero_grad()
loss.backward()
optimizer.step()
losses.update(loss.item(), imgs.size(0))
## training offset eval: if correct with gt center loc
## convert offset pred to boxes
pred_coord = torch.zeros(args.batch_size,4)
for ii in range(args.batch_size):
best_scale_ii = best_n_list[ii]//3
grid, grid_size = args.size//(32//(2**best_scale_ii)), 32//(2**best_scale_ii)
anchor_idxs = [x + 3*best_scale_ii for x in [0,1,2]]
anchors = [anchors_full[i] for i in anchor_idxs]
scaled_anchors = [ (x[0] / (args.anchor_imsize/grid), \
x[1] / (args.anchor_imsize/grid)) for x in anchors]
pred_coord[ii,0] = F.sigmoid(pred_anchor[best_scale_ii][ii, best_n_list[ii]%3, 0, gj[ii], gi[ii]]) + gi[ii].float()
pred_coord[ii,1] = F.sigmoid(pred_anchor[best_scale_ii][ii, best_n_list[ii]%3, 1, gj[ii], gi[ii]]) + gj[ii].float()
pred_coord[ii,2] = torch.exp(pred_anchor[best_scale_ii][ii, best_n_list[ii]%3, 2, gj[ii], gi[ii]]) * scaled_anchors[best_n_list[ii]%3][0]
pred_coord[ii,3] = torch.exp(pred_anchor[best_scale_ii][ii, best_n_list[ii]%3, 3, gj[ii], gi[ii]]) * scaled_anchors[best_n_list[ii]%3][1]
pred_coord[ii,:] = pred_coord[ii,:] * grid_size
pred_coord = xywh2xyxy(pred_coord)
## box iou
target_bbox = bbox
iou = bbox_iou(pred_coord, target_bbox.data.cpu(), x1y1x2y2=True)
accu = np.sum(np.array((iou.data.cpu().numpy()>0.5),dtype=float))/args.batch_size
## evaluate if center location is correct
pred_conf_list, gt_conf_list = [], []
for ii in range(len(pred_anchor)):
pred_conf_list.append(pred_anchor[ii][:,:,4,:,:].contiguous().view(args.batch_size,-1))
gt_conf_list.append(gt_param[ii][:,:,4,:,:].contiguous().view(args.batch_size,-1))
pred_conf = torch.cat(pred_conf_list, dim=1).cpu()
gt_conf = torch.cat(gt_conf_list, dim=1).cpu()
accu_center = np.sum(np.array(pred_conf.max(1)[1] == gt_conf.max(1)[1], dtype=float))/args.batch_size
## metrics
miou.update(iou.data[0], imgs.size(0))
acc.update(accu, imgs.size(0))
acc_center.update(accu_center, imgs.size(0))
# measure elapsed time
batch_time.update(time.time() - end)
end = time.time()
if batch_idx % args.print_freq == 0:
print_str = 'Epoch: [{0}][{1}/{2}]\t' \
'Time {batch_time.val:.3f} ({batch_time.avg:.3f})\t' \
'Data Time {data_time.val:.3f} ({data_time.avg:.3f})\t' \
'Loss {loss.val:.4f} ({loss.avg:.4f})\t' \
'Accu {acc.val:.4f} ({acc.avg:.4f})\t' \
'Mean_iu {miou.val:.4f} ({miou.avg:.4f})\t' \
'Accu_c {acc_c.val:.4f} ({acc_c.avg:.4f})\t' \
.format( \
epoch, batch_idx, len(train_loader), batch_time=batch_time, \
data_time=data_time, loss=losses, miou=miou, acc=acc, acc_c=acc_center)
print(print_str)
logging.info(print_str)
def validate_epoch(val_loader, model, size_average, mode='val'):
batch_time = AverageMeter()
data_time = AverageMeter()
losses = AverageMeter()
acc = AverageMeter()
acc_center = AverageMeter()
miou = AverageMeter()
model.eval()
end = time.time()
for batch_idx, (imgs, ob8, ob16, ob32, word_id, bbox) in enumerate(val_loader):
imgs = imgs.cuda()
word_id = word_id.cuda()
bbox = bbox.cuda()
image = Variable(imgs)
word_id = Variable(word_id)
bbox = Variable(bbox)
bbox = torch.clamp(bbox,min=0,max=args.size-1)
with torch.no_grad():
pred_anchor, attn_map = model(image, word_id)
for ii in range(len(pred_anchor)):
pred_anchor[ii] = pred_anchor[ii].view( \
pred_anchor[ii].size(0),3,5,pred_anchor[ii].size(2),pred_anchor[ii].size(3))
gt_param, target_gi, target_gj, best_n_list = build_target(bbox, pred_anchor)
## eval: convert center+offset to box prediction
## calculate at rescaled image during validation for speed-up
pred_conf_list, gt_conf_list = [], []
for ii in range(len(pred_anchor)):
pred_conf_list.append(pred_anchor[ii][:,:,4,:,:].contiguous().view(args.batch_size,-1))
gt_conf_list.append(gt_param[ii][:,:,4,:,:].contiguous().view(args.batch_size,-1))
pred_conf = torch.cat(pred_conf_list, dim=1)
gt_conf = torch.cat(gt_conf_list, dim=1)
max_conf, max_loc = torch.max(pred_conf, dim=1)
pred_bbox = torch.zeros(args.batch_size,4)
pred_gi, pred_gj, pred_best_n = [],[],[]
for ii in range(args.batch_size):
if max_loc[ii] < 3*(args.size//32)**2:
best_scale = 0
elif max_loc[ii] < 3*(args.size//32)**2 + 3*(args.size//16)**2:
best_scale = 1
else:
best_scale = 2
grid, grid_size = args.size//(32//(2**best_scale)), 32//(2**best_scale)
anchor_idxs = [x + 3*best_scale for x in [0,1,2]]
anchors = [anchors_full[i] for i in anchor_idxs]
scaled_anchors = [ (x[0] / (args.anchor_imsize/grid), \
x[1] / (args.anchor_imsize/grid)) for x in anchors]
pred_conf = pred_conf_list[best_scale].view(args.batch_size,3,grid,grid).data.cpu().numpy()
max_conf_ii = max_conf.data.cpu().numpy()
# print(max_conf[ii],max_loc[ii],pred_conf_list[best_scale][ii,max_loc[ii]-64])
(best_n, gj, gi) = np.where(pred_conf[ii,:,:,:] == max_conf_ii[ii])
best_n, gi, gj = int(best_n[0]), int(gi[0]), int(gj[0])
pred_gi.append(gi)
pred_gj.append(gj)
pred_best_n.append(best_n+best_scale*3)
pred_bbox[ii,0] = F.sigmoid(pred_anchor[best_scale][ii, best_n, 0, gj, gi]) + gi
pred_bbox[ii,1] = F.sigmoid(pred_anchor[best_scale][ii, best_n, 1, gj, gi]) + gj
pred_bbox[ii,2] = torch.exp(pred_anchor[best_scale][ii, best_n, 2, gj, gi]) * scaled_anchors[best_n][0]
pred_bbox[ii,3] = torch.exp(pred_anchor[best_scale][ii, best_n, 3, gj, gi]) * scaled_anchors[best_n][1]
pred_bbox[ii,:] = pred_bbox[ii,:] * grid_size
pred_bbox = xywh2xyxy(pred_bbox)
target_bbox = bbox
## metrics
iou = bbox_iou(pred_bbox, target_bbox.data.cpu(), x1y1x2y2=True)
accu_center = np.sum(np.array((target_gi == np.array(pred_gi)) * (target_gj == np.array(pred_gj)), dtype=float))/args.batch_size
accu = np.sum(np.array((iou.data.cpu().numpy()>0.5),dtype=float))/args.batch_size
acc.update(accu, imgs.size(0))
acc_center.update(accu_center, imgs.size(0))
miou.update(iou.data[0], imgs.size(0))
# measure elapsed time
batch_time.update(time.time() - end)
end = time.time()
if batch_idx % args.print_freq == 0:
print_str = '[{0}/{1}]\t' \
'Time {batch_time.val:.3f} ({batch_time.avg:.3f})\t' \
'Data Time {data_time.val:.3f} ({data_time.avg:.3f})\t' \
'Accu {acc.val:.4f} ({acc.avg:.4f})\t' \
'Mean_iu {miou.val:.4f} ({miou.avg:.4f})\t' \
'Accu_c {acc_c.val:.4f} ({acc_c.avg:.4f})\t' \
.format( \
batch_idx, len(val_loader), batch_time=batch_time, \
data_time=data_time, \
acc=acc, acc_c=acc_center, miou=miou)
print(print_str)
logging.info(print_str)
print(best_n_list, pred_best_n)
print(np.array(target_gi), np.array(pred_gi))
print(np.array(target_gj), np.array(pred_gj),'-')
print(acc.avg, miou.avg,acc_center.avg)
logging.info("%f,%f,%f"%(acc.avg, float(miou.avg),acc_center.avg))
return acc.avg
if __name__ == "__main__":
main()