inference-models 0.18.3__py3-none-any.whl

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (195) hide show
  1. inference_models/__init__.py +36 -0
  2. inference_models/configuration.py +72 -0
  3. inference_models/constants.py +2 -0
  4. inference_models/entities.py +5 -0
  5. inference_models/errors.py +137 -0
  6. inference_models/logger.py +52 -0
  7. inference_models/model_pipelines/__init__.py +0 -0
  8. inference_models/model_pipelines/auto_loaders/__init__.py +0 -0
  9. inference_models/model_pipelines/auto_loaders/core.py +120 -0
  10. inference_models/model_pipelines/auto_loaders/pipelines_registry.py +36 -0
  11. inference_models/model_pipelines/face_and_gaze_detection/__init__.py +0 -0
  12. inference_models/model_pipelines/face_and_gaze_detection/mediapipe_l2cs.py +200 -0
  13. inference_models/models/__init__.py +0 -0
  14. inference_models/models/auto_loaders/__init__.py +0 -0
  15. inference_models/models/auto_loaders/access_manager.py +168 -0
  16. inference_models/models/auto_loaders/auto_negotiation.py +1329 -0
  17. inference_models/models/auto_loaders/auto_resolution_cache.py +129 -0
  18. inference_models/models/auto_loaders/constants.py +7 -0
  19. inference_models/models/auto_loaders/core.py +1341 -0
  20. inference_models/models/auto_loaders/dependency_models.py +52 -0
  21. inference_models/models/auto_loaders/entities.py +57 -0
  22. inference_models/models/auto_loaders/models_registry.py +497 -0
  23. inference_models/models/auto_loaders/presentation_utils.py +333 -0
  24. inference_models/models/auto_loaders/ranking.py +413 -0
  25. inference_models/models/auto_loaders/utils.py +31 -0
  26. inference_models/models/base/__init__.py +0 -0
  27. inference_models/models/base/classification.py +123 -0
  28. inference_models/models/base/depth_estimation.py +62 -0
  29. inference_models/models/base/documents_parsing.py +111 -0
  30. inference_models/models/base/embeddings.py +66 -0
  31. inference_models/models/base/instance_segmentation.py +87 -0
  32. inference_models/models/base/keypoints_detection.py +93 -0
  33. inference_models/models/base/object_detection.py +143 -0
  34. inference_models/models/base/semantic_segmentation.py +74 -0
  35. inference_models/models/base/types.py +5 -0
  36. inference_models/models/clip/__init__.py +0 -0
  37. inference_models/models/clip/clip_onnx.py +148 -0
  38. inference_models/models/clip/clip_pytorch.py +104 -0
  39. inference_models/models/clip/preprocessing.py +162 -0
  40. inference_models/models/common/__init__.py +0 -0
  41. inference_models/models/common/cuda.py +30 -0
  42. inference_models/models/common/model_packages.py +25 -0
  43. inference_models/models/common/onnx.py +379 -0
  44. inference_models/models/common/roboflow/__init__.py +0 -0
  45. inference_models/models/common/roboflow/model_packages.py +361 -0
  46. inference_models/models/common/roboflow/post_processing.py +436 -0
  47. inference_models/models/common/roboflow/pre_processing.py +1332 -0
  48. inference_models/models/common/torch.py +20 -0
  49. inference_models/models/common/trt.py +266 -0
  50. inference_models/models/deep_lab_v3_plus/__init__.py +0 -0
  51. inference_models/models/deep_lab_v3_plus/deep_lab_v3_plus_segmentation_onnx.py +282 -0
  52. inference_models/models/deep_lab_v3_plus/deep_lab_v3_plus_segmentation_torch.py +264 -0
  53. inference_models/models/deep_lab_v3_plus/deep_lab_v3_plus_segmentation_trt.py +313 -0
  54. inference_models/models/depth_anything_v2/__init__.py +0 -0
  55. inference_models/models/depth_anything_v2/depth_anything_v2_hf.py +77 -0
  56. inference_models/models/dinov3/__init__.py +0 -0
  57. inference_models/models/dinov3/dinov3_classification_onnx.py +348 -0
  58. inference_models/models/dinov3/dinov3_classification_torch.py +323 -0
  59. inference_models/models/doctr/__init__.py +0 -0
  60. inference_models/models/doctr/doctr_torch.py +304 -0
  61. inference_models/models/easy_ocr/__init__.py +0 -0
  62. inference_models/models/easy_ocr/easy_ocr_torch.py +222 -0
  63. inference_models/models/florence2/__init__.py +0 -0
  64. inference_models/models/florence2/florence2_hf.py +897 -0
  65. inference_models/models/grounding_dino/__init__.py +0 -0
  66. inference_models/models/grounding_dino/grounding_dino_torch.py +227 -0
  67. inference_models/models/l2cs/__init__.py +0 -0
  68. inference_models/models/l2cs/l2cs_onnx.py +216 -0
  69. inference_models/models/mediapipe_face_detection/__init__.py +0 -0
  70. inference_models/models/mediapipe_face_detection/face_detection.py +203 -0
  71. inference_models/models/moondream2/__init__.py +0 -0
  72. inference_models/models/moondream2/moondream2_hf.py +281 -0
  73. inference_models/models/owlv2/__init__.py +0 -0
  74. inference_models/models/owlv2/cache.py +182 -0
  75. inference_models/models/owlv2/entities.py +112 -0
  76. inference_models/models/owlv2/owlv2_hf.py +695 -0
  77. inference_models/models/owlv2/reference_dataset.py +291 -0
  78. inference_models/models/paligemma/__init__.py +0 -0
  79. inference_models/models/paligemma/paligemma_hf.py +209 -0
  80. inference_models/models/perception_encoder/__init__.py +0 -0
  81. inference_models/models/perception_encoder/perception_encoder_pytorch.py +197 -0
  82. inference_models/models/perception_encoder/vision_encoder/__init__.py +0 -0
  83. inference_models/models/perception_encoder/vision_encoder/config.py +160 -0
  84. inference_models/models/perception_encoder/vision_encoder/pe.py +742 -0
  85. inference_models/models/perception_encoder/vision_encoder/rope.py +344 -0
  86. inference_models/models/perception_encoder/vision_encoder/tokenizer.py +342 -0
  87. inference_models/models/perception_encoder/vision_encoder/transforms.py +33 -0
  88. inference_models/models/qwen25vl/__init__.py +1 -0
  89. inference_models/models/qwen25vl/qwen25vl_hf.py +285 -0
  90. inference_models/models/resnet/__init__.py +0 -0
  91. inference_models/models/resnet/resnet_classification_onnx.py +330 -0
  92. inference_models/models/resnet/resnet_classification_torch.py +305 -0
  93. inference_models/models/resnet/resnet_classification_trt.py +369 -0
  94. inference_models/models/rfdetr/__init__.py +0 -0
  95. inference_models/models/rfdetr/backbone_builder.py +101 -0
  96. inference_models/models/rfdetr/class_remapping.py +41 -0
  97. inference_models/models/rfdetr/common.py +115 -0
  98. inference_models/models/rfdetr/default_labels.py +108 -0
  99. inference_models/models/rfdetr/dinov2_with_windowed_attn.py +1330 -0
  100. inference_models/models/rfdetr/misc.py +26 -0
  101. inference_models/models/rfdetr/ms_deform_attn.py +180 -0
  102. inference_models/models/rfdetr/ms_deform_attn_func.py +60 -0
  103. inference_models/models/rfdetr/position_encoding.py +166 -0
  104. inference_models/models/rfdetr/post_processor.py +83 -0
  105. inference_models/models/rfdetr/projector.py +373 -0
  106. inference_models/models/rfdetr/rfdetr_backbone_pytorch.py +394 -0
  107. inference_models/models/rfdetr/rfdetr_base_pytorch.py +807 -0
  108. inference_models/models/rfdetr/rfdetr_instance_segmentation_onnx.py +206 -0
  109. inference_models/models/rfdetr/rfdetr_instance_segmentation_pytorch.py +373 -0
  110. inference_models/models/rfdetr/rfdetr_instance_segmentation_trt.py +227 -0
  111. inference_models/models/rfdetr/rfdetr_object_detection_onnx.py +244 -0
  112. inference_models/models/rfdetr/rfdetr_object_detection_pytorch.py +470 -0
  113. inference_models/models/rfdetr/rfdetr_object_detection_trt.py +270 -0
  114. inference_models/models/rfdetr/segmentation_head.py +273 -0
  115. inference_models/models/rfdetr/transformer.py +767 -0
  116. inference_models/models/roboflow_instant/__init__.py +0 -0
  117. inference_models/models/roboflow_instant/roboflow_instant_hf.py +141 -0
  118. inference_models/models/sam/__init__.py +0 -0
  119. inference_models/models/sam/cache.py +147 -0
  120. inference_models/models/sam/entities.py +25 -0
  121. inference_models/models/sam/sam_torch.py +675 -0
  122. inference_models/models/sam2/__init__.py +0 -0
  123. inference_models/models/sam2/cache.py +162 -0
  124. inference_models/models/sam2/entities.py +43 -0
  125. inference_models/models/sam2/sam2_torch.py +905 -0
  126. inference_models/models/sam2_rt/__init__.py +0 -0
  127. inference_models/models/sam2_rt/sam2_pytorch.py +119 -0
  128. inference_models/models/smolvlm/__init__.py +0 -0
  129. inference_models/models/smolvlm/smolvlm_hf.py +245 -0
  130. inference_models/models/trocr/__init__.py +0 -0
  131. inference_models/models/trocr/trocr_hf.py +53 -0
  132. inference_models/models/vit/__init__.py +0 -0
  133. inference_models/models/vit/vit_classification_huggingface.py +319 -0
  134. inference_models/models/vit/vit_classification_onnx.py +326 -0
  135. inference_models/models/vit/vit_classification_trt.py +365 -0
  136. inference_models/models/yolact/__init__.py +1 -0
  137. inference_models/models/yolact/yolact_instance_segmentation_onnx.py +336 -0
  138. inference_models/models/yolact/yolact_instance_segmentation_trt.py +361 -0
  139. inference_models/models/yolo_world/__init__.py +1 -0
  140. inference_models/models/yolonas/__init__.py +0 -0
  141. inference_models/models/yolonas/nms.py +44 -0
  142. inference_models/models/yolonas/yolonas_object_detection_onnx.py +204 -0
  143. inference_models/models/yolonas/yolonas_object_detection_trt.py +230 -0
  144. inference_models/models/yolov10/__init__.py +0 -0
  145. inference_models/models/yolov10/yolov10_object_detection_onnx.py +187 -0
  146. inference_models/models/yolov10/yolov10_object_detection_trt.py +215 -0
  147. inference_models/models/yolov11/__init__.py +0 -0
  148. inference_models/models/yolov11/yolov11_onnx.py +28 -0
  149. inference_models/models/yolov11/yolov11_torch_script.py +25 -0
  150. inference_models/models/yolov11/yolov11_trt.py +21 -0
  151. inference_models/models/yolov12/__init__.py +0 -0
  152. inference_models/models/yolov12/yolov12_onnx.py +7 -0
  153. inference_models/models/yolov12/yolov12_torch_script.py +7 -0
  154. inference_models/models/yolov12/yolov12_trt.py +7 -0
  155. inference_models/models/yolov5/__init__.py +0 -0
  156. inference_models/models/yolov5/nms.py +99 -0
  157. inference_models/models/yolov5/yolov5_instance_segmentation_onnx.py +225 -0
  158. inference_models/models/yolov5/yolov5_instance_segmentation_trt.py +255 -0
  159. inference_models/models/yolov5/yolov5_object_detection_onnx.py +192 -0
  160. inference_models/models/yolov5/yolov5_object_detection_trt.py +218 -0
  161. inference_models/models/yolov7/__init__.py +0 -0
  162. inference_models/models/yolov7/yolov7_instance_segmentation_onnx.py +226 -0
  163. inference_models/models/yolov7/yolov7_instance_segmentation_trt.py +253 -0
  164. inference_models/models/yolov8/__init__.py +0 -0
  165. inference_models/models/yolov8/yolov8_classification_onnx.py +181 -0
  166. inference_models/models/yolov8/yolov8_instance_segmentation_onnx.py +239 -0
  167. inference_models/models/yolov8/yolov8_instance_segmentation_torch_script.py +201 -0
  168. inference_models/models/yolov8/yolov8_instance_segmentation_trt.py +268 -0
  169. inference_models/models/yolov8/yolov8_key_points_detection_onnx.py +263 -0
  170. inference_models/models/yolov8/yolov8_key_points_detection_torch_script.py +218 -0
  171. inference_models/models/yolov8/yolov8_key_points_detection_trt.py +287 -0
  172. inference_models/models/yolov8/yolov8_object_detection_onnx.py +213 -0
  173. inference_models/models/yolov8/yolov8_object_detection_torch_script.py +166 -0
  174. inference_models/models/yolov8/yolov8_object_detection_trt.py +231 -0
  175. inference_models/models/yolov9/__init__.py +0 -0
  176. inference_models/models/yolov9/yolov9_onnx.py +7 -0
  177. inference_models/models/yolov9/yolov9_torch_script.py +7 -0
  178. inference_models/models/yolov9/yolov9_trt.py +7 -0
  179. inference_models/runtime_introspection/__init__.py +0 -0
  180. inference_models/runtime_introspection/core.py +410 -0
  181. inference_models/utils/__init__.py +0 -0
  182. inference_models/utils/download.py +608 -0
  183. inference_models/utils/environment.py +28 -0
  184. inference_models/utils/file_system.py +51 -0
  185. inference_models/utils/hashing.py +7 -0
  186. inference_models/utils/imports.py +48 -0
  187. inference_models/utils/onnx_introspection.py +17 -0
  188. inference_models/weights_providers/__init__.py +0 -0
  189. inference_models/weights_providers/core.py +20 -0
  190. inference_models/weights_providers/entities.py +159 -0
  191. inference_models/weights_providers/roboflow.py +601 -0
  192. inference_models-0.18.3.dist-info/METADATA +466 -0
  193. inference_models-0.18.3.dist-info/RECORD +195 -0
  194. inference_models-0.18.3.dist-info/WHEEL +5 -0
  195. inference_models-0.18.3.dist-info/top_level.txt +1 -0
