def accuracy(data, out): vgt = data[4] # ss_gt = vgt[:, 0:3] sr1_gt = vgt[:, 3:6] sr2_gt = vgt[:, 6:9] sb_gt = sr2box(ss_gt, sr1_gt, sr2_gt) # ts_gt = vgt[:, 9:12] tr1_gt = vgt[:, 15:18] tr2_gt = vgt[:, 18:21] tb_gt = sr2box(ts_gt, tr1_gt, tr2_gt) # ss = out['ss'] sr1 = out['sr1'] sr2 = out['sr2'] sb = out['sb'] # ts = out['ts'] tr1 = out['tr1'] tr2 = out['tr2'] tb = out['tb'] # loss = {} loss['size'] = 0.5 * torch.sum( (ss - ss_gt.data)**2 + (ts - ts_gt.data)**2, dim=1) erot = (sr1 - sr1_gt.data)**2 erot += (sr2 - sr2_gt.data)**2 erot += (tr1 - tr1_gt.data)**2 erot += (tr2 - tr2_gt.data)**2 loss['rot6'] = 0.5 * torch.sum(erot, dim=1) loss['box'] = 0.5 * (chmf(sb, sb_gt.data) + chmf(tb, tb_gt.data)) loss['overall'] = loss['box'] return loss
def accuracy(data,out): vgt = data[5]; # ss_gt = vgt[:,0:3].contiguous(); ss_gt = norm_size(ss_gt); sr1_gt = vgt[:,3:6].contiguous(); sr2_gt = vgt[:,6:9].contiguous(); sb_gt = sr2box(ss_gt,sr1_gt,sr2_gt); # ts_gt = vgt[:,9:12].contiguous(); ts_gt = norm_size(ts_gt); tr1_gt = vgt[:,15:18].contiguous(); tr2_gt = vgt[:,18:21].contiguous(); tb_gt = sr2box(ts_gt,tr1_gt,tr2_gt); # ss = out['ss']; sr1 = out['sr1']; sr2 = out['sr2']; sb = out['sb']; # ts = out['ts']; tr1 = out['tr1']; tr2 = out['tr2']; tb = out['tb']; # loss = {}; loss['size'] = 0.5*torch.sum( ( ss - ss_gt.data )**2 + ( ts - ts_gt.data )**2, dim = 1 ); erot = ( sr1 - sr1_gt.data )**2; erot += ( sr2 - sr2_gt.data )**2; erot += ( tr1 - tr1_gt.data )**2; erot += ( tr2 - tr2_gt.data )**2; loss['rot6'] = 0.5*torch.sum( erot, dim=1 ); loss['bcd'] = 0.5*( box_cd(ss,sr1,sr2,ss_gt,sr1_gt,sr2_gt) + box_cd(ts,tr1,tr2,ts_gt,tr1_gt,tr2_gt) ); loss['overall'] = loss['bcd']; return loss;
def run(**kwargs): nepoch = kwargs['nepoch'] bs = kwargs['batch_size'] size_gt = torch.from_numpy( np.random.uniform(0.1, 1, [bs, 3]).astype(np.float32)) r1_gt = torch.from_numpy( np.random.uniform(-1, 1, [bs, 3]).astype(np.float32)) r2_gt = torch.from_numpy( np.random.uniform(-1, 1, [bs, 3]).astype(np.float32)) size = torch.zeros([bs, 3], requires_grad=True) r1 = torch.zeros([bs, 3], requires_grad=True) r2 = torch.zeros([bs, 3], requires_grad=True) size.data.uniform_(0.1, 1) r1.data.normal_(0.0, 1.0) r2.data.normal_(0.0, 1.0) opt = torch.optim.Adam([size, r1, r2], lr=0.001) boxgt = sr2box(size_gt.cuda(), r1_gt.cuda(), r2_gt.cuda()) write_box('./log/boxcd/gt', boxgt.data.cpu().numpy()) for i in range(nepoch): opt.zero_grad() cd = box_cd(size.cuda(), r1.cuda(), r2.cuda(), size_gt.cuda(), r1_gt.cuda(), r2_gt.cuda()) loss = torch.mean(cd) loss.backward() print(loss.data.cpu().numpy()) opt.step() boxout = sr2box(size.cuda(), r1.cuda(), r2.cuda()) if i % 100 == 0: write_box('./log/boxcd/out_%d' % i, boxout.data.cpu().numpy()) return
def accuracy(data, out): vgt = data[5] # ss_gt = vgt[:, 0:3].contiguous() srs_gt, _ = torch.max(ss_gt, dim=1, keepdim=True) sr1_gt = vgt[:, 3:6].contiguous() sr2_gt = vgt[:, 6:9].contiguous() sb_gt = sr2box(ss_gt, sr1_gt, sr2_gt) # ts_gt = vgt[:, 9:12].contiguous() trs_gt, _ = torch.max(ts_gt, dim=1, keepdim=True) tr1_gt = vgt[:, 15:18].contiguous() tr2_gt = vgt[:, 18:21].contiguous() tb_gt = sr2box(ts_gt, tr1_gt, tr2_gt) # ss = out['ss'] sr1 = out['sr1'] sr2 = out['sr2'] sb = out['sb'] # ts = out['ts'] tr1 = out['tr1'] tr2 = out['tr2'] tb = out['tb'] # loss = {} loss['size'] = 0.5 * torch.sum((ss * srs_gt.data - ss_gt.data)**2 + (ts * trs_gt.data - ts_gt.data)**2, dim=1) erot = (sr1 - sr1_gt.data)**2 erot += (sr2 - sr2_gt.data)**2 erot += (tr1 - tr1_gt.data)**2 erot += (tr2 - tr2_gt.data)**2 loss['rot6'] = 0.5 * torch.sum(erot, dim=1) loss['bcd'] = 0.5 * ( box_cd(ss * srs_gt.data, sr1, sr2, ss_gt, sr1_gt, sr2_gt) + box_cd(ts * trs_gt.data, tr1, tr2, ts_gt, tr1_gt, tr2_gt)) loss['overall'] = loss['bcd'] return loss
def loss(data, out): vgt = data[5] # ss_gt = vgt[:, 0:3].contiguous() ss_gt = norm_size(ss_gt) sr1_gt = vgt[:, 3:6].contiguous() sr2_gt = vgt[:, 6:9].contiguous() sb_gt = sr2box(ss_gt, sr1_gt, sr2_gt) # ts_gt = vgt[:, 9:12].contiguous() ts_gt = norm_size(ts_gt) tr1_gt = vgt[:, 15:18].contiguous() tr2_gt = vgt[:, 18:21].contiguous() tb_gt = sr2box(ts_gt, tr1_gt, tr2_gt) # ss = out['ss'] sr1 = out['sr1'] sr2 = out['sr2'] sb = out['sb'] # ts = out['ts'] tr1 = out['tr1'] tr2 = out['tr2'] tb = out['tb'] # loss = {} loss['size'] = 0.5 * torch.sum( (ss - ss_gt.data)**2 + (ts - ts_gt.data)**2, dim=1) erot = (sr1 - sr1_gt.data)**2 erot += (sr2 - sr2_gt.data)**2 erot += (tr1 - tr1_gt.data)**2 erot += (tr2 - tr2_gt.data)**2 loss['rot6'] = 0.5 * torch.sum(erot, dim=1) loss['bcd'] = 0.5 * (box_cd(ss, sr1, sr2, ss_gt, sr1_gt, sr2_gt) + box_cd(ts, tr1, tr2, ts_gt, tr1_gt, tr2_gt)) loss['overall'] = torch.mean(loss['bcd']) return loss