def test_gaussian(self): np.testing.assert_allclose( gaussian_1d(0.5, 8), torch.tensor( [ 0.0000e00, 2.9802e-07, 1.3496e-03, 1.5731e-01, 6.8269e-01, 1.5731e-01, 1.3496e-03, 2.9802e-07, 0.0000e00, ] ), rtol=1e-4, ) np.testing.assert_allclose( gaussian_1d(1, 1), torch.tensor([0.24173, 0.382925, 0.24173]), rtol=1e-4, ) np.testing.assert_allclose( gaussian_1d(1, 1, normalize=True), torch.tensor([0.2790, 0.4420, 0.2790]), rtol=1e-4, )
def test_norm_false(self, variance, expected): extent = 6 atol = 1e-4 sigma = np.sqrt(variance) k_erf = gaussian_1d(sigma, truncated=extent / sigma, approx="erf", normalize=False).numpy() k_sampled = gaussian_1d(sigma, truncated=extent / sigma, approx="sampled").numpy() k_scalespace = gaussian_1d(sigma, truncated=extent / sigma, approx="scalespace").numpy() np.testing.assert_allclose(k_erf, expected[0], atol=atol) np.testing.assert_allclose(k_sampled, expected[1], atol=atol) np.testing.assert_allclose(k_scalespace, expected[2], atol=atol)
def test_scalespace_gaussian(self): np.testing.assert_allclose( gaussian_1d(0.5, 8, "scalespace"), torch.tensor( [ 7.9472e-06, 2.5451e-04, 6.1161e-03, 9.8113e-02, 7.9102e-01, 9.8113e-02, 6.1161e-03, 2.5451e-04, 7.9472e-06, ] ), rtol=1e-4, ) np.testing.assert_allclose( gaussian_1d(1, 1, "scalespace"), torch.tensor([0.20791, 0.46576, 0.20791]), rtol=1e-3, ) np.testing.assert_allclose( gaussian_1d(1, 1, "scalespace", normalize=True), torch.tensor([0.2358, 0.5283, 0.2358]), rtol=1e-3, ) np.testing.assert_allclose( gaussian_1d(5, 1, "scalespace"), torch.tensor( [ 0.048225, 0.057891, 0.06675, 0.073911, 0.078576, 0.080197, 0.078576, 0.073911, 0.06675, 0.057891, 0.048225, ] ), rtol=1e-3, )
def forward(self, x: torch.Tensor) -> torch.Tensor: """ Args: x: in shape [Batch, chns, H, W, D]. """ _kernel = [gaussian_1d(s, truncated=self.truncated, approx=self.approx) for s in self.sigma] return separable_filtering(x=x, kernels=_kernel)
def __init__(self, spatial_dims: int, sigma: Union[Sequence[float], float], truncated: float = 4.0) -> None: """ Args: spatial_dims: number of spatial dimensions of the input image. must have shape (Batch, channels, H[, W, ...]). sigma: std. truncated: spreads how many stds. """ super().__init__() self.spatial_dims = int(spatial_dims) _sigma = ensure_tuple_rep(sigma, self.spatial_dims) self.kernel = [ torch.nn.Parameter( torch.as_tensor(gaussian_1d(s, truncated), dtype=torch.float), False) for s in _sigma ] self.padding = [ cast(int, (same_padding(k.size()[0]))) for k in self.kernel ] self.conv_n = [F.conv1d, F.conv2d, F.conv3d][spatial_dims - 1] for idx, param in enumerate(self.kernel): self.register_parameter(f"kernel_{idx}", param)
def __init__(self, spatial_dims, sigma, truncated=4., device=None): """ Args: spatial_dims (int): number of spatial dimensions of the input image. must have shape (Batch, channels, H[, W, ...]). sigma (float): std. truncated (float): spreads how many stds. device (torch.device): device on which the tensor will be allocated. """ self.kernel = torch.nn.Parameter(torch.tensor(gaussian_1d(sigma, truncated)), False) self.spatial_dims = spatial_dims self.conv_n = [F.conv1d, F.conv2d, F.conv3d][spatial_dims - 1] self.padding = same_padding(self.kernel.size()[0]) self.device = device self.kernel = self.kernel.to(self.device)
def test_wrong_sigma(self): with self.assertRaises(ValueError): gaussian_1d(-1, 10) with self.assertRaises(ValueError): gaussian_1d(1, -10)
def test_wrong_sigma(self): with self.assertRaises(ValueError): gaussian_1d(1, -10) with self.assertRaises(NotImplementedError): gaussian_1d(1, 10, "wrong_arg")