@@ -0,0 +1,230 @@
1
+ from threading import Lock
2
+ from typing import List, Optional, Tuple, Union
3
+
4
+ import numpy as np
5
+ import torch
6
+
7
+ from inference_models import Detections, ObjectDetectionModel
8
+ from inference_models.configuration import DEFAULT_DEVICE
9
+ from inference_models.entities import ColorFormat
10
+ from inference_models.errors import (
11
+ CorruptedModelPackageError,
12
+ MissingDependencyError,
13
+ ModelRuntimeError,
14
+ )
15
+ from inference_models.models.common.cuda import (
16
+ use_cuda_context,
17
+ use_primary_cuda_context,
18
+ )
19
+ from inference_models.models.common.model_packages import get_model_package_contents
20
+ from inference_models.models.common.roboflow.model_packages import (
21
+ InferenceConfig,
22
+ PreProcessingMetadata,
23
+ ResizeMode,
24
+ TRTConfig,
25
+ parse_class_names_file,
26
+ parse_inference_config,
27
+ parse_trt_config,
28
+ )
29
+ from inference_models.models.common.roboflow.post_processing import rescale_detections
30
+ from inference_models.models.common.roboflow.pre_processing import (
31
+ pre_process_network_input,
32
+ )
33
+ from inference_models.models.common.trt import (
34
+ get_engine_inputs_and_outputs,
35
+ infer_from_trt_engine,
36
+ load_model,
37
+ )
38
+ from inference_models.models.yolonas.nms import run_yolonas_nms_for_object_detection
39
+
40
+ try:
41
+ import tensorrt as trt
42
+ except ImportError as import_error:
43
+ raise MissingDependencyError(
44
+ message=f"Could not import Yolo NAS model with TRT backend - this error means that some additional dependencies "
45
+ f"are not installed in the environment. If you run the `inference-models` library directly in your Python "
46
+ f"program, make sure the following extras of the package are installed: `trt10` - installation can only "
47
+ f"succeed for Linux and Windows machines with Cuda 12 installed. Jetson devices, should have TRT 10.x "
48
+ f"installed for all builds with Jetpack 6. "
49
+ f"If you see this error using Roboflow infrastructure, make sure the service you use does support the model. "
50
+ f"You can also contact Roboflow to get support.",
51
+ help_url="https://todo",
52
+ ) from import_error
53
+
54
+
55
+ try:
56
+ import pycuda.driver as cuda
57
+ except ImportError as import_error:
58
+ raise MissingDependencyError(
59
+ message="TODO", help_url="https://todo"
60
+ ) from import_error
61
+
62
+
63
+ class YOLONasForObjectDetectionTRT(
64
+ ObjectDetectionModel[torch.Tensor, PreProcessingMetadata, torch.Tensor]
65
+ ):
66
+
67
+ @classmethod
68
+ def from_pretrained(
69
+ cls,
70
+ model_name_or_path: str,
71
+ device: torch.device = DEFAULT_DEVICE,
72
+ engine_host_code_allowed: bool = False,
73
+ **kwargs,
74
+ ) -> "YOLONasForObjectDetectionTRT":
75
+ if device.type != "cuda":
76
+ raise ModelRuntimeError(
77
+ message=f"TRT engine only runs on CUDA device - {device} device detected.",
78
+ help_url="https://todo",
79
+ )
80
+ model_package_content = get_model_package_contents(
81
+ model_package_dir=model_name_or_path,
82
+ elements=[
83
+ "class_names.txt",
84
+ "inference_config.json",
85
+ "trt_config.json",
86
+ "engine.plan",
87
+ ],
88
+ )
89
+ class_names = parse_class_names_file(
90
+ class_names_path=model_package_content["class_names.txt"]
91
+ )
92
+ inference_config = parse_inference_config(
93
+ config_path=model_package_content["inference_config.json"],
94
+ allowed_resize_modes={
95
+ ResizeMode.STRETCH_TO,
96
+ ResizeMode.LETTERBOX,
97
+ ResizeMode.CENTER_CROP,
98
+ ResizeMode.LETTERBOX_REFLECT_EDGES,
99
+ },
100
+ )
101
+ if inference_config.post_processing.type != "nms":
102
+ raise CorruptedModelPackageError(
103
+ message="Expected NMS to be the post-processing",
104
+ help_url="https://todo",
105
+ )
106
+ if inference_config.post_processing.fused is True:
107
+ raise CorruptedModelPackageError(
108
+ message="Model implementation does not support fused NMS",
109
+ help_url="https://todo",
110
+ )
111
+ trt_config = parse_trt_config(
112
+ config_path=model_package_content["trt_config.json"]
113
+ )
114
+ cuda.init()
115
+ cuda_device = cuda.Device(device.index or 0)
116
+ with use_primary_cuda_context(cuda_device=cuda_device) as cuda_context:
117
+ engine = load_model(
118
+ model_path=model_package_content["engine.plan"],
119
+ engine_host_code_allowed=engine_host_code_allowed,
120
+ )
121
+ execution_context = engine.create_execution_context()
122
+ inputs, outputs = get_engine_inputs_and_outputs(engine=engine)
123
+ if len(inputs) != 1:
124
+ raise CorruptedModelPackageError(
125
+ message=f"Implementation assume single model input, found: {len(inputs)}.",
126
+ help_url="https://todo",
127
+ )
128
+ if len(outputs) != 2:
129
+ raise CorruptedModelPackageError(
130
+ message=f"Implementation assume 2 model outputs, found: {len(outputs)}.",
131
+ help_url="https://todo",
132
+ )
133
+ # git rid of outputs order and names verification, as YOLO-NAS clearly produces different outputs
134
+ return cls(
135
+ engine=engine,
136
+ input_name=inputs[0],
137
+ output_names=outputs,
138
+ class_names=class_names,
139
+ inference_config=inference_config,
140
+ trt_config=trt_config,
141
+ device=device,
142
+ cuda_context=cuda_context,
143
+ execution_context=execution_context,
144
+ )
145
+
146
+ def __init__(
147
+ self,
148
+ engine: trt.ICudaEngine,
149
+ input_name: str,
150
+ output_names: List[str],
151
+ class_names: List[str],
152
+ inference_config: InferenceConfig,
153
+ trt_config: TRTConfig,
154
+ device: torch.device,
155
+ cuda_context: cuda.Context,
156
+ execution_context: trt.IExecutionContext,
157
+ ):
158
+ self._engine = engine
159
+ self._input_name = input_name
160
+ self._output_names = output_names
161
+ self._class_names = class_names
162
+ self._inference_config = inference_config
163
+ self._trt_config = trt_config
164
+ self._device = device
165
+ self._cuda_context = cuda_context
166
+ self._execution_context = execution_context
167
+ self._session_thread_lock = Lock()
168
+
169
+ @property
170
+ def class_names(self) -> List[str]:
171
+ return self._class_names
172
+
173
+ def pre_process(
174
+ self,
175
+ images: Union[torch.Tensor, List[torch.Tensor], np.ndarray, List[np.ndarray]],
176
+ input_color_format: Optional[ColorFormat] = None,
177
+ **kwargs,
178
+ ) -> Tuple[torch.Tensor, List[PreProcessingMetadata]]:
179
+ return pre_process_network_input(
180
+ images=images,
181
+ image_pre_processing=self._inference_config.image_pre_processing,
182
+ network_input=self._inference_config.network_input,
183
+ target_device=self._device,
184
+ input_color_format=input_color_format,
185
+ )
186
+
187
+ def forward(self, pre_processed_images: torch.Tensor, **kwargs) -> torch.Tensor:
188
+ with self._session_thread_lock:
189
+ with use_cuda_context(context=self._cuda_context):
190
+ results = infer_from_trt_engine(
191
+ pre_processed_images=pre_processed_images,
192
+ trt_config=self._trt_config,
193
+ engine=self._engine,
194
+ context=self._execution_context,
195
+ device=self._device,
196
+ input_name=self._input_name,
197
+ outputs=self._output_names,
198
+ )
199
+ return torch.cat(results, dim=-1)
200
+
201
+ def post_process(
202
+ self,
203
+ model_results: torch.Tensor,
204
+ pre_processing_meta: List[PreProcessingMetadata],
205
+ conf_thresh: float = 0.25,
206
+ iou_thresh: float = 0.45,
207
+ max_detections: int = 100,
208
+ class_agnostic: bool = False,
209
+ **kwargs,
210
+ ) -> List[Detections]:
211
+ nms_results = run_yolonas_nms_for_object_detection(
212
+ output=model_results,
213
+ conf_thresh=conf_thresh,
214
+ iou_thresh=iou_thresh,
215
+ max_detections=max_detections,
216
+ )
217
+ rescaled_results = rescale_detections(
218
+ detections=nms_results,
219
+ images_metadata=pre_processing_meta,
220
+ )
221
+ results = []
222
+ for result in rescaled_results:
223
+ results.append(
224
+ Detections(
225
+ xyxy=result[:, :4].round().int(),
226
+ class_id=result[:, 5].int(),
227
+ confidence=result[:, 4],
228
+ )
229
+ )
230
+ return results
File without changes
@@ -0,0 +1,187 @@
1
+ from threading import Lock
2
+ from typing import List, Optional, Tuple, Union
3
+
4
+ import numpy as np
5
+ import torch
6
+
7
+ from inference_models import Detections, ObjectDetectionModel
8
+ from inference_models.configuration import DEFAULT_DEVICE
9
+ from inference_models.entities import ColorFormat
10
+ from inference_models.errors import (
11
+ CorruptedModelPackageError,
12
+ EnvironmentConfigurationError,
13
+ MissingDependencyError,
14
+ )
15
+ from inference_models.models.common.model_packages import get_model_package_contents
16
+ from inference_models.models.common.onnx import (
17
+ run_session_with_batch_size_limit,
18
+ set_execution_provider_defaults,
19
+ )
20
+ from inference_models.models.common.roboflow.model_packages import (
21
+ InferenceConfig,
22
+ PreProcessingMetadata,
23
+ ResizeMode,
24
+ parse_class_names_file,
25
+ parse_inference_config,
26
+ )
27
+ from inference_models.models.common.roboflow.post_processing import (
28
+ rescale_image_detections,
29
+ )
30
+ from inference_models.models.common.roboflow.pre_processing import (
31
+ pre_process_network_input,
32
+ )
33
+ from inference_models.utils.onnx_introspection import (
34
+ get_selected_onnx_execution_providers,
35
+ )
36
+
37
+ try:
38
+ import onnxruntime
39
+ except ImportError as import_error:
40
+ raise MissingDependencyError(
41
+ message=f"Could not import YOLOv10 model with ONNX backend - this error means that some additional dependencies "
42
+ f"are not installed in the environment. If you run the `inference-models` library directly in your Python "
43
+ f"program, make sure the following extras of the package are installed: \n"
44
+ f"\t* `onnx-cpu` - when you wish to use library with CPU support only\n"
45
+ f"\t* `onnx-cu12` - for running on GPU with Cuda 12 installed\n"
46
+ f"\t* `onnx-cu118` - for running on GPU with Cuda 11.8 installed\n"
47
+ f"\t* `onnx-jp6-cu126` - for running on Jetson with Jetpack 6\n"
48
+ f"If you see this error using Roboflow infrastructure, make sure the service you use does support the model. "
49
+ f"You can also contact Roboflow to get support.",
50
+ help_url="https://todo",
51
+ ) from import_error
52
+
53
+
54
+ class YOLOv10ForObjectDetectionOnnx(
55
+ ObjectDetectionModel[torch.Tensor, PreProcessingMetadata, torch.Tensor]
56
+ ):
57
+
58
+ @classmethod
59
+ def from_pretrained(
60
+ cls,
61
+ model_name_or_path: str,
62
+ onnx_execution_providers: Optional[List[Union[str, tuple]]] = None,
63
+ default_onnx_trt_options: bool = True,
64
+ device: torch.device = DEFAULT_DEVICE,
65
+ **kwargs,
66
+ ) -> "YOLOv10ForObjectDetectionOnnx":
67
+ if onnx_execution_providers is None:
68
+ onnx_execution_providers = get_selected_onnx_execution_providers()
69
+ if not onnx_execution_providers:
70
+ raise EnvironmentConfigurationError(
71
+ message=f"Could not initialize model - selected backend is ONNX which requires execution provider to "
72
+ f"be specified - explicitly in `from_pretrained(...)` method or via env variable "
73
+ f"`ONNXRUNTIME_EXECUTION_PROVIDERS`. If you run model locally - adjust your setup, otherwise "
74
+ f"contact the platform support.",
75
+ help_url="https://todo",
76
+ )
77
+ onnx_execution_providers = set_execution_provider_defaults(
78
+ providers=onnx_execution_providers,
79
+ model_package_path=model_name_or_path,
80
+ device=device,
81
+ default_onnx_trt_options=default_onnx_trt_options,
82
+ )
83
+ model_package_content = get_model_package_contents(
84
+ model_package_dir=model_name_or_path,
85
+ elements=[
86
+ "class_names.txt",
87
+ "inference_config.json",
88
+ "weights.onnx",
89
+ ],
90
+ )
91
+ class_names = parse_class_names_file(
92
+ class_names_path=model_package_content["class_names.txt"]
93
+ )
94
+ inference_config = parse_inference_config(
95
+ config_path=model_package_content["inference_config.json"],
96
+ allowed_resize_modes={
97
+ ResizeMode.STRETCH_TO,
98
+ ResizeMode.LETTERBOX,
99
+ ResizeMode.CENTER_CROP,
100
+ ResizeMode.LETTERBOX_REFLECT_EDGES,
101
+ },
102
+ )
103
+ session = onnxruntime.InferenceSession(
104
+ path_or_bytes=model_package_content["weights.onnx"],
105
+ providers=onnx_execution_providers,
106
+ )
107
+ input_batch_size = session.get_inputs()[0].shape[0]
108
+ if isinstance(input_batch_size, str):
109
+ input_batch_size = None
110
+ input_name = session.get_inputs()[0].name
111
+ return cls(
112
+ session=session,
113
+ input_name=input_name,
114
+ class_names=class_names,
115
+ inference_config=inference_config,
116
+ device=device,
117
+ input_batch_size=input_batch_size,
118
+ )
119
+
120
+ def __init__(
121
+ self,
122
+ session: onnxruntime.InferenceSession,
123
+ input_name: str,
124
+ inference_config: InferenceConfig,
125
+ class_names: List[str],
126
+ device: torch.device,
127
+ input_batch_size: Optional[int],
128
+ ):
129
+ self._session = session
130
+ self._input_name = input_name
131
+ self._inference_config = inference_config
132
+ self._class_names = class_names
133
+ self._device = device
134
+ self._input_batch_size = input_batch_size
135
+ self._session_thread_lock = Lock()
136
+
137
+ @property
138
+ def class_names(self) -> List[str]:
139
+ return self._class_names
140
+
141
+ def pre_process(
142
+ self,
143
+ images: Union[torch.Tensor, List[torch.Tensor], np.ndarray, List[np.ndarray]],
144
+ input_color_format: Optional[ColorFormat] = None,
145
+ **kwargs,
146
+ ) -> Tuple[torch.Tensor, List[PreProcessingMetadata]]:
147
+ return pre_process_network_input(
148
+ images=images,
149
+ image_pre_processing=self._inference_config.image_pre_processing,
150
+ network_input=self._inference_config.network_input,
151
+ target_device=self._device,
152
+ input_color_format=input_color_format,
153
+ )
154
+
155
+ def forward(self, pre_processed_images: torch.Tensor, **kwargs) -> torch.Tensor:
156
+ with self._session_thread_lock:
157
+ return run_session_with_batch_size_limit(
158
+ session=self._session,
159
+ inputs={self._input_name: pre_processed_images},
160
+ min_batch_size=self._input_batch_size,
161
+ max_batch_size=self._input_batch_size,
162
+ )[0]
163
+
164
+ def post_process(
165
+ self,
166
+ model_results: torch.Tensor,
167
+ pre_processing_meta: List[PreProcessingMetadata],
168
+ conf_thresh: float = 0.25,
169
+ max_detections: int = 100,
170
+ **kwargs,
171
+ ) -> List[Detections]:
172
+ results = []
173
+ for image_result, metadata in zip(model_results, pre_processing_meta):
174
+ mask = image_result[:, 4] > conf_thresh
175
+ filtered = image_result[mask][:max_detections]
176
+ rescaled = rescale_image_detections(
177
+ image_detections=filtered,
178
+ image_metadata=metadata,
179
+ )
180
+ results.append(
181
+ Detections(
182
+ xyxy=rescaled[:, :4].round().int(),
183
+ class_id=rescaled[:, 5].int(),
184
+ confidence=rescaled[:, 4],
185
+ )
186
+ )
187
+ return results
@@ -0,0 +1,215 @@
1
+ from threading import Lock
2
+ from typing import List, Optional, Tuple, Union
3
+
4
+ import numpy as np
5
+ import torch
6
+
7
+ from inference_models import Detections, ObjectDetectionModel
8
+ from inference_models.configuration import DEFAULT_DEVICE
9
+ from inference_models.entities import ColorFormat
10
+ from inference_models.errors import (
11
+ CorruptedModelPackageError,
12
+ MissingDependencyError,
13
+ ModelRuntimeError,
14
+ )
15
+ from inference_models.models.common.cuda import (
16
+ use_cuda_context,
17
+ use_primary_cuda_context,
18
+ )
19
+ from inference_models.models.common.model_packages import get_model_package_contents
20
+ from inference_models.models.common.roboflow.model_packages import (
21
+ InferenceConfig,
22
+ PreProcessingMetadata,
23
+ ResizeMode,
24
+ TRTConfig,
25
+ parse_class_names_file,
26
+ parse_inference_config,
27
+ parse_trt_config,
28
+ )
29
+ from inference_models.models.common.roboflow.post_processing import (
30
+ rescale_image_detections,
31
+ )
32
+ from inference_models.models.common.roboflow.pre_processing import (
33
+ pre_process_network_input,
34
+ )
35
+ from inference_models.models.common.trt import (
36
+ get_engine_inputs_and_outputs,
37
+ infer_from_trt_engine,
38
+ load_model,
39
+ )
40
+
41
+ try:
42
+ import tensorrt as trt
43
+ except ImportError as import_error:
44
+ raise MissingDependencyError(
45
+ message=f"Could not import YOLOv10 model with TRT backend - this error means that some additional dependencies "
46
+ f"are not installed in the environment. If you run the `inference-models` library directly in your Python "
47
+ f"program, make sure the following extras of the package are installed: `trt10` - installation can only "
48
+ f"succeed for Linux and Windows machines with Cuda 12 installed. Jetson devices, should have TRT 10.x "
49
+ f"installed for all builds with Jetpack 6. "
50
+ f"If you see this error using Roboflow infrastructure, make sure the service you use does support the model. "
51
+ f"You can also contact Roboflow to get support.",
52
+ help_url="https://todo",
53
+ ) from import_error
54
+
55
+ try:
56
+ import pycuda.driver as cuda
57
+ except ImportError as import_error:
58
+ raise MissingDependencyError(
59
+ message="TODO",
60
+ help_url="https://todo",
61
+ ) from import_error
62
+
63
+
64
+ class YOLOv10ForObjectDetectionTRT(
65
+ ObjectDetectionModel[torch.Tensor, PreProcessingMetadata, torch.Tensor]
66
+ ):
67
+
68
+ @classmethod
69
+ def from_pretrained(
70
+ cls,
71
+ model_name_or_path: str,
72
+ device: torch.device = DEFAULT_DEVICE,
73
+ engine_host_code_allowed: bool = False,
74
+ **kwargs,
75
+ ) -> "YOLOv10ForObjectDetectionTRT":
76
+ if device.type != "cuda":
77
+ raise ModelRuntimeError(
78
+ message="TRT engine only runs on CUDA device - {device} device detected.",
79
+ help_url="https://todo",
80
+ )
81
+ model_package_content = get_model_package_contents(
82
+ model_package_dir=model_name_or_path,
83
+ elements=[
84
+ "class_names.txt",
85
+ "inference_config.json",
86
+ "trt_config.json",
87
+ "engine.plan",
88
+ ],
89
+ )
90
+ class_names = parse_class_names_file(
91
+ class_names_path=model_package_content["class_names.txt"]
92
+ )
93
+ inference_config = parse_inference_config(
94
+ config_path=model_package_content["inference_config.json"],
95
+ allowed_resize_modes={
96
+ ResizeMode.STRETCH_TO,
97
+ ResizeMode.LETTERBOX,
98
+ ResizeMode.CENTER_CROP,
99
+ ResizeMode.LETTERBOX_REFLECT_EDGES,
100
+ },
101
+ )
102
+ trt_config = parse_trt_config(
103
+ config_path=model_package_content["trt_config.json"]
104
+ )
105
+ cuda.init()
106
+ cuda_device = cuda.Device(device.index or 0)
107
+ with use_primary_cuda_context(cuda_device=cuda_device) as cuda_context:
108
+ engine = load_model(
109
+ model_path=model_package_content["engine.plan"],
110
+ engine_host_code_allowed=engine_host_code_allowed,
111
+ )
112
+ execution_context = engine.create_execution_context()
113
+ inputs, outputs = get_engine_inputs_and_outputs(engine=engine)
114
+ if len(inputs) != 1:
115
+ raise CorruptedModelPackageError(
116
+ message=f"Implementation assume single model input, found: {len(inputs)}.",
117
+ help_url="https://todo",
118
+ )
119
+ if len(outputs) != 1:
120
+ raise CorruptedModelPackageError(
121
+ message=f"Implementation assume single model output, found: {len(outputs)}.",
122
+ help_url="https://todo",
123
+ )
124
+ return cls(
125
+ engine=engine,
126
+ input_name=inputs[0],
127
+ output_name=outputs[0],
128
+ class_names=class_names,
129
+ inference_config=inference_config,
130
+ trt_config=trt_config,
131
+ device=device,
132
+ cuda_context=cuda_context,
133
+ execution_context=execution_context,
134
+ )
135
+
136
+ def __init__(
137
+ self,
138
+ engine: trt.ICudaEngine,
139
+ input_name: str,
140
+ output_name: str,
141
+ class_names: List[str],
142
+ inference_config: InferenceConfig,
143
+ trt_config: TRTConfig,
144
+ device: torch.device,
145
+ cuda_context: cuda.Context,
146
+ execution_context: trt.IExecutionContext,
147
+ ):
148
+ self._engine = engine
149
+ self._input_name = input_name
150
+ self._output_names = [output_name]
151
+ self._class_names = class_names
152
+ self._inference_config = inference_config
153
+ self._trt_config = trt_config
154
+ self._device = device
155
+ self._cuda_context = cuda_context
156
+ self._execution_context = execution_context
157
+ self._session_thread_lock = Lock()
158
+
159
+ @property
160
+ def class_names(self) -> List[str]:
161
+ return self._class_names
162
+
163
+ def pre_process(
164
+ self,
165
+ images: Union[torch.Tensor, List[torch.Tensor], np.ndarray, List[np.ndarray]],
166
+ input_color_format: Optional[ColorFormat] = None,
167
+ **kwargs,
168
+ ) -> Tuple[torch.Tensor, List[PreProcessingMetadata]]:
169
+ return pre_process_network_input(
170
+ images=images,
171
+ image_pre_processing=self._inference_config.image_pre_processing,
172
+ network_input=self._inference_config.network_input,
173
+ target_device=self._device,
174
+ input_color_format=input_color_format,
175
+ )
176
+
177
+ def forward(self, pre_processed_images: torch.Tensor, **kwargs) -> torch.Tensor:
178
+ with self._session_thread_lock:
179
+ with use_cuda_context(context=self._cuda_context):
180
+ return infer_from_trt_engine(
181
+ pre_processed_images=pre_processed_images,
182
+ trt_config=self._trt_config,
183
+ engine=self._engine,
184
+ context=self._execution_context,
185
+ device=self._device,
186
+ input_name=self._input_name,
187
+ outputs=self._output_names,
188
+ )[0]
189
+
190
+ def post_process(
191
+ self,
192
+ model_results: torch.Tensor,
193
+ pre_processing_meta: List[PreProcessingMetadata],
194
+ conf_thresh: float = 0.25,
195
+ iou_thresh: float = 0.45,
196
+ max_detections: int = 100,
197
+ class_agnostic: bool = False,
198
+ **kwargs,
199
+ ) -> List[Detections]:
200
+ results = []
201
+ for image_result, metadata in zip(model_results, pre_processing_meta):
202
+ mask = image_result[:, 4] > conf_thresh
203
+ filtered = image_result[mask][:max_detections]
204
+ rescaled = rescale_image_detections(
205
+ image_detections=filtered,
206
+ image_metadata=metadata,
207
+ )
208
+ results.append(
209
+ Detections(
210
+ xyxy=rescaled[:, :4].round().int(),
211
+ class_id=rescaled[:, 5].int(),
212
+ confidence=rescaled[:, 4],
213
+ )
214
+ )
215
+ return results
File without changes
@@ -0,0 +1,28 @@
1
+ from inference_models.models.yolov8.yolov8_classification_onnx import (
2
+ YOLOv8ForClassificationOnnx,
3
+ )
4
+ from inference_models.models.yolov8.yolov8_instance_segmentation_onnx import (
5
+ YOLOv8ForInstanceSegmentationOnnx,
6
+ )
7
+ from inference_models.models.yolov8.yolov8_key_points_detection_onnx import (
8
+ YOLOv8ForKeyPointsDetectionOnnx,
9
+ )
10
+ from inference_models.models.yolov8.yolov8_object_detection_onnx import (
11
+ YOLOv8ForObjectDetectionOnnx,
12
+ )
13
+
14
+
15
+ class YOLOv11ForObjectDetectionOnnx(YOLOv8ForObjectDetectionOnnx):
16
+ pass
17
+
18
+
19
+ class YOLOv11ForInstanceSegmentationOnnx(YOLOv8ForInstanceSegmentationOnnx):
20
+ pass
21
+
22
+
23
+ class YOLOv11ForForKeyPointsDetectionOnnx(YOLOv8ForKeyPointsDetectionOnnx):
24
+ pass
25
+
26
+
27
+ class YOLOv11ForClassificationOnnx(YOLOv8ForClassificationOnnx):
28
+ pass
@@ -0,0 +1,25 @@
1
+ from inference_models.models.yolov8.yolov8_instance_segmentation_torch_script import (
2
+ YOLOv8ForInstanceSegmentationTorchScript,
3
+ )
4
+ from inference_models.models.yolov8.yolov8_key_points_detection_torch_script import (
5
+ YOLOv8ForKeyPointsDetectionTorchScript,
6
+ )
7
+ from inference_models.models.yolov8.yolov8_object_detection_torch_script import (
8
+ YOLOv8ForObjectDetectionTorchScript,
9
+ )
10
+
11
+
12
+ class YOLOv11ForObjectDetectionTorchScript(YOLOv8ForObjectDetectionTorchScript):
13
+ pass
14
+
15
+
16
+ class YOLOv11ForInstanceSegmentationTorchScript(
17
+ YOLOv8ForInstanceSegmentationTorchScript
18
+ ):
19
+ pass
20
+
21
+
22
+ class YOLOv11ForForKeyPointsDetectionTorchScript(
23
+ YOLOv8ForKeyPointsDetectionTorchScript
24
+ ):
25
+ pass