Exemple #1
0
def cw_local_softmax_filter_sl1(seg_logit,Teacher_seg_logit, n, alpha):
    B, C, H, W = seg_logit.shape
    # H_new = H // n * n
    # W_new = W // n * n
    T_logit = Teacher_seg_logit.detach()
    S_logit = seg_logit * 1

    kernel = torch.ones([1,n,n])
    with torch.no_grad():
        Teacher_seg_logit_exp = T_logit.exp()
        T_filter = kornia.filter2D(Teacher_seg_logit_exp, kernel)
        T_filter = Teacher_seg_logit_exp / T_filter *n*n

    seg_logit_exp = S_logit.exp()
    S_filter = kornia.filter2D(seg_logit_exp, kernel)
    S_filter = seg_logit_exp / S_filter *n*n


    # smooth_l1_loss
    loss_kd = F.smooth_l1_loss(S_filter, T_filter)

    # alpha = 66666 / (4*4) * (n*n)
    # alpha = 33333 / (4*4) * (n*n)
    # alpha = 22222 / (4*4) * (n*n)
    # alpha = 42666 # 66666/25*16
    return  loss_kd * alpha
Exemple #2
0
def gaussian_blur2d(input: torch.Tensor,
                    kernel_size: Tuple[int, int],
                    sigma: Tuple[float, float],
                    border_type: str = 'reflect') -> torch.Tensor:
    r"""Creates an operator that blurs a tensor using a Gaussian filter.

    The operator smooths the given tensor with a gaussian kernel by convolving
    it to each channel. It supports batched operation.

    Arguments:
        input (torch.Tensor): the input tensor with shape :math:`(B,C,H,W)`.
        kernel_size (Tuple[int, int]): the size of the kernel.
        sigma (Tuple[float, float]): the standard deviation of the kernel.
        border_type (str): the padding mode to be applied before convolving.
          The expected modes are: ``'constant'``, ``'reflect'``,
          ``'replicate'`` or ``'circular'``. Default: ``'reflect'``.

    Returns:
        torch.Tensor: the blurred tensor with shape :math:`(B, C, H, W)`.

    Examples:
        >>> input = torch.rand(2, 4, 5, 5)
        >>> output = gaussian_blur2d(input, (3, 3), (1.5, 1.5))
        >>> output.shape
        torch.Size([2, 4, 5, 5])
    """
    kernel: torch.Tensor = torch.unsqueeze(get_gaussian_kernel2d(
        kernel_size, sigma),
                                           dim=0)

    return kornia.filter2D(input, kernel, border_type)
Exemple #3
0
    def test_normalized_mean_filter(self, device, dtype):
        kernel = torch.ones(1, 3, 3).to(device)
        input = torch.tensor([[[
            [0., 0., 0., 0., 0.],
            [0., 0., 0., 0., 0.],
            [0., 0., 5., 0., 0.],
            [0., 0., 0., 0., 0.],
            [0., 0., 0., 0., 0.],
        ]]],
                             device=device,
                             dtype=dtype).expand(2, 2, -1, -1)

        nv: float = 5. / 9  # normalization value
        expected = torch.tensor([[[
            [0., 0., 0., 0., 0.],
            [0., nv, nv, nv, 0.],
            [0., nv, nv, nv, 0.],
            [0., nv, nv, nv, 0.],
            [0., 0., 0., 0., 0.],
        ]]],
                                device=device,
                                dtype=dtype)

        actual = kornia.filter2D(input, kernel, normalized=True)
        assert_allclose(actual, expected)
Exemple #4
0
    def test_normalized_mean_filter(self, device, dtype):
        kernel = torch.ones(1, 3, 3).to(device)
        input = torch.tensor([[[
            [0., 0., 0., 0., 0.],
            [0., 0., 0., 0., 0.],
            [0., 0., 5., 0., 0.],
            [0., 0., 0., 0., 0.],
            [0., 0., 0., 0., 0.],
        ]]],
                             device=device,
                             dtype=dtype).expand(2, 2, -1, -1)

        nv: float = 5. / 9  # normalization value
        expected = torch.tensor([[[
            [0., 0., 0., 0., 0.],
            [0., nv, nv, nv, 0.],
            [0., nv, nv, nv, 0.],
            [0., nv, nv, nv, 0.],
            [0., 0., 0., 0., 0.],
        ]]],
                                device=device,
                                dtype=dtype).expand(2, 2, -1, -1)

        actual = kornia.filter2D(input, kernel, normalized=True)

        tol_val: float = utils._get_precision_by_name(device, 'xla', 1e-1,
                                                      1e-4)
        assert_allclose(actual, expected, rtol=tol_val, atol=tol_val)
