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,111 @@
1
+ from abc import ABC, abstractmethod
2
+ from typing import Generic, List, Tuple, Union
3
+
4
+ import numpy as np
5
+ import torch
6
+
7
+ from inference_models.models.base.object_detection import Detections
8
+ from inference_models.models.base.types import (
9
+ PreprocessedInputs,
10
+ PreprocessingMetadata,
11
+ RawPrediction,
12
+ )
13
+
14
+
15
+ class StructuredOCRModel(
16
+ ABC, Generic[PreprocessedInputs, PreprocessingMetadata, RawPrediction]
17
+ ):
18
+
19
+ @classmethod
20
+ @abstractmethod
21
+ def from_pretrained(cls, model_name_or_path: str, **kwargs) -> "StructuredOCRModel":
22
+ pass
23
+
24
+ @property
25
+ @abstractmethod
26
+ def class_names(self) -> List[str]:
27
+ pass
28
+
29
+ def infer(
30
+ self,
31
+ images: Union[torch.Tensor, List[torch.Tensor], np.ndarray, List[np.ndarray]],
32
+ **kwargs,
33
+ ) -> Tuple[List[str], List[Detections]]:
34
+ pre_processed_images, pre_processing_meta = self.pre_process(images, **kwargs)
35
+ model_results = self.forward(pre_processed_images, **kwargs)
36
+ return self.post_process(model_results, pre_processing_meta, **kwargs)
37
+
38
+ @abstractmethod
39
+ def pre_process(
40
+ self,
41
+ images: Union[torch.Tensor, List[torch.Tensor], np.ndarray, List[np.ndarray]],
42
+ **kwargs,
43
+ ) -> Tuple[PreprocessedInputs, PreprocessingMetadata]:
44
+ pass
45
+
46
+ @abstractmethod
47
+ def forward(
48
+ self, pre_processed_images: PreprocessedInputs, **kwargs
49
+ ) -> RawPrediction:
50
+ pass
51
+
52
+ @abstractmethod
53
+ def post_process(
54
+ self,
55
+ model_results: RawPrediction,
56
+ pre_processing_meta: PreprocessingMetadata,
57
+ **kwargs,
58
+ ) -> Tuple[List[str], List[Detections]]:
59
+ pass
60
+
61
+ def __call__(
62
+ self,
63
+ images: Union[torch.Tensor, List[torch.Tensor], np.ndarray, List[np.ndarray]],
64
+ **kwargs,
65
+ ) -> Tuple[List[str], List[Detections]]:
66
+ return self.infer(images, **kwargs)
67
+
68
+
69
+ class TextOnlyOCRModel(ABC, Generic[PreprocessedInputs, RawPrediction]):
70
+ @classmethod
71
+ @abstractmethod
72
+ def from_pretrained(cls, model_name_or_path: str, **kwargs) -> "TextOnlyOCRModel":
73
+ pass
74
+
75
+ def infer(
76
+ self,
77
+ images: Union[torch.Tensor, List[torch.Tensor], np.ndarray, List[np.ndarray]],
78
+ **kwargs,
79
+ ) -> List[str]:
80
+ pre_processed_images = self.pre_process(images, **kwargs)
81
+ model_results = self.forward(pre_processed_images, **kwargs)
82
+ return self.post_process(model_results, **kwargs)
83
+
84
+ @abstractmethod
85
+ def pre_process(
86
+ self,
87
+ images: Union[torch.Tensor, List[torch.Tensor], np.ndarray, List[np.ndarray]],
88
+ **kwargs,
89
+ ) -> PreprocessedInputs:
90
+ pass
91
+
92
+ @abstractmethod
93
+ def forward(
94
+ self, pre_processed_images: PreprocessedInputs, **kwargs
95
+ ) -> RawPrediction:
96
+ pass
97
+
98
+ @abstractmethod
99
+ def post_process(
100
+ self,
101
+ model_results: RawPrediction,
102
+ **kwargs,
103
+ ) -> List[str]:
104
+ pass
105
+
106
+ def __call__(
107
+ self,
108
+ images: Union[torch.Tensor, List[torch.Tensor], np.ndarray, List[np.ndarray]],
109
+ **kwargs,
110
+ ) -> List[str]:
111
+ return self.infer(images, **kwargs)
@@ -0,0 +1,66 @@
1
+ from abc import ABC, abstractmethod
2
+ from typing import List, Literal, Union
3
+
4
+ import numpy as np
5
+ import torch
6
+ import torch.nn.functional as F
7
+
8
+
9
+ class TextImageEmbeddingModel(ABC):
10
+
11
+ @classmethod
12
+ @abstractmethod
13
+ def from_pretrained(
14
+ cls, model_name_or_path: str, **kwargs
15
+ ) -> "TextImageEmbeddingModel":
16
+ pass
17
+
18
+ def compare_embeddings(
19
+ self,
20
+ x: Union[
21
+ torch.Tensor,
22
+ List[torch.Tensor],
23
+ np.ndarray,
24
+ List[np.ndarray],
25
+ str,
26
+ List[str],
27
+ ],
28
+ y: Union[
29
+ torch.Tensor,
30
+ List[torch.Tensor],
31
+ np.ndarray,
32
+ List[np.ndarray],
33
+ str,
34
+ List[str],
35
+ ],
36
+ x_type: Literal["image", "text"] = "image",
37
+ y_type: Literal["image", "text"] = "text",
38
+ **kwargs,
39
+ ) -> torch.Tensor:
40
+ if x_type == "image":
41
+ x_embeddings = self.embed_images(images=x, **kwargs)
42
+ else:
43
+ x_embeddings = self.embed_text(texts=x, **kwargs)
44
+ if y_type == "image":
45
+ y_embeddings = self.embed_images(images=y, **kwargs)
46
+ else:
47
+ y_embeddings = self.embed_text(texts=y, **kwargs)
48
+ x_embeddings_norm = F.normalize(x_embeddings, p=2, dim=1)
49
+ y_embeddings_morm = F.normalize(y_embeddings, p=2, dim=1)
50
+ return x_embeddings_norm @ y_embeddings_morm.T
51
+
52
+ @abstractmethod
53
+ def embed_images(
54
+ self,
55
+ images: Union[torch.Tensor, List[torch.Tensor], np.ndarray, List[np.ndarray]],
56
+ **kwargs,
57
+ ) -> torch.Tensor:
58
+ pass
59
+
60
+ @abstractmethod
61
+ def embed_text(
62
+ self,
63
+ texts: Union[str, List[str]],
64
+ **kwargs,
65
+ ) -> torch.Tensor:
66
+ pass
@@ -0,0 +1,87 @@
1
+ from abc import ABC, abstractmethod
2
+ from dataclasses import dataclass
3
+ from typing import Generic, List, Optional, Tuple, Union
4
+
5
+ import numpy as np
6
+ import supervision as sv
7
+ import torch
8
+
9
+ from inference_models.models.base.types import (
10
+ PreprocessedInputs,
11
+ PreprocessingMetadata,
12
+ RawPrediction,
13
+ )
14
+
15
+
16
+ @dataclass
17
+ class InstanceDetections:
18
+ xyxy: torch.Tensor # (n_boxes, 4)
19
+ class_id: torch.Tensor # (n_boxes, )
20
+ confidence: torch.Tensor # (n_boxes, )
21
+ mask: torch.Tensor # (n_boxes, mask_height, mask_width)
22
+ image_metadata: Optional[dict] = None
23
+ bboxes_metadata: Optional[List[dict]] = (
24
+ None # if given, list of size equal to # of bboxes
25
+ )
26
+
27
+ def to_supervision(self) -> sv.Detections:
28
+ return sv.Detections(
29
+ xyxy=self.xyxy.cpu().numpy(),
30
+ class_id=self.class_id.cpu().numpy(),
31
+ confidence=self.confidence.cpu().numpy(),
32
+ mask=self.mask.cpu().numpy(),
33
+ )
34
+
35
+
36
+ class InstanceSegmentationModel(
37
+ ABC, Generic[PreprocessedInputs, PreprocessingMetadata, RawPrediction]
38
+ ):
39
+
40
+ @classmethod
41
+ @abstractmethod
42
+ def from_pretrained(
43
+ cls, model_name_or_path: str, **kwargs
44
+ ) -> "InstanceSegmentationModel":
45
+ pass
46
+
47
+ @property
48
+ @abstractmethod
49
+ def class_names(self) -> List[str]:
50
+ pass
51
+
52
+ def infer(
53
+ self,
54
+ images: Union[torch.Tensor, List[torch.Tensor], np.ndarray, List[np.ndarray]],
55
+ **kwargs,
56
+ ) -> List[InstanceDetections]:
57
+ pre_processed_images, pre_processing_meta = self.pre_process(images, **kwargs)
58
+ model_results = self.forward(pre_processed_images, **kwargs)
59
+ return self.post_process(model_results, pre_processing_meta, **kwargs)
60
+
61
+ @abstractmethod
62
+ def pre_process(
63
+ self, images: Union[torch.Tensor, List[torch.Tensor]], **kwargs
64
+ ) -> Tuple[PreprocessedInputs, PreprocessingMetadata]:
65
+ pass
66
+
67
+ @abstractmethod
68
+ def forward(
69
+ self, pre_processed_images: PreprocessedInputs, **kwargs
70
+ ) -> RawPrediction:
71
+ pass
72
+
73
+ @abstractmethod
74
+ def post_process(
75
+ self,
76
+ model_results: RawPrediction,
77
+ pre_processing_meta: PreprocessedInputs,
78
+ **kwargs,
79
+ ) -> List[InstanceDetections]:
80
+ pass
81
+
82
+ def __call__(
83
+ self,
84
+ images: Union[torch.Tensor, List[torch.Tensor], np.ndarray, List[np.ndarray]],
85
+ **kwargs,
86
+ ) -> List[InstanceDetections]:
87
+ return self.infer(images, **kwargs)
@@ -0,0 +1,93 @@
1
+ from abc import ABC, abstractmethod
2
+ from dataclasses import dataclass
3
+ from typing import Generic, List, Optional, Tuple, Union
4
+
5
+ import numpy as np
6
+ import supervision as sv
7
+ import torch
8
+
9
+ from inference_models.models.base.object_detection import Detections
10
+ from inference_models.models.base.types import (
11
+ PreprocessedInputs,
12
+ PreprocessingMetadata,
13
+ RawPrediction,
14
+ )
15
+
16
+
17
+ @dataclass
18
+ class KeyPoints:
19
+ xy: torch.Tensor # (instances, instance_key_points, 2)
20
+ class_id: torch.Tensor # (instances, )
21
+ confidence: torch.Tensor # (instances, instance_key_points)
22
+ image_metadata: Optional[dict] = None
23
+ key_points_metadata: Optional[List[dict]] = (
24
+ None # if given, list of size equal to # of instances
25
+ )
26
+
27
+ def to_supervision(self) -> sv.KeyPoints:
28
+ return sv.KeyPoints(
29
+ xy=self.xy.cpu().numpy(),
30
+ class_id=self.class_id.cpu().numpy(),
31
+ confidence=self.confidence.cpu().numpy(),
32
+ )
33
+
34
+
35
+ class KeyPointsDetectionModel(
36
+ ABC, Generic[PreprocessedInputs, PreprocessingMetadata, RawPrediction]
37
+ ):
38
+
39
+ @classmethod
40
+ @abstractmethod
41
+ def from_pretrained(
42
+ cls, model_name_or_path: str, **kwargs
43
+ ) -> "KeyPointsDetectionModel":
44
+ pass
45
+
46
+ @property
47
+ @abstractmethod
48
+ def class_names(self) -> List[str]:
49
+ pass
50
+
51
+ @property
52
+ @abstractmethod
53
+ def skeletons(self) -> List[List[Tuple[int, int]]]:
54
+ pass
55
+
56
+ def infer(
57
+ self,
58
+ images: Union[torch.Tensor, List[torch.Tensor], np.ndarray, List[np.ndarray]],
59
+ **kwargs,
60
+ ) -> Tuple[List[KeyPoints], Optional[List[Detections]]]:
61
+ pre_processed_images, pre_processing_meta = self.pre_process(images, **kwargs)
62
+ model_results = self.forward(pre_processed_images, **kwargs)
63
+ return self.post_process(model_results, pre_processing_meta, **kwargs)
64
+
65
+ @abstractmethod
66
+ def pre_process(
67
+ self,
68
+ images: Union[torch.Tensor, List[torch.Tensor], np.ndarray, List[np.ndarray]],
69
+ **kwargs,
70
+ ) -> Tuple[PreprocessedInputs, PreprocessingMetadata]:
71
+ pass
72
+
73
+ @abstractmethod
74
+ def forward(
75
+ self, pre_processed_images: PreprocessedInputs, **kwargs
76
+ ) -> RawPrediction:
77
+ pass
78
+
79
+ @abstractmethod
80
+ def post_process(
81
+ self,
82
+ model_results: RawPrediction,
83
+ pre_processing_meta: PreprocessingMetadata,
84
+ **kwargs,
85
+ ) -> Tuple[List[KeyPoints], Optional[List[Detections]]]:
86
+ pass
87
+
88
+ def __call__(
89
+ self,
90
+ images: Union[torch.Tensor, List[torch.Tensor], np.ndarray, List[np.ndarray]],
91
+ **kwargs,
92
+ ) -> Tuple[List[KeyPoints], Optional[List[Detections]]]:
93
+ return self.infer(images, **kwargs)
@@ -0,0 +1,143 @@
1
+ from abc import ABC, abstractmethod
2
+ from dataclasses import dataclass
3
+ from typing import Generic, List, Optional, Tuple, Union
4
+
5
+ import numpy as np
6
+ import supervision as sv
7
+ import torch
8
+
9
+ from inference_models.models.base.types import (
10
+ PreprocessedInputs,
11
+ PreprocessingMetadata,
12
+ RawPrediction,
13
+ )
14
+
15
+
16
+ @dataclass
17
+ class Detections:
18
+ xyxy: torch.Tensor # (n_boxes, 4)
19
+ class_id: torch.Tensor # (n_boxes, )
20
+ confidence: torch.Tensor # (n_boxes, )
21
+ image_metadata: Optional[dict] = None
22
+ bboxes_metadata: Optional[List[dict]] = (
23
+ None # if given, list of size equal to # of bboxes
24
+ )
25
+
26
+ def to_supervision(self) -> sv.Detections:
27
+ return sv.Detections(
28
+ xyxy=self.xyxy.cpu().numpy(),
29
+ class_id=self.class_id.cpu().numpy(),
30
+ confidence=self.confidence.cpu().numpy(),
31
+ )
32
+
33
+
34
+ class ObjectDetectionModel(
35
+ ABC, Generic[PreprocessedInputs, PreprocessingMetadata, RawPrediction]
36
+ ):
37
+
38
+ @classmethod
39
+ @abstractmethod
40
+ def from_pretrained(
41
+ cls, model_name_or_path: str, **kwargs
42
+ ) -> "ObjectDetectionModel":
43
+ pass
44
+
45
+ @property
46
+ @abstractmethod
47
+ def class_names(self) -> List[str]:
48
+ pass
49
+
50
+ def infer(
51
+ self,
52
+ images: Union[torch.Tensor, List[torch.Tensor], np.ndarray, List[np.ndarray]],
53
+ **kwargs,
54
+ ) -> List[Detections]:
55
+ pre_processed_images, pre_processing_meta = self.pre_process(images, **kwargs)
56
+ model_results = self.forward(pre_processed_images, **kwargs)
57
+ return self.post_process(model_results, pre_processing_meta, **kwargs)
58
+
59
+ @abstractmethod
60
+ def pre_process(
61
+ self,
62
+ images: Union[torch.Tensor, List[torch.Tensor], np.ndarray, List[np.ndarray]],
63
+ **kwargs,
64
+ ) -> Tuple[PreprocessedInputs, PreprocessingMetadata]:
65
+ pass
66
+
67
+ @abstractmethod
68
+ def forward(
69
+ self, pre_processed_images: PreprocessedInputs, **kwargs
70
+ ) -> RawPrediction:
71
+ pass
72
+
73
+ @abstractmethod
74
+ def post_process(
75
+ self,
76
+ model_results: RawPrediction,
77
+ pre_processing_meta: PreprocessingMetadata,
78
+ **kwargs,
79
+ ) -> List[Detections]:
80
+ pass
81
+
82
+ def __call__(
83
+ self,
84
+ images: Union[torch.Tensor, List[torch.Tensor], np.ndarray, List[np.ndarray]],
85
+ **kwargs,
86
+ ) -> List[Detections]:
87
+ return self.infer(images, **kwargs)
88
+
89
+
90
+ class OpenVocabularyObjectDetectionModel(
91
+ ABC, Generic[PreprocessedInputs, PreprocessingMetadata, RawPrediction]
92
+ ):
93
+
94
+ @classmethod
95
+ @abstractmethod
96
+ def from_pretrained(
97
+ cls, model_name_or_path: str, **kwargs
98
+ ) -> "OpenVocabularyObjectDetectionModel":
99
+ pass
100
+
101
+ def infer(
102
+ self,
103
+ images: Union[torch.Tensor, List[torch.Tensor], np.ndarray, List[np.ndarray]],
104
+ classes: Union[str, List[str]],
105
+ **kwargs,
106
+ ) -> List[Detections]:
107
+ pre_processed_images, pre_processing_meta = self.pre_process(images, **kwargs)
108
+ model_results = self.forward(pre_processed_images, classes, **kwargs)
109
+ return self.post_process(model_results, pre_processing_meta, **kwargs)
110
+
111
+ @abstractmethod
112
+ def pre_process(
113
+ self,
114
+ images: Union[torch.Tensor, List[torch.Tensor], np.ndarray, List[np.ndarray]],
115
+ **kwargs,
116
+ ) -> Tuple[PreprocessedInputs, PreprocessingMetadata]:
117
+ pass
118
+
119
+ @abstractmethod
120
+ def forward(
121
+ self,
122
+ pre_processed_images: PreprocessedInputs,
123
+ classes: List[str],
124
+ **kwargs,
125
+ ) -> RawPrediction:
126
+ pass
127
+
128
+ @abstractmethod
129
+ def post_process(
130
+ self,
131
+ model_results: RawPrediction,
132
+ pre_processing_meta: PreprocessingMetadata,
133
+ **kwargs,
134
+ ) -> List[Detections]:
135
+ pass
136
+
137
+ def __call__(
138
+ self,
139
+ images: Union[torch.Tensor, List[torch.Tensor], np.ndarray, List[np.ndarray]],
140
+ classes: List[str],
141
+ **kwargs,
142
+ ) -> List[Detections]:
143
+ return self.infer(images=images, classes=classes, **kwargs)
@@ -0,0 +1,74 @@
1
+ from abc import ABC, abstractmethod
2
+ from dataclasses import dataclass
3
+ from typing import Generic, List, Optional, Tuple, Union
4
+
5
+ import numpy as np
6
+ import supervision as sv
7
+ import torch
8
+
9
+ from inference_models.models.base.types import (
10
+ PreprocessedInputs,
11
+ PreprocessingMetadata,
12
+ RawPrediction,
13
+ )
14
+
15
+
16
+ @dataclass
17
+ class SemanticSegmentationResult:
18
+ segmentation_map: torch.Tensor
19
+ confidence: torch.Tensor
20
+ image_metadata: Optional[dict] = None
21
+
22
+
23
+ class SemanticSegmentationModel(
24
+ ABC, Generic[PreprocessedInputs, PreprocessingMetadata, RawPrediction]
25
+ ):
26
+
27
+ @classmethod
28
+ @abstractmethod
29
+ def from_pretrained(
30
+ cls, model_name_or_path: str, **kwargs
31
+ ) -> "SemanticSegmentationModel":
32
+ pass
33
+
34
+ @property
35
+ @abstractmethod
36
+ def class_names(self) -> List[str]:
37
+ pass
38
+
39
+ def infer(
40
+ self,
41
+ images: Union[torch.Tensor, List[torch.Tensor], np.ndarray, List[np.ndarray]],
42
+ **kwargs,
43
+ ) -> List[SemanticSegmentationResult]:
44
+ pre_processed_images, pre_processing_meta = self.pre_process(images, **kwargs)
45
+ model_results = self.forward(pre_processed_images, **kwargs)
46
+ return self.post_process(model_results, pre_processing_meta, **kwargs)
47
+
48
+ @abstractmethod
49
+ def pre_process(
50
+ self, images: Union[torch.Tensor, List[torch.Tensor]], **kwargs
51
+ ) -> PreprocessedInputs:
52
+ pass
53
+
54
+ @abstractmethod
55
+ def forward(
56
+ self, pre_processed_images: PreprocessedInputs, **kwargs
57
+ ) -> RawPrediction:
58
+ pass
59
+
60
+ @abstractmethod
61
+ def post_process(
62
+ self,
63
+ model_results: RawPrediction,
64
+ pre_processing_meta: PreprocessedInputs,
65
+ **kwargs,
66
+ ) -> List[SemanticSegmentationResult]:
67
+ pass
68
+
69
+ def __call__(
70
+ self,
71
+ images: Union[torch.Tensor, List[torch.Tensor], np.ndarray, List[np.ndarray]],
72
+ **kwargs,
73
+ ) -> List[SemanticSegmentationResult]:
74
+ return self.infer(images, **kwargs)
@@ -0,0 +1,5 @@
1
+ from typing import TypeVar
2
+
3
+ PreprocessedInputs = TypeVar("PreprocessedInputs")
4
+ PreprocessingMetadata = TypeVar("PreprocessingMetadata")
5
+ RawPrediction = TypeVar("RawPrediction")
File without changes