def test_th_batch_map_offsets(): np.random.seed(42) input = np.random.random((4, 100, 156)) offsets = (np.random.random((4, 100, 156, 2)) * 2) sp_mapped_vals = sp_batch_map_offsets(input, offsets) th_mapped_vals = th_batch_map_offsets(Variable(torch.from_numpy(input)), Variable(torch.from_numpy(offsets))) assert np.allclose(sp_mapped_vals, th_mapped_vals.data.numpy(), atol=1e-5)
def test_th_batch_map_offsets_grad(): np.random.seed(42) input = np.random.random((4, 100, 156)) offsets = (np.random.random((4, 100, 156, 2)) * 2) input = Variable(torch.from_numpy(input), requires_grad=True) offsets = Variable(torch.from_numpy(offsets), requires_grad=True) th_mapped_vals = th_batch_map_offsets(input, offsets) e = torch.from_numpy(np.random.random((4, 100, 156))) th_mapped_vals.backward(e) assert not np.allclose(input.grad.data.numpy(), 0) assert not np.allclose(offsets.grad.data.numpy(), 0)
def forward(self, x): """Return the deformed featured map""" x_shape = x.size() offsets = super(ConvOffset2D, self).forward(x) # offsets: (b*c, h, w, 2) offsets = self._to_bc_h_w_2(offsets, x_shape) # x: (b*c, h, w) x = self._to_bc_h_w(x, x_shape) # X_offset: (b*c, h, w) x_offset = th_batch_map_offsets(x, offsets) # x_offset: (b, h, w, c) x_offset = self._to_b_c_h_w(x_offset, x_shape) return x_offset
def forward(self, x): """Return the deformed featured map""" x_shape = x.size() offsets = super(ConvOffset2D, self).forward( x) #Gallus: perform normal convolution on 2D featuremap # offsets: (b*c, h, w, 2) Gallus: why 2? offsets = self._to_bc_h_w_2(offsets, x_shape) # x: (b*c, h, w) x = self._to_bc_h_w(x, x_shape) # X_offset: (b*c, h, w) x_offset = th_batch_map_offsets(x, offsets, grid=self._get_grid(self, x)) # x_offset: (b, h, w, c) x_offset = self._to_b_c_h_w(x_offset, x_shape) return x_offset
def forward(self, x): """Return the deformed featured map""" x_shape = x.size() offsets = F.conv2d(x, self.weight, self.bias, self.stride, self.padding, self.dilation, self.groups) # offsets: (b*c, h, w, 2) offsets = self._to_bc_h_w_2(offsets, x_shape) # x: (b*c, h, w) x = self._to_bc_h_w(x, x_shape) # X_offset: (b*c, h, w) x_offset = th_batch_map_offsets(x, offsets, grid=self._get_grid(self, x)) # x_offset: (b, h, w, c) x_offset = self._to_b_c_h_w(x_offset, x_shape) return x_offset