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 23 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
8 changes: 8 additions & 0 deletions docs/source/core.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
kornia.core
===========

.. currentmodule:: kornia.core

.. autoclass:: TensorWrapper
:members:
:undoc-members:
26 changes: 26 additions & 0 deletions docs/source/image.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
kornia.image
============

Module to provide a high level API to process images.

.. currentmodule:: kornia.image

.. autoclass:: ImageSize
:members:
:undoc-members:

.. autoclass:: PixelFormat
:members:
:undoc-members:

.. autoclass:: ChannelsOrder
:members:
:undoc-members:

.. autoclass:: ImageLayout
:members:
:undoc-members:

.. autoclass:: Image
:members:
:undoc-members:
1 change: 1 addition & 0 deletions docs/source/index.rst
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,7 @@ Join the community
geometry
sensors
io
image
losses
metrics
morphology
Expand Down
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 @@ def grayscale_to_rgb(image: Tensor) -> Tensor:
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:
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 @@ def rgb_to_grayscale(image: Tensor, rgb_weights: Optional[Tensor] = None) -> Ten
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 @@ def rgb_to_grayscale(image: Tensor, rgb_weights: Optional[Tensor] = None) -> Ten
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 @@ def bgr_to_grayscale(image: torch.Tensor) -> torch.Tensor:
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 @@ class GrayscaleToRgb(Module):
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 @@ class RgbToGrayscale(Module):
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])
self.rgb_weights = rgb_weights

def forward(self, image: Tensor) -> Tensor:
Expand All @@ -166,6 +173,7 @@ class BgrToGrayscale(Module):
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
2 changes: 2 additions & 0 deletions kornia/core/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
zeros,
zeros_like,
)
from .tensor_wrapper import TensorWrapper

__all__ = [
"concatenate",
Expand All @@ -40,4 +41,5 @@
"zeros_like",
"linspace",
"diag",
"TensorWrapper",
]
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 @@ -31,5 +29,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
5 changes: 4 additions & 1 deletion kornia/image/__init__.py
Original file line number Diff line number Diff line change
@@ -1 +1,4 @@
from .base import ImageSize
from .base import ChannelsOrder, ImageLayout, ImageSize, PixelFormat
from .image import Image

__all__ = ["ImageSize", "PixelFormat", "ChannelsOrder", "ImageLayout", "Image"]
54 changes: 49 additions & 5 deletions kornia/image/base.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
from __future__ import annotations

from dataclasses import dataclass

from kornia.core import Tensor
from enum import Enum


@dataclass
Expand All @@ -11,13 +10,58 @@ class ImageSize:

Args:
height: image height.
width: image width
width: image width.

Example:
>>> size = ImageSize(3, 4)
>>> size.height
3
>>> size.width
4
"""
height: int | Tensor
width: int | Tensor
height: int
width: int


class PixelFormat(Enum):
r"""Enum that represents the pixel format of an image."""
edgarriba marked this conversation as resolved.
Show resolved Hide resolved
GRAY = 0
RGB = 1
BGR = 2


class ChannelsOrder(Enum):
r"""Enum that represents the channels order of an image."""
CHANNELS_FIRST = 0
CHANNELS_LAST = 1


@dataclass
class ImageLayout:
gau-nernst marked this conversation as resolved.
Show resolved Hide resolved
"""Data class to represent the layout of an image.

Args:
image_size: image size.
channels: number of channels.
pixel_format: pixel format.
channels_order: channels order.

Example:
>>> layout = ImageLayout(ImageSize(3, 4), 3, PixelFormat.RGB, ChannelsOrder.CHANNEL_LAST)
>>> layout.image_size
ImageSize(height=3, width=4)
>>> layout.channels
3
>>> layout.pixel_format
<PixelFormat.RGB: 1>
>>> layout.channels_order
<ChannelsOrder.CHANNEL_LAST: 1>
"""

edgarriba marked this conversation as resolved.
Show resolved Hide resolved
image_size: ImageSize
channels: int
pixel_format: PixelFormat
channels_order: ChannelsOrder


# TODO: define CompressedImage