π Geometric Computer Vision Library for Spatial AI
Python artificial-intelligencecomputer-visiondeep-learninghacktoberfestimage-processingmachine-learningneural-networkpythonpytorchroboticsspatial-ai
add get_safe_device util (#1662)
Dmytro Mishkin377b3bf2f798
diff --git a/docs/source/utils.rst b/docs/source/utils.rstindex 95ac5d742..2a8664bfb 100644--- a/docs/source/utils.rst+++ b/docs/source/utils.rst@@ -34,3 +34,8 @@ Memory.. autofunction:: one_hot.. autofunction:: batched_forward++Device+-------++.. autofunction:: get_cuda_device_if_availablediff --git a/kornia/utils/__init__.py b/kornia/utils/__init__.pyindex 99651566e..caaefa060 100644--- a/kornia/utils/__init__.py+++ b/kornia/utils/__init__.py@@ -1,6 +1,6 @@from .draw import draw_convex_polygon, draw_line, draw_rectanglefrom .grid import create_meshgrid, create_meshgrid3d-from .helpers import _extract_device_dtype, safe_inverse_with_mask, safe_solve_with_mask+from .helpers import _extract_device_dtype, get_cuda_device_if_available, safe_inverse_with_mask, safe_solve_with_maskfrom .image import ImageToTensor, image_list_to_tensor, image_to_tensor, tensor_to_imagefrom .memory import batched_forwardfrom .misc import eye_like, vec_like@@ -12,6 +12,7 @@ __all__ = ["one_hot","create_meshgrid","create_meshgrid3d",+ "get_cuda_device_if_available","tensor_to_image","image_to_tensor","image_list_to_tensor",diff --git a/kornia/utils/helpers.py b/kornia/utils/helpers.pyindex e4596b8df..812a580a7 100644--- a/kornia/utils/helpers.py+++ b/kornia/utils/helpers.py@@ -8,6 +8,25 @@ import torchfrom kornia.utils._compat import solve, torch_version_geq+def get_cuda_device_if_available(index: int = 0) -> torch.device:+ """Tries to get cuda device, if fail, returns cpu.++ Args:+ index: cuda device index++ Returns:+ torch.device+ """+ try:+ if torch.cuda.is_available():+ dev = torch.device(f'cuda:{index}')+ else:+ dev = torch.device('cpu')+ except BaseException as e: # noqa: F841+ dev = torch.device('cpu')+ return dev++def _deprecated(func: Callable = None, replace_with: Optional[str] = None):if func is None:return partial(_deprecated, replace_with=replace_with)