def preprocess_sample(self, x: TensorList) -> TensorList: x *= self.window sample_xf = fourier.cfft2(x) return TensorList([ dcf.interpolate_dft(xf, bf) for xf, bf in zip(sample_xf, self.interp_fs) ])
def localize_target(self, scores_raw): if self.params.score_fusion_strategy == 'weightedsum': weight = self.fparams.attribute('translation_weight') scores_raw = weight * scores_raw sf_weighted = fourier.cfft2(scores_raw) / (scores_raw.size(2) * scores_raw.size(3)) for i, (sz, ksz) in enumerate(zip(self.feature_sz, self.kernel_size)): sf_weighted[i] = fourier.shift_fs( sf_weighted[i], math.pi * (1 - torch.Tensor([ksz[0] % 2, ksz[1] % 2]) / sz)) scores_fs = fourier.sum_fs(sf_weighted) scores = fourier.sample_fs(scores_fs, self.output_sz) elif self.params.score_fusion_strategy == 'default': if len(scores_raw) > 1: raise NotImplementedError('Not implemented') scores = scores_raw[0] ksz = self.kernel_size[0] offset = torch.Tensor([ksz[0] % 2, ksz[1] % 2]) / 2 else: raise ValueError('Unknown score fusion strategy.') if self.output_window is not None and not getattr( self.params, 'perform_hn_without_windowing', False): raise NotImplementedError scores *= self.output_window if getattr(self.params, 'advanced_localization', False): return self.localize_advanced(scores) # Get maximum max_score, max_disp = dcf.max2d(scores) _, scale_ind = torch.max(max_score, dim=0) max_disp = max_disp.float().cpu() # Convert to displacements in the base scale if self.params.score_fusion_strategy == 'default': disp = max_disp + offset else: disp = (max_disp + self.output_sz / 2) % self.output_sz - self.output_sz / 2 # Compute translation vector and scale change factor translation_vec = disp[scale_ind, ...].view(-1) * ( self.img_support_sz / self.output_sz) * self.target_scale translation_vec *= self.params.scale_factors[scale_ind] # Shift the score output for visualization purposes if self.params.debug >= 2: sz = scores.shape[-2:] scores = torch.cat( [scores[..., sz[0] // 2:, :], scores[..., :sz[0] // 2, :]], -2) scores = torch.cat( [scores[..., :, sz[1] // 2:], scores[..., :, :sz[1] // 2]], -1) return translation_vec, scale_ind, scores, None
def localize_target(self, scores_raw): # Weighted sum (if multiple features) with interpolation in fourier domain weight = self.fparams.attribute('translation_weight', 1.0) #weight 没什么用 if (Debug): print("weight : ", weight) # scores_raw = weight * scores_raw # if (Debug): print("scores_raw: ", scores_raw) sf_weighted = fourier.cfft2(scores_raw) / (scores_raw.size(2) * scores_raw.size(3)) for i, (sz, ksz) in enumerate(zip(self.feature_sz, self.kernel_size)): # """Shift a sample a in the Fourier domain. sf_weighted[i] = fourier.shift_fs( sf_weighted[i], math.pi * (1 - torch.Tensor([ksz[0] % 2, ksz[1] % 2]) / sz)) #"""Sum a list of Fourier series expansions.""" scores_fs = fourier.sum_fs(sf_weighted) if (Debug): print("scores_fs : ", scores_fs) #"""Samples the Fourier series.""" scores = fourier.sample_fs(scores_fs, self.output_sz) if (Debug): print("scores: ", scores) if self.output_window is not None and not getattr( self.params, 'perform_hn_without_windowing', False): scores *= self.output_window if getattr(self.params, 'advanced_localization', False): if (Debug): print("advanced: ") return self.localize_advanced(scores) # Get maximum max_score, max_disp = dcf.max2d(scores) _, scale_ind = torch.max(max_score, dim=0) max_disp = max_disp.float().cpu() # Convert to displacements in the base scale disp = (max_disp + self.output_sz / 2) % self.output_sz - self.output_sz / 2 # Compute translation vector and scale change factor translation_vec = disp[scale_ind, ...].view(-1) * ( self.img_support_sz / self.output_sz) * self.target_scale translation_vec *= self.params.scale_factors[scale_ind] # Shift the score output for visualization purposes if self.params.debug >= 2: sz = scores.shape[-2:] scores = torch.cat( [scores[..., sz[0] // 2:, :], scores[..., :sz[0] // 2, :]], -2) scores = torch.cat( [scores[..., :, sz[1] // 2:], scores[..., :, :sz[1] // 2]], -1) return translation_vec, scale_ind, scores, None