Skip to content

🐍 Geometric Computer Vision Library for Spatial AI

feat: differentiable jpeg (#2760)

João Gustavo A. Amorimde2bbbde04d7
diff --git a/docs/generate_examples.py b/docs/generate_examples.py
index 99dbc09be..41e40ed86 100644
--- a/docs/generate_examples.py
+++ b/docs/generate_examples.py
@@ -382,10 +382,13 @@ def main():
"invert": ((), 1),
"equalize_clahe": ((), 1),
"add_weighted": ((0.75, 0.25, 2.0), 1),
+ "jpeg_codec_differentiable": ((torch.tensor([50]),), 1),
}
# ITERATE OVER THE TRANSFORMS
for fn_name, (args, num_samples) in transforms.items():
img_in = img3.repeat(num_samples, 1, 1, 1)
+ if fn_name == "jpeg_codec_differentiable":
+ img_in = img_in[..., :176, :]
if fn_name == "add_weighted":
args_in = (img_in, args[0], img2, args[1], args[2])
else:
diff --git a/docs/source/enhance.rst b/docs/source/enhance.rst
index 126a77cd9..d27ebe525 100644
--- a/docs/source/enhance.rst
+++ b/docs/source/enhance.rst
@@ -51,6 +51,11 @@ Normalizations
.. autofunction:: zca_whiten
.. autofunction:: linear_transform
+Codec
+-----
+
+.. autofunction:: jpeg_codec_differentiable
+
Modules
-------
@@ -71,6 +76,8 @@ Modules
.. autoclass:: Invert
+.. autoclass:: JPEGCodecDifferentiable
+
ZCA Whitening Interactive Demo
------------------------------
diff --git a/docs/source/references.bib b/docs/source/references.bib
index 35283e61d..55b988b0d 100644
--- a/docs/source/references.bib
+++ b/docs/source/references.bib
@@ -374,3 +374,19 @@ month = {mar}
booktitle = {ICCV},
year = {2023}
}
+
+@inproceedings{shin2017,
+ title={JPEG-resistant Adversarial Images},
+ author={Shin, Richard and Song, Dawn},
+ booktitle={NIPS Workshop on Machine Learning and Computer Security},
+ volume={1},
+ pages={8},
+ year={2017}
+}
+
+@inproceedings{reich2024,
+ title={Differentiable JPEG: The Devil is in the Details},
+ author={Reich, Christoph and Debnath, Biplob and Patel, Deep and Chakradhar, Srimat},
+ booktitle={IEEE/CVF Winter Conference on Applications of Computer Vision (WACV)},
+ year={2024}
+}
diff --git a/kornia/enhance/__init__.py b/kornia/enhance/__init__.py
index d2fb0a678..cf076f36c 100644
--- a/kornia/enhance/__init__.py
+++ b/kornia/enhance/__init__.py
@@ -33,6 +33,7 @@ from .core import AddWeighted, add_weighted
from .equalization import equalize_clahe
from .histogram import histogram, histogram2d, image_histogram2d
from .integral import IntegralImage, IntegralTensor, integral_image, integral_tensor
+from .jpeg import JPEGCodecDifferentiable, jpeg_codec_differentiable
from .normalize import Denormalize, Normalize, denormalize, normalize, normalize_min_max
from .shift_rgb import shift_rgb
from .zca import ZCAWhitening, linear_transform, zca_mean, zca_whiten
@@ -50,6 +51,8 @@ __all__ = [
"adjust_saturation_raw",
"adjust_sigmoid",
"adjust_log",
+ "JPEGCodecDifferentiable",
+ "jpeg_codec_differentiable",
"solarize",
"equalize",
"equalize3d",
@@ -67,6 +70,7 @@ __all__ = [
"AdjustSaturationWithGraySubtraction",
"AdjustSigmoid",
"AdjustLog",
+ "JPEGCodecDifferentiable",
"Invert",
"add_weighted",
"AddWeighted",
diff --git a/kornia/enhance/jpeg.py b/kornia/enhance/jpeg.py
new file mode 100644
index 000000000..014baa714
--- /dev/null
+++ b/kornia/enhance/jpeg.py
@@ -0,0 +1,637 @@
+from __future__ import annotations
+
+from typing import Optional
+
+import torch
+
+from kornia.color import rgb_to_ycbcr, ycbcr_to_rgb
+from kornia.core import Device, Dtype, Module, Parameter, Tensor
+from kornia.core.check import (
+ KORNIA_CHECK,
+ KORNIA_CHECK_IS_TENSOR,
+ KORNIA_CHECK_SHAPE,
+)
+from kornia.geometry.transform.affwarp import rescale
+
+__all__ = ["jpeg_codec_differentiable", "JPEGCodecDifferentiable"]
+
+
+def _get_default_qt_y(device: Device, dtype: Dtype) -> Tensor:
+ """Generates default Quantization table of Y channel."""
+ return torch.tensor(
+ [
+ [16, 11, 10, 16, 24, 40, 51, 61],
+ [12, 12, 14, 19, 26, 58, 60, 55],
+ [14, 13, 16, 24, 40, 57, 69, 56],
+ [14, 17, 22, 29, 51, 87, 80, 62],
+ [18, 22, 37, 56, 68, 109, 103, 77],
+ [24, 35, 55, 64, 81, 104, 113, 92],
+ [49, 64, 78, 87, 103, 121, 120, 101],
+ [72, 92, 95, 98, 112, 100, 103, 99],
+ ],
+ device=device,
+ dtype=dtype,
+ )
+
+
+def _get_default_qt_c(device: Device, dtype: Dtype) -> Tensor:
+ """Generates default Quantization table of C channels."""
+ return torch.tensor(
+ [
+ [17, 18, 24, 47, 99, 99, 99, 99],
+ [18, 21, 26, 66, 99, 99, 99, 99],
+ [24, 26, 56, 99, 99, 99, 99, 99],
+ [47, 66, 99, 99, 99, 99, 99, 99],
+ [99, 99, 99, 99, 99, 99, 99, 99],
+ [99, 99, 99, 99, 99, 99, 99, 99],
+ [99, 99, 99, 99, 99, 99, 99, 99],
+ [99, 99, 99, 99, 99, 99, 99, 99],
+ ],
+ device=device,
+ dtype=dtype,
+ )
+
+
+def _patchify_8x8(input: Tensor) -> Tensor:
+ """Function extracts non-overlapping 8 x 8 patches from the given input image.
+
+ Args:
+ input (Tensor): Input image of the shape :math:`(B, H, W)`.
+
+ Returns:
+ output (Tensor): Image patchify of the shape :math:`(B, N, 8, 8)`.
+ """
+ # Get input shape
+ B, H, W = input.shape
+ # Patchify to shape [B, N, H // 8, W // 8]
+ output: Tensor = input.view(B, H // 8, 8, W // 8, 8).permute(0, 1, 3, 2, 4).reshape(B, -1, 8, 8)
+ return output
+
+
+def _unpatchify_8x8(input: Tensor, H: int, W: int) -> Tensor:
+ """Function reverses non-overlapping 8 x 8 patching.
+
+ Args:
+ input (Tensor): Input image of the shape :math:`(B, N, 8, 8)`.
+
+ Returns:
+ output (Tensor): Image patchify of the shape :math:`(B, H, W)`.
+ """
+ # Get input shape
+ B, N = input.shape[:2]
+ # Unpatch to [B, H, W]
+ output: Tensor = input.view(B, H // 8, W // 8, 8, 8).permute(0, 1, 3, 2, 4).reshape(B, H, W)
+ return output
+
+
+def _dct_8x8(input: Tensor) -> Tensor:
+ """Performs an 8 x 8 discrete cosine transform.
+
+ Args:
+ input (Tensor): Patched input tensor of the shape :math:`(B, N, 8, 8)`.
+
+ Returns:
+ output (Tensor): DCT output tensor of the shape :math:`(B, N, 8, 8)`.
+ """
+ # Get dtype and device
+ dtype: Dtype = input.dtype
+ device: Device = input.device
+ # Make DCT tensor and scaling
+ index: Tensor = torch.arange(8, dtype=dtype, device=device)
+ x, y, u, v = torch.meshgrid(index, index, index, index)
+ dct_tensor: Tensor = ((2.0 * x + 1.0) * u * torch.pi / 16.0).cos() * ((2.0 * y + 1.0) * v * torch.pi / 16.0).cos()
+ alpha: Tensor = torch.ones(8, dtype=dtype, device=device)
+ alpha[0] = 1.0 / (2**0.5)
+ dct_scale: Tensor = torch.einsum("i, j -> ij", alpha, alpha) * 0.25
+ # Apply DCT
+ output: Tensor = dct_scale[None, None] * torch.tensordot(input - 128.0, dct_tensor)
+ return output
+
+
+def _idct_8x8(input: Tensor) -> Tensor:
+ """Performs an 8 x 8 discrete cosine transform.
+
+ Args:
+ input (Tensor): Patched input tensor of the shape :math:`(B, N, 8, 8)`.
+
+ Returns:
+ output (Tensor): DCT output tensor of the shape :math:`(B, N, 8, 8)`.
+ """
+ # Get dtype and device
+ dtype: Dtype = input.dtype
+ device: Device = input.device
+ # Make and apply scaling
+ alpha: Tensor = torch.ones(8, dtype=dtype, device=device)
+ alpha[0] = 1.0 / (2**0.5)
+ dct_scale: Tensor = torch.outer(alpha, alpha)
+ input = input * dct_scale[None, None]
+ # Make DCT tensor and scaling
+ index: Tensor = torch.arange(8, dtype=dtype, device=device)
+ x, y, u, v = torch.meshgrid(index, index, index, index)
+ idct_tensor: Tensor = ((2.0 * u + 1.0) * x * torch.pi / 16.0).cos() * ((2.0 * v + 1.0) * y * torch.pi / 16.0).cos()
+ # Apply DCT
+ output: Tensor = 0.25 * torch.tensordot(input, idct_tensor, dims=2) + 128.0
+ return output
+
+
+def _differentiable_polynomial_rounding(input: Tensor) -> Tensor:
+ """This function implements differentiable rounding.
+
+ Args:
+ input (Tensor): Input tensor of any shape to be rounded.
+
+ Returns:
+ output (Tensor): Pseudo rounded tensor of the same shape as input tensor.
+ """
+ # Perform differentiable rounding
+ output: Tensor = torch.round(input) + (input - torch.round(input)) ** 3
+ return output
+
+
+def _differentiable_polynomial_floor(input: Tensor) -> Tensor:
+ """This function implements differentiable floor.
+
+ Args:
+ input (Tensor): Input tensor of any shape to be floored.
+
+ Returns:
+ output (Tensor): Pseudo rounded tensor of the same shape as input tensor.
+ """
+ # Perform differentiable rounding
+ output: Tensor = torch.floor(input) + (input - 0.5 - torch.floor(input)) ** 3
+ return output
+
+
+def _differentiable_clipping(
+ input: Tensor,
+ min: Optional[float] = None,
+ max: Optional[float] = None,
+ scale: float = 0.02,
+) -> Tensor:
+ """This function implements a differentiable and soft approximation of the clipping operation.
+
+ Args:
+ input (Tensor): Input tensor of any shape.
+ min (Optional[float]): Minimum value.
+ max (Optional[float]): Maximum value.
+ scale (float): Scale value. Default 0.02.
+
+ Returns:
+ output (Tensor): Clipped output tensor of the same shape as the input tensor.
+ """
+ # Make a copy of the input tensor
+ output: Tensor = input.clone()
+ # Perform differentiable soft clipping
+ if max is not None:
+ output[output > max] = -scale * (torch.exp(-output[output > max] + max) - 1.0) + max
+ if min is not None:
+ output[output < min] = scale * (torch.exp(output[output < min] - min) - 1.0) + min
+ return output
+
+
+def _jpeg_quality_to_scale(
+ compression_strength: Tensor,
+) -> Tensor:
+ """Converts a given JPEG quality to the scaling factor.
+
+ Args:
+ compression_strength (Tensor): Compression strength ranging from 0 to 100. Any shape is supported.
+
+ Returns:
+ scale (Tensor): Scaling factor to be applied to quantization matrix. Same shape as input.
+ """
+ # Get scale
+ scale: Tensor = _differentiable_polynomial_floor(
+ torch.where(compression_strength < 50, 5000.0 / compression_strength, 200.0 - 2.0 * compression_strength)
+ )
+ return scale
+
+
+def _quantize(
+ input: Tensor,
+ jpeg_quality: Tensor,
+ quantization_table: Tensor,
+) -> Tensor:
+ """Function performs quantization.
+
+ Args:
+ input (Tensor): Input tensor of the shape :math:`(B, N, 8, 8)`.
+ jpeg_quality (Tensor): Compression strength to be applied, shape is :math:`(B)`.
+ quantization_table (Tensor): Quantization table of the shape :math:`(1, 8, 8)` or :math:`(B, 8, 8)`.
+
+ Returns:
+ output (Tensor): Quantized output tensor of the shape :math:`(B, N, 8, 8)`.
+ """
+ # Scale quantization table
+ quantization_table_scaled: Tensor = (
+ quantization_table[:, None] * _jpeg_quality_to_scale(jpeg_quality)[:, None, None, None]
+ )
+ # Perform scaling
+ quantization_table = _differentiable_polynomial_floor(
+ _differentiable_clipping((quantization_table_scaled + 50.0) / 100.0, 1, 255)
+ )
+ output: Tensor = input / quantization_table
+ # Perform rounding
+ output = _differentiable_polynomial_rounding(output)
+ return output
+
+
+def _dequantize(
+ input: Tensor,
+ jpeg_quality: Tensor,
+ quantization_table: Tensor,
+) -> Tensor:
+ """Function performs dequantization.
+
+ Args:
+ input (Tensor): Input tensor of the shape :math:`(B, N, 8, 8)`.
+ jpeg_quality (Tensor): Compression strength to be applied, shape is :math:`(B)`.
+ quantization_table (Tensor): Quantization table of the shape :math:`(1, 8, 8)` or :math:`(B, 8, 8)`.
+
+ Returns:
+ output (Tensor): Quantized output tensor of the shape :math:`(B, N, 8, 8)`.
+ """
+ # Scale quantization table
+ quantization_table_scaled: Tensor = (
+ quantization_table[:, None] * _jpeg_quality_to_scale(jpeg_quality)[:, None, None, None]
+ )
+ # Perform scaling
+ output: Tensor = input * _differentiable_polynomial_floor(
+ _differentiable_clipping((quantization_table_scaled + 50.0) / 100.0, 1, 255)
+ )
+ return output
+
+
+def _chroma_subsampling(input_ycbcr: Tensor) -> tuple[Tensor, Tensor, Tensor]:
+ """This function implements chroma subsampling.
+
+ Args:
+ input_ycbcr (Tensor): YCbCr input tensor of the shape :math:`(B, 3, H, W)`.
+
+ Returns:
+ output_y (Tensor): Y component (not-subsampled), shape is :math:`(B, H, W)`.
+ output_cb (Tensor): Cb component (subsampled), shape is :math:`(B, H // 2, W // 2)`.
+ output_cr (Tensor): Cr component (subsampled), shape is :math:`(B, H // 2, W // 2)`.
+ """
+ # Get components
+ output_y: Tensor = input_ycbcr[:, 0]
+ output_cb: Tensor = input_ycbcr[:, 1]
+ output_cr: Tensor = input_ycbcr[:, 2]
+ # Perform average pooling of Cb and Cr channels
+ output_cb = rescale(output_cb[:, None], factor=0.5, interpolation="bilinear", align_corners=False, antialias=True)
+ output_cr = rescale(output_cr[:, None], factor=0.5, interpolation="bilinear", align_corners=False, antialias=True)
+ return output_y, output_cb[:, 0], output_cr[:, 0]
+
+
+def _chroma_upsampling(input_c: Tensor) -> Tensor:
+ """Function performs chroma upsampling.
+
+ Args:
+ input_c (Tensor): Cb or Cr component to be upsampled of the shape :math:`(B, H, W)`.
+
+ Returns:
+ output_c (Tensor): Upsampled C(b or r) component of the shape :math:`(B, H * 2, W * 2)`.
+ """
+ # Upsample component
+ output_c: Tensor = rescale(
+ input_c[:, None], factor=2.0, interpolation="bilinear", align_corners=False, antialias=False
+ )
+ return output_c[:, 0]
+
+
+def _jpeg_encode(
+ image_rgb: Tensor,
+ jpeg_quality: Tensor,
+ quantization_table_y: Tensor,
+ quantization_table_c: Tensor,
+) -> tuple[Tensor, Tensor, Tensor]:
+ """Performs JPEG encoding.
+
+ Args:
+ image_rgb (Tensor): RGB input images of the shape :math:`(B, 3, H, W)`.
+ jpeg_quality (Tensor): Compression strength of the shape :math:`(B)`.
+ quantization_table_y (Tensor): Quantization table for Y channel.
+ quantization_table_c (Tensor): Quantization table for C channels.
+
+ Returns:
+ y_encoded (Tensor): Encoded Y component of the shape :math:`(B, N, 8, 8)`.
+ cb_encoded (Tensor): Encoded Cb component of the shape :math:`(B, N, 8, 8)`.
+ cr_encoded (Tensor): Encoded Cr component of the shape :math:`(B, N, 8, 8)`.
+ """
+ # Convert RGB image to YCbCr.
+ image_ycbcr: Tensor = rgb_to_ycbcr(image_rgb)
+ # Scale pixel-range to [0, 255]
+ image_ycbcr = 255.0 * image_ycbcr
+ # Perform chroma subsampling
+ input_y, input_cb, input_cr = _chroma_subsampling(image_ycbcr)
+ # Patchify, DCT, and rounding
+ input_y, input_cb, input_cr = _patchify_8x8(input_y), _patchify_8x8(input_cb), _patchify_8x8(input_cr)
+ dct_y = _dct_8x8(input_y)
+ dct_cb_cr = _dct_8x8(torch.cat((input_cb, input_cr), dim=1))
+ y_encoded: Tensor = _quantize(
+ dct_y,
+ jpeg_quality,
+ quantization_table_y,
+ )
+ cb_encoded, cr_encoded = _quantize(
+ dct_cb_cr,
+ jpeg_quality,
+ quantization_table_c,
+ ).chunk(2, dim=1)
+ return y_encoded, cb_encoded, cr_encoded
+
+
+def _jpeg_decode(
+ input_y: Tensor,
+ input_cb: Tensor,
+ input_cr: Tensor,
+ jpeg_quality: Tensor,
+ H: int,
+ W: int,
+ quantization_table_y: Tensor,
+ quantization_table_c: Tensor,
+) -> Tensor:
+ """Performs JPEG decoding.
+
+ Args:
+ input_y (Tensor): Compressed Y component of the shape :math:`(B, N, 8, 8)`.
+ input_cb (Tensor): Compressed Cb component of the shape :math:`(B, N, 8, 8)`.
+ input_cr (Tensor): Compressed Cr component of the shape :math:`(B, N, 8, 8)`.
+ jpeg_quality (Tensor): Compression strength of the shape :math:`(B)`.
+ H (int): Original image height.
+ W (int): Original image width.
+ quantization_table_y (Tensor): Quantization table for Y channel.
+ quantization_table_c (Tensor): Quantization table for C channels.
+
+ Returns:
+ rgb_decoded (Tensor): Decompressed RGB image of the shape :math:`(B, 3, H, W)`.
+ """
+ # Dequantize inputs
+ input_y = _dequantize(
+ input_y,
+ jpeg_quality,
+ quantization_table_y,
+ )
+ input_cb_cr = _dequantize(
+ torch.cat((input_cb, input_cr), dim=1),
+ jpeg_quality,
+ quantization_table_c,
+ )
+ # Perform inverse DCT
+ idct_y: Tensor = _idct_8x8(input_y)
+ idct_cb, idct_cr = _idct_8x8(input_cb_cr).chunk(2, dim=1)
+ # Reverse patching
+ image_y: Tensor = _unpatchify_8x8(idct_y, H, W)
+ image_cb: Tensor = _unpatchify_8x8(idct_cb, H // 2, W // 2)
+ image_cr: Tensor = _unpatchify_8x8(idct_cr, H // 2, W // 2)
+ # Perform chroma upsampling
+ image_cb = _chroma_upsampling(image_cb)
+ image_cr = _chroma_upsampling(image_cr)
+ # Back to [0, 1] pixel-range
+ image_ycbcr: Tensor = torch.stack((image_y, image_cb, image_cr), dim=1) / 255.0
+ # Convert back to RGB space.
+ rgb_decoded: Tensor = ycbcr_to_rgb(image_ycbcr)
+ return rgb_decoded
+
+
+def jpeg_codec_differentiable(
+ image_rgb: Tensor,
+ jpeg_quality: Tensor,
+ quantization_table_y: Tensor | None = None,
+ quantization_table_c: Tensor | None = None,
+) -> Tensor:
+ r"""Differentiable JPEG encoding-decoding module.
+
+ Based on :cite:`reich2024` :cite:`shin2017`, we perform differentiable JPEG encoding-decoding as follows:
+
+ .. image:: _static/img/jpeg_codec_differentiable.png
+
+ .. math::
+
+ \text{JPEG}_{\text{diff}}(I, q, QT_{y}, QT_{c}) = \hat{I}
+
+ Where:
+ - :math:`I` is the original image to be coded.
+ - :math:`q` is the JPEG quality controlling the compression strength.
+ - :math:`QT_{y}` is the luma quantization table.
+ - :math:`QT_{c}` is the chroma quantization table.
+ - :math:`\hat{I}` is the resulting JPEG encoded-decoded image.
+
+ .. note:::
+ The input (and output) pixel range is :math:`[0, 1]`. In case you want to handle normalized images you are
+ required to first perform denormalization followed by normalizing the output images again.
+
+ Note, that this implementation models the encoding-decoding mapping of JPEG in a differentiable setting,
+ however, does not allow the excess of the JPEG-coded byte file itself.
+ For more details please refer to :cite:`reich2024`.
+
+ This implementation is not meant for data loading. For loading JPEG images please refer to `kornia.io`.
+ There we provide an optimized Rust implementation for fast JPEG loading.
+
+ Args:
+ image_rgb: the RGB image to be coded.
+ jpeg_quality: JPEG quality in the range :math:`[0, 100]` controlling the compression strength.
+ quantization_table_y: quantization table for Y channel. Default: `None`, which will load the standard
+ quantization table.
+ quantization_table_c: quantization table for C channels. Default: `None`, which will load the standard
+ quantization table.
+
+ Shape:
+ - image_rgb: :math:`(B, 3, H, W)`.
+ - jpeg_quality: :math:`(1)` or :math:`(B)`.
+ - quantization_table_y: :math:`(8, 8)` or :math:`(B, 8, 8)`.
+ - quantization_table_c: :math:`(8, 8)` or :math:`(B, 8, 8)`.
+
+ Return:
+ JPEG coded image of the shape :math:`(B, 3, H, W)`
+
+ Example:
+
+ To perform JPEG coding with the standard quantization tables just provide a JPEG quality
+
+ >>> img = torch.rand(3, 3, 64, 64, requires_grad=True, dtype=torch.float)
+ >>> jpeg_quality = torch.tensor((99.0, 25.0, 1.0), requires_grad=True)
+ >>> img_jpeg = jpeg_codec_differentiable(img, jpeg_quality)
+ >>> img_jpeg.sum().backward()
+
+ You also have the option to provide custom quantization tables
+
+ >>> img = torch.rand(3, 3, 64, 64, requires_grad=True, dtype=torch.float)
+ >>> jpeg_quality = torch.tensor((99.0, 25.0, 1.0), requires_grad=True)
+ >>> quantization_table_y = torch.randint(1, 256, size=(3, 8, 8), dtype=torch.float)
+ >>> quantization_table_c = torch.randint(1, 256, size=(3, 8, 8), dtype=torch.float)
+ >>> img_jpeg = jpeg_codec_differentiable(img, jpeg_quality, quantization_table_y, quantization_table_c)
+ >>> img_jpeg.sum().backward()
+
+ In case you want to control the quantization purly base on the quantization tables use a JPEG quality of 99.5.
+ Setting the JPEG quality to 99.5 leads to a QT scaling of 1, see Eq. 2 of :cite:`reich2024` for details.
+
+ >>> img = torch.rand(3, 3, 64, 64, requires_grad=True, dtype=torch.float)
+ >>> jpeg_quality = torch.ones(3) * 99.5
+ >>> quantization_table_y = torch.randint(1, 256, size=(3, 8, 8), dtype=torch.float)
+ >>> quantization_table_c = torch.randint(1, 256, size=(3, 8, 8), dtype=torch.float)
+ >>> img_jpeg = jpeg_codec_differentiable(img, jpeg_quality, quantization_table_y, quantization_table_c)
+ >>> img_jpeg.sum().backward()
+ """
+ # Check that inputs are tensors
+ KORNIA_CHECK_IS_TENSOR(image_rgb)
+ KORNIA_CHECK_IS_TENSOR(jpeg_quality)
+ # Get device and dtype
+ dtype: Dtype = image_rgb.dtype
+ device: Device = image_rgb.device
+ # Use default QT if QT is not given
+ quantization_table_y = _get_default_qt_y(device, dtype) if quantization_table_y is None else quantization_table_y
+ quantization_table_c = _get_default_qt_c(device, dtype) if quantization_table_c is None else quantization_table_c
+ KORNIA_CHECK_IS_TENSOR(quantization_table_y)
+ KORNIA_CHECK_IS_TENSOR(quantization_table_c)
+ # Check shape of inputs
+ KORNIA_CHECK_SHAPE(image_rgb, ["B", "3", "H", "W"])
+ KORNIA_CHECK(
+ (image_rgb.shape[2] % 16 == 0) and (image_rgb.shape[3] % 16 == 0),
+ f"image dimension must be divisible by 16. Got the shape {image_rgb.shape}.",
+ )
+ KORNIA_CHECK_SHAPE(jpeg_quality, ["B"])
+ # Add batch dimension to quantization tables if needed
+ if quantization_table_y.ndim == 2:
+ quantization_table_y = quantization_table_y.unsqueeze(dim=0)
+ if quantization_table_c.ndim == 2:
+ quantization_table_c = quantization_table_c.unsqueeze(dim=0)
+ # Check resulting shape of quantization tables
+ KORNIA_CHECK_SHAPE(quantization_table_y, ["B", "8", "8"])
+ KORNIA_CHECK_SHAPE(quantization_table_c, ["B", "8", "8"])
+ # Check value range of JPEG quality
+ KORNIA_CHECK(
+ (jpeg_quality.amin().item() >= 0.0) and (jpeg_quality.amax().item() <= 100.0),
+ f"JPEG quality is out of range. Expected range is [0, 99], "
+ f"got [{jpeg_quality.amin().item()}, {jpeg_quality.amax().item()}]. Consider clipping jpeg_quality.",
+ )
+ # Get original shape
+ H, W = image_rgb.shape[2:]
+ # Quantization tables to same device and dtype as input image
+ quantization_table_y = quantization_table_y.to(device, dtype)
+ quantization_table_c = quantization_table_c.to(device, dtype)
+ # Perform encoding
+ y_encoded, cb_encoded, cr_encoded = _jpeg_encode(
+ image_rgb=image_rgb,
+ jpeg_quality=jpeg_quality,
+ quantization_table_c=quantization_table_c,
+ quantization_table_y=quantization_table_y,
+ )
+ image_rgb_jpeg: Tensor = _jpeg_decode(
+ input_y=y_encoded,
+ input_cb=cb_encoded,
+ input_cr=cr_encoded,
+ jpeg_quality=jpeg_quality,
+ H=H,
+ W=W,
+ quantization_table_c=quantization_table_c,
+ quantization_table_y=quantization_table_y,
+ )
+ # Clip coded image
+ image_rgb_jpeg = _differentiable_clipping(input=image_rgb_jpeg, min=0.0, max=255.0)
+ return image_rgb_jpeg
+
+
+class JPEGCodecDifferentiable(Module):
+ r"""Differentiable JPEG encoding-decoding module.
+
+ Based on :cite:`reich2024` :cite:`shin2017`, we perform differentiable JPEG encoding-decoding as follows:
+
+ .. math::
+
+ \text{JPEG}_{\text{diff}}(I, q, QT_{y}, QT_{c}) = \hat{I}
+
+ Where:
+ - :math:`I` is the original image to be coded.
+ - :math:`q` is the JPEG quality controlling the compression strength.
+ - :math:`QT_{y}` is the luma quantization table.
+ - :math:`QT_{c}` is the chroma quantization table.
+ - :math:`\hat{I}` is the resulting JPEG encoded-decoded image.
+
+ .. image:: _static/img/jpeg_codec_differentiable.png
+
+ .. note::
+ The input (and output) pixel range is :math:`[0, 1]`. In case you want to handle normalized images you are
+ required to first perform denormalization followed by normalizing the output images again.
+
+ Note, that this implementation models the encoding-decoding mapping of JPEG in a differentiable setting,
+ however, does not allow the excess of the JPEG-coded byte file itself.
+ For more details please refer to :cite:`reich2024`.
+
+ This implementation is not meant for data loading. For loading JPEG images please refer to `kornia.io`.
+ There we provide an optimized Rust implementation for fast JPEG loading.
+
+ Args:
+ quantization_table_y: quantization table for Y channel. Default: `None`, which will load the standard
+ quantization table.
+ quantization_table_c: quantization table for C channels. Default: `None`, which will load the standard
+ quantization table.
+
+ Shape:
+ - quantization_table_y: :math:`(8, 8)` or :math:`(B, 8, 8)`.
+ - quantization_table_c: :math:`(8, 8)` or :math:`(B, 8, 8)`.
+ - image_rgb: :math:`(B, 3, H, W)`.
+ - jpeg_quality: :math:`(1)` or :math:`(B)`.
+
+ Example:
+
+ You can use the differentiable JPEG module with standard quantization tables by
+
+ >>> diff_jpeg_module = JPEGCodecDifferentiable()
+ >>> img = torch.rand(2, 3, 32, 32, requires_grad=True, dtype=torch.float)
+ >>> jpeg_quality = torch.tensor((99.0, 1.0), requires_grad=True)
+ >>> img_jpeg = diff_jpeg_module(img, jpeg_quality)
+ >>> img_jpeg.sum().backward()
+
+ You can also specify custom quantization tables to be used by
+
+ >>> quantization_table_y = torch.randint(1, 256, size=(2, 8, 8), dtype=torch.float)
+ >>> quantization_table_c = torch.randint(1, 256, size=(2, 8, 8), dtype=torch.float)
+ >>> diff_jpeg_module = JPEGCodecDifferentiable(quantization_table_y, quantization_table_c)
+ >>> img = torch.rand(2, 3, 32, 32, requires_grad=True, dtype=torch.float)
+ >>> jpeg_quality = torch.tensor((99.0, 1.0), requires_grad=True)
+ >>> img_jpeg = diff_jpeg_module(img, jpeg_quality)
+ >>> img_jpeg.sum().backward()
+
+ In case you want to learn the quantization tables just pass parameters `nn.Parameter`
+
+ >>> quantization_table_y = torch.nn.Parameter(torch.randint(1, 256, size=(2, 8, 8), dtype=torch.float))
+ >>> quantization_table_c = torch.nn.Parameter(torch.randint(1, 256, size=(2, 8, 8), dtype=torch.float))
+ >>> diff_jpeg_module = JPEGCodecDifferentiable(quantization_table_y, quantization_table_c)
+ >>> img = torch.rand(2, 3, 32, 32, requires_grad=True, dtype=torch.float)
+ >>> jpeg_quality = torch.tensor((99.0, 1.0), requires_grad=True)
+ >>> img_jpeg = diff_jpeg_module(img, jpeg_quality)
+ >>> img_jpeg.sum().backward()
+ """
+
+ def __init__(
+ self,
+ quantization_table_y: Tensor | Parameter | None = None,
+ quantization_table_c: Tensor | Parameter | None = None,
+ ) -> None:
+ super().__init__()
+ # Get default quantization tables if needed
+ quantization_table_y = _get_default_qt_y(None, None) if quantization_table_y is None else quantization_table_y
+ quantization_table_c = _get_default_qt_c(None, None) if quantization_table_c is None else quantization_table_c
+ if isinstance(quantization_table_y, Parameter):
+ self.register_parameter("quantization_table_y", quantization_table_y)
+ else:
+ self.register_buffer("quantization_table_y", quantization_table_y)
+ if isinstance(quantization_table_c, Parameter):
+ self.register_parameter("quantization_table_c", quantization_table_c)
+ else:
+ self.register_buffer("quantization_table_c", quantization_table_c)
+
+ def forward(
+ self,
+ image_rgb: Tensor,
+ jpeg_quality: Tensor,
+ ) -> Tensor:
+ # Perform encoding-decoding
+ image_rgb_jpeg: Tensor = jpeg_codec_differentiable(
+ image_rgb=image_rgb,
+ jpeg_quality=jpeg_quality,
+ quantization_table_c=self.quantization_table_c,
+ quantization_table_y=self.quantization_table_y,
+ )
+ return image_rgb_jpeg
diff --git a/tests/enhance/test_jpeg.py b/tests/enhance/test_jpeg.py
new file mode 100644
index 000000000..e8d5b77ff
--- /dev/null
+++ b/tests/enhance/test_jpeg.py
@@ -0,0 +1,1018 @@
+import pytest
+import torch
+
+import kornia
+
+from testing.base import BaseTester
+
+
+class TestDiffJPEG(BaseTester):
+ def test_smoke(self, device, dtype) -> None:
+ """This test standard usage."""
+ B, H, W = 2, 32, 32
+ img = torch.rand(B, 3, H, W, device=device, dtype=dtype)
+ jpeg_quality = torch.randint(low=0, high=100, size=(B,), device=device, dtype=dtype)
+ img_jpeg = kornia.enhance.jpeg_codec_differentiable(img, jpeg_quality)
+ assert img_jpeg is not None
+ assert img_jpeg.shape == img.shape
+
+ def test_custom_qt(self, device, dtype) -> None:
+ """Here we test if we can handle custom quantization tables."""
+ B, H, W = 4, 32, 32
+ img = torch.rand(B, 3, H, W, device=device, dtype=dtype)
+ jpeg_quality = torch.randint(low=0, high=100, size=(B,), device=device, dtype=dtype)
+ qt_y = torch.randint(low=1, high=255, size=(B, 8, 8), device=device, dtype=dtype)
+ qt_c = torch.randint(low=1, high=255, size=(B, 8, 8), device=device, dtype=dtype)
+ img_jpeg = kornia.enhance.jpeg_codec_differentiable(img, jpeg_quality, qt_y, qt_c)
+ assert img_jpeg is not None
+ assert img_jpeg.shape == img.shape
+
+ def test_non_batch_param(self, device, dtype) -> None:
+ """Here we test if we can handle non-batched JPEG parameters (JPEG quality and QT's)."""
+ B, H, W = 3, 32, 32
+ img = torch.rand(B, 3, H, W, device=device, dtype=dtype)
+ jpeg_quality = torch.randint(low=0, high=100, size=(1,), device=device, dtype=dtype)
+ qt_y = torch.randint(low=1, high=255, size=(1, 8, 8), device=device, dtype=dtype)
+ qt_c = torch.randint(low=1, high=255, size=(1, 8, 8), device=device, dtype=dtype)
+ img_jpeg = kornia.enhance.jpeg_codec_differentiable(img, jpeg_quality, qt_y, qt_c)
+ assert img_jpeg is not None
+ assert img_jpeg.shape == img.shape
+
+ def test_exception(self, device, dtype) -> None:
+ """Test exceptions (non-tensor input, wrong JPEG quality shape, wrong img shape, and wrong QT shape.)"""
+ with pytest.raises(TypeError) as errinfo:
+ B = 2
+ jpeg_quality = torch.randint(low=0, high=100, size=(B,), device=device, dtype=dtype)
+ kornia.enhance.jpeg_codec_differentiable(1904.0, jpeg_quality)
+ assert "Not a Tensor type. Got" in str(errinfo)
+
+ with pytest.raises(TypeError) as errinfo:
+ B, H, W = 2, 32, 32
+ img = torch.rand(B, 3, H, W, device=device, dtype=dtype)
+ kornia.enhance.jpeg_codec_differentiable(img, None)
+ assert "Not a Tensor type. Got" in str(errinfo)
+
+ with pytest.raises(TypeError) as errinfo:
+ B, H, W = 2, 32, 32
+ img = torch.rand(B, 3, H, W, device=device, dtype=dtype)
+ jpeg_quality = torch.randint(low=0, high=100, size=(B, 3, 2, 1), device=device, dtype=dtype)
+ kornia.enhance.jpeg_codec_differentiable(img, jpeg_quality)
+ assert "shape must be [" in str(errinfo)
+
+ with pytest.raises(Exception) as errinfo:
+ B, H, W = 2, 31, 31
+ img = torch.rand(B, 3, H, W, device=device, dtype=dtype)
+ jpeg_quality = torch.randint(low=0, high=100, size=(B,), device=device, dtype=dtype)
+ kornia.enhance.jpeg_codec_differentiable(img, jpeg_quality)
+ assert "divisible" in str(errinfo)
+
+ with pytest.raises(Exception) as errinfo:
+ B, H, W = 4, 32, 32
+ img = torch.rand(B, 3, H, W, device=device, dtype=dtype)
+ jpeg_quality = torch.randint(low=0, high=100, size=(B,), device=device, dtype=dtype)
+ qt_y = torch.randint(low=1, high=255, size=(B, 7, 8), device=device, dtype=dtype)
+ qt_c = torch.randint(low=1, high=255, size=(B, 8, 8), device=device, dtype=dtype)
+ kornia.enhance.jpeg_codec_differentiable(img, jpeg_quality, qt_y, qt_c)
+ assert "shape must be [" in str(errinfo)
+
+ with pytest.raises(Exception) as errinfo:
+ B, H, W = 4, 32, 32
+ img = torch.rand(B, 3, H, W, device=device, dtype=dtype)
+ jpeg_quality = torch.randint(low=0, high=100, size=(B,), device=device, dtype=dtype)
+ qt_y = torch.randint(low=1, high=255, size=(B, 8, 8), device=device, dtype=dtype)
+ qt_c = torch.randint(low=1, high=255, size=(B, 8, 7), device=device, dtype=dtype)
+ kornia.enhance.jpeg_codec_differentiable(img, jpeg_quality, qt_y, qt_c)
+ assert "shape must be [" in str(errinfo)
+
+ def test_cardinality(self, device, dtype) -> None:
+ B, H, W = 1, 16, 16
+ img = torch.zeros(B, 3, H, W, device=device, dtype=dtype)
+ img[..., 4:-4, 4:-4] = 1.0
+ jpeg_quality = torch.tensor([2.0], device=device, dtype=dtype)
+ img_jpeg = kornia.enhance.jpeg_codec_differentiable(img, jpeg_quality)
+ # Numbers generated based on reference implementation
+ img_jpeg_ref = torch.tensor(
+ [
+ [
+ [
+ [
+ -0.000,
+ 0.002,
+ 0.063,
+ 0.060,
+ 0.020,
+ 0.017,
+ 0.078,
+ 0.146,
+ 0.146,
+ 0.078,
+ 0.017,
+ 0.020,
+ 0.060,
+ 0.063,
+ 0.002,
+ -0.000,
+ ],
+ [
+ 0.002,
+ 0.015,
+ 0.008,
+ -0.000,
+ -0.000,
+ -0.000,
+ -0.000,
+ -0.000,
+ -0.000,
+ -0.000,
+ -0.000,
+ -0.000,
+ -0.000,
+ 0.008,
+ 0.015,
+ 0.002,
+ ],
+ [
+ 0.063,
+ 0.009,
+ -0.000,
+ -0.000,
+ -0.000,
+ -0.000,
+ -0.000,
+ -0.000,
+ -0.000,
+ -0.000,
+ -0.000,
+ -0.000,
+ -0.000,
+ -0.000,
+ 0.009,
+ 0.063,
+ ],
+ [
+ 0.060,
+ -0.000,
+ -0.000,
+ -0.000,
+ 0.173,
+ 0.246,
+ 0.178,
+ 0.080,
+ 0.080,
+ 0.178,
+ 0.246,
+ 0.173,
+ -0.000,
+ -0.000,
+ -0.000,
+ 0.060,
+ ],
+ [
+ 0.020,
+ -0.000,
+ -0.000,
+ 0.173,
+ 0.694,
+ 0.971,
+ 0.960,
+ 0.847,
+ 0.847,
+ 0.960,
+ 0.971,
+ 0.694,
+ 0.173,
+ -0.000,
+ -0.000,
+ 0.020,
+ ],
+ [
+ 0.017,
+ -0.000,
+ -0.000,
+ 0.246,
+ 0.971,
+ 1.000,
+ 1.000,
+ 1.000,
+ 1.000,
+ 1.000,
+ 1.000,
+ 0.971,
+ 0.246,
+ -0.000,
+ -0.000,
+ 0.017,
+ ],
+ [
+ 0.078,
+ -0.000,
+ -0.000,
+ 0.178,
+ 0.960,
+ 1.000,
+ 1.000,
+ 1.000,
+ 1.000,
+ 1.000,
+ 1.000,
+ 0.960,
+ 0.178,
+ -0.000,
+ -0.000,
+ 0.078,
+ ],
+ [
+ 0.146,
+ -0.000,
+ -0.000,
+ 0.080,
+ 0.847,
+ 1.000,
+ 1.000,
+ 0.781,
+ 0.781,
+ 1.000,
+ 1.000,
+ 0.847,
+ 0.080,
+ -0.000,
+ -0.000,
+ 0.146,
+ ],
+ [
+ 0.146,
+ -0.000,
+ -0.000,
+ 0.080,
+ 0.847,
+ 1.000,
+ 1.000,
+ 0.781,
+ 0.781,
+ 1.000,
+ 1.000,
+ 0.847,
+ 0.080,
+ -0.000,
+ -0.000,
+ 0.146,
+ ],
+ [
+ 0.078,
+ -0.000,
+ -0.000,
+ 0.178,
+ 0.960,
+ 1.000,
+ 1.000,
+ 1.000,
+ 1.000,
+ 1.000,
+ 1.000,
+ 0.960,
+ 0.178,
+ -0.000,
+ -0.000,
+ 0.078,
+ ],
+ [
+ 0.017,
+ -0.000,
+ -0.000,
+ 0.246,
+ 0.971,
+ 1.000,
+ 1.000,
+ 1.000,
+ 1.000,
+ 1.000,
+ 1.000,
+ 0.971,
+ 0.246,
+ -0.000,
+ -0.000,
+ 0.017,
+ ],
+ [
+ 0.020,
+ -0.000,
+ -0.000,
+ 0.173,
+ 0.694,
+ 0.971,
+ 0.960,
+ 0.847,
+ 0.847,
+ 0.960,
+ 0.971,
+ 0.694,
+ 0.173,
+ -0.000,
+ -0.000,
+ 0.020,
+ ],
+ [
+ 0.060,
+ -0.000,
+ -0.000,
+ -0.000,
+ 0.173,
+ 0.246,
+ 0.178,
+ 0.080,
+ 0.080,
+ 0.178,
+ 0.246,
+ 0.173,
+ -0.000,
+ -0.000,
+ -0.000,
+ 0.060,
+ ],
+ [
+ 0.063,
+ 0.009,
+ -0.000,
+ -0.000,
+ -0.000,
+ -0.000,
+ -0.000,
+ -0.000,
+ -0.000,
+ -0.000,
+ -0.000,
+ -0.000,
+ -0.000,
+ -0.000,
+ 0.009,
+ 0.063,
+ ],
+ [
+ 0.002,
+ 0.015,
+ 0.008,
+ -0.000,
+ -0.000,
+ -0.000,
+ -0.000,
+ -0.000,
+ -0.000,
+ -0.000,
+ -0.000,
+ -0.000,
+ -0.000,
+ 0.008,
+ 0.015,
+ 0.002,
+ ],
+ [
+ -0.000,
+ 0.002,
+ 0.063,
+ 0.060,
+ 0.020,
+ 0.017,
+ 0.078,
+ 0.146,
+ 0.146,
+ 0.078,
+ 0.017,
+ 0.020,
+ 0.060,
+ 0.063,
+ 0.002,
+ -0.000,
+ ],
+ ],
+ [
+ [
+ -0.000,
+ 0.002,
+ 0.063,
+ 0.060,
+ 0.020,
+ 0.017,
+ 0.078,
+ 0.146,
+ 0.146,
+ 0.078,
+ 0.017,
+ 0.020,
+ 0.060,
+ 0.063,
+ 0.002,
+ -0.000,
+ ],
+ [
+ 0.002,
+ 0.015,
+ 0.008,
+ -0.000,
+ -0.000,
+ -0.000,
+ -0.000,
+ -0.000,
+ -0.000,
+ -0.000,
+ -0.000,
+ -0.000,
+ -0.000,
+ 0.008,
+ 0.015,
+ 0.002,
+ ],
+ [
+ 0.063,
+ 0.009,
+ -0.000,
+ -0.000,
+ -0.000,
+ -0.000,
+ -0.000,
+ -0.000,
+ -0.000,
+ -0.000,
+ -0.000,
+ -0.000,
+ -0.000,
+ -0.000,
+ 0.009,
+ 0.063,
+ ],
+ [
+ 0.060,
+ -0.000,
+ -0.000,
+ -0.000,
+ 0.173,
+ 0.246,
+ 0.178,
+ 0.080,
+ 0.080,
+ 0.178,
+ 0.246,
+ 0.173,
+ -0.000,
+ -0.000,
+ -0.000,
+ 0.060,
+ ],
+ [
+ 0.020,
+ -0.000,
+ -0.000,
+ 0.173,
+ 0.694,
+ 0.971,
+ 0.960,
+ 0.847,
+ 0.847,
+ 0.960,
+ 0.971,
+ 0.694,
+ 0.173,
+ -0.000,
+ -0.000,
+ 0.020,
+ ],
+ [
+ 0.017,
+ -0.000,
+ -0.000,
+ 0.246,
+ 0.971,
+ 1.000,
+ 1.000,
+ 1.000,
+ 1.000,
+ 1.000,
+ 1.000,
+ 0.971,
+ 0.246,
+ -0.000,
+ -0.000,
+ 0.017,
+ ],
+ [
+ 0.078,
+ -0.000,
+ -0.000,
+ 0.178,
+ 0.960,
+ 1.000,
+ 1.000,
+ 1.000,
+ 1.000,
+ 1.000,
+ 1.000,
+ 0.960,
+ 0.178,
+ -0.000,
+ -0.000,
+ 0.078,
+ ],
+ [
+ 0.146,
+ -0.000,
+ -0.000,
+ 0.080,
+ 0.847,
+ 1.000,
+ 1.000,
+ 0.781,
+ 0.781,
+ 1.000,
+ 1.000,
+ 0.847,
+ 0.080,
+ -0.000,
+ -0.000,
+ 0.146,
+ ],
+ [
+ 0.146,
+ -0.000,
+ -0.000,
+ 0.080,
+ 0.847,
+ 1.000,
+ 1.000,
+ 0.781,
+ 0.781,
+ 1.000,
+ 1.000,
+ 0.847,
+ 0.080,
+ -0.000,
+ -0.000,
+ 0.146,
+ ],
+ [
+ 0.078,
+ -0.000,
+ -0.000,
+ 0.178,
+ 0.960,
+ 1.000,
+ 1.000,
+ 1.000,
+ 1.000,
+ 1.000,
+ 1.000,
+ 0.960,
+ 0.178,
+ -0.000,
+ -0.000,
+ 0.078,
+ ],
+ [
+ 0.017,
+ -0.000,
+ -0.000,
+ 0.246,
+ 0.971,
+ 1.000,
+ 1.000,
+ 1.000,
+ 1.000,
+ 1.000,
+ 1.000,
+ 0.971,
+ 0.246,
+ -0.000,
+ -0.000,
+ 0.017,
+ ],
+ [
+ 0.020,
+ -0.000,
+ -0.000,
+ 0.173,
+ 0.694,
+ 0.971,
+ 0.960,
+ 0.847,
+ 0.847,
+ 0.960,
+ 0.971,
+ 0.694,
+ 0.173,
+ -0.000,
+ -0.000,
+ 0.020,
+ ],
+ [
+ 0.060,
+ -0.000,
+ -0.000,
+ -0.000,
+ 0.173,
+ 0.246,
+ 0.178,
+ 0.080,
+ 0.080,
+ 0.178,
+ 0.246,
+ 0.173,
+ -0.000,
+ -0.000,
+ -0.000,
+ 0.060,
+ ],
+ [
+ 0.063,
+ 0.009,
+ -0.000,
+ -0.000,
+ -0.000,
+ -0.000,
+ -0.000,
+ -0.000,
+ -0.000,
+ -0.000,
+ -0.000,
+ -0.000,
+ -0.000,
+ -0.000,
+ 0.009,
+ 0.063,
+ ],
+ [
+ 0.002,
+ 0.015,
+ 0.008,
+ -0.000,
+ -0.000,
+ -0.000,
+ -0.000,
+ -0.000,
+ -0.000,
+ -0.000,
+ -0.000,
+ -0.000,
+ -0.000,
+ 0.008,
+ 0.015,
+ 0.002,
+ ],
+ [
+ -0.000,
+ 0.002,
+ 0.063,
+ 0.060,
+ 0.020,
+ 0.017,
+ 0.078,
+ 0.146,
+ 0.146,
+ 0.078,
+ 0.017,
+ 0.020,
+ 0.060,
+ 0.063,
+ 0.002,
+ -0.000,
+ ],
+ ],
+ [
+ [
+ -0.000,
+ 0.002,
+ 0.063,
+ 0.060,
+ 0.020,
+ 0.017,
+ 0.078,
+ 0.146,
+ 0.146,
+ 0.078,
+ 0.017,
+ 0.020,
+ 0.060,
+ 0.063,
+ 0.002,
+ -0.000,
+ ],
+ [
+ 0.002,
+ 0.015,
+ 0.008,
+ -0.000,
+ -0.000,
+ -0.000,
+ -0.000,
+ -0.000,
+ -0.000,
+ -0.000,
+ -0.000,
+ -0.000,
+ -0.000,
+ 0.008,
+ 0.015,
+ 0.002,
+ ],
+ [
+ 0.063,
+ 0.009,
+ -0.000,
+ -0.000,
+ -0.000,
+ -0.000,
+ -0.000,
+ -0.000,
+ -0.000,
+ -0.000,
+ -0.000,
+ -0.000,
+ -0.000,
+ -0.000,
+ 0.009,
+ 0.063,
+ ],
+ [
+ 0.060,
+ -0.000,
+ -0.000,
+ -0.000,
+ 0.173,
+ 0.246,
+ 0.178,
+ 0.080,
+ 0.080,
+ 0.178,
+ 0.246,
+ 0.173,
+ -0.000,
+ -0.000,
+ -0.000,
+ 0.060,
+ ],
+ [
+ 0.020,
+ -0.000,
+ -0.000,
+ 0.173,
+ 0.694,
+ 0.971,
+ 0.960,
+ 0.847,
+ 0.847,
+ 0.960,
+ 0.971,
+ 0.694,
+ 0.173,
+ -0.000,
+ -0.000,
+ 0.020,
+ ],
+ [
+ 0.017,
+ -0.000,
+ -0.000,
+ 0.246,
+ 0.971,
+ 1.000,
+ 1.000,
+ 1.000,
+ 1.000,
+ 1.000,
+ 1.000,
+ 0.971,
+ 0.246,
+ -0.000,
+ -0.000,
+ 0.017,
+ ],
+ [
+ 0.078,
+ -0.000,
+ -0.000,
+ 0.178,
+ 0.960,
+ 1.000,
+ 1.000,
+ 1.000,
+ 1.000,
+ 1.000,
+ 1.000,
+ 0.960,
+ 0.178,
+ -0.000,
+ -0.000,
+ 0.078,
+ ],
+ [
+ 0.146,
+ -0.000,
+ -0.000,
+ 0.080,
+ 0.847,
+ 1.000,
+ 1.000,
+ 0.781,
+ 0.781,
+ 1.000,
+ 1.000,
+ 0.847,
+ 0.080,
+ -0.000,
+ -0.000,
+ 0.146,
+ ],
+ [
+ 0.146,
+ -0.000,
+ -0.000,
+ 0.080,
+ 0.847,
+ 1.000,
+ 1.000,
+ 0.781,
+ 0.781,
+ 1.000,
+ 1.000,
+ 0.847,
+ 0.080,
+ -0.000,
+ -0.000,
+ 0.146,
+ ],
+ [
+ 0.078,
+ -0.000,
+ -0.000,
+ 0.178,
+ 0.960,
+ 1.000,
+ 1.000,
+ 1.000,
+ 1.000,
+ 1.000,
+ 1.000,
+ 0.960,
+ 0.178,
+ -0.000,
+ -0.000,
+ 0.078,
+ ],
+ [
+ 0.017,
+ -0.000,
+ -0.000,
+ 0.246,
+ 0.971,
+ 1.000,
+ 1.000,
+ 1.000,
+ 1.000,
+ 1.000,
+ 1.000,
+ 0.971,
+ 0.246,
+ -0.000,
+ -0.000,
+ 0.017,
+ ],
+ [
+ 0.020,
+ -0.000,
+ -0.000,
+ 0.173,
+ 0.694,
+ 0.971,
+ 0.960,
+ 0.847,
+ 0.847,
+ 0.960,
+ 0.971,
+ 0.694,
+ 0.173,
+ -0.000,
+ -0.000,
+ 0.020,
+ ],
+ [
+ 0.060,
+ -0.000,
+ -0.000,
+ -0.000,
+ 0.173,
+ 0.246,
+ 0.178,
+ 0.080,
+ 0.080,
+ 0.178,
+ 0.246,
+ 0.173,
+ -0.000,
+ -0.000,
+ -0.000,
+ 0.060,
+ ],
+ [
+ 0.063,
+ 0.009,
+ -0.000,
+ -0.000,
+ -0.000,
+ -0.000,
+ -0.000,
+ -0.000,
+ -0.000,
+ -0.000,
+ -0.000,
+ -0.000,
+ -0.000,
+ -0.000,
+ 0.009,
+ 0.063,
+ ],
+ [
+ 0.002,
+ 0.015,
+ 0.008,
+ -0.000,
+ -0.000,
+ -0.000,
+ -0.000,
+ -0.000,
+ -0.000,
+ -0.000,
+ -0.000,
+ -0.000,
+ -0.000,
+ 0.008,
+ 0.015,
+ 0.002,
+ ],
+ [
+ -0.000,
+ 0.002,
+ 0.063,
+ 0.060,
+ 0.020,
+ 0.017,
+ 0.078,
+ 0.146,
+ 0.146,
+ 0.078,
+ 0.017,
+ 0.020,
+ 0.060,
+ 0.063,
+ 0.002,
+ -0.000,
+ ],
+ ],
+ ]
+ ],
+ device=device,
+ dtype=dtype,
+ )
+ # We use a slightly higher tolerance since our implementation varies from the reference implementation
+ self.assert_close(img_jpeg, img_jpeg_ref, rtol=0.01, atol=0.01)
+
+ def test_module(self, device, dtype) -> None:
+ B, H, W = 4, 16, 16
+ img = torch.rand(B, 3, H, W, device=device, dtype=dtype)
+ jpeg_quality = torch.randint(low=0, high=100, size=(B,), device=device, dtype=dtype)
+ qt_y = torch.randint(low=1, high=255, size=(B, 8, 8), device=device, dtype=dtype)
+ qt_c = torch.randint(low=1, high=255, size=(B, 8, 8), device=device, dtype=dtype)
+ diff_jpeg_module = kornia.enhance.JPEGCodecDifferentiable(qt_y, qt_c)
+ for name, _ in diff_jpeg_module.named_parameters():
+ print(name)
+ img_jpeg = diff_jpeg_module(img, jpeg_quality)
+ assert img_jpeg is not None
+ assert img_jpeg.shape == img.shape
+
+ def test_module_with_param(self, device, dtype) -> None:
+ B, H, W = 4, 16, 16
+ img = torch.rand(B, 3, H, W, device=device, dtype=dtype)
+ jpeg_quality = torch.randint(low=0, high=100, size=(B,), device=device, dtype=dtype)
+ qt_y = torch.nn.Parameter(torch.randint(low=1, high=255, size=(B, 8, 8), device=device, dtype=dtype))
+ qt_c = torch.nn.Parameter(torch.randint(low=1, high=255, size=(B, 8, 8), device=device, dtype=dtype))
+ diff_jpeg_module = kornia.enhance.JPEGCodecDifferentiable(qt_y, qt_c)
+ for name, _ in diff_jpeg_module.named_parameters():
+ print(name)
+ img_jpeg = diff_jpeg_module(img, jpeg_quality)
+ assert img_jpeg is not None
+ assert img_jpeg.shape == img.shape
+
+ # @pytest.mark.slow
+ def test_gradcheck(self, device) -> None:
+ """We test that the gradient matches the gradient of the reference implementation."""
+ B, H, W = 1, 16, 16
+ img = torch.zeros(B, 3, H, W, device=device, dtype=torch.float)
+ img[..., 0, 4:-4, 4:-4] = 1.0
+ img[..., 1, 4:-4, 4:-4] = 0.5
+ img[..., 2, 4:-4, 4:-4] = 0.5
+ img.requires_grad = True
+ jpeg_quality = torch.tensor([10.0], device=device, dtype=torch.float, requires_grad=True)
+ img_jpeg = kornia.enhance.jpeg_codec_differentiable(img, jpeg_quality)
+ (img_jpeg - torch.zeros_like(img_jpeg)).abs().sum().backward()
+ # Numbers generated based on reference implementation
+ img_jpeg_mean_grad_ref = torch.tensor([0.1919])
+ jpeg_quality_grad_ref = torch.tensor([0.0042])
+ # We use a slightly higher tolerance since our implementation varies from the reference implementation
+ self.assert_close(img.grad.mean().view(-1), img_jpeg_mean_grad_ref, rtol=0.01, atol=0.01)
+ self.assert_close(jpeg_quality.grad, jpeg_quality_grad_ref, rtol=0.01, atol=0.01)