How to use the kornia.utils function in kornia

To help you get started, we’ve selected a few kornia examples, based on popular ways it is used in public projects.

Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.

github kornia / kornia / test / test_contrib.py View on Github external
def test_jit(self, device):
        @torch.jit.script
        def op_script(input: torch.Tensor, height: int,
                      width: int) -> torch.Tensor:
            return kornia.denormalize_pixel_coordinates(input, height, width)
        height, width = 3, 4
        grid = kornia.utils.create_meshgrid(
            height, width, normalized_coordinates=True).to(device)

        actual = op_script(grid, height, width)
        expected = kornia.denormalize_pixel_coordinates(
            grid, height, width)

        assert_allclose(actual, expected)
github kornia / kornia / test / geometry / test_conversions.py View on Github external
def test_tensor_bhw2(self, device):
        height, width = 3, 4
        grid = kornia.utils.create_meshgrid(
            height, width, normalized_coordinates=True).to(device)

        expected = kornia.utils.create_meshgrid(
            height, width, normalized_coordinates=False).to(device)

        grid_norm = kornia.denormalize_pixel_coordinates(
            grid, height, width)

        assert_allclose(grid_norm, expected)
github kornia / kornia / test / utils / test_one_hot.py View on Github external
def test_smoke(self):
        num_classes = 4
        labels = torch.zeros(2, 2, 1, dtype=torch.int64)
        labels[0, 0, 0] = 0
        labels[0, 1, 0] = 1
        labels[1, 0, 0] = 2
        labels[1, 1, 0] = 3

        # convert labels to one hot tensor
        one_hot = kornia.utils.one_hot(labels, num_classes)

        assert pytest.approx(one_hot[0, labels[0, 0, 0], 0, 0].item(), 1.0)
        assert pytest.approx(one_hot[0, labels[0, 1, 0], 1, 0].item(), 1.0)
        assert pytest.approx(one_hot[1, labels[1, 0, 0], 0, 0].item(), 1.0)
        assert pytest.approx(one_hot[1, labels[1, 1, 0], 1, 0].item(), 1.0)
github kornia / kornia / test / geometry / test_conversions.py View on Github external
def test_list(self, device):
        height, width = 3, 4
        grid = kornia.utils.create_meshgrid(
            height, width, normalized_coordinates=False).to(device)
        grid = grid.contiguous().view(-1, 2)

        expected = kornia.utils.create_meshgrid(
            height, width, normalized_coordinates=True).to(device)
        expected = expected.contiguous().view(-1, 2)

        grid_norm = kornia.normalize_pixel_coordinates(
            grid, height, width)

        assert_allclose(grid_norm, expected)
github kornia / kornia / test / utils / test_metrics.py View on Github external
def test_four_classes_2d_perfect(self):
        batch_size = 1
        num_classes = 4
        actual = torch.tensor(
            [[[0, 0, 1, 1],
              [0, 0, 1, 1],
              [2, 2, 3, 3],
              [2, 2, 3, 3]]])
        predicted = torch.tensor(
            [[[0, 0, 1, 1],
              [0, 0, 1, 1],
              [2, 2, 3, 3],
              [2, 2, 3, 3]]])

        mean_iou = kornia.utils.metrics.mean_iou(predicted, actual, num_classes)
        mean_iou_real = torch.tensor(
            [[1.0, 1.0, 1.0, 1.0]], dtype=torch.float32)
        assert mean_iou.shape == (batch_size, num_classes)
        assert_allclose(mean_iou, mean_iou_real)
github kornia / kornia / test / utils / test_image.py View on Github external
def test_image_to_tensor(input_shape, expected):
    image = np.ones(input_shape)
    tensor = kornia.utils.image_to_tensor(image)
    assert tensor.shape == expected
    assert isinstance(tensor, torch.Tensor)
github kornia / kornia / test / utils / test_image.py View on Github external
def test_tensor_to_image(input_shape, expected):
    tensor = torch.ones(input_shape)
    image = kornia.utils.tensor_to_image(tensor)
    assert image.shape == expected
    assert isinstance(image, np.ndarray)
github kornia / kornia / test / utils / test_metrics.py View on Github external
def test_four_classes_2d_one_class_no_predicted(self):
        num_classes = 4
        actual = torch.tensor(
            [[[0, 0, 0, 0],
              [0, 0, 0, 0],
              [2, 2, 3, 3],
              [2, 2, 3, 3]]])
        predicted = torch.tensor(
            [[[3, 3, 2, 2],
              [3, 3, 2, 2],
              [2, 2, 3, 3],
              [2, 2, 3, 3]]])

        conf_mat = kornia.utils.metrics.confusion_matrix(
            predicted, actual, num_classes)
        conf_mat_real = torch.tensor(
            [[[0, 0, 4, 4],
              [0, 0, 0, 0],
              [0, 0, 4, 0],
              [0, 0, 0, 4]]], dtype=torch.float32)
        assert_allclose(conf_mat, conf_mat_real)
github kornia / kornia / examples / homography_regression / main.py View on Github external
pts_dst[..., 0] = convert_coordinates_to_pixel(
                pts_dst[..., 0], x_factor)
            pts_dst[..., 1] = convert_coordinates_to_pixel(
                pts_dst[..., 1], y_factor)

            # do the actual drawing
            for i in range(4):
                pt_i, pt_ii = tuple(pts_dst[i % 4]), tuple(pts_dst[(i + 1) % 4])
                image = cv2.line(image, pt_i, pt_ii, (255, 0, 0), 3)
            return image

        if iter_idx % args.log_interval_vis == 0:
            # merge warped and target image for visualization
            img_src_to_dst = warper(img_src, dst_homo_src())
            img_vis = 255. * 0.5 * (img_src_to_dst + img_dst)
            img_vis_np = dgm.utils.tensor_to_image(img_vis)
            image_draw = draw_rectangle(img_vis_np, dst_homo_src())
            # save warped image to disk
            file_name = os.path.join(
                args.output_dir, 'warped_{}.png'.format(iter_idx))
            cv2.imwrite(file_name, image_draw)
github kornia / kornia / examples / homography_regression / main.py View on Github external
def load_image(file_name):
    """Loads the image with OpenCV and converts to torch.Tensor
    """
    assert os.path.isfile(file_name), "Invalid file {}".format(file_name)

    # load image with OpenCV
    img = cv2.imread(file_name, cv2.IMREAD_COLOR)

    # convert image to torch tensor
    tensor = dgm.utils.image_to_tensor(img).float() / 255.
    tensor = tensor.view(1, *tensor.shape)  # 1xCxHxW

    return tensor, img