def forward(self, input, return_A_matrix=False): xy = self.features(self.input_norm(input)).view(-1, 3) a1 = torch.cat([ 1.0 + xy[:, 0].contiguous().view(-1, 1, 1), 0 * xy[:, 0].contiguous().view(-1, 1, 1) ], dim=2).contiguous() a2 = torch.cat([ xy[:, 1].contiguous().view(-1, 1, 1), 1.0 + xy[:, 2].contiguous().view(-1, 1, 1) ], dim=2).contiguous() return rectifyAffineTransformationUpIsUp( torch.cat([a1, a2], dim=1).contiguous())
def forward(self,x): if x.is_cuda: self.gk = self.gk.cuda() else: self.gk = self.gk.cpu() gx = self.gx(F.pad(x, (1, 1, 0, 0), 'replicate')) gy = self.gy(F.pad(x, (0, 0, 1, 1), 'replicate')) a1 = (gx * gx * self.gk.unsqueeze(0).unsqueeze(0).expand_as(gx)).view(x.size(0),-1).mean(dim=1) b1 = (gx * gy * self.gk.unsqueeze(0).unsqueeze(0).expand_as(gx)).view(x.size(0),-1).mean(dim=1) c1 = (gy * gy * self.gk.unsqueeze(0).unsqueeze(0).expand_as(gx)).view(x.size(0),-1).mean(dim=1) a, b, c, l1, l2 = self.invSqrt(a1,b1,c1) rat1 = l1/l2 mask = (torch.abs(rat1) <= 6.).float().view(-1); return rectifyAffineTransformationUpIsUp(abc2A(a,b,c)), mask
def get_GT_correspondence_indexes_Fro_and_center(LAFs1, LAFs2, H1to2, dist_threshold=4, center_dist_th=2.0, skip_center_in_Fro=False, do_up_is_up=False, return_LAF2_in_1=False): LHF2_in_1_pre = reprojectLAFs(LAFs2, torch.inverse(H1to2), True) if do_up_is_up: sc = torch.sqrt(LHF2_in_1_pre[:, 0, 0] * LHF2_in_1_pre[:, 1, 1] - LHF2_in_1_pre[:, 1, 0] * LHF2_in_1_pre[:, 0, 1] ).unsqueeze(-1).unsqueeze(-1).expand( LHF2_in_1_pre.size(0), 2, 2) LHF2_in_1 = torch.zeros(LHF2_in_1_pre.size()) if LHF2_in_1_pre.is_cuda: LHF2_in_1 = LHF2_in_1.cuda() LHF2_in_1 = Variable(LHF2_in_1) LHF2_in_1[:, :2, :2] = rectifyAffineTransformationUpIsUp( LHF2_in_1_pre[:, :2, :2] // sc) * sc LHF2_in_1[:, :, 2] = LHF2_in_1_pre[:, :, 2] else: LHF2_in_1 = LHF2_in_1_pre LHF1_inv = inverseLHFs(LAFs_to_H_frames(LAFs1)) frob_norm_dist = reproject_to_canonical_Frob_batched( LHF1_inv, LHF2_in_1, batch_size=2, skip_center=skip_center_in_Fro) #### Center replated just_centers1 = LAFs1[:, :, 2] just_centers2_repr_to_1 = LHF2_in_1[:, 0:2, 2] center_dist_mask = distance_matrix_vector(just_centers2_repr_to_1, just_centers1) >= center_dist_th frob_norm_dist_masked = center_dist_mask.float() * 1000. + frob_norm_dist min_dist, idxs_in_2 = torch.min(frob_norm_dist_masked, 1) plain_indxs_in1 = torch.arange(0, idxs_in_2.size(0)) if LAFs1.is_cuda: plain_indxs_in1 = plain_indxs_in1.cuda() plain_indxs_in1 = torch.autograd.Variable(plain_indxs_in1, requires_grad=False) #min_dist, idxs_in_2 = torch.min(dist,1) #print min_dist.min(), min_dist.max(), min_dist.mean() mask = (min_dist <= dist_threshold) if return_LAF2_in_1: return min_dist[mask], plain_indxs_in1[mask], idxs_in_2[ mask], LHF2_in_1[:, 0:2, :] else: return min_dist[mask], plain_indxs_in1[mask], idxs_in_2[mask]
def forward(self, input, return_A_matrix=False): x = self.features(self.input_norm(input)).view(-1, 5) angle = torch.atan2(x[:, 3], x[:, 4] + 1e-8) rot = get_rotation_matrix(angle) return torch.bmm( rot, rectifyAffineTransformationUpIsUp( torch.cat([ torch.cat([ x[:, 0:1].view(-1, 1, 1), x[:, 1:2].view( x.size(0), 1, 1).contiguous() ], dim=2), x[:, 1:3].view(-1, 1, 2).contiguous() ], dim=1)).contiguous())
def get_GT_correspondence_indexes_Fro_and_center(LAFs1, LAFs2, H1to2, dist_threshold=4, center_dist_th=2.0, scale_diff_coef=0.3, skip_center_in_Fro=False, do_up_is_up=False, return_LAF2_in_1=False, inv_to_eye=True): LHF2_in_1_pre = reprojectLAFs(LAFs2, torch.inverse(H1to2), True) if do_up_is_up: sc2 = torch.sqrt( torch.abs(LHF2_in_1_pre[:, 0, 0] * LHF2_in_1_pre[:, 1, 1] - LHF2_in_1_pre[:, 1, 0] * LHF2_in_1_pre[:, 0, 1]) ).unsqueeze(-1).unsqueeze(-1).expand(LHF2_in_1_pre.size(0), 2, 2) LHF2_in_1 = torch.zeros(LHF2_in_1_pre.size()) if LHF2_in_1_pre.is_cuda: LHF2_in_1 = LHF2_in_1.cuda() LHF2_in_1[:, :2, :2] = rectifyAffineTransformationUpIsUp( LHF2_in_1_pre[:, :2, :2] / sc2) * sc2 LHF2_in_1[:, :, 2] = LHF2_in_1_pre[:, :, 2] sc1 = torch.sqrt( torch.abs(LAFs1[:, 0, 0] * LAFs1[:, 1, 1] - LAFs1[:, 1, 0] * LAFs1[:, 0, 1])).unsqueeze(-1).unsqueeze(-1).expand( LAFs1.size(0), 2, 2) LHF1 = LAFs_to_H_frames( torch.cat([ rectifyAffineTransformationUpIsUp(LAFs1[:, :2, :2] / sc1) * sc1, LAFs1[:, :, 2:] ], dim=2)) else: LHF2_in_1 = LHF2_in_1_pre LHF1 = LAFs_to_H_frames(LAFs1) if inv_to_eye: LHF1_inv = inverseLHFs(LHF1) frob_norm_dist = reproject_to_canonical_Frob_batched( LHF1_inv, LHF2_in_1, batch_size=2, skip_center=skip_center_in_Fro) else: if not skip_center_in_Fro: frob_norm_dist = distance_matrix_vector( LHF2_in_1.view(LHF2_in_1.size(0), -1), LHF1.view(LHF1.size(0), -1)) else: frob_norm_dist = distance_matrix_vector( LHF2_in_1[:, 0:2, 0:2].contiguous().view(LHF2_in_1.size(0), -1), LHF1[:, 0:2, 0:2].contiguous().view(LHF1.size(0), -1)) #### Center replated just_centers1 = LAFs1[:, :, 2] just_centers2_repr_to_1 = LHF2_in_1[:, 0:2, 2] if scale_diff_coef > 0: scales1 = torch.sqrt( torch.abs(LAFs1[:, 0, 0] * LAFs1[:, 1, 1] - LAFs1[:, 1, 0] * LAFs1[:, 0, 1])) scales2 = torch.sqrt( torch.abs(LHF2_in_1[:, 0, 0] * LHF2_in_1[:, 1, 1] - LHF2_in_1[:, 1, 0] * LHF2_in_1[:, 0, 1])) scale_matrix = ratio_matrix_vector(scales2, scales1) scale_dist_mask = (torch.abs(1.0 - scale_matrix) <= scale_diff_coef) center_dist_mask = distance_matrix_vector(just_centers2_repr_to_1, just_centers1) >= center_dist_th frob_norm_dist_masked = (1.0 - scale_dist_mask.float() + center_dist_mask.float()) * 1000. + frob_norm_dist min_dist, idxs_in_2 = torch.min(frob_norm_dist_masked, 1) plain_indxs_in1 = torch.arange(0, idxs_in_2.size(0)) if LAFs1.is_cuda: plain_indxs_in1 = plain_indxs_in1.cuda() #min_dist, idxs_in_2 = torch.min(dist,1) #print min_dist.min(), min_dist.max(), min_dist.mean() mask = (min_dist <= dist_threshold) if return_LAF2_in_1: return min_dist[mask], plain_indxs_in1[mask], idxs_in_2[ mask], LHF2_in_1[:, 0:2, :] else: return min_dist[mask], plain_indxs_in1[mask], idxs_in_2[mask]
def forward(self, input, return_A_matrix=False): xy = self.features(self.input_norm(input)).view(-1, 2, 2).contiguous() return rectifyAffineTransformationUpIsUp(xy).contiguous()
def getAffineShape(self, final_resp, LAFs, final_pyr_idxs, final_level_idxs, num_features=0): pe_time = 0 affnet_time = 0 pyr_inv_idxs = get_inverted_pyr_index(self.scale_pyr, final_pyr_idxs, final_level_idxs) t = time.time() patches_small = extract_patches_from_pyramid_with_inv_index( self.scale_pyr, pyr_inv_idxs, LAFs, PS=self.AffNet.PS) pe_time += time.time() - t t = time.time() base_A = torch.eye(2).unsqueeze(0).expand(final_pyr_idxs.size(0), 2, 2) if final_resp.is_cuda: base_A = base_A.cuda() base_A = Variable(base_A) is_good = None n_patches = patches_small.size(0) for i in range(self.num_Baum_iters): t = time.time() A = batched_forward(self.AffNet, patches_small, 512) is_good_current = 1 affnet_time += time.time() - t if is_good is None: is_good = is_good_current else: is_good = is_good * is_good_current base_A = torch.bmm(A, base_A) new_LAFs = torch.cat( [torch.bmm(base_A, LAFs[:, :, 0:2]), LAFs[:, :, 2:]], dim=2) #print torch.sqrt(new_LAFs[0,0,0]*new_LAFs[0,1,1] - new_LAFs[0,1,0] *new_LAFs[0,0,1]) * scale_pyr[0][0].size(2) if i != self.num_Baum_iters - 1: pe_time += time.time() - t t = time.time() patches_small = extract_patches_from_pyramid_with_inv_index( self.scale_pyr, pyr_inv_idxs, new_LAFs, PS=self.AffNet.PS) pe_time += time.time() - t l1, l2 = batch_eig2x2(A) ratio1 = torch.abs(l1 / (l2 + 1e-8)) converged_mask = (ratio1 <= 1.2) * (ratio1 >= (0.8)) l1, l2 = batch_eig2x2(base_A) ratio = torch.abs(l1 / (l2 + 1e-8)) idxs_mask = ( (ratio < 6.0) * (ratio > (1. / 6.))) # * converged_mask.float()) > 0 num_survived = idxs_mask.float().sum() if (num_features > 0) and (num_survived.data[0] > num_features): final_resp = final_resp * idxs_mask.float() #zero bad points final_resp, idxs = torch.topk(final_resp, k=num_features) else: idxs = torch.nonzero(idxs_mask.data).view(-1).long() final_resp = final_resp[idxs] final_pyr_idxs = final_pyr_idxs[idxs] final_level_idxs = final_level_idxs[idxs] base_A = torch.index_select(base_A, 0, idxs) LAFs = torch.index_select(LAFs, 0, idxs) new_LAFs = torch.cat([ torch.bmm(rectifyAffineTransformationUpIsUp(base_A), LAFs[:, :, 0:2]), LAFs[:, :, 2:] ], dim=2) print 'affnet_time', affnet_time print 'pe_time', pe_time return final_resp, new_LAFs, final_pyr_idxs, final_level_idxs