Skip to content

🐍 Geometric Computer Vision Library for Spatial AI

autogenerate image samples in the docs (#1105)

Edgar Ribad8f8d1380808
diff --git a/.gitignore b/.gitignore
index 2ff7c6da2..e4c31eb81 100644
--- a/.gitignore
+++ b/.gitignore
@@ -10,6 +10,7 @@ kornia.egg-info/
*.swp
*.swo
*.swn
+*.swm
.eggs/*
.cache/*
.pytest_cache/*
@@ -18,3 +19,4 @@ kornia.egg-info/
.coverage
*bak
*ipynb_checkpoints*
+docs/source/_static/*jpg
diff --git a/Makefile b/Makefile
index 6cc611f18..fd5efff05 100644
--- a/Makefile
+++ b/Makefile
@@ -48,7 +48,7 @@ docstyle: FORCE
pydocstyle kornia/
build-docs: FORCE
- cd docs; make clean html
+ cd docs; make clean images html
install: FORCE
python setup.py install
diff --git a/docs/Makefile b/docs/Makefile
index 316e362da..ea1edd839 100644
--- a/docs/Makefile
+++ b/docs/Makefile
@@ -22,3 +22,7 @@ help:
clean:
@echo "Removing everything under 'build'.."
@rm -rf $(BUILDDIR)/html/ $(BUILDDIR)/doctrees
+ @rm -rf $(SOURCEDIR)/_static/*.jpg
+
+images:
+ python3 generate_example_images.py
diff --git a/docs/generate_example_images.py b/docs/generate_example_images.py
new file mode 100644
index 000000000..b0f66f781
--- /dev/null
+++ b/docs/generate_example_images.py
@@ -0,0 +1,68 @@
+import importlib
+from pathlib import Path
+
+import cv2
+import numpy as np
+import requests
+import torch
+
+import kornia as K
+
+mod = importlib.import_module("kornia.augmentation")
+
+BASE_IMAGE_URL: str = "https://raw.githubusercontent.com/kornia/data/main/panda.jpg"
+OUTPUT_PATH = Path(__file__).absolute().parent / "source/_static"
+
+# perform request
+response = requests.get(BASE_IMAGE_URL).content
+
+# convert to array of ints
+nparr = np.frombuffer(response, np.uint8)
+
+# convert to image array
+img: np.ndarray = cv2.imdecode(nparr, cv2.IMREAD_UNCHANGED)
+
+# convert the image to a tensor
+img_t: torch.Tensor = K.utils.image_to_tensor(img, keepdim=False) # 1xCxHXW
+img_t = img_t.float() / 255.0
+
+# TODO: make this more generic for modules out of kornia.augmentation
+# Dictionary containing the transforms to generate the sample images:
+# Key: Name of the transform class.
+# Value: The default parameters to use.
+augmentations_list: dict = {
+ "ColorJitter": (0.1, 0.1, 0.1, 0.1),
+ "RandomAffine": ((-15.0, 20.0),),
+ "RandomBoxBlur": ((7, 7),),
+ "RandomChannelShuffle": (),
+ "RandomErasing": ((0.4, 0.8), (0.3, 1 / 0.3)),
+ "RandomElasticTransform": ((63, 63), (32, 32), (1.0, 1.0)),
+ "RandomEqualize": (),
+ "RandomFisheye": (torch.tensor([-0.3, 0.3]), torch.tensor([-0.3, 0.3]), torch.tensor([0.9, 1.0])),
+ "RandomGrayscale": (),
+ "RandomGaussianNoise": (0.0, 0.05),
+ "RandomHorizontalFlip": (),
+ "RandomInvert": (),
+ "RandomMotionBlur": (7, 35.0, 0.5),
+ "RandomPerspective": (0.2,),
+ "RandomPosterize": (3,),
+ "RandomResizedCrop": ((510, 1020), (3.0, 3.0), (2.0, 2.0)),
+ "RandomRotation": (45.0,),
+ "RandomSharpness": (1.0,),
+ "RandomSolarize": (0.1,),
+ "RandomVerticalFlip": (),
+ "RandomThinPlateSpline": (),
+}
+
+# ITERATE OVER THE TRANSFORMS
+
+for aug_name, args in augmentations_list.items():
+ # dynamically create the class instance
+ cls = getattr(mod, aug_name)
+ aug = cls(*args, p=1.0)
+ # apply the augmentaiton to the image and concat
+ out = aug(img_t)
+ out = torch.cat([img_t, out], dim=-1)
+ # save the output image
+ out_np = K.utils.tensor_to_image((out * 255.0).byte())
+ cv2.imwrite(str(OUTPUT_PATH / f"{aug_name}.jpg"), out_np)
diff --git a/kornia/augmentation/augmentation.py b/kornia/augmentation/augmentation.py
index d150c058b..fc64b36a9 100644
--- a/kornia/augmentation/augmentation.py
+++ b/kornia/augmentation/augmentation.py
@@ -48,6 +48,8 @@ from .utils import _range_bound, _transform_input
class RandomHorizontalFlip(GeometricAugmentationBase2D):
r"""Applies a random horizontal flip to a tensor image or a batch of tensor images with a given probability.
+ .. image:: _static/RandomHorizontalFlip.jpg
+
Input should be a tensor of shape (C, H, W) or a batch of tensors :math:`(B, C, H, W)`.
If Input is a tuple it is assumed that the first element contains the aforementioned tensors and the second,
the corresponding transformation matrix that has been applied to them. In this case the module
@@ -107,6 +109,8 @@ class RandomHorizontalFlip(GeometricAugmentationBase2D):
class RandomVerticalFlip(GeometricAugmentationBase2D):
r"""Applies a random vertical flip to a tensor image or a batch of tensor images with a given probability.
+ .. image:: _static/RandomVerticalFlip.jpg
+
Args:
p (float): probability of the image being flipped. Default value is 0.5
return_transform (bool): if ``True`` return the matrix describing the transformation applied to each
@@ -160,6 +164,8 @@ class RandomVerticalFlip(GeometricAugmentationBase2D):
class ColorJitter(IntensityAugmentationBase2D):
r"""Applies a random transformation to the brightness, contrast, saturation and hue of a tensor image.
+ .. image:: _static/ColorJitter.jpg
+
Args:
p (float): probability of applying the transformation. Default value is 1.
brightness (float or tuple): Default value is 0.
@@ -260,6 +266,8 @@ class ColorJitter(IntensityAugmentationBase2D):
class RandomGrayscale(IntensityAugmentationBase2D):
r"""Applies random transformation to Grayscale according to a probability p value.
+ .. image:: _static/RandomGrayscale.jpg
+
Args:
p (float): probability of the image to be transformed to grayscale. Default value is 0.1.
return_transform (bool): if ``True`` return the matrix describing the transformation applied to each
@@ -318,6 +326,8 @@ class RandomGrayscale(IntensityAugmentationBase2D):
class RandomErasing(IntensityAugmentationBase2D):
r"""Erases a random rectangle of a tensor image according to a probability p value.
+ .. image:: _static/RandomErasing.jpg
+
The operator removes image parts and fills them with zero values at a selected rectangle
for each of the images in the batch.
@@ -406,6 +416,8 @@ class RandomErasing(IntensityAugmentationBase2D):
class RandomPerspective(GeometricAugmentationBase2D):
r"""Applies a random perspective transformation to an image tensor with a given probability.
+ .. image:: _static/RandomPerspective.jpg
+
Args:
p (float): probability of the image being perspectively transformed. Default value is 0.5.
distortion_scale(float): it controls the degree of distortion and ranges from 0 to 1. Default value is 0.5.
@@ -499,6 +511,8 @@ class RandomPerspective(GeometricAugmentationBase2D):
class RandomAffine(GeometricAugmentationBase2D):
r"""Applies a random 2D affine transformation to a tensor image.
+ .. image:: _static/RandomAffine.jpg
+
The transformation is computed so that the image center is kept invariant.
Args:
@@ -823,6 +837,8 @@ class CenterCrop(GeometricAugmentationBase2D):
class RandomRotation(GeometricAugmentationBase2D):
r"""Applies a random rotation to a tensor image or a batch of tensor images given an amount of degrees.
+ .. image:: _static/RandomRotation.jpg
+
Args:
p (float): probability of applying the transformation. Default value is 0.5.
degrees (sequence or float or tensor): range of degrees to select from. If degrees is a number the
@@ -1152,6 +1168,8 @@ class RandomCrop(GeometricAugmentationBase2D):
class RandomResizedCrop(GeometricAugmentationBase2D):
r"""Crops random patches in an image tensor and resizes to a given size.
+ .. image:: _static/RandomResizedCrop.jpg
+
Args:
size (Tuple[int, int]): Desired output size (out_h, out_w) of each edge.
Must be Tuple[int, int], then out_h = size[0], out_w = size[1].
@@ -1399,6 +1417,8 @@ class Denormalize(IntensityAugmentationBase2D):
class RandomMotionBlur(IntensityAugmentationBase2D):
r"""Perform motion blur on 2D images (4D tensor).
+ .. image:: _static/RandomMotionBlur.jpg
+
Args:
p (float): probability of applying the transformation. Default value is 0.5.
kernel_size (int or Tuple[int, int]): motion kernel size (odd and positive).
@@ -1505,6 +1525,8 @@ class RandomMotionBlur(IntensityAugmentationBase2D):
class RandomSolarize(IntensityAugmentationBase2D):
r"""Solarize given tensor image or a batch of tensor images randomly.
+ .. image:: _static/RandomSolarize.jpg
+
Args:
p (float): probability of applying the transformation. Default value is 0.5.
thresholds (float or tuple): Default value is 0.1.
@@ -1584,6 +1606,8 @@ class RandomSolarize(IntensityAugmentationBase2D):
class RandomPosterize(IntensityAugmentationBase2D):
r"""Posterize given tensor image or a batch of tensor images randomly.
+ .. image:: _static/RandomPosterize.jpg
+
Args:
p (float): probability of applying the transformation. Default value is 0.5.
bits (int or tuple): Integer that ranged from (0, 8], in which 0 gives black image and 8 gives the original.
@@ -1653,6 +1677,8 @@ class RandomPosterize(IntensityAugmentationBase2D):
class RandomSharpness(IntensityAugmentationBase2D):
r"""Sharpen given tensor image or a batch of tensor images randomly.
+ .. image:: _static/RandomSharpness.jpg
+
Args:
p (float): probability of applying the transformation. Default value is 0.5.
sharpness (float or tuple): factor of sharpness strength. Must be above 0. Default value is 0.5.
@@ -1720,6 +1746,8 @@ class RandomSharpness(IntensityAugmentationBase2D):
class RandomEqualize(IntensityAugmentationBase2D):
r"""Equalize given tensor image or a batch of tensor images randomly.
+ .. image:: _static/RandomEqualize.jpg
+
Args:
p (float): Probability to equalize an image. Default value is 0.5.
same_on_batch (bool): apply the same transformation across the batch. Default: False.
@@ -1837,6 +1865,8 @@ class GaussianBlur(RandomGaussianBlur):
class RandomInvert(IntensityAugmentationBase2D):
r"""Invert the tensor images values randomly.
+ .. image:: _static/RandomInvert.jpg
+
Args:
max_val (torch.Tensor): The expected maximum value in the input tensor. The shape has to
according to the input tensor shape, or at least has to work with broadcasting. Default: 1.0.
@@ -1880,6 +1910,8 @@ class RandomInvert(IntensityAugmentationBase2D):
class RandomChannelShuffle(IntensityAugmentationBase2D):
r"""Shuffles the channels of a batch of multi-dimensional images.
+ .. image:: _static/RandomChannelShuffle.jpg
+
Args:
return_transform (bool): if ``True`` return the matrix describing the transformation applied to each
input tensor. If ``False`` and the input is a tuple the applied transformation wont be concatenated.
@@ -1922,6 +1954,8 @@ class RandomChannelShuffle(IntensityAugmentationBase2D):
class RandomGaussianNoise(IntensityAugmentationBase2D):
r"""Add gaussian noise to a batch of multi-dimensional images.
+ .. image:: _static/RandomGaussianNoise.jpg
+
Args:
mean (float): The mean of the gaussian distribution. Default: 0.
std (float): The standard deviation of the gaussian distribution. Default: 1.
@@ -1968,6 +2002,8 @@ class RandomGaussianNoise(IntensityAugmentationBase2D):
class RandomFisheye(GeometricAugmentationBase2D):
r"""Add random camera radial distortion.
+ .. image:: _static/RandomFisheye.jpg
+
Args:
center_x (torch.Tensor): Ranges to sample respect to x-coordinate center with shape (2,).
center_y (torch.Tensor): Ranges to sample respect to y-coordinate center with shape (2,).
@@ -2048,6 +2084,8 @@ class RandomFisheye(GeometricAugmentationBase2D):
class RandomElasticTransform(GeometricAugmentationBase2D):
r"""Add random elastic transformation to a tensor image.
+ .. image:: _static/RandomElasticTransform.jpg
+
Args:
kernel_size (Tuple[int, int]): the size of the Gaussian kernel. Default: (63, 63).
sigma (Tuple[float, float]): The standard deviation of the Gaussian in the y and x directions,
@@ -2109,6 +2147,8 @@ class RandomElasticTransform(GeometricAugmentationBase2D):
class RandomThinPlateSpline(GeometricAugmentationBase2D):
r"""Add random noise to the Thin Plate Spline algorithm.
+ .. image:: _static/RandomThinPlateSpline.jpg
+
Args:
scale (float): the scale factor to apply to the destionation points. Default: 0.2.
align_corners (bool): Interpolation flag used by `grid_sample`. Default: False.
@@ -2163,6 +2203,8 @@ class RandomThinPlateSpline(GeometricAugmentationBase2D):
class RandomBoxBlur(GeometricAugmentationBase2D):
"""Adds random blur with a box filter to an image tensor.
+ .. image:: _static/RandomBoxBlur.jpg
+
Args:
kernel_size (Tuple[int, int]): the blurring kernel size.
border_type (str): the padding mode to be applied before convolving.