π Geometric Computer Vision Library for Spatial AI
Python artificial-intelligencecomputer-visiondeep-learninghacktoberfestimage-processingmachine-learningneural-networkpythonpytorchroboticsspatial-ai
refactor imports and fix augmentation module docs
edgarribab75ad9209777
diff --git a/docs/source/augmentation.functional.rst b/docs/source/augmentation.functional.rstdeleted file mode 100644index 9c1fc74f3..000000000--- a/docs/source/augmentation.functional.rst+++ /dev/null@@ -1,16 +0,0 @@-kornia.augmentation----------------------.. currentmodule:: kornia.augmentation.functional--The functions in this section perform various data augmentation operations--.. autofunction:: apply_hflip-.. autofunction:: random_hflip-.. autofunction:: apply_vflip-.. autofunction:: random_vflip-.. autofunction:: color_jitter-.. autofunction:: apply_color_jitter-.. autofunction:: random_grayscale-.. autofunction:: apply_grayscale-.. autofunction:: random_rectangle_erasediff --git a/docs/source/augmentation.param_gen.rst b/docs/source/augmentation.param_gen.rstdeleted file mode 100644index 45b042985..000000000--- a/docs/source/augmentation.param_gen.rst+++ /dev/null@@ -1,9 +0,0 @@-kornia.augmentation----------------------.. currentmodule:: kornia.augmentation.param_gen--The functions in this section generate random parameters for various data augmentation operations--.. autofunction:: _random_color_jitter_gen-.. autofunction:: _random_prob_gendiff --git a/docs/source/augmentation.rst b/docs/source/augmentation.rstindex eb96f9955..45fdf5f4d 100644--- a/docs/source/augmentation.rst+++ b/docs/source/augmentation.rst@@ -7,6 +7,9 @@ The classes in this section perform various data augmentation operations.. autoclass:: RandomHorizontalFlip.. autoclass:: RandomVerticalFlip-.. autoclass:: ColorJitter.. autoclass:: RandomRectangleErasing.. autoclass:: RandomGrayscale+.. autoclass:: ColorJitter++.. automodule:: kornia.augmentation.functional+ :members:diff --git a/docs/source/conf.py b/docs/source/conf.pyindex 6a252a611..e2ff5ba37 100644--- a/docs/source/conf.py+++ b/docs/source/conf.py@@ -1,6 +1,9 @@import osimport sys+import torch+import kornia+import sphinx_galleryimport sphinx_rtd_themediff --git a/docs/source/index.rst b/docs/source/index.rstindex d73cd0d3d..406e04150 100644--- a/docs/source/index.rst+++ b/docs/source/index.rst@@ -18,8 +18,6 @@ Kornia Documentation:caption: Package Referenceaugmentation- augmentation.functional- augmentation.param_gencolorcontribfeaturediff --git a/kornia/__init__.py b/kornia/__init__.pyindex 5a00924bf..3adedb314 100644--- a/kornia/__init__.py+++ b/kornia/__init__.py@@ -6,6 +6,7 @@ if sys.version_info < (3, 6, 0):from .version import __version__+from kornia import augmentationfrom kornia import colorfrom kornia import contribfrom kornia import feature@@ -13,10 +14,10 @@ from kornia import filtersfrom kornia import geometryfrom kornia import lossesfrom kornia import utils-from kornia import augmentation# Exposes package functional to top level+from kornia.augmentation.functional import *from kornia.color import (rgb_to_grayscale,bgr_to_grayscale,@@ -86,12 +87,6 @@ from kornia.utils import (save_pointcloud_ply,load_pointcloud_ply,)-from kornia.augmentation import (- random_hflip,- random_vflip,- color_jitter,- random_grayscale,-)from kornia.geometry import *from kornia.geometry import pidiff --git a/kornia/augmentation/__init__.py b/kornia/augmentation/__init__.pyindex d702d2576..0f42d4a00 100644--- a/kornia/augmentation/__init__.py+++ b/kornia/augmentation/__init__.py@@ -1,16 +1 @@-from kornia.augmentation.augmentations import *-from kornia.augmentation.functional import *-from kornia.augmentation.random_erasing import *--__all__ = [- "random_hflip",- "RandomHorizontalFlip",- "random_vflip",- "RandomVerticalFlip",- "color_jitter",- "ColorJitter",- "random_rectangle_erase",- "RandomRectangleErasing",- "random_grayscale",- "RandomGrayscale",-]+from .augmentation import *diff --git a/kornia/augmentation/augmentations.py b/kornia/augmentation/augmentation.pysimilarity index 85%rename from kornia/augmentation/augmentations.pyrename to kornia/augmentation/augmentation.pyindex 4e2afb6b9..24aaa54e9 100644--- a/kornia/augmentation/augmentations.py+++ b/kornia/augmentation/augmentation.py@@ -3,8 +3,17 @@ from typing import Callable, Tuple, Union, List, Optional, Dict, castimport torchimport torch.nn as nn-import kornia.augmentation.functional as F-import kornia.augmentation.param_gen as pg+from . import functional as F+from . import param_gen as pg+++__all__ = [+ "RandomHorizontalFlip",+ "RandomVerticalFlip",+ "RandomRectangleErasing",+ "RandomGrayscale",+ "ColorJitter",+]UnionType = Union[torch.Tensor, Tuple[torch.Tensor, torch.Tensor]]FloatUnionType = Union[torch.Tensor, float, Tuple[float, float], List[float]]@@ -204,3 +213,38 @@ class RandomGrayscale(AugmentationBase):batch_size = self.infer_batch_size(input)params = RandomGrayscale.get_params(batch_size, self.p)return super().forward(input, params)+++class RandomRectangleErasing(nn.Module):+ r"""+ Erases a random selected rectangle for each image in the batch, putting the value to zero.+ The rectangle will have an area equal to the original image area multiplied by a value uniformly+ sampled between the range [erase_scale_range[0], erase_scale_range[1]) and an aspect ratio sampled+ between [aspect_ratio_range[0], aspect_ratio_range[1])++ Args:+ erase_scale_range (Tuple[float, float]): range of proportion of erased area against input image.+ aspect_ratio_range (Tuple[float, float]): range of aspect ratio of erased area.++ Examples:+ >>> inputs = torch.ones(1, 1, 3, 3)+ >>> rec_er = kornia.augmentation.RandomRectangleErasing((.4, .8), (.3, 1/.3))+ >>> rec_er(inputs)+ tensor([[[[1., 0., 0.],+ [1., 0., 0.],+ [1., 0., 0.]]]])+ """++ def __init__(+ self, erase_scale_range: Tuple[float, float], aspect_ratio_range: Tuple[float, float]+ ) -> None:+ super(RandomRectangleErasing, self).__init__()+ self.erase_scale_range: Tuple[float, float] = erase_scale_range+ self.aspect_ratio_range: Tuple[float, float] = aspect_ratio_range++ def forward(self, images: torch.Tensor) -> torch.Tensor: # type: ignore+ return F.random_rectangle_erase(+ images,+ self.erase_scale_range,+ self.aspect_ratio_range+ )diff --git a/kornia/augmentation/random_erasing.py b/kornia/augmentation/erasing.pysimilarity index 98%rename from kornia/augmentation/random_erasing.pyrename to kornia/augmentation/erasing.pyindex 2a2d6e83e..4ed622171 100644--- a/kornia/augmentation/random_erasing.py+++ b/kornia/augmentation/erasing.py@@ -5,7 +5,7 @@ import torch.nn as nnimport torch.distributions as tdist-class RandomRectangleErasing(nn.Module):+'''class RandomRectangleErasing(nn.Module):r"""Erases a random selected rectangle for each image in the batch, putting the value to zero.The rectangle will have an area equal to the original image area multiplied by a value uniformly@@ -37,7 +37,7 @@ class RandomRectangleErasing(nn.Module):images,self.erase_scale_range,self.aspect_ratio_range- )+ )'''def get_random_rectangles_params(@@ -108,7 +108,7 @@ def erase_rectangles(images, rectangle_params):return images * mask-def random_rectangle_erase(+'''def random_rectangle_erase(images: torch.Tensor,erase_scale_range: Tuple[float, float],aspect_ratio_range: Tuple[float, float]@@ -144,4 +144,4 @@ def random_rectangle_erase((b, ), h, w, erase_scale_range, aspect_ratio_range)images = erase_rectangles(images, rect_params)- return images+ return images'''diff --git a/kornia/augmentation/functional.py b/kornia/augmentation/functional.pyindex 4dca901a5..c5edf1ec3 100644--- a/kornia/augmentation/functional.py+++ b/kornia/augmentation/functional.py@@ -3,11 +3,12 @@ from typing import Tuple, List, Union, Dictimport torchimport torch.nn as nn-import kornia.augmentation.param_gen as pgfrom kornia.geometry.transform.flips import hflip, vflipfrom kornia.color.adjust import AdjustBrightness, AdjustContrast, AdjustSaturation, AdjustHuefrom kornia.color.gray import rgb_to_grayscale-from kornia.augmentation.random_erasing import random_rectangle_erase++from . import param_gen as pg+from .erasing import erase_rectangles, get_random_rectangles_paramsUnionType = Union[torch.Tensor, Tuple[torch.Tensor, torch.Tensor]]@@ -290,3 +291,42 @@ def apply_grayscale(input: torch.Tensor, params: Dict[str, torch.Tensor], returnreturn grayscale, identityreturn grayscale+++def random_rectangle_erase(+ images: torch.Tensor,+ erase_scale_range: Tuple[float, float],+ aspect_ratio_range: Tuple[float, float]+) -> torch.Tensor:+ r"""+ Function that erases a random selected rectangle for each image in the batch, putting+ the value to zero.+ The rectangle will have an area equal to the original image area multiplied by a value uniformly+ sampled between the range [erase_scale_range[0], erase_scale_range[1]) and an aspect ratio sampled+ between [aspect_ratio_range[0], aspect_ratio_range[1])++ Args:+ images (torch.Tensor): input images.+ erase_scale_range (Tuple[float, float]): range of proportion of erased area against input image.+ aspect_ratio_range (Tuple[float, float]): range of aspect ratio of erased area.+ """+ if not (isinstance(erase_scale_range[0], float) and+ isinstance(erase_scale_range[1], float) and+ erase_scale_range[0] > 0. and erase_scale_range[1] > 0.):+ raise TypeError(+ f"'erase_scale_range' must be a Tuple[float, float] with positive values"+ )+ if not (isinstance(aspect_ratio_range[0], float) and+ isinstance(aspect_ratio_range[1], float) and+ aspect_ratio_range[0] > 0. and aspect_ratio_range[1] > 0.):+ raise TypeError(+ f"'aspect_ratio_range' must be a Tuple[float, float] with positive values"+ )++ images_size = images.size()+ b, _, h, w = images_size+ rect_params = get_random_rectangles_params(+ (b, ), h, w, erase_scale_range, aspect_ratio_range+ )+ images = erase_rectangles(images, rect_params)+ return imagesdiff --git a/test/augmentation/test_augmentation.py b/test/augmentation/test_augmentation.pyindex 722530fc5..07bf2b75f 100644--- a/test/augmentation/test_augmentation.py+++ b/test/augmentation/test_augmentation.py@@ -9,7 +9,7 @@ import korniaimport kornia.testing as utils # test utilsfrom kornia.augmentation import RandomHorizontalFlip, RandomVerticalFlip, ColorJitter, \RandomRectangleErasing, RandomGrayscale-from kornia.augmentation.random_erasing import get_random_rectangles_params, erase_rectangles+from kornia.augmentation.erasing import get_random_rectangles_params, erase_rectanglesfrom test.common import devicediff --git a/test/augmentation/test_functional.py b/test/augmentation/test_functional.pyindex 969243f99..31f793fa4 100644--- a/test/augmentation/test_functional.py+++ b/test/augmentation/test_functional.py@@ -8,7 +8,7 @@ from torch.autograd import gradcheckimport korniaimport kornia.testing as utils # test utilsimport kornia.augmentation.functional as F-from kornia.augmentation.augmentations import ColorJitter+from kornia.augmentation import ColorJitterfrom test.common import device