Skip to content

Commit

Permalink
chore(powerboxesrs): stop using useless epsilon value (#56)
Browse files Browse the repository at this point in the history
  • Loading branch information
Smirkey committed May 4, 2024
1 parent 3d615a6 commit f9e08fd
Show file tree
Hide file tree
Showing 7 changed files with 20 additions and 21 deletions.
18 changes: 9 additions & 9 deletions bindings/python/powerboxes/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -136,7 +136,7 @@ def parallel_iou_distance(
raise TypeError(_BOXES_NOT_NP_ARRAY)
if boxes1.dtype == boxes2.dtype:
try:
return _dtype_to_func_parallel_iou_distance[boxes1.dtype](boxes1, boxes2)
return _dtype_to_func_parallel_iou_distance[boxes1.dtype](boxes1, boxes2) # type: ignore
except KeyError:
raise TypeError(
f"Box dtype: {boxes1.dtype} not in supported dtypes {supported_dtypes}"
Expand Down Expand Up @@ -167,7 +167,7 @@ def parallel_giou_distance(
raise TypeError(_BOXES_NOT_NP_ARRAY)
if boxes1.dtype == boxes2.dtype:
try:
return _dtype_to_func_parallel_giou_distance[boxes1.dtype](boxes1, boxes2)
return _dtype_to_func_parallel_giou_distance[boxes1.dtype](boxes1, boxes2) # type: ignore
except KeyError:
raise TypeError(
f"Box dtype: {boxes1.dtype} not in supported dtypes {supported_dtypes}"
Expand Down Expand Up @@ -198,7 +198,7 @@ def giou_distance(
raise TypeError(_BOXES_NOT_NP_ARRAY)
if boxes1.dtype == boxes2.dtype:
try:
return _dtype_to_func_giou_distance[boxes1.dtype](boxes1, boxes2)
return _dtype_to_func_giou_distance[boxes1.dtype](boxes1, boxes2) # type: ignore
except KeyError:
raise TypeError(
f"Box dtype: {boxes1.dtype} not in supported dtypes {supported_dtypes}"
Expand Down Expand Up @@ -229,7 +229,7 @@ def tiou_distance(
raise TypeError(_BOXES_NOT_NP_ARRAY)
if boxes1.dtype == boxes2.dtype:
try:
return _dtype_to_func_tiou_distance[boxes1.dtype](boxes1, boxes2)
return _dtype_to_func_tiou_distance[boxes1.dtype](boxes1, boxes2) # type: ignore
except KeyError:
raise TypeError(
f"Box dtype: {boxes1.dtype} not in supported dtypes {supported_dtypes}"
Expand Down Expand Up @@ -346,7 +346,7 @@ def remove_small_boxes(boxes: npt.NDArray[T], min_size: float) -> npt.NDArray[T]
if not isinstance(boxes, np.ndarray):
raise TypeError(_BOXES_NOT_NP_ARRAY)
try:
return _dtype_to_func_remove_small_boxes[boxes.dtype](boxes, min_size)
return _dtype_to_func_remove_small_boxes[boxes.dtype](boxes, min_size) # type: ignore
except KeyError:
raise TypeError(
f"Box dtype: {boxes.dtype} not in supported dtypes {supported_dtypes}"
Expand All @@ -365,7 +365,7 @@ def boxes_areas(boxes: npt.NDArray[T]) -> npt.NDArray[np.float64]:
if not isinstance(boxes, np.ndarray):
raise TypeError(_BOXES_NOT_NP_ARRAY)
try:
return _dtype_to_func_box_areas[boxes.dtype](boxes)
return _dtype_to_func_box_areas[boxes.dtype](boxes) # type: ignore
except KeyError:
raise TypeError(
f"Box dtype: {boxes.dtype} not in supported dtypes {supported_dtypes}"
Expand All @@ -391,7 +391,7 @@ def box_convert(boxes: npt.NDArray[T], in_fmt: str, out_fmt: str) -> npt.NDArray
if not isinstance(boxes, np.ndarray):
raise TypeError(_BOXES_NOT_NP_ARRAY)
try:
return _dtype_to_func_box_convert[boxes.dtype](boxes, in_fmt, out_fmt)
return _dtype_to_func_box_convert[boxes.dtype](boxes, in_fmt, out_fmt) # type: ignore
except KeyError:
raise TypeError(
f"Box dtype: {boxes.dtype} not in supported dtypes {supported_dtypes}"
Expand Down Expand Up @@ -438,7 +438,7 @@ def nms(
if not isinstance(boxes, np.ndarray) or not isinstance(scores, np.ndarray):
raise TypeError("Boxes and scores must be numpy arrays")
try:
return _dtype_to_func_nms[boxes.dtype](
return _dtype_to_func_nms[boxes.dtype]( # type: ignore
boxes, scores, iou_threshold, score_threshold
)
except KeyError:
Expand Down Expand Up @@ -473,7 +473,7 @@ def rtree_nms(
if not isinstance(boxes, np.ndarray) or not isinstance(scores, np.ndarray):
raise TypeError("Boxes and scores must be numpy arrays")
try:
return _dtype_to_func_rtree_nms[boxes.dtype](
return _dtype_to_func_rtree_nms[boxes.dtype]( # type: ignore
boxes, scores, iou_threshold, score_threshold
)
except KeyError:
Expand Down
5 changes: 3 additions & 2 deletions bindings/tests/test_boxes.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
from powerboxes import masks_to_boxes
import numpy as np
import os

import numpy as np
from PIL import Image
from powerboxes import masks_to_boxes


def test_masks_box():
Expand Down
2 changes: 1 addition & 1 deletion powerboxesrs/src/boxes.rs
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ pub enum BoxFormat {
/// ```
pub fn box_areas<'a, N, BA>(boxes: BA) -> Array1<f64>
where
N: Num + PartialEq + PartialOrd + ToPrimitive + Copy + 'a,
N: Num + PartialEq + ToPrimitive + Copy + 'a,
BA: Into<ArrayView2<'a, N>>,
{
let boxes = boxes.into();
Expand Down
2 changes: 1 addition & 1 deletion powerboxesrs/src/diou.rs
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ where
let intersection = (x2 - x1) * (y2 - y1);
let intersection = intersection.to_f64().unwrap();
let intersection = utils::min(intersection, utils::min(area1, area2));
let iou = intersection / (area1 + area2 - intersection + utils::EPS);
let iou = intersection / (area1 + area2 - intersection);

let center_box1 = [(a1_x1 + a1_x2) / two, (a1_y1 + a1_y2) / two];
let center_box2 = [(a2_x1 + a2_x2) / two, (a2_y1 + a2_y2) / two];
Expand Down
6 changes: 3 additions & 3 deletions powerboxesrs/src/giou.rs
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ where
let intersection = (x2 - x1) * (y2 - y1);
let intersection = intersection.to_f64().unwrap();
let intersection = utils::min(intersection, utils::min(area1, area2));
let union = area1 + area2 - intersection + utils::EPS;
let union = area1 + area2 - intersection;
(intersection / union, union)
};
// Calculate the enclosing box (C) coordinates
Expand Down Expand Up @@ -152,7 +152,7 @@ where
let intersection = (x2 - x1) * (y2 - y1);
let intersection = intersection.to_f64().unwrap();
let intersection = utils::min(intersection, utils::min(area1, area2));
let union = area1 + area2 - intersection + utils::EPS;
let union = area1 + area2 - intersection;
(intersection / union, union)
};
// Calculate the enclosing box (C) coordinates
Expand Down Expand Up @@ -254,7 +254,7 @@ where
let rect1 = boxes1_rects[box1.index];
let rect2 = boxes2_rects[box2.index];
let intersection = intersection_area(&rect1, &rect2);
let union = area1 + area2 - intersection + utils::EPS;
let union = area1 + area2 - intersection;
// Calculate the enclosing box (C) coordinates
let c_x1 = utils::min(box1.x1, box2.x1);
let c_y1 = utils::min(box1.y1, box2.y1);
Expand Down
7 changes: 3 additions & 4 deletions powerboxesrs/src/iou.rs
Original file line number Diff line number Diff line change
Expand Up @@ -66,8 +66,7 @@ where
let intersection = (x2 - x1) * (y2 - y1);
let intersection = intersection.to_f64().unwrap();
let intersection = utils::min(intersection, utils::min(area1, area2));
iou_matrix[[i, j]] =
utils::ONE - (intersection / (area1 + area2 - intersection + utils::EPS));
iou_matrix[[i, j]] = utils::ONE - (intersection / (area1 + area2 - intersection));
}
}

Expand Down Expand Up @@ -137,7 +136,7 @@ where
let intersection = (x2 - x1) * (y2 - y1);
let intersection = intersection.to_f64().unwrap();
let intersection = utils::min(intersection, utils::min(area1, area2));
*d = 1. - (intersection / (area1 + area2 - intersection + utils::EPS));
*d = 1. - (intersection / (area1 + area2 - intersection));
}
});
});
Expand Down Expand Up @@ -217,7 +216,7 @@ where
let area1 = areas1[box1.index];
let area2 = areas2[box2.index];
let intersection = intersection_area(&boxes1_rects[box1.index], &boxes2_rects[box2.index]);
let union = area1 + area2 - intersection + utils::EPS;
let union = area1 + area2 - intersection;
iou_matrix[[box1.index, box2.index]] = utils::ONE - intersection / union;
}

Expand Down
1 change: 0 additions & 1 deletion powerboxesrs/src/utils.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
use num_traits::{Num, ToPrimitive};
use rstar::{RStarInsertionStrategy, RTreeNum, RTreeObject, RTreeParams, AABB};

pub const EPS: f64 = 1e-16;
pub const ONE: f64 = 1.0;
pub const ZERO: f64 = 0.0;

Expand Down

0 comments on commit f9e08fd

Please sign in to comment.