Skip to content

Commit 8f836dd

Browse files
fix: 🐞 fix incorrect type annotations in postprocess module (#1327)
1 parent 2790cd4 commit 8f836dd

2 files changed

Lines changed: 31 additions & 27 deletions

File tree

sahi/postprocess/combine.py

Lines changed: 15 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
from sahi.utils.import_utils import check_requirements
1111

1212

13-
def batched_nms(predictions: torch.tensor, match_metric: str = "IOU", match_threshold: float = 0.5):
13+
def batched_nms(predictions: torch.Tensor, match_metric: str = "IOU", match_threshold: float = 0.5) -> list[int]:
1414
"""Apply non-maximum suppression to avoid detecting too many overlapping bounding boxes for a given object.
1515
1616
Args:
@@ -40,7 +40,7 @@ def nms(
4040
predictions: torch.Tensor,
4141
match_metric: str = "IOU",
4242
match_threshold: float = 0.5,
43-
):
43+
) -> list[int]:
4444
"""
4545
Optimized non-maximum suppression for axis-aligned bounding boxes using STRTree.
4646
@@ -145,10 +145,10 @@ def nms(
145145

146146

147147
def batched_greedy_nmm(
148-
object_predictions_as_tensor: torch.tensor,
148+
object_predictions_as_tensor: torch.Tensor,
149149
match_metric: str = "IOU",
150150
match_threshold: float = 0.5,
151-
):
151+
) -> dict[int, list[int]]:
152152
"""Apply greedy version of non-maximum merging per category to avoid detecting too many overlapping bounding boxes
153153
for a given object.
154154
@@ -159,7 +159,7 @@ def batched_greedy_nmm(
159159
match_threshold: (float) The overlap thresh for
160160
match metric.
161161
Returns:
162-
keep_to_merge_list: (Dict[int:List[int]]) mapping from prediction indices
162+
keep_to_merge_list: (dict[int, list[int]]) mapping from prediction indices
163163
to keep to a list of prediction indices to be merged.
164164
"""
165165
category_ids = object_predictions_as_tensor[:, 5].squeeze()
@@ -179,7 +179,7 @@ def greedy_nmm(
179179
object_predictions_as_tensor: torch.Tensor,
180180
match_metric: str = "IOU",
181181
match_threshold: float = 0.5,
182-
):
182+
) -> dict[int, list[int]]:
183183
"""
184184
Optimized greedy non-maximum merging for axis-aligned bounding boxes using STRTree.
185185
@@ -290,7 +290,7 @@ def batched_nmm(
290290
object_predictions_as_tensor: torch.Tensor,
291291
match_metric: str = "IOU",
292292
match_threshold: float = 0.5,
293-
):
293+
) -> dict[int, list[int]]:
294294
"""Apply non-maximum merging per category to avoid detecting too many overlapping bounding boxes for a given object.
295295
296296
Args:
@@ -300,7 +300,7 @@ def batched_nmm(
300300
match_threshold: (float) The overlap thresh for
301301
match metric.
302302
Returns:
303-
keep_to_merge_list: (Dict[int:List[int]]) mapping from prediction indices
303+
keep_to_merge_list: (dict[int, list[int]]) mapping from prediction indices
304304
to keep to a list of prediction indices to be merged.
305305
"""
306306
category_ids = object_predictions_as_tensor[:, 5].squeeze()
@@ -320,7 +320,7 @@ def nmm(
320320
object_predictions_as_tensor: torch.Tensor,
321321
match_metric: str = "IOU",
322322
match_threshold: float = 0.5,
323-
):
323+
) -> dict[int, list[int]]:
324324
"""Apply non-maximum merging to avoid detecting too many overlapping bounding boxes for a given object.
325325
326326
Args:
@@ -329,7 +329,7 @@ def nmm(
329329
match_metric: (str) IOU or IOS
330330
match_threshold: (float) The overlap thresh for match metric.
331331
Returns:
332-
keep_to_merge_list: (Dict[int:List[int]]) mapping from prediction indices
332+
keep_to_merge_list: (dict[int, list[int]]) mapping from prediction indices
333333
to keep to a list of prediction indices to be merged.
334334
"""
335335
# Extract coordinates and scores as tensors
@@ -460,15 +460,15 @@ def __init__(
460460

461461
check_requirements(["torch"])
462462

463-
def __call__(self, predictions: list[ObjectPrediction]):
463+
def __call__(self, predictions: list[ObjectPrediction]) -> list[ObjectPrediction]:
464464
raise NotImplementedError()
465465

466466

467467
class NMSPostprocess(PostprocessPredictions):
468468
def __call__(
469469
self,
470470
object_predictions: list[ObjectPrediction],
471-
):
471+
) -> list[ObjectPrediction]:
472472
object_prediction_list = ObjectPredictionList(object_predictions)
473473
object_predictions_as_torch = object_prediction_list.totensor()
474474
if self.class_agnostic:
@@ -491,7 +491,7 @@ class NMMPostprocess(PostprocessPredictions):
491491
def __call__(
492492
self,
493493
object_predictions: list[ObjectPrediction],
494-
):
494+
) -> list[ObjectPrediction]:
495495
object_prediction_list = ObjectPredictionList(object_predictions)
496496
object_predictions_as_torch = object_prediction_list.totensor()
497497
if self.class_agnostic:
@@ -528,7 +528,7 @@ class GreedyNMMPostprocess(PostprocessPredictions):
528528
def __call__(
529529
self,
530530
object_predictions: list[ObjectPrediction],
531-
):
531+
) -> list[ObjectPrediction]:
532532
object_prediction_list = ObjectPredictionList(object_predictions)
533533
object_predictions_as_torch = object_prediction_list.totensor()
534534
if self.class_agnostic:
@@ -566,7 +566,7 @@ class LSNMSPostprocess(PostprocessPredictions):
566566
def __call__(
567567
self,
568568
object_predictions: list[ObjectPrediction],
569-
):
569+
) -> list[ObjectPrediction]:
570570
try:
571571
from lsnms import nms
572572
except ModuleNotFoundError:

sahi/postprocess/utils.py

Lines changed: 16 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -13,11 +13,11 @@
1313

1414

1515
class ObjectPredictionList(Sequence):
16-
def __init__(self, list):
17-
self.list = list
16+
def __init__(self, prediction_list: list[ObjectPrediction]) -> None:
17+
self.list: list[ObjectPrediction] = prediction_list
1818
super().__init__()
1919

20-
def __getitem__(self, i):
20+
def __getitem__(self, i: int | tuple | list | torch.Tensor | np.ndarray) -> ObjectPredictionList:
2121
if torch.is_tensor(i) or isinstance(i, np.ndarray):
2222
i = i.tolist()
2323
if isinstance(i, int):
@@ -28,7 +28,11 @@ def __getitem__(self, i):
2828
else:
2929
raise NotImplementedError(f"{type(i)}")
3030

31-
def __setitem__(self, i, elem):
31+
def __setitem__(
32+
self,
33+
i: int | tuple | list | torch.Tensor | np.ndarray,
34+
elem: ObjectPredictionList | ObjectPrediction | list[ObjectPrediction],
35+
) -> None:
3236
if torch.is_tensor(i) or isinstance(i, np.ndarray):
3337
i = i.tolist()
3438
if isinstance(i, int):
@@ -45,22 +49,22 @@ def __setitem__(self, i, elem):
4549
else:
4650
raise NotImplementedError(f"{type(i)}")
4751

48-
def __len__(self):
52+
def __len__(self) -> int:
4953
return len(self.list)
5054

51-
def __str__(self):
55+
def __str__(self) -> str:
5256
return str(self.list)
5357

54-
def extend(self, object_prediction_list):
58+
def extend(self, object_prediction_list: ObjectPredictionList) -> None:
5559
self.list.extend(object_prediction_list.list)
5660

57-
def totensor(self):
61+
def totensor(self) -> torch.Tensor:
5862
return object_prediction_list_to_torch(self)
5963

60-
def tonumpy(self):
64+
def tonumpy(self) -> np.ndarray:
6165
return object_prediction_list_to_numpy(self)
6266

63-
def tolist(self):
67+
def tolist(self) -> ObjectPrediction | list[ObjectPrediction]:
6468
if len(self.list) == 1:
6569
return self.list[0]
6670
else:
@@ -100,7 +104,7 @@ def repair_multipolygon(shapely_multipolygon: MultiPolygon) -> MultiPolygon:
100104
return shapely_multipolygon
101105

102106

103-
def coco_segmentation_to_shapely(segmentation: list | list[list]):
107+
def coco_segmentation_to_shapely(segmentation: list | list[list]) -> MultiPolygon:
104108
"""Fix segment data in COCO format :param segmentation: segment data in COCO format :return:"""
105109
if isinstance(segmentation, list) and all([not isinstance(seg, list) for seg in segmentation]):
106110
segmentation = [segmentation]
@@ -120,7 +124,7 @@ def coco_segmentation_to_shapely(segmentation: list | list[list]):
120124
return shapely_multipolygon
121125

122126

123-
def object_prediction_list_to_torch(object_prediction_list: ObjectPredictionList) -> torch.tensor:
127+
def object_prediction_list_to_torch(object_prediction_list: ObjectPredictionList) -> torch.Tensor:
124128
"""
125129
Returns:
126130
torch.tensor of size N x [x1, y1, x2, y2, score, category_id]

0 commit comments

Comments
 (0)