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,21 @@
1
+ from inference_models.models.yolov8.yolov8_instance_segmentation_trt import (
2
+ YOLOv8ForInstanceSegmentationTRT,
3
+ )
4
+ from inference_models.models.yolov8.yolov8_key_points_detection_trt import (
5
+ YOLOv8ForKeyPointsDetectionTRT,
6
+ )
7
+ from inference_models.models.yolov8.yolov8_object_detection_trt import (
8
+ YOLOv8ForObjectDetectionTRT,
9
+ )
10
+
11
+
12
+ class YOLOv11ForObjectDetectionTRT(YOLOv8ForObjectDetectionTRT):
13
+ pass
14
+
15
+
16
+ class YOLOv11ForInstanceSegmentationTRT(YOLOv8ForInstanceSegmentationTRT):
17
+ pass
18
+
19
+
20
+ class YOLOv11ForForKeyPointsDetectionTRT(YOLOv8ForKeyPointsDetectionTRT):
21
+ pass
File without changes
@@ -0,0 +1,7 @@
1
+ from inference_models.models.yolov8.yolov8_object_detection_onnx import (
2
+ YOLOv8ForObjectDetectionOnnx,
3
+ )
4
+
5
+
6
+ class YOLOv12ForObjectDetectionOnnx(YOLOv8ForObjectDetectionOnnx):
7
+ pass
@@ -0,0 +1,7 @@
1
+ from inference_models.models.yolov8.yolov8_object_detection_torch_script import (
2
+ YOLOv8ForObjectDetectionTorchScript,
3
+ )
4
+
5
+
6
+ class YOLOv12ForObjectDetectionTorchScript(YOLOv8ForObjectDetectionTorchScript):
7
+ pass
@@ -0,0 +1,7 @@
1
+ from inference_models.models.yolov8.yolov8_object_detection_trt import (
2
+ YOLOv8ForObjectDetectionTRT,
3
+ )
4
+
5
+
6
+ class YOLOv12ForObjectDetectionTRT(YOLOv8ForObjectDetectionTRT):
7
+ pass
File without changes
@@ -0,0 +1,99 @@
1
+ from typing import List
2
+
3
+ import torch
4
+ import torchvision
5
+
6
+
7
+ def run_nms_yolov5(
8
+ output: torch.Tensor,
9
+ conf_thresh: float = 0.25,
10
+ iou_thresh: float = 0.45,
11
+ max_detections: int = 100,
12
+ class_agnostic: bool = False,
13
+ ) -> List[torch.Tensor]:
14
+ bs = output.shape[0]
15
+ boxes = output[:, :4, :]
16
+ top_classes_conf = output[:, 4, :]
17
+ scores = output[:, 5:, :]
18
+ results = []
19
+ for b in range(bs):
20
+ class_scores = scores[b]
21
+ class_conf, class_ids = class_scores.max(0)
22
+ mask = top_classes_conf[b] > conf_thresh
23
+ if not torch.any(mask):
24
+ results.append(torch.zeros((0, 6), device=output.device))
25
+ continue
26
+ bboxes = boxes[b][:, mask].T
27
+ class_conf = class_conf[mask]
28
+ class_ids = class_ids[mask]
29
+ # Vectorized [x, y, w, h] -> [x1, y1, x2, y2]
30
+ xy = bboxes[:, :2]
31
+ wh = bboxes[:, 2:]
32
+ half_wh = wh / 2
33
+ xyxy = torch.cat((xy - half_wh, xy + half_wh), 1)
34
+ # Class-agnostic NMS -> use dummy class ids
35
+ nms_class_ids = torch.zeros_like(class_ids) if class_agnostic else class_ids
36
+ # NMS and limiting max detections
37
+ keep = torchvision.ops.batched_nms(xyxy, class_conf, nms_class_ids, iou_thresh)
38
+ if keep.numel() > max_detections:
39
+ keep = keep[:max_detections]
40
+ detections = torch.cat(
41
+ (
42
+ xyxy[keep],
43
+ class_conf[keep, None], # unsqueeze(1) is replaced with None
44
+ class_ids[keep, None].float(),
45
+ ),
46
+ 1,
47
+ ) # [x1, y1, x2, y2, conf, cls]
48
+ results.append(detections)
49
+ return results
50
+
51
+
52
+ def run_yolov5_nms_for_instance_segmentation(
53
+ output: torch.Tensor,
54
+ conf_thresh: float = 0.25,
55
+ iou_thresh: float = 0.45,
56
+ max_detections: int = 100,
57
+ class_agnostic: bool = False,
58
+ ) -> List[torch.Tensor]:
59
+ bs = output.shape[0]
60
+ boxes = output[:, :4, :]
61
+ top_classes_conf = output[:, 4, :]
62
+ scores = output[:, 4:-32, :]
63
+ masks = output[:, -32:, :]
64
+ results = []
65
+
66
+ for b in range(bs):
67
+ bboxes = boxes[b].T
68
+ class_scores = scores[b].T
69
+ box_masks = masks[b].T
70
+ class_conf, class_ids = class_scores.max(1)
71
+ mask = top_classes_conf[b] > conf_thresh
72
+ if mask.sum() == 0:
73
+ results.append(torch.zeros((0, 38), device=output.device))
74
+ continue
75
+ bboxes = bboxes[mask]
76
+ class_conf = class_conf[mask]
77
+ class_ids = class_ids[mask]
78
+ box_masks = box_masks[mask]
79
+ # Convert [x, y, w, h] -> [x1, y1, x2, y2]
80
+ xyxy = torch.zeros_like(bboxes)
81
+ xyxy[:, 0] = bboxes[:, 0] - bboxes[:, 2] / 2 # x1
82
+ xyxy[:, 1] = bboxes[:, 1] - bboxes[:, 3] / 2 # y1
83
+ xyxy[:, 2] = bboxes[:, 0] + bboxes[:, 2] / 2 # x2
84
+ xyxy[:, 3] = bboxes[:, 1] + bboxes[:, 3] / 2 # y2
85
+ # Class-agnostic NMS -> use dummy class ids
86
+ nms_class_ids = torch.zeros_like(class_ids) if class_agnostic else class_ids
87
+ keep = torchvision.ops.batched_nms(xyxy, class_conf, nms_class_ids, iou_thresh)
88
+ keep = keep[:max_detections]
89
+ detections = torch.cat(
90
+ [
91
+ xyxy[keep],
92
+ class_conf[keep].unsqueeze(1),
93
+ class_ids[keep].unsqueeze(1).float(),
94
+ box_masks[keep],
95
+ ],
96
+ dim=1,
97
+ ) # [x1, y1, x2, y2, conf, cls]
98
+ results.append(detections)
99
+ return results
@@ -0,0 +1,225 @@
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 InstanceDetections, InstanceSegmentationModel
8
+ from inference_models.configuration import DEFAULT_DEVICE
9
+ from inference_models.entities import ColorFormat
10
+ from inference_models.errors import (
11
+ EnvironmentConfigurationError,
12
+ MissingDependencyError,
13
+ )
14
+ from inference_models.models.common.model_packages import get_model_package_contents
15
+ from inference_models.models.common.onnx import (
16
+ run_session_with_batch_size_limit,
17
+ set_execution_provider_defaults,
18
+ )
19
+ from inference_models.models.common.roboflow.model_packages import (
20
+ InferenceConfig,
21
+ PreProcessingMetadata,
22
+ ResizeMode,
23
+ parse_class_names_file,
24
+ parse_inference_config,
25
+ )
26
+ from inference_models.models.common.roboflow.post_processing import (
27
+ align_instance_segmentation_results,
28
+ crop_masks_to_boxes,
29
+ preprocess_segmentation_masks,
30
+ )
31
+ from inference_models.models.common.roboflow.pre_processing import (
32
+ pre_process_network_input,
33
+ )
34
+ from inference_models.models.yolov5.nms import run_yolov5_nms_for_instance_segmentation
35
+ from inference_models.utils.onnx_introspection import (
36
+ get_selected_onnx_execution_providers,
37
+ )
38
+
39
+ try:
40
+ import onnxruntime
41
+ except ImportError as import_error:
42
+ raise MissingDependencyError(
43
+ message=f"Could not import YOLOv5 model with ONNX backend - this error means that some additional dependencies "
44
+ f"are not installed in the environment. If you run the `inference-models` library directly in your Python "
45
+ f"program, make sure the following extras of the package are installed: \n"
46
+ f"\t* `onnx-cpu` - when you wish to use library with CPU support only\n"
47
+ f"\t* `onnx-cu12` - for running on GPU with Cuda 12 installed\n"
48
+ f"\t* `onnx-cu118` - for running on GPU with Cuda 11.8 installed\n"
49
+ f"\t* `onnx-jp6-cu126` - for running on Jetson with Jetpack 6\n"
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
+
56
+ class YOLOv5ForInstanceSegmentationOnnx(
57
+ InstanceSegmentationModel[
58
+ torch.Tensor, PreProcessingMetadata, Tuple[torch.Tensor, torch.Tensor]
59
+ ]
60
+ ):
61
+
62
+ @classmethod
63
+ def from_pretrained(
64
+ cls,
65
+ model_name_or_path: str,
66
+ onnx_execution_providers: Optional[List[Union[str, tuple]]] = None,
67
+ default_onnx_trt_options: bool = True,
68
+ device: torch.device = DEFAULT_DEVICE,
69
+ **kwargs,
70
+ ) -> "YOLOv5ForInstanceSegmentationOnnx":
71
+ if onnx_execution_providers is None:
72
+ onnx_execution_providers = get_selected_onnx_execution_providers()
73
+ if not onnx_execution_providers:
74
+ raise EnvironmentConfigurationError(
75
+ message=f"Could not initialize model - selected backend is ONNX which requires execution provider to "
76
+ f"be specified - explicitly in `from_pretrained(...)` method or via env variable "
77
+ f"`ONNXRUNTIME_EXECUTION_PROVIDERS`. If you run model locally - adjust your setup, otherwise "
78
+ f"contact the platform support.",
79
+ help_url="https://todo",
80
+ )
81
+ onnx_execution_providers = set_execution_provider_defaults(
82
+ providers=onnx_execution_providers,
83
+ model_package_path=model_name_or_path,
84
+ device=device,
85
+ default_onnx_trt_options=default_onnx_trt_options,
86
+ )
87
+ model_package_content = get_model_package_contents(
88
+ model_package_dir=model_name_or_path,
89
+ elements=[
90
+ "class_names.txt",
91
+ "inference_config.json",
92
+ "weights.onnx",
93
+ ],
94
+ )
95
+ class_names = parse_class_names_file(
96
+ class_names_path=model_package_content["class_names.txt"]
97
+ )
98
+ inference_config = parse_inference_config(
99
+ config_path=model_package_content["inference_config.json"],
100
+ allowed_resize_modes={
101
+ ResizeMode.STRETCH_TO,
102
+ ResizeMode.LETTERBOX,
103
+ ResizeMode.CENTER_CROP,
104
+ ResizeMode.LETTERBOX_REFLECT_EDGES,
105
+ },
106
+ )
107
+ session = onnxruntime.InferenceSession(
108
+ path_or_bytes=model_package_content["weights.onnx"],
109
+ providers=onnx_execution_providers,
110
+ )
111
+ input_batch_size = session.get_inputs()[0].shape[0]
112
+ if isinstance(input_batch_size, str):
113
+ input_batch_size = None
114
+ input_name = session.get_inputs()[0].name
115
+ return cls(
116
+ session=session,
117
+ input_name=input_name,
118
+ class_names=class_names,
119
+ inference_config=inference_config,
120
+ device=device,
121
+ input_batch_size=input_batch_size,
122
+ )
123
+
124
+ def __init__(
125
+ self,
126
+ session: onnxruntime.InferenceSession,
127
+ input_name: str,
128
+ inference_config: InferenceConfig,
129
+ class_names: List[str],
130
+ device: torch.device,
131
+ input_batch_size: Optional[int],
132
+ ):
133
+ self._session = session
134
+ self._input_name = input_name
135
+ self._inference_config = inference_config
136
+ self._class_names = class_names
137
+ self._device = device
138
+ self._input_batch_size = input_batch_size
139
+ self._session_thread_lock = Lock()
140
+
141
+ @property
142
+ def class_names(self) -> List[str]:
143
+ return self._class_names
144
+
145
+ def pre_process(
146
+ self,
147
+ images: Union[torch.Tensor, List[torch.Tensor], np.ndarray, List[np.ndarray]],
148
+ input_color_format: Optional[ColorFormat] = None,
149
+ **kwargs,
150
+ ) -> Tuple[torch.Tensor, List[PreProcessingMetadata]]:
151
+ return pre_process_network_input(
152
+ images=images,
153
+ image_pre_processing=self._inference_config.image_pre_processing,
154
+ network_input=self._inference_config.network_input,
155
+ target_device=self._device,
156
+ input_color_format=input_color_format,
157
+ )
158
+
159
+ def forward(
160
+ self, pre_processed_images: torch.Tensor, **kwargs
161
+ ) -> Tuple[torch.Tensor, torch.Tensor]:
162
+ with self._session_thread_lock:
163
+ instances, protos = run_session_with_batch_size_limit(
164
+ session=self._session,
165
+ inputs={self._input_name: pre_processed_images},
166
+ min_batch_size=self._input_batch_size,
167
+ max_batch_size=self._input_batch_size,
168
+ )
169
+ return instances, protos
170
+
171
+ def post_process(
172
+ self,
173
+ model_results: Tuple[torch.Tensor, torch.Tensor],
174
+ pre_processing_meta: List[PreProcessingMetadata],
175
+ conf_thresh: float = 0.25,
176
+ iou_thresh: float = 0.45,
177
+ max_detections: int = 100,
178
+ class_agnostic: bool = False,
179
+ **kwargs,
180
+ ) -> List[InstanceDetections]:
181
+ instances, protos = model_results
182
+ nms_results = run_yolov5_nms_for_instance_segmentation(
183
+ output=instances.permute(0, 2, 1),
184
+ conf_thresh=conf_thresh,
185
+ iou_thresh=iou_thresh,
186
+ max_detections=max_detections,
187
+ class_agnostic=class_agnostic,
188
+ )
189
+ final_results = []
190
+ for image_bboxes, image_protos, image_meta in zip(
191
+ nms_results, protos, pre_processing_meta
192
+ ):
193
+ pre_processed_masks = preprocess_segmentation_masks(
194
+ protos=image_protos,
195
+ masks_in=image_bboxes[:, 6:],
196
+ )
197
+ cropped_masks = crop_masks_to_boxes(
198
+ image_bboxes[:, :4], pre_processed_masks
199
+ )
200
+ padding = (
201
+ image_meta.pad_left,
202
+ image_meta.pad_top,
203
+ image_meta.pad_right,
204
+ image_meta.pad_bottom,
205
+ )
206
+ aligned_boxes, aligned_masks = align_instance_segmentation_results(
207
+ image_bboxes=image_bboxes,
208
+ masks=cropped_masks,
209
+ padding=padding,
210
+ scale_height=image_meta.scale_height,
211
+ scale_width=image_meta.scale_width,
212
+ original_size=image_meta.original_size,
213
+ size_after_pre_processing=image_meta.size_after_pre_processing,
214
+ inference_size=image_meta.inference_size,
215
+ static_crop_offset=image_meta.static_crop_offset,
216
+ )
217
+ final_results.append(
218
+ InstanceDetections(
219
+ xyxy=aligned_boxes[:, :4].round().int(),
220
+ class_id=aligned_boxes[:, 5].int(),
221
+ confidence=aligned_boxes[:, 4],
222
+ mask=aligned_masks,
223
+ )
224
+ )
225
+ return final_results
@@ -0,0 +1,255 @@
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 InstanceDetections, InstanceSegmentationModel
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
+ align_instance_segmentation_results,
31
+ crop_masks_to_boxes,
32
+ preprocess_segmentation_masks,
33
+ )
34
+ from inference_models.models.common.roboflow.pre_processing import (
35
+ pre_process_network_input,
36
+ )
37
+ from inference_models.models.common.trt import (
38
+ get_engine_inputs_and_outputs,
39
+ infer_from_trt_engine,
40
+ load_model,
41
+ )
42
+ from inference_models.models.yolov5.nms import run_yolov5_nms_for_instance_segmentation
43
+
44
+ try:
45
+ import tensorrt as trt
46
+ except ImportError as import_error:
47
+ raise MissingDependencyError(
48
+ message=f"Could not import YOLOv5 model with TRT backend - this error means that some additional dependencies "
49
+ f"are not installed in the environment. If you run the `inference-models` library directly in your Python "
50
+ f"program, make sure the following extras of the package are installed: `trt10` - installation can only "
51
+ f"succeed for Linux and Windows machines with Cuda 12 installed. Jetson devices, should have TRT 10.x "
52
+ f"installed for all builds with Jetpack 6. "
53
+ f"If you see this error using Roboflow infrastructure, make sure the service you use does support the model. "
54
+ f"You can also contact Roboflow to get support.",
55
+ help_url="https://todo",
56
+ ) from import_error
57
+
58
+ try:
59
+ import pycuda.driver as cuda
60
+ except ImportError as import_error:
61
+ raise MissingDependencyError(
62
+ message="TODO", help_url="https://todo"
63
+ ) from import_error
64
+
65
+
66
+ class YOLOv5ForInstanceSegmentationTRT(
67
+ InstanceSegmentationModel[
68
+ torch.Tensor, PreProcessingMetadata, Tuple[torch.Tensor, torch.Tensor]
69
+ ]
70
+ ):
71
+
72
+ @classmethod
73
+ def from_pretrained(
74
+ cls,
75
+ model_name_or_path: str,
76
+ device: torch.device = DEFAULT_DEVICE,
77
+ engine_host_code_allowed: bool = False,
78
+ **kwargs,
79
+ ) -> "YOLOv5ForInstanceSegmentationTRT":
80
+ if device.type != "cuda":
81
+ raise ModelRuntimeError(
82
+ message=f"TRT engine only runs on CUDA device - {device} device detected.",
83
+ help_url="https://todo",
84
+ )
85
+ model_package_content = get_model_package_contents(
86
+ model_package_dir=model_name_or_path,
87
+ elements=[
88
+ "class_names.txt",
89
+ "inference_config.json" "trt_config.json",
90
+ "engine.plan",
91
+ ],
92
+ )
93
+ class_names = parse_class_names_file(
94
+ class_names_path=model_package_content["class_names.txt"]
95
+ )
96
+ inference_config = parse_inference_config(
97
+ config_path=model_package_content["inference_config.json"],
98
+ allowed_resize_modes={
99
+ ResizeMode.STRETCH_TO,
100
+ ResizeMode.LETTERBOX,
101
+ ResizeMode.CENTER_CROP,
102
+ ResizeMode.LETTERBOX_REFLECT_EDGES,
103
+ },
104
+ )
105
+ trt_config = parse_trt_config(
106
+ config_path=model_package_content["trt_config.json"]
107
+ )
108
+ cuda.init()
109
+ cuda_device = cuda.Device(device.index or 0)
110
+ with use_primary_cuda_context(cuda_device=cuda_device) as cuda_context:
111
+ engine = load_model(
112
+ model_path=model_package_content["engine.plan"],
113
+ engine_host_code_allowed=engine_host_code_allowed,
114
+ )
115
+ execution_context = engine.create_execution_context()
116
+ inputs, outputs = get_engine_inputs_and_outputs(engine=engine)
117
+ if len(inputs) != 1:
118
+ raise CorruptedModelPackageError(
119
+ message=f"Implementation assume single model input, found: {len(inputs)}.",
120
+ help_url="https://todo",
121
+ )
122
+ if len(outputs) != 2:
123
+ raise CorruptedModelPackageError(
124
+ message=f"Implementation assume 2 model outputs, found: {len(outputs)}.",
125
+ help_url="https://todo",
126
+ )
127
+ if "output0" not in outputs or "output1" not in outputs:
128
+ raise CorruptedModelPackageError(
129
+ message=f"Expected model outputs to be named `output0` and `output1`, but found: {outputs}.",
130
+ help_url="https://todo",
131
+ )
132
+ return cls(
133
+ engine=engine,
134
+ input_name=inputs[0],
135
+ output_names=["output0", "output1"],
136
+ class_names=class_names,
137
+ inference_config=inference_config,
138
+ trt_config=trt_config,
139
+ device=device,
140
+ cuda_context=cuda_context,
141
+ execution_context=execution_context,
142
+ )
143
+
144
+ def __init__(
145
+ self,
146
+ engine: trt.ICudaEngine,
147
+ input_name: str,
148
+ output_names: List[str],
149
+ class_names: List[str],
150
+ inference_config: InferenceConfig,
151
+ trt_config: TRTConfig,
152
+ device: torch.device,
153
+ cuda_context: cuda.Context,
154
+ execution_context: trt.IExecutionContext,
155
+ ):
156
+ self._engine = engine
157
+ self._input_name = input_name
158
+ self._output_names = output_names
159
+ self._class_names = class_names
160
+ self._inference_config = inference_config
161
+ self._trt_config = trt_config
162
+ self._device = device
163
+ self._cuda_context = cuda_context
164
+ self._execution_context = execution_context
165
+ self._session_thread_lock = Lock()
166
+
167
+ @property
168
+ def class_names(self) -> List[str]:
169
+ return self._class_names
170
+
171
+ def pre_process(
172
+ self,
173
+ images: Union[torch.Tensor, List[torch.Tensor], np.ndarray, List[np.ndarray]],
174
+ input_color_format: Optional[ColorFormat] = None,
175
+ **kwargs,
176
+ ) -> Tuple[torch.Tensor, List[PreProcessingMetadata]]:
177
+ return pre_process_network_input(
178
+ images=images,
179
+ image_pre_processing=self._inference_config.image_pre_processing,
180
+ network_input=self._inference_config.network_input,
181
+ target_device=self._device,
182
+ input_color_format=input_color_format,
183
+ )
184
+
185
+ def forward(
186
+ self, pre_processed_images: torch.Tensor, **kwargs
187
+ ) -> Tuple[torch.Tensor, torch.Tensor]:
188
+ with self._session_thread_lock:
189
+ with use_cuda_context(context=self._cuda_context):
190
+ instances, protos = 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 instances, protos
200
+
201
+ def post_process(
202
+ self,
203
+ model_results: Tuple[torch.Tensor, 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[InstanceDetections]:
211
+ instances, protos = model_results
212
+ nms_results = run_yolov5_nms_for_instance_segmentation(
213
+ output=instances.permute(0, 2, 1),
214
+ conf_thresh=conf_thresh,
215
+ iou_thresh=iou_thresh,
216
+ max_detections=max_detections,
217
+ class_agnostic=class_agnostic,
218
+ )
219
+ final_results = []
220
+ for image_bboxes, image_protos, image_meta in zip(
221
+ nms_results, protos, pre_processing_meta
222
+ ):
223
+ pre_processed_masks = preprocess_segmentation_masks(
224
+ protos=image_protos,
225
+ masks_in=image_bboxes[:, 6:],
226
+ )
227
+ cropped_masks = crop_masks_to_boxes(
228
+ image_bboxes[:, :4], pre_processed_masks
229
+ )
230
+ padding = (
231
+ image_meta.pad_left,
232
+ image_meta.pad_top,
233
+ image_meta.pad_right,
234
+ image_meta.pad_bottom,
235
+ )
236
+ aligned_boxes, aligned_masks = align_instance_segmentation_results(
237
+ image_bboxes=image_bboxes,
238
+ masks=cropped_masks,
239
+ padding=padding,
240
+ scale_height=image_meta.scale_height,
241
+ scale_width=image_meta.scale_width,
242
+ original_size=image_meta.original_size,
243
+ size_after_pre_processing=image_meta.size_after_pre_processing,
244
+ inference_size=image_meta.inference_size,
245
+ static_crop_offset=image_meta.static_crop_offset,
246
+ )
247
+ final_results.append(
248
+ InstanceDetections(
249
+ xyxy=aligned_boxes[:, :4].round().int(),
250
+ class_id=aligned_boxes[:, 5].int(),
251
+ confidence=aligned_boxes[:, 4],
252
+ mask=aligned_masks,
253
+ )
254
+ )
255
+ return final_results