Skip to content

🐍 Geometric Computer Vision Library for Spatial AI

Fixed tests and docs (#654)

shijianjian75778bf9428d
diff --git a/kornia/enhance/adjust.py b/kornia/enhance/adjust.py
index 8be6d56f7..938542f70 100644
--- a/kornia/enhance/adjust.py
+++ b/kornia/enhance/adjust.py
@@ -230,11 +230,13 @@ def adjust_brightness(input: torch.Tensor,
def _solarize(input: torch.Tensor, thresholds: Union[float, torch.Tensor] = 0.5) -> torch.Tensor:
r""" For each pixel in the image, select the pixel if the value is less than the threshold.
Otherwise, subtract 1.0 from the pixel.
+
Args:
input (torch.Tensor): image or batched images to solarize.
thresholds (float or torch.Tensor): solarize thresholds.
If int or one element tensor, input will be solarized across the whole batch.
If 1-d tensor, input will be solarized element-wise, len(thresholds) == len(input).
+
Returns:
torch.Tensor: Solarized images.
"""
@@ -257,8 +259,9 @@ def _solarize(input: torch.Tensor, thresholds: Union[float, torch.Tensor] = 0.5)
def solarize(input: torch.Tensor, thresholds: Union[float, torch.Tensor] = 0.5,
additions: Optional[Union[float, torch.Tensor]] = None) -> torch.Tensor:
- r""" For each pixel in the image less than threshold, we add 'addition' amount to it and then clip the
+ r"""For each pixel in the image less than threshold, we add 'addition' amount to it and then clip the
pixel value to be between 0 and 1.0. The value of 'addition' is between -0.5 and 0.5.
+
Args:
input (torch.Tensor): image tensor with shapes like (C, H, W) or (B, C, H, W) to solarize.
thresholds (float or torch.Tensor): solarize thresholds.
@@ -268,6 +271,7 @@ def solarize(input: torch.Tensor, thresholds: Union[float, torch.Tensor] = 0.5,
If None, no addition will be performed.
If int or one element tensor, same addition will be added across the whole batch.
If 1-d tensor, additions will be added element-wisely, len(additions) == len(input).
+
Returns:
torch.Tensor: Solarized images.
"""
@@ -306,12 +310,14 @@ def solarize(input: torch.Tensor, thresholds: Union[float, torch.Tensor] = 0.5,
def posterize(input: torch.Tensor, bits: Union[int, torch.Tensor]) -> torch.Tensor:
r"""Reduce the number of bits for each color channel. Non-differentiable function, uint8 involved.
+
Args:
input (torch.Tensor): image tensor with shapes like (C, H, W) or (B, C, H, W) to posterize.
bits (int or torch.Tensor): number of high bits. Must be in range [0, 8].
If int or one element tensor, input will be posterized by this bits.
If 1-d tensor, input will be posterized element-wisely, len(bits) == input.shape[1].
If n-d tensor, input will be posterized element-channel-wisely, bits.shape == input.shape[:len(bits.shape)]
+
Returns:
torch.Tensor: Image with reduced color channels.
"""
@@ -370,11 +376,13 @@ def posterize(input: torch.Tensor, bits: Union[int, torch.Tensor]) -> torch.Tens
def sharpness(input: torch.Tensor, factor: Union[float, torch.Tensor]) -> torch.Tensor:
r"""Implements Sharpness function from PIL using torch ops.
+
Args:
input (torch.Tensor): image tensor with shapes like (C, H, W) or (B, C, H, W) to sharpen.
factor (float or torch.Tensor): factor of sharpness strength. Must be above 0.
If float or one element tensor, input will be sharpened by the same factor across the whole batch.
If 1-d tensor, input will be sharpened element-wisely, len(factor) == len(input).
+
Returns:
torch.Tensor: Sharpened image or images.
"""
@@ -421,10 +429,14 @@ def sharpness(input: torch.Tensor, factor: Union[float, torch.Tensor]) -> torch.
def equalize(input: torch.Tensor) -> torch.Tensor:
- """Implements Equalize function from PIL using PyTorch ops based on uint8 format:
+ """ Apply equalize on the input tensor.
+
+ Implements Equalize function from PIL using PyTorch ops based on uint8 format:
https://github.com/tensorflow/tpu/blob/master/models/official/efficientnet/autoaugment.py#L352
+
Args:
input (torch.Tensor): image tensor with shapes like (C, H, W) or (B, C, H, W) to equalize.
+
Returns:
torch.Tensor: Sharpened image or images.
"""
diff --git a/kornia/geometry/transform/affwarp.py b/kornia/geometry/transform/affwarp.py
index 4768de47b..4059564d9 100644
--- a/kornia/geometry/transform/affwarp.py
+++ b/kornia/geometry/transform/affwarp.py
@@ -472,7 +472,7 @@ class Affine(nn.Module):
self.translation = translation
if scale_factor is None:
- scale_factor = torch.ones(batch_size)
+ scale_factor = torch.ones(batch_size, 2)
self.scale_factor = scale_factor
self.shear = shear
diff --git a/kornia/geometry/transform/imgwarp.py b/kornia/geometry/transform/imgwarp.py
index e65dc5633..804871562 100644
--- a/kornia/geometry/transform/imgwarp.py
+++ b/kornia/geometry/transform/imgwarp.py
@@ -23,7 +23,9 @@ __all__ = [
"invert_affine_transform",
"angle_to_rotation_matrix",
"get_affine_matrix2d",
- "get_affine_matrix3d"
+ "get_affine_matrix3d",
+ "get_shear_matrix2d",
+ "get_shear_matrix3d"
]
@@ -486,17 +488,16 @@ def get_affine_matrix2d(translations: torch.Tensor, center: torch.Tensor, scale:
def get_shear_matrix2d(center: torch.Tensor, sx: Optional[torch.Tensor] = None, sy: Optional[torch.Tensor] = None):
r"""Composes shear matrix Bx4x4 from the components.
- Note: Ordered shearing, shear x-axis then y-axis
+
+ Note: Ordered shearing, shear x-axis then y-axis.
.. math::
\begin{bmatrix}
1 & b \\
a & ab + 1 \\
\end{bmatrix}
- Returns:
- torch.Tensor: params to be passed to the affine transformation.
- Params:
+ Args:
center (torch.Tensor): shearing center coordinates of (x, y).
sx (torch.Tensor, optional): shearing degree along x axis.
sy (torch.Tensor, optional): shearing degree along y axis.
diff --git a/test/geometry/transform/test_affine.py b/test/geometry/transform/test_affine.py
index a82fa3566..4dd0237d2 100644
--- a/test/geometry/transform/test_affine.py
+++ b/test/geometry/transform/test_affine.py
@@ -491,7 +491,8 @@ class TestAffine2d:
def test_affine_scale(self, device):
torch.manual_seed(0)
- scale_factor = torch.rand(1, device=device) * 2.0
+ _scale_factor = torch.rand(1, device=device) * 2.0
+ scale_factor = torch.stack([_scale_factor, _scale_factor], dim=1)
input = torch.rand(1, 2, 3, 4, device=device)
transform = kornia.Affine(scale_factor=scale_factor).to(device)
diff --git a/tutorials/warp_affine.py b/tutorials/warp_affine.py
index 9d7ed643c..7dd161ba2 100644
--- a/tutorials/warp_affine.py
+++ b/tutorials/warp_affine.py
@@ -29,7 +29,7 @@ center[..., 0] = data.shape[3] / 2 # x
center[..., 1] = data.shape[2] / 2 # y
# define the scale factor
-scale: torch.tensor = torch.ones(1)
+scale: torch.tensor = torch.ones(1, 2)
# compute the transformation matrix
M: torch.tensor = kornia.get_rotation_matrix2d(center, angle, scale)