module.py 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  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. from typing import Any, Optional, Union
  19. from kornia.core.external import onnx
  20. from kornia.core.external import onnxruntime as ort
  21. from kornia.core.mixin.onnx import ONNXMixin, ONNXRuntimeMixin
  22. __all__ = ["ONNXModule", "load"]
  23. class ONNXModule(ONNXMixin, ONNXRuntimeMixin):
  24. """ONNXModule to wrap an ONNX operator.
  25. Args:
  26. arg: A variable number of ONNX models (either ONNX ModelProto objects or file paths).
  27. For Hugging Face-hosted models, use the format 'hf://model_name'. Valid `model_name` can be found on
  28. https://huggingface.co/kornia/ONNX_models. Or a URL to the ONNX model.
  29. providers: A list of execution providers for ONNXRuntime
  30. (e.g., ['CUDAExecutionProvider', 'CPUExecutionProvider']).
  31. session_options: Optional ONNXRuntime session options for optimizing the session.
  32. cache_dir: The directory where ONNX models are cached locally (only for downloading from HuggingFace).
  33. Defaults to None, which will use a default `kornia.config.hub_onnx_dir` directory.
  34. target_ir_version: The target IR version to convert to.
  35. target_opset_version: The target OPSET version to convert to.
  36. """
  37. def __init__(
  38. self,
  39. op: Union[onnx.ModelProto, str], # type:ignore
  40. providers: Optional[list[str]] = None,
  41. session_options: Optional[ort.SessionOptions] = None, # type:ignore
  42. cache_dir: Optional[str] = None,
  43. target_ir_version: Optional[int] = None,
  44. target_opset_version: Optional[int] = None,
  45. ) -> None:
  46. self.op = self._load_op(op, cache_dir)
  47. if target_ir_version is not None or target_opset_version is not None:
  48. self.op = self._onnx_version_conversion(
  49. self.op, target_ir_version=target_ir_version, target_opset_version=target_opset_version
  50. )
  51. session = self.create_session(providers=providers, session_options=session_options)
  52. self.set_session(session=session)
  53. def create_session(
  54. self, providers: list[str] | None = None, session_options: Any | None = None
  55. ) -> ort.InferenceSession: # type: ignore
  56. return super()._create_session(self.op, providers, session_options)
  57. def export(self, file_path: str, **kwargs: Any) -> None:
  58. return super()._export(self.op, file_path, **kwargs)
  59. def add_metadata(self, additional_metadata: Optional[list[tuple[str, str]]] = None) -> onnx.ModelProto: # type:ignore
  60. return super()._add_metadata(self.op, additional_metadata)
  61. def load(model_name: Union[onnx.ModelProto, str]) -> ONNXModule: # type:ignore
  62. """Load an ONNX model from either a file path or HuggingFace.
  63. The loaded model is an ONNXModule object, of which you may run the model with
  64. the `__call__` method, with less boilerplate.
  65. Args:
  66. model_name: The name of the model to load. For Hugging Face-hosted models,
  67. use the format 'hf://model_name'. Valid `model_name` can be found on
  68. https://huggingface.co/kornia/ONNX_models. Or a URL to the ONNX model.
  69. """
  70. return ONNXModule(model_name)