Exemple #5
0
def laplacian(input: torch.Tensor,
              kernel_size: int,
              border_type: str = 'reflect',
              normalized: bool = True) -> torch.Tensor:
    r"""Creates an operator that returns a tensor using a Laplacian filter.

    The operator smooths the given tensor with a laplacian kernel by convolving
    it to each channel. It supports batched operation.

    Arguments:
        input (torch.Tensor): the input image tensor with shape :math:`(B, C, H, W)`.
        kernel_size (int): the size of the kernel.
        border_type (str): the padding mode to be applied before convolving.
          The expected modes are: ``'constant'``, ``'reflect'``,
          ``'replicate'`` or ``'circular'``. Default: ``'reflect'``.
        normalized (bool): if True, L1 norm of the kernel is set to 1.

    Return:
        torch.Tensor: the blurred image with shape :math:`(B, C, H, W)`.

    Examples:
        >>> input = torch.rand(2, 4, 5, 5)
        >>> output = laplacian(input, 3)
        >>> output.shape
        torch.Size([2, 4, 5, 5])
    """
    kernel: torch.Tensor = torch.unsqueeze(get_laplacian_kernel2d(kernel_size),
                                           dim=0)

    if normalized:
        kernel = normalize_kernel2d(kernel)

    return kornia.filter2D(input, kernel, border_type)
Exemple #6
0
    def test_noncontiguous(self, device):
        batch_size = 3
        inp = torch.rand(3, 5, 5).expand(batch_size, -1, -1, -1).to(device)
        kernel = torch.ones(1, 2, 2).to(device)

        actual = kornia.filter2D(inp, kernel)
        expected = actual
        assert_allclose(actual, actual)
Exemple #7
0
    def forward(self, input: torch.Tensor) -> torch.Tensor:  # type: ignore
        if not torch.is_tensor(input):
            raise TypeError("Input type is not a torch.Tensor. Got {}".format(
                type(input)))
        if not len(input.shape) == 4:
            raise ValueError(
                "Invalid input shape, we expect BxCxHxW. Got: {}".format(
                    input.shape))
        # blur image
        x_blur: torch.Tensor = kornia.filter2D(input, self.kernel,
                                               self.border_type)

        # reject even rows and columns.
        out: torch.Tensor = x_blur[..., ::2, ::2]
        return out
Exemple #8
0
 def test_normalized_mean_filter(self):
     kernel = torch.ones(1, 3, 3)
     input = torch.tensor([[[
         [0., 0., 0., 0., 0.],
         [0., 0., 0., 0., 0.],
         [0., 0., 5., 0., 0.],
         [0., 0., 0., 0., 0.],
         [0., 0., 0., 0., 0.],
     ]]]).expand(2, 2, -1, -1)
     expected = torch.tensor([[[
         [0., 0., 0., 0., 0.],
         [0., 5. / 9., 5. / 9., 5. / 9., 0.],
         [0., 5. / 9., 5. / 9., 5. / 9., 0.],
         [0., 5. / 9., 5. / 9., 5. / 9., 0.],
         [0., 0., 0., 0., 0.],
     ]]])
     actual = kornia.filter2D(input, kernel, normalized=True)
     assert_allclose(actual, expected)
Exemple #9
0
    def forward(self, input: torch.Tensor) -> torch.Tensor:  # type: ignore
        if not torch.is_tensor(input):
            raise TypeError("Input type is not a torch.Tensor. Got {}".format(
                type(input)))
        if not len(input.shape) == 4:
            raise ValueError(
                "Invalid input shape, we expect BxCxHxW. Got: {}".format(
                    input.shape))
        # upsample tensor
        b, c, height, width = input.shape
        x_up: torch.Tensor = F.interpolate(input,
                                           size=(height * 2, width * 2),
                                           mode='bilinear',
                                           align_corners=True)

        # blurs upsampled tensor
        x_blur: torch.Tensor = kornia.filter2D(x_up, self.kernel,
                                               self.border_type)
        return x_blur
Exemple #10
0
    def test_mean_filter_2batch_2ch(self):
        kernel = torch.ones(1, 3, 3)
        input = torch.tensor([[[
            [0., 0., 0., 0., 0.],
            [0., 0., 0., 0., 0.],
            [0., 0., 5., 0., 0.],
            [0., 0., 0., 0., 0.],
            [0., 0., 0., 0., 0.],
        ]]]).expand(2, 2, -1, -1)
        expected = torch.tensor([[[
            [0., 0., 0., 0., 0.],
            [0., 5., 5., 5., 0.],
            [0., 5., 5., 5., 0.],
            [0., 5., 5., 5., 0.],
            [0., 0., 0., 0., 0.],
        ]]])

        actual = kornia.filter2D(input, kernel)
        assert_allclose(actual, expected)
