def indexed_forward(self, x, shift=None): if self.positive: positive(self.features) self.grid.data = torch.clamp(self.grid.data, -1, 1) N, c, w, h = x.size() m = self.gauss_pyramid.scale_n + 1 feat = self.features.view(1, m * c, self.outdims) if shift is None: grid = self.grid.expand(N, self.outdims, 1, 2) else: grid = self.grid.expand(N, self.outdims, 1, 2) + shift[:, None, None, :] pools = [] for xx in self.gauss_pyramid(x): _, _, img_w, img_h = xx.size() img_indexer = torch.tensor([(img_w -1) * img_h, img_h - 1]).type_as(grid) (grid + 1) / 2 img_index = torch.round((grid + 1) / 2 * (img_shape - 1)) pools.append(F.grid_sample(xx, adj_grid)) y = torch.cat(pools, dim=1).squeeze(-1) y = (y * feat).sum(1).view(N, self.outdims) if self.bias is not None: y = y + self.bias return y
def all_forward(self, x, shift=None): if self.positive: positive(self.features) self.grid.data = torch.clamp(self.grid.data, -1, 1) N, c, w, h = x.size() m = self.gauss_pyramid.scale_n + 1 feat = self.features.view(m * c, self.outdims, 1, 1).permute([1, 0, 2, 3]).contiguous() stacked = torch.cat(self.gauss_pyramid(x), dim=1) y = F.conv2d(stacked, feat, self.bias) _, _, yw, yh = y.size() return y
def simple_forward(self, x, shift=None): if self.positive: positive(self.features) N, ch, h, w = x.size() feat = self.features.view(1, -1, self.outdims) feat = feat[:, 0:ch, :] ctr_h, ctr_w = h // 2, w // 2 y = x[..., ctr_h, ctr_w].unsqueeze(-1).expand(N, ch, self.outdims) y = (y * feat).sum(1).view(N, self.outdims) if self.bias is not None: y = y + self.bias return y
def fixed_forward(self, x, shift=None): if self.positive: positive(self.features) N, ch, h, w = x.size() feat = self.features.view(1, -1, self.outdims) feat_scale = feat.mean() ctr_h, ctr_w = h // 2, w // 2 y = x[..., ctr_h, ctr_w].unsqueeze(-1).expand(N, ch, self.outdims) y = (y * feat_scale).sum(1).view(N, self.outdims) if self.bias is not None: y = y + self.bias return y
def disc_center_forward(self, x, shift=None): if self.positive: positive(self.features) self.grid.data = torch.clamp(self.grid.data, -1, 1) N, c, h, w = x.size() m = self.gauss_pyramid.scale_n + 1 feat = self.features.view(1, m * c, self.outdims) pools = [] for xx in self.gauss_pyramid(x): N, ch, img_h, img_w = xx.size() ctr_h, ctr_w = img_h // 2, img_w // 2 pools.append(xx[..., ctr_h, ctr_w].unsqueeze(-1).expand(N, ch, self.outdims)) y = torch.cat(pools, dim=1) y = (y * feat).sum(1).view(N, self.outdims) if self.bias is not None: y = y + self.bias return y