Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We鈥檒l occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add Image API #1562

Merged
merged 47 commits into from
Jul 16, 2023
Merged
Show file tree
Hide file tree
Changes from 15 commits
Commits
Show all changes
47 commits
Select commit Hold shift + click to select a range
7feaf52
initial commit for image api
edgarriba Feb 6, 2022
39f00dd
implement normalize/denormalize
edgarriba Feb 27, 2022
76d11ed
add more tests
edgarriba Feb 28, 2022
c9539aa
add docs
edgarriba Feb 28, 2022
f6300ce
remove normalize and polish api
edgarriba Mar 5, 2022
54f1cbe
add docs
edgarriba Mar 5, 2022
6c482de
polishing gray functionality
edgarriba Mar 5, 2022
3f22d91
more polishing
edgarriba Mar 6, 2022
2737f48
implement onxx tests
edgarriba Mar 6, 2022
28d5f06
rename ImageColor, and add contrib.io
edgarriba Mar 20, 2022
4c92b62
image as non-tensor
edgarriba Mar 24, 2022
fe8f6bf
magix functions
edgarriba Mar 26, 2022
8669dcd
rename ImagePrompter to VisualPrompter
edgarriba Apr 29, 2023
8d34619
Merge branch 'master' into feat/image_api
edgarriba May 27, 2023
762ec63
Merge branch 'master' into feat/image_api
edgarriba May 30, 2023
8ae1bea
[pre-commit.ci] pre-commit suggestions (#2394)
pre-commit-ci[bot] May 30, 2023
a5f973a
remove artifacts
edgarriba May 31, 2023
fd60abe
Merge branch 'master' into feat/image_api
edgarriba May 31, 2023
60952ee
remove requirements
edgarriba May 31, 2023
ac3ab33
Merge branch 'master' into feat/image_api
edgarriba Jul 10, 2023
aefdcea
simplify base class
edgarriba Jul 10, 2023
30ab7e9
add TensorWrapper
edgarriba Jul 10, 2023
6c499ee
fix docs generation
edgarriba Jul 10, 2023
3fcaec2
fix test
edgarriba Jul 12, 2023
0c57bb4
recover setup
edgarriba Jul 12, 2023
d211998
more fixes
edgarriba Jul 12, 2023
c7e2a86
implement write
edgarriba Jul 12, 2023
2379134
Merge branch 'master' into feat/image_api
edgarriba Jul 12, 2023
9621d72
add docs
edgarriba Jul 12, 2023
9e9e5ff
fix backend
edgarriba Jul 12, 2023
cf69857
fix backend
edgarriba Jul 12, 2023
009ec77
fix mypy
edgarriba Jul 12, 2023
fbf8daa
Fix write shape
edgarriba Jul 13, 2023
a5aa2cb
apply code review
edgarriba Jul 13, 2023
d631498
artifact
edgarriba Jul 13, 2023
58ba513
fix docstring
edgarriba Jul 13, 2023
af2030b
fix annotations
edgarriba Jul 13, 2023
2a83848
skip test because of dlpack
edgarriba Jul 13, 2023
53f7893
Update kornia/image/image.py
edgarriba Jul 13, 2023
0d9d455
simplify
edgarriba Jul 13, 2023
dd9c050
fix doctest
edgarriba Jul 13, 2023
ed5f92f
fix doctest
edgarriba Jul 13, 2023
c31123c
fix doctest
edgarriba Jul 13, 2023
21728c2
fix build docs
edgarriba Jul 13, 2023
089f648
iterate on the pixel format definition
edgarriba Jul 15, 2023
7e5ff6d
Update kornia/image/base.py
edgarriba Jul 16, 2023
42a1509
adjust test tolerance
edgarriba Jul 16, 2023
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
Binary file added docs/source/_static/img/disk_outdoor_depth.jpg
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added docs/source/_static/img/keynet.jpg
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added docs/source/_static/img/keynet_affnet.jpg
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
12 changes: 12 additions & 0 deletions docs/source/core.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
kornia.core
===========

.. currentmodule:: kornia.core

.. autoclass:: ImageColor
:members:
:undoc-members:

.. autoclass:: Image
:members:
:undoc-members:
14 changes: 11 additions & 3 deletions kornia/color/gray.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,18 +15,19 @@
The image data is assumed to be in the range of (0, 1).

Args:
image: grayscale image to be converted to RGB with shape :math:`(*,1,H,W)`.
image: grayscale image tensor to be converted to RGB with shape :math:`(*,1,H,W)`.

Returns:
RGB version of the image with shape :math:`(*,3,H,W)`.

Example:
>>> import torch
edgarriba marked this conversation as resolved.
Show resolved Hide resolved
>>> input = torch.randn(2, 1, 4, 5)
>>> gray = grayscale_to_rgb(input) # 2x3x4x5
"""
KORNIA_CHECK_IS_TENSOR(image)

if image.dim() < 3 or image.size(-3) != 1:
if len(image.shape) < 3 or image.shape[-3] != 1:

Check warning on line 30 in kornia/color/gray.py

View check run for this annotation

Codecov / codecov/patch

kornia/color/gray.py#L30

Added line #L30 was not covered by tests
raise ValueError(f"Input size must have a shape of (*, 1, H, W). " f"Got {image.shape}.")

return concatenate([image, image, image], -3)
Expand All @@ -51,6 +52,7 @@
color_conversions.html>`__.

Example:
>>> import torch
>>> input = torch.rand(2, 3, 4, 5)
>>> gray = rgb_to_grayscale(input) # 2x1x4x5
"""
Expand Down Expand Up @@ -81,7 +83,7 @@
return w_r * r + w_g * g + w_b * b


def bgr_to_grayscale(image: torch.Tensor) -> torch.Tensor:
def bgr_to_grayscale(image: Tensor) -> Tensor:
r"""Convert a BGR image to grayscale.

The image data is assumed to be in the range of (0, 1). First flips to RGB, then converts.
Expand All @@ -93,6 +95,7 @@
grayscale version of the image with shape :math:`(*,1,H,W)`.

Example:
>>> import torch
>>> input = torch.rand(2, 3, 4, 5)
>>> gray = bgr_to_grayscale(input) # 2x1x4x5
"""
Expand All @@ -118,6 +121,7 @@
https://docs.opencv.org/4.0.1/de/d25/imgproc_color_conversions.html

Example:
>>> import torch
>>> input = torch.rand(2, 1, 4, 5)
>>> rgb = GrayscaleToRgb()
>>> output = rgb(input) # 2x3x4x5
Expand All @@ -140,13 +144,16 @@
https://docs.opencv.org/4.0.1/de/d25/imgproc_color_conversions.html

Example:
>>> import torch
>>> input = torch.rand(2, 3, 4, 5)
>>> gray = RgbToGrayscale()
>>> output = gray(input) # 2x1x4x5
"""

def __init__(self, rgb_weights: Optional[Tensor] = None) -> None:
super().__init__()
if rgb_weights is None:
rgb_weights = Tensor([0.299, 0.587, 0.114])

Check warning on line 156 in kornia/color/gray.py

View check run for this annotation

Codecov / codecov/patch

kornia/color/gray.py#L155-L156

Added lines #L155 - L156 were not covered by tests
self.rgb_weights = rgb_weights

def forward(self, image: Tensor) -> Tensor:
Expand All @@ -166,6 +173,7 @@
https://docs.opencv.org/4.0.1/de/d25/imgproc_color_conversions.html

Example:
>>> import torch
>>> input = torch.rand(2, 3, 4, 5)
>>> gray = BgrToGrayscale()
>>> output = gray(input) # 2x1x4x5
Expand Down
25 changes: 25 additions & 0 deletions kornia/contrib/io.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
try:
import kornia_rs
except ImportError:
kornia_rs = None

Check warning on line 4 in kornia/contrib/io.py

View check run for this annotation

Codecov / codecov/patch

kornia/contrib/io.py#L1-L4

Added lines #L1 - L4 were not covered by tests

import torch

Check warning on line 6 in kornia/contrib/io.py

View check run for this annotation

Codecov / codecov/patch

kornia/contrib/io.py#L6

Added line #L6 was not covered by tests

from kornia.core import Tensor

Check warning on line 8 in kornia/contrib/io.py

View check run for this annotation

Codecov / codecov/patch

kornia/contrib/io.py#L8

Added line #L8 was not covered by tests


def read_image(path_file: str) -> Tensor:

Check warning on line 11 in kornia/contrib/io.py

View check run for this annotation

Codecov / codecov/patch

kornia/contrib/io.py#L11

Added line #L11 was not covered by tests
"""Read an image file and decode using the Kornia Rust backend.

Args:
path_file: Path to a valid image file.

Return:
Returns an image tensor with shape :math:`(3,H,W)`.
"""
if kornia_rs is None:

Check warning on line 20 in kornia/contrib/io.py

View check run for this annotation

Codecov / codecov/patch

kornia/contrib/io.py#L20

Added line #L20 was not covered by tests
raise ModuleNotFoundError("The io API is not available: `pip install kornia_rs` in a Linux system.")

cv_tensor = kornia_rs.read_image_rs(path_file)
th_tensor = torch.utils.dlpack.from_dlpack(cv_tensor) # HxWx3
return th_tensor.permute(2, 0, 1) # CxHxW

Check warning on line 25 in kornia/contrib/io.py

View check run for this annotation

Codecov / codecov/patch

kornia/contrib/io.py#L23-L25

Added lines #L23 - L25 were not covered by tests
12 changes: 5 additions & 7 deletions kornia/core/_backend.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
from typing import Union
from __future__ import annotations

import torch
import torch.nn.functional as F
from torch import device

# classes
Tensor = torch.Tensor
Expand All @@ -15,8 +13,8 @@
concatenate = torch.cat
stack = torch.stack
linspace = torch.linspace
normalize = F.normalize
pad = F.pad
normalize = torch.nn.functional.normalize
pad = torch.nn.functional.pad
eye = torch.eye
zeros = torch.zeros
zeros_like = torch.zeros_like
Expand All @@ -30,5 +28,5 @@
rand = torch.rand

# type alias
Device = Union[str, device, None]
Dtype = Union[torch.dtype, None]
Device = str | torch.device | None
Dtype = torch.dtype | None