π Geometric Computer Vision Library for Spatial AI
Python artificial-intelligencecomputer-visiondeep-learninghacktoberfestimage-processingmachine-learningneural-networkpythonpytorchroboticsspatial-ai
add missing type checking in pyrup and pyrdown
edgarriba594dfb3ce602
diff --git a/torchgeometry/image/pyramid.py b/torchgeometry/image/pyramid.pyindex 1b09d82c3..d1ba17b79 100644--- a/torchgeometry/image/pyramid.py+++ b/torchgeometry/image/pyramid.py@@ -25,8 +25,8 @@ class PyrDown(nn.Module):r"""Blurs a tensor and downsamples it.Args:- x (torch.Tensor): the tensor to be downsampled. The tensor must be in- the shape of BxCxHxW.+ input (torch.Tensor): the tensor to be downsampled. The tensor must be+ in the shape of BxCxHxW.Return:torch.Tensor: the downsampled tensor.@@ -40,15 +40,21 @@ class PyrDown(nn.Module):super(PyrDown, self).__init__()self.kernel: torch.Tensor = _get_pyramid_gaussian_kernel()- def forward(self, x: torch.Tensor) -> torch.Tensor: # type: ignore+ def forward(self, input: torch.Tensor) -> torch.Tensor: # type: ignore+ if not torch.is_tensor(input):+ raise TypeError("Input type is not a torch.Tensor. Got {}"+ .format(type(input)))+ if not len(input.shape) == 4:+ raise ValueError("Invalid input shape, we expect BxCxHxW. Got: {}"+ .format(input.shape))# prepare kernel- b, c, h, w = x.shape- tmp_kernel: torch.Tensor = self.kernel.to(x.device).to(x.dtype)+ b, c, h, w = input.shape+ tmp_kernel: torch.Tensor = self.kernel.to(input.device).to(input.dtype)kernel: torch.Tensor = tmp_kernel.repeat(c, 1, 1, 1)# blur imagex_blur: torch.Tensor = F.conv2d(- x, kernel, padding=2, stride=1, groups=c)+ input, kernel, padding=2, stride=1, groups=c)# reject even rows and columns.out: torch.Tensor = x_blur[..., ::2, ::2]@@ -59,7 +65,7 @@ class PyrUp(nn.Module):r"""Upsamples a tensor and then blurs it.Args:- x (torch.Tensor): the tensor to be upsampled. The tensor must be in+ input (torch.Tensor): the tensor to be upsampled. The tensor must be inthe shape of BxCxHxW.Return:@@ -74,14 +80,20 @@ class PyrUp(nn.Module):super(PyrUp, self).__init__()self.kernel: torch.Tensor = _get_pyramid_gaussian_kernel()- def forward(self, x: torch.Tensor) -> torch.Tensor: # type: ignore+ def forward(self, input: torch.Tensor) -> torch.Tensor: # type: ignore+ if not torch.is_tensor(input):+ raise TypeError("Input type is not a torch.Tensor. Got {}"+ .format(type(input)))+ if not len(input.shape) == 4:+ raise ValueError("Invalid input shape, we expect BxCxHxW. Got: {}"+ .format(input.shape))# prepare kernel- b, c, height, width = x.shape- tmp_kernel: torch.Tensor = self.kernel.to(x.device).to(x.dtype)+ b, c, height, width = input.shape+ tmp_kernel: torch.Tensor = self.kernel.to(input.device).to(input.dtype)kernel: torch.Tensor = tmp_kernel.repeat(c, 1, 1, 1)# upsample tensor- x_up: torch.Tensor = F.interpolate(x, size=(height * 2, width * 2),+ x_up: torch.Tensor = F.interpolate(input, size=(height * 2, width * 2),mode='bilinear', align_corners=True)# blurs upsampled tensor@@ -93,17 +105,17 @@ class PyrUp(nn.Module):# functiona api-def pyrdown(x: torch.Tensor) -> torch.Tensor:+def pyrdown(input: torch.Tensor) -> torch.Tensor:r"""Blurs a tensor and downsamples it.See :class:`~torchgeometry.image.PyrDown` for details."""- return PyrDown()(x)+ return PyrDown()(input)-def pyrup(x: torch.Tensor) -> torch.Tensor:+def pyrup(input: torch.Tensor) -> torch.Tensor:r"""Upsamples a tensor and then blurs it.See :class:`~torchgeometry.image.PyrUp` for details."""- return PyrUp()(x)+ return PyrUp()(input)