Skip to content

🐍 Geometric Computer Vision Library for Spatial AI

Merge pull request #1136 from kornia/allow_new_numpt_versions

lferraz3948078e4d6f
diff --cc kornia/utils/image.py
index 92da9a2c4,fb92077b1..964c550c7
--- a/kornia/utils/image.py
+++ b/kornia/utils/image.py
@@@ -3,24 -4,23 +4,23 @@@ from typing import TYPE_CHECKIN
import torch
import torch.nn as nn
+ if TYPE_CHECKING:
+ import numpy as np
- def image_to_tensor(image: np.ndarray, keepdim: bool = True) -> torch.Tensor:
- """Converts a numpy image to a PyTorch 4d tensor image.
+
+ def image_to_tensor(image: "np.ndarray", keepdim: bool = True) -> torch.Tensor:
+ """Convert a numpy image to a PyTorch 4d tensor image.
Args:
- image (numpy.ndarray): image of the form :math:`(H, W, C)`, :math:`(H, W)` or
+ image: image of the form :math:`(H, W, C)`, :math:`(H, W)` or
+ :math:`(B, H, W, C)`.
+ keepdim: If ``False`` unsqueeze the input image to match the shape
:math:`(B, H, W, C)`.
- keepdim (bool): If ``False`` unsqueeze the input image to match the shape
- :math:`(B, H, W, C)`. Default: ``True``
Returns:
- torch.Tensor: tensor of the form :math:`(B, C, H, W)` if keepdim is ``False``,
+ tensor of the form :math:`(B, C, H, W)` if keepdim is ``False``,
:math:`(C, H, W)` otherwise.
"""
- if not isinstance(image, (np.ndarray,)):
- raise TypeError("Input type must be a numpy.ndarray. Got {}".format(type(image)))
-
if len(image.shape) > 4 or len(image.shape) < 2:
raise ValueError("Input size must be a two, three or four dimensional array")
@@@ -44,10 -44,10 +44,10 @@@
def _to_bchw(tensor: torch.Tensor) -> torch.Tensor:
- """Converts a PyTorch tensor image to BCHW format.
+ """Convert a PyTorch tensor image to BCHW format.
Args:
- tensor (torch.Tensor): image of the form :math:`(H, W)`, :math:`(C, H, W)`, :math:`(H, W, C)` or
+ tensor: image of the form :math:`(H, W)`, :math:`(C, H, W)`, :math:`(H, W, C)` or
:math:`(B, C, H, W)`.
Returns:
@@@ -69,10 -69,10 +69,10 @@@
def _to_bcdhw(tensor: torch.Tensor) -> torch.Tensor:
- """Converts a PyTorch tensor image to BCDHW format.
+ """Convert a PyTorch tensor image to BCDHW format.
Args:
- tensor (torch.Tensor): image of the form :math:`(D, H, W)`, :math:`(C, D, H, W)`, :math:`(D, H, W, C)` or
+ tensor: image of the form :math:`(D, H, W)`, :math:`(C, D, H, W)`, :math:`(D, H, W, C)` or
:math:`(B, C, D, H, W)`.
Returns: