def forward(self, theta): # shape of theta: [b, 6] b = theta.size(0) if not theta.size() == (b, 6): theta = theta.view(b, 6) theta = theta.contiguous() # shape: [1, h, w, 1] grid_X = torch.Tensor(self.grid_X).unsqueeze(0).unsqueeze(3).cuda() grid_Y = torch.Tensor(self.grid_Y).unsqueeze(0).unsqueeze(3).cuda() grid_X = Variable(grid_X, requires_grad=False) grid_Y = Variable(grid_Y, requires_grad=False) # shape of each theta element: [b, 1, 1, 1] t0 = theta[:, 0].unsqueeze(1).unsqueeze(2).unsqueeze(3) t1 = theta[:, 1].unsqueeze(1).unsqueeze(2).unsqueeze(3) t2 = theta[:, 2].unsqueeze(1).unsqueeze(2).unsqueeze(3) t3 = theta[:, 3].unsqueeze(1).unsqueeze(2).unsqueeze(3) t4 = theta[:, 4].unsqueeze(1).unsqueeze(2).unsqueeze(3) t5 = theta[:, 5].unsqueeze(1).unsqueeze(2).unsqueeze(3) X = expand_dim(grid_X, 0, b) # shape: [b, h, w, 1] Y = expand_dim(grid_Y, 0, b) # shape: [b, h, w, 1] # P = Theta @ [X, Y, 1].T Xp = X * t0 + Y * t1 + t2 # shape: [b, h, w, 1] Yp = X * t3 + Y * t4 + t5 # shape: [b, h, w, 1] return torch.cat((Xp, Yp), 3) # shape: [b, h, w, 2]
def forward(self, theta, matches, return_outliers=False): if isinstance(theta, Variable): # handle normal batch transformations batch_size = theta.size()[0] theta = theta.clone() # print(self.mask_id.size()) mask = self.geometricTnf(expand_dim(self.mask_id, 0, batch_size), theta) # print(mask.size()) # mask = self.maxpool2d(mask) if return_outliers: mask_outliers = self.geometricTnf( expand_dim(1.0 - self.mask_id, 0, batch_size), theta) if self.normalize: epsilon = 1e-5 mask = torch.div( mask, torch.sum( torch.sum(torch.sum(mask + epsilon, 3), 2), 1).unsqueeze(1).unsqueeze(2).unsqueeze(3).expand_as( mask)) # 有个吉儿用?mask 本来就是 0/1 矩阵,搁这 normalize 你🐎呢 if return_outliers: mask_outliers = torch.div( mask_outliers, torch.sum( torch.sum(torch.sum(mask_outliers + epsilon, 3), 2), 1).unsqueeze(1).unsqueeze(2).unsqueeze( 3).expand_as(mask_outliers)) score = torch.sum( torch.sum(torch.sum(torch.mul(mask, matches), 3), 2), 1) if return_outliers: score_outliers = torch.sum( torch.sum(torch.sum(torch.mul(mask_outliers, matches), 3), 2), 1) return (score, score_outliers) elif isinstance( theta, list ): # handle multiple transformations per batch item, batch is in list format (used for RANSAC) batch_size = len(theta) score = [] for b in range(batch_size): sample_size = theta[b].size(0) s = self.forward( theta[b], expand_dim(matches[b, :, :, :].unsqueeze(0), 0, sample_size)) score.append(s) return score
def forward(self, theta): b = theta.size(0) if not theta.size() == (b, 6): theta = theta.view(b, 6) theta = theta.contiguous() t0 = theta[:, 0].unsqueeze(1).unsqueeze(2).unsqueeze(3) t1 = theta[:, 1].unsqueeze(1).unsqueeze(2).unsqueeze(3) t2 = theta[:, 2].unsqueeze(1).unsqueeze(2).unsqueeze(3) t3 = theta[:, 3].unsqueeze(1).unsqueeze(2).unsqueeze(3) t4 = theta[:, 4].unsqueeze(1).unsqueeze(2).unsqueeze(3) t5 = theta[:, 5].unsqueeze(1).unsqueeze(2).unsqueeze(3) X = expand_dim(self.grid_X, 0, b) Y = expand_dim(self.grid_Y, 0, b) Xp = X * t0 + Y * t1 + t2 Yp = X * t3 + Y * t4 + t5 return torch.cat((Xp, Yp), 3)
def forward(self, theta): b = theta.size(0) if not theta.size() == (b, 6): theta = theta.view(b, 6) theta = theta.contiguous() grid_X = torch.Tensor(self.grid_X).unsqueeze(0).unsqueeze(3).cuda() grid_Y = torch.Tensor(self.grid_Y).unsqueeze(0).unsqueeze(3).cuda() grid_X = Variable(grid_X, requires_grad=False) grid_Y = Variable(grid_Y, requires_grad=False) t0 = theta[:, 0].unsqueeze(1).unsqueeze(2).unsqueeze(3) t1 = theta[:, 1].unsqueeze(1).unsqueeze(2).unsqueeze(3) t2 = theta[:, 2].unsqueeze(1).unsqueeze(2).unsqueeze(3) t3 = theta[:, 3].unsqueeze(1).unsqueeze(2).unsqueeze(3) t4 = theta[:, 4].unsqueeze(1).unsqueeze(2).unsqueeze(3) t5 = theta[:, 5].unsqueeze(1).unsqueeze(2).unsqueeze(3) X = expand_dim(grid_X, 0, b) Y = expand_dim(grid_Y, 0, b) Xp = X * t0 + Y * t1 + t2 Yp = X * t3 + Y * t4 + t5 return torch.cat((Xp, Yp), 3)