def pck_metric(batch, batch_start_idx, matches, stats, args, use_cuda=True): source_im_size = batch['source_im_size'] target_im_size = batch['target_im_size'] import ipdb ipdb.set_trace() source_points = batch['source_points'] target_points = batch['target_points'] # warp points with estimated transformations target_points_norm = PointsToUnitCoords(target_points, target_im_size) # compute points stage 1 only warped_points_norm = bilinearInterpPointTnf(matches, target_points_norm) warped_points = PointsToPixelCoords(warped_points_norm, source_im_size) L_pck = batch['L_pck'].data current_batch_size = batch['source_im_size'].size(0) indices = range(batch_start_idx, batch_start_idx + current_batch_size) # compute PCK pck_batch = pck(source_points.data, warped_points.data, L_pck) stats['point_tnf']['pck'][indices] = pck_batch.unsqueeze(1).cpu().numpy() return stats
def pck_metric(batch, batch_start_idx, matches, stats, alpha=0.1, use_cuda=True): source_im_size = batch["source_im_size"] target_im_size = batch["target_im_size"] source_points = batch[ "source_points"] # w.r.t. the original image coordinate #224 target_points = batch["target_points"] # B x 2 x N # warp points with estimated transformations target_points_norm = PointsToUnitCoords( target_points, target_im_size) # convert from image coordinate to -1, 1 # print('target_im_size',target_im_size) # compute points stage 1 only warped_points_norm = bilinearInterpPointTnf(matches, target_points_norm) warped_points = PointsToPixelCoords(warped_points_norm, source_im_size) L_pck = batch["L_pck"].data current_batch_size = batch["source_im_size"].size(0) indices = range(batch_start_idx, batch_start_idx + current_batch_size) # compute PCK pck_batch = pck(source_points.data, warped_points.data, L_pck, alpha=alpha) stats["point_tnf"]["pck"][indices] = pck_batch.unsqueeze(1).cpu().numpy() return stats
def pck_metric(batch, batch_start_idx, matches, stats, args, use_cuda=True, alpha=0.1): source_im_size = batch['source_im_size'] target_im_size = batch['target_im_size'] source_points = batch['source_points'] target_points = batch['target_points'] # warp points with estimated transformations target_points_norm = PointsToUnitCoords(target_points, target_im_size) # compute points stage 1 only warped_points_norm = bilinearInterpPointTnf( matches, target_points_norm) # B中特征点warp到A中的coordnate位置i(-1~1) warped_points = PointsToPixelCoords(warped_points_norm, source_im_size) # warp后的坐标反标准化 L_pck = batch['L_pck'].data # for scNet rule, the L_pck is 224 current_batch_size = batch['source_im_size'].size(0) indices = range(batch_start_idx, batch_start_idx + current_batch_size) # compute PCK pck_batch = pck(source_points.data, warped_points.data, L_pck, alpha) stats['point_tnf']['pck'][indices] = pck_batch.unsqueeze(1).cpu().numpy() return stats
def flow_metrics(batch, batch_start_idx, matches, stats, args, use_cuda=True): result_path = args.flow_output_dir pt = PointTnf(use_cuda=use_cuda) batch_size = batch['source_im_size'].size(0) for b in range(batch_size): h_src = int(batch['source_im_size'][b, 0].data.cpu().numpy()) w_src = int(batch['source_im_size'][b, 1].data.cpu().numpy()) h_tgt = int(batch['target_im_size'][b, 0].data.cpu().numpy()) w_tgt = int(batch['target_im_size'][b, 1].data.cpu().numpy()) grid_X, grid_Y = np.meshgrid(np.linspace(-1, 1, w_tgt), np.linspace(-1, 1, h_tgt)) grid_X = torch.FloatTensor(grid_X).unsqueeze(0).unsqueeze(3) grid_Y = torch.FloatTensor(grid_Y).unsqueeze(0).unsqueeze(3) grid_X = Variable(grid_X, requires_grad=False) grid_Y = Variable(grid_Y, requires_grad=False) if use_cuda: grid_X = grid_X.cuda() grid_Y = grid_Y.cuda() grid_X_vec = grid_X.view(1, 1, -1) grid_Y_vec = grid_Y.view(1, 1, -1) grid_XY_vec = torch.cat((grid_X_vec, grid_Y_vec), 1) def pointsToGrid(x, h_tgt=h_tgt, w_tgt=w_tgt): return x.contiguous().view(1, 2, h_tgt, w_tgt).transpose(1, 2).transpose(2, 3) idx = batch_start_idx + b source_im_size = batch['source_im_size'] warped_points_norm = bilinearInterpPointTnf(matches, grid_XY_vec) # warped_points = PointsToPixelCoords(warped_points_norm,source_im_size) warped_points = pointsToGrid(warped_points_norm) # grid_aff = pointsToGrid(pt.affPointTnf(theta_aff[b, :].unsqueeze(0), grid_XY_vec)) flow_aff = th_sampling_grid_to_np_flow(source_grid=warped_points, h_src=h_src, w_src=w_src) flow_aff_path = os.path.join(result_path, 'nc', batch['flow_path'][b]) create_file_path(flow_aff_path) write_flo_file(flow_aff, flow_aff_path) idx = batch_start_idx + b return stats
def pck_metric(batch, batch_start_idx, matches, stats, args, use_cuda=True, interp='bilinear'): source_im_size = batch['source_im_size'] target_im_size = batch['target_im_size'] source_points = batch['source_points'] target_points = batch['target_points'] # warp points with estimated transformations target_points_norm = PointsToUnitCoords(target_points, target_im_size) # compute points stage 1 only if interp == 'bilinear': warped_points_norm = bilinearInterpPointTnf(matches, target_points_norm) elif interp == 'nearest': warped_points_norm = nearestNeighPointTnf(matches, target_points_norm) else: raise ValueError('interpolation method {} invalid'.format(interp)) warped_points = PointsToPixelCoords(warped_points_norm, source_im_size) L_pck = batch['L_pck'].data current_batch_size = batch['source_im_size'].size(0) indices = range(batch_start_idx, batch_start_idx + current_batch_size) # compute PCK pck_batch = pck(source_points.data, warped_points.data, L_pck) stats['point_tnf']['pck'][indices] = pck_batch.unsqueeze(1).cpu().numpy() return stats
'target_image': Variable(tgt.unsqueeze(0)) } if use_cuda: src_pts = src_pts.cuda() tgt_pts = tgt_pts.cuda() batch_tnf['source_image'] = batch_tnf['source_image'].cuda( ) # torch.size([1, 3, image_size, image_size]) batch_tnf['target_image'] = batch_tnf['target_image'].cuda() corr4d = model(batch_tnf) # torch.size([1, 1, 16, 16, 16, 16]) # compute matches from output c2m = corr_to_matches xA, yA, xB, yB, sB = c2m(corr4d, do_softmax=True) warped_points = bilinearInterpPointTnf( (xA, yA, xB, yB), tgt_pts) # B中特征点warp到A中的coordinate位置(-1~1) ###############################################MatchingGridCellPoints############################################## colA = unnormalize_axis(xA, sample['source_im_size'][1]) rowA = unnormalize_axis(yA, sample['source_im_size'][0]) colB = unnormalize_axis(xB, sample['target_im_size'][1]) rowB = unnormalize_axis(yB, sample['target_im_size'][0]) plt.imshow(imgTotal) # tar + src beginScore, endScore = torch.min(sB), torch.max(sB) flag = endScore - 0.0001 # - 0.005 * (endScore-beginScore) for i in range(colA.shape[1]): a = sB[0][i].data > flag.data if int(a.cpu()): xa = float(colB[0][i]) ya = float(rowB[0][i]) xb = float(colA[0][i]) + imgDst.shape[1]