download.py 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  1. # LICENSE HEADER MANAGED BY add-license-header
  2. #
  3. # Copyright 2018 Kornia Team
  4. #
  5. # Licensed under the Apache License, Version 2.0 (the "License");
  6. # you may not use this file except in compliance with the License.
  7. # You may obtain a copy of the License at
  8. #
  9. # http://www.apache.org/licenses/LICENSE-2.0
  10. #
  11. # Unless required by applicable law or agreed to in writing, software
  12. # distributed under the License is distributed on an "AS IS" BASIS,
  13. # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  14. # See the License for the specific language governing permissions and
  15. # limitations under the License.
  16. #
  17. from __future__ import annotations
  18. import logging
  19. import os
  20. import urllib.request
  21. from typing import Any, Optional
  22. from kornia.config import kornia_config
  23. __all__ = ["CachedDownloader"]
  24. logger = logging.getLogger(__name__)
  25. class CachedDownloader:
  26. """Downloads files from URLs to the local cache or .kornia_hub directory."""
  27. @classmethod
  28. def _get_file_path(cls, model_name: str, cache_dir: Optional[str], suffix: Optional[str] = None) -> str:
  29. """Construct the file path for the ONNX model based on the model name and cache directory.
  30. Args:
  31. model_name: The name of the model or operator, typically in the format 'operators/model_name'.
  32. cache_dir: The directory where the model should be cached.
  33. Defaults to None, which will use a default `kornia.config.hub_onnx_dir` directory.
  34. suffix: Optional file suffix when the filename is the model name.
  35. Returns:
  36. str: The full local path where the model should be stored or loaded from.
  37. """
  38. # Determine the local file path
  39. if cache_dir is None:
  40. cache_dir = kornia_config.hub_cache_dir
  41. # The filename is the model name (without directory path)
  42. if suffix is not None and not model_name.endswith(suffix):
  43. file_name = f"{os.path.split(model_name)[-1]}{suffix}"
  44. else:
  45. file_name = os.path.split(model_name)[-1]
  46. file_path = os.path.join(*cache_dir.split(os.sep), *model_name.split(os.sep)[:-1], file_name)
  47. return file_path
  48. @classmethod
  49. def download_to_cache(cls, url: str, name: str, download: bool = True, **kwargs: Any) -> str:
  50. if url.startswith(("http:", "https:")):
  51. cache_dir = kwargs.get("cache_dir", None)
  52. suffix = kwargs.get("suffix", None)
  53. file_path = cls._get_file_path(name, cache_dir, suffix=suffix)
  54. cls.download(url, file_path, download_if_not_exists=download)
  55. return file_path
  56. raise ValueError(f"URL must start with 'http:' or 'https:'. Got {url}")
  57. @classmethod
  58. def download(
  59. cls,
  60. url: str,
  61. file_path: str,
  62. download_if_not_exists: bool = True,
  63. ) -> None:
  64. """Download an ONNX model from the specified URL and save it to the specified file path.
  65. Args:
  66. url: The URL of the ONNX model to download.
  67. file_path: The local path where the downloaded model should be saved.
  68. download_if_not_exists: If True, the file will be downloaded if it's not already downloaded.
  69. """
  70. if os.path.exists(file_path):
  71. logger.info(f"Loading `{url}` from `{file_path}`.")
  72. return
  73. if not download_if_not_exists:
  74. raise ValueError(f"`{file_path}` not found. You may set `download=True`.")
  75. os.makedirs(os.path.dirname(file_path), exist_ok=True) # Create the cache directory if it doesn't exist
  76. if url.startswith(("http:", "https:")):
  77. try:
  78. logger.info(f"Downloading `{url}` to `{file_path}`.")
  79. urllib.request.urlretrieve(url, file_path) # noqa: S310
  80. except urllib.error.HTTPError as e:
  81. raise ValueError(f"Error in resolving `{url}`.") from e
  82. else:
  83. raise ValueError("URL must start with 'http:' or 'https:'")