Skip to content

🐍 Geometric Computer Vision Library for Spatial AI

lint for gaussian blur

edgarriba8b3a951855ca
diff --git a/.travis.yml b/.travis.yml
index b1892c0f5..fbbb5b9b0 100644
--- a/.travis.yml
+++ b/.travis.yml
@@ -6,10 +6,10 @@ notifications:
matrix:
include:
- env: LINT_CHECK
- python: "2.7"
+ python: "3.6"
install: pip install flake8
script: flake8
- - python: "3.5"
+ - python: "3.6"
install:
- sudo apt-get update
diff --git a/test/test_image.py b/test/test_image.py
index fa47ce4a0..9e4ae608d 100644
--- a/test/test_image.py
+++ b/test/test_image.py
@@ -7,6 +7,7 @@ from torch.autograd import gradcheck
import utils
from common import TEST_DEVICES
+
@pytest.mark.parametrize("window_size", [5, 11, 15])
@pytest.mark.parametrize("sigma", [1.5, 5.0, 21.0])
def test_get_gaussian_kernel(window_size, sigma):
@@ -14,6 +15,7 @@ def test_get_gaussian_kernel(window_size, sigma):
assert kernel.shape == (window_size,)
assert kernel.sum().item() == pytest.approx(1.0)
+
@pytest.mark.parametrize("ksize_x", [5, 11])
@pytest.mark.parametrize("ksize_y", [3, 7])
@pytest.mark.parametrize("sigma", [1.5, 21.0])
@@ -23,12 +25,13 @@ def test_get_gaussian_kernel2d(ksize_x, ksize_y, sigma):
assert kernel.shape == (ksize_x, ksize_y)
assert kernel.sum().item() == pytest.approx(1.0)
+
@pytest.mark.parametrize("ksize_x", [5, 11])
@pytest.mark.parametrize("ksize_y", [3, 7])
@pytest.mark.parametrize("sigma", [1.5, 21.0])
@pytest.mark.parametrize("device_type", TEST_DEVICES)
@pytest.mark.parametrize("batch_shape",
- [(1, 1, 10, 16), (1, 4, 8, 15), (2, 3, 11, 7)])
+ [(1, 1, 10, 16), (1, 4, 8, 15), (2, 3, 11, 7)])
def test_gaussian_blur(batch_shape, device_type, ksize_x, ksize_y, sigma):
kernel_size = (ksize_x, ksize_y)
sigma = (sigma, sigma)
diff --git a/torchgeometry/conversions.py b/torchgeometry/conversions.py
index 11d9bd89a..25ad1287e 100644
--- a/torchgeometry/conversions.py
+++ b/torchgeometry/conversions.py
@@ -361,8 +361,8 @@ def rotation_matrix_to_quaternion(rotation_matrix, eps=1e-6):
mask_c3 = mask_c3.view(-1, 1).type_as(q3)
q = q0 * mask_c0 + q1 * mask_c1 + q2 * mask_c2 + q3 * mask_c3
- q /= torch.sqrt(t0_rep * mask_c0 + t1_rep * mask_c1 + # noqa
- t2_rep * mask_c2 + t3_rep * mask_c3)
+ q /= torch.sqrt(t0_rep * mask_c0 + t1_rep * mask_c1 # noqa
+ + t2_rep * mask_c2 + t3_rep * mask_c3) # noqa
q *= 0.5
if len(input_shape) == 2:
@@ -456,6 +456,7 @@ class RadToDeg(nn.Module):
>>> input = tgm.pi * torch.rand(1, 3, 3)
>>> output = tgm.RadToDeg()(input)
"""
+
def __init__(self):
super(RadToDeg, self).__init__()
@@ -477,6 +478,7 @@ class DegToRad(nn.Module):
>>> input = 360. * torch.rand(1, 3, 3)
>>> output = tgm.DegToRad()(input)
"""
+
def __init__(self):
super(DegToRad, self).__init__()
@@ -504,6 +506,7 @@ class ConvertPointsFromHomogeneous(nn.Module):
>>> transform = tgm.ConvertPointsFromHomogeneous()
>>> output = transform(input) # BxNx2
"""
+
def __init__(self):
super(ConvertPointsFromHomogeneous, self).__init__()
@@ -531,6 +534,7 @@ class ConvertPointsToHomogeneous(nn.Module):
>>> transform = tgm.ConvertPointsToHomogeneous()
>>> output = transform(input) # BxNx4
"""
+
def __init__(self):
super(ConvertPointsToHomogeneous, self).__init__()
@@ -559,6 +563,7 @@ class TransformPoints(nn.Module):
>>> transform_op = tgm.TransformPoints(transform)
>>> output = transform_op(input) # BxNx3
"""
+
def __init__(self, dst_homo_src):
super(TransformPoints, self).__init__()
self.dst_homo_src = dst_homo_src
diff --git a/torchgeometry/image/gaussian.py b/torchgeometry/image/gaussian.py
index f627831b6..f0cf87430 100644
--- a/torchgeometry/image/gaussian.py
+++ b/torchgeometry/image/gaussian.py
@@ -4,6 +4,7 @@ import torch
import torch.nn as nn
from torch.nn.functional import conv2d
+
def gaussian(window_size, sigma):
def gauss_fcn(x):
return -(x - window_size // 2)**2 / float(2 * sigma**2)
@@ -11,6 +12,7 @@ def gaussian(window_size, sigma):
[torch.exp(torch.tensor(gauss_fcn(x))) for x in range(window_size)])
return gauss / gauss.sum()
+
def get_gaussian_kernel(ksize: int, sigma: float) -> torch.Tensor:
r"""Function that returns Gaussian filter coefficients.
@@ -34,10 +36,11 @@ def get_gaussian_kernel(ksize: int, sigma: float) -> torch.Tensor:
"""
if not isinstance(ksize, int) or ksize % 2 == 0 or ksize <= 0:
raise TypeError("ksize must be an odd positive integer. Got {}"
- .format(ksize))
+ .format(ksize))
window_1d: torch.Tensor = gaussian(ksize, sigma)
return window_1d
+
def get_gaussian_kernel2d(ksize: Tuple[int, int],
sigma: Tuple[float, float]) -> torch.Tensor:
r"""Function that returns Gaussian filter matrix coefficients.
@@ -68,10 +71,10 @@ def get_gaussian_kernel2d(ksize: Tuple[int, int],
"""
if not isinstance(ksize, tuple) or len(ksize) != 2:
raise TypeError("ksize must be a tuple of length two. Got {}"
- .format(ksize))
+ .format(ksize))
if not isinstance(sigma, tuple) or len(sigma) != 2:
raise TypeError("sigma must be a tuple of length two. Got {}"
- .format(sigma))
+ .format(sigma))
ksize_x, ksize_y = ksize
sigma_x, sigma_y = sigma
kernel_x: torch.Tensor = get_gaussian_kernel(ksize_x, sigma_x)
@@ -80,9 +83,10 @@ def get_gaussian_kernel2d(ksize: Tuple[int, int],
kernel_x.unsqueeze(-1), kernel_y.unsqueeze(-1).t())
return kernel_2d
-class GaussianBlur(nn.Module):
+
+class GaussianBlur(nn.Module):
r"""Creates an operator that blurs a tensor using a Gaussian filter.
-
+
The operator smooths the given tensor with a gaussian kernel by convolving
it to each channel. It suports batched operation.
@@ -103,12 +107,15 @@ class GaussianBlur(nn.Module):
>>> gauss = tgm.image.GaussianBlur((3, 3), (1.5, 1.5))
>>> output = gauss(input) # 2x4x5x5
"""
- def __init__(self, kernel_size: Tuple[int, int], sigma: Tuple[float, float]) -> None:
+
+ def __init__(self, kernel_size: Tuple[int, int],
+ sigma: Tuple[float, float]) -> None:
super(GaussianBlur, self).__init__()
self.kernel_size: Tuple[int, int] = kernel_size
self.sigma: Tuple[float, float] = sigma
self._padding: Tuple[int, int] = self.compute_zero_padding(kernel_size)
- self.kernel: torch.Tensor = self.create_gaussian_kernel(kernel_size, sigma)
+ self.kernel: torch.Tensor = self.create_gaussian_kernel(
+ kernel_size, sigma)
@staticmethod
def create_gaussian_kernel(kernel_size, sigma) -> torch.Tensor:
@@ -142,9 +149,13 @@ class GaussianBlur(nn.Module):
######################
-def gaussian_blur(src: torch.Tensor, kernel_size: Tuple[int, int], sigma: Tuple[float, float]) -> torch.Tensor:
+def gaussian_blur(src: torch.Tensor,
+ kernel_size: Tuple[int,
+ int],
+ sigma: Tuple[float,
+ float]) -> torch.Tensor:
r"""Function that blurs a tensor using a Gaussian filter.
-
+
The operator smooths the given tensor with a gaussian kernel by convolving
it to each channel. It suports batched operation.
@@ -163,6 +174,6 @@ def gaussian_blur(src: torch.Tensor, kernel_size: Tuple[int, int], sigma: Tuple[
Examples::
>>> input = torch.rand(2, 4, 5, 5)
- >>> output = tgm.image.gaussian_blur(input, (3, 3), (1.5, 1.5)) # 2x4x5x5
+ >>> output = tgm.image.gaussian_blur(input, (3, 3), (1.5, 1.5))
"""
return GaussianBlur(kernel_size, sigma)(src)
diff --git a/torchgeometry/pinhole.py b/torchgeometry/pinhole.py
index 519e08b98..4526bca98 100644
--- a/torchgeometry/pinhole.py
+++ b/torchgeometry/pinhole.py
@@ -213,6 +213,7 @@ class InversePose(nn.Module):
>>> transform = tgm.InversePose()
>>> pose_inv = transform(pose) # Nx4x4
"""
+
def __init__(self):
super(InversePose, self).__init__()
@@ -238,6 +239,7 @@ class PinholeMatrix(nn.Module):
>>> transform = tgm.PinholeMatrix()
>>> pinhole_matrix = transform(pinhole) # Nx4x4
"""
+
def __init__(self):
super(PinholeMatrix, self).__init__()
@@ -263,6 +265,7 @@ class InversePinholeMatrix(nn.Module):
>>> transform = tgm.InversePinholeMatrix()
>>> pinhole_matrix_inv = transform(pinhole) # Nx4x4
"""
+
def __init__(self):
super(InversePinholeMatrix, self).__init__()