Exemple #11
0
    def test_even_sized_filter(self, device):
        kernel = torch.ones(1, 2, 2).to(device)
        input = torch.tensor([[[
            [0., 0., 0., 0., 0.],
            [0., 0., 0., 0., 0.],
            [0., 0., 5., 0., 0.],
            [0., 0., 0., 0., 0.],
            [0., 0., 0., 0., 0.],
        ]]]).to(device)
        expected = torch.tensor([[[
            [0., 0., 0., 0., 0.],
            [0., 5., 5., 0., 0.],
            [0., 5., 5., 0., 0.],
            [0., 0., 0., 0., 0.],
            [0., 0., 0., 0., 0.],
        ]]]).to(device)

        actual = kornia.filter2D(input, kernel)
        assert_allclose(actual, expected)
Exemple #12
0
def motion_blur(input: torch.Tensor,
                kernel_size: int,
                angle: Union[float, torch.Tensor],
                direction: Union[float, torch.Tensor],
                border_type: str = 'constant',
                mode: str = 'nearest') -> torch.Tensor:
    r"""Perform motion blur on 2D images (4D tensor).

    Args:
        input (torch.Tensor): the input tensor with shape :math:`(B, C, H, W)`.
        kernel_size (int): motion kernel width and height. It should be odd and positive.
        angle (Union[torch.Tensor, float]): angle of the motion blur in degrees (anti-clockwise rotation).
            If tensor, it must be :math:`(B,)`.
        direction (tensor or float): forward/backward direction of the motion blur.
            Lower values towards -1.0 will point the motion blur towards the back (with angle provided via angle),
            while higher values towards 1.0 will point the motion blur forward. A value of 0.0 leads to a
            uniformly (but still angled) motion blur.
            If tensor, it must be :math:`(B,)`.
        border_type (str): the padding mode to be applied before convolving. The expected modes are:
            ``'constant'``, ``'reflect'``, ``'replicate'`` or ``'circular'``. Default: ``'constant'``.
        mode (str): interpolation mode for rotating the kernel. ``'bilinear'`` or ``'nearest'``.
            Default: ``'nearest'``

    Return:
        torch.Tensor: the blurred image with shape :math:`(B, C, H, W)`.

    Example:
        >>> input = torch.randn(1, 3, 80, 90).repeat(2, 1, 1, 1)
        >>> # perform exact motion blur across the batch
        >>> out_1 = motion_blur(input, 5, 90., 1)
        >>> torch.allclose(out_1[0], out_1[1])
        True
        >>> # perform element-wise motion blur across the batch
        >>> out_1 = motion_blur(input, 5, torch.tensor([90., 180,]), torch.tensor([1., -1.]))
        >>> torch.allclose(out_1[0], out_1[1])
        False
    """
    assert border_type in ["constant", "reflect", "replicate", "circular"]
    kernel: torch.Tensor = get_motion_kernel2d(kernel_size, angle, direction,
                                               mode)
    return kornia.filter2D(input, kernel, border_type)
Exemple #13
0
    def test_mean_filter(self, device, dtype):
        kernel = torch.ones(1, 3, 3, device=device, dtype=dtype)
        input = torch.tensor([[[
            [0., 0., 0., 0., 0.],
            [0., 0., 0., 0., 0.],
            [0., 0., 5., 0., 0.],
            [0., 0., 0., 0., 0.],
            [0., 0., 0., 0., 0.],
        ]]],
                             device=device,
                             dtype=dtype)
        expected = torch.tensor([[[
            [0., 0., 0., 0., 0.],
            [0., 5., 5., 5., 0.],
            [0., 5., 5., 5., 0.],
            [0., 5., 5., 5., 0.],
            [0., 0., 0., 0., 0.],
        ]]],
                                device=device,
                                dtype=dtype)

        actual = kornia.filter2D(input, kernel)
        assert_allclose(actual, expected)
Exemple #14
0
 def test_smoke(self, device, dtype):
     kernel = torch.rand(1, 3, 3, device=device, dtype=dtype)
     input = torch.ones(1, 1, 7, 8, device=device, dtype=dtype)
     assert kornia.filter2D(input, kernel).shape == input.shape
Exemple #15
0
 def forward(self, x: torch.Tensor):  # type: ignore
     return kornia.filter2D(x, self.kernel, self.border_type)
Exemple #16
0
    def test_smoke(self):
        kernel = torch.rand(1, 3, 3)
        input = torch.ones(1, 1, 7, 8)

        assert kornia.filter2D(input, kernel).shape == input.shape
Exemple #17
0
 def forward(self, x):
     return kornia.filter2D(x, self.f, normalized=True)
 def forward(self, x):
     f = self.f
     f = f[None, None, :] * f[None, :, None]
     return filter2D(x, f, normalized=True)
Exemple #19
0
 def test_batch(self, batch_size, device, dtype):
     B: int = batch_size
     kernel = torch.rand(1, 3, 3, device=device, dtype=dtype)
     input = torch.ones(B, 3, 7, 8, device=device, dtype=dtype)
     assert kornia.filter2D(input, kernel).shape == input.shape