From 8fc3559d6c069f83f2da6387bca761edfac7e65d Mon Sep 17 00:00:00 2001 From: franksim Date: Fri, 7 Nov 2025 11:15:51 +0100 Subject: [PATCH] impoves code --- mmp/a4/anchor_grid.py | 19 +++++++++---------- mmp/a4/label_grid.py | 6 +----- 2 files changed, 10 insertions(+), 15 deletions(-) diff --git a/mmp/a4/anchor_grid.py b/mmp/a4/anchor_grid.py index 0511d16..42b92ce 100644 --- a/mmp/a4/anchor_grid.py +++ b/mmp/a4/anchor_grid.py @@ -10,21 +10,20 @@ def get_anchor_grid( aspect_ratios: Sequence[float], ) -> np.ndarray: anchor_grid = np.empty( - [len(width), len(ratio), num_rows, num_cols, 4], dtype=int) - for width_idx, width in enumerate(anchor_widths): - for ratio_idx, ratio in enumerate(aspect_ratios): - for row in range(num_rows): - for col in range(num_cols): - anchor_point = ( - col * scale_factor + scale_factor/2, row * scale_factor + scale_factor / 2) - anchor_grid[width_idx, ratio_idx, row, col] = get_box( - width, ratio, anchor_point) + [len(anchor_widths), len(aspect_ratios), num_rows, num_cols, 4], dtype=float) + for (width_idx, ratio_idx, row, col) in np.ndindex(anchor_grid.shape[:-1]): + anchor_point = ( + col * scale_factor + scale_factor / 2, row * scale_factor + scale_factor / 2) + width = anchor_widths[width_idx] + ratio = aspect_ratios[ratio_idx] + anchor_grid[width_idx, ratio_idx, row, col] = get_box( + width, ratio, anchor_point) return anchor_grid def get_box(width: float, ratio: float, anchor_point: tuple[float, float]) -> np.ndarray: - box = np.empty(4) + box = np.empty(4, dtype=float) box[0] = anchor_point[0] - (width / 2) box[1] = anchor_point[1] - (width * ratio / 2) box[2] = anchor_point[0] + (width / 2) diff --git a/mmp/a4/label_grid.py b/mmp/a4/label_grid.py index e29e283..70c748e 100644 --- a/mmp/a4/label_grid.py +++ b/mmp/a4/label_grid.py @@ -10,11 +10,7 @@ def iou(rect1: AnnotationRect, rect2: AnnotationRect) -> float: x_right = min(rect1.x2, rect2.x2) y_bottom = min(rect1.y2, rect2.y2) - # Returns 0 if no overlap - if x_right <= x_left or y_bottom <= y_top: - return 0.0 - - intersection_area = (x_right - x_left) * (y_bottom - y_top) + intersection_area = max(x_right - x_left, 0) * max(y_bottom - y_top, 0) rect1_area = rect1.area() rect2_area = rect2.area()