From 1029ef1c6b7a45720045153c9e1859206d57f682 Mon Sep 17 00:00:00 2001 From: anyaprice Date: Tue, 6 Apr 2021 16:06:36 -0500 Subject: [PATCH 1/4] Added function to possibly fix tiling error in find_targets.py --- hawk_eye/inference/find_targets.py | 32 ++++++++++++++++++++++++++++++ 1 file changed, 32 insertions(+) diff --git a/hawk_eye/inference/find_targets.py b/hawk_eye/inference/find_targets.py index 17ae2cad..887c7814 100755 --- a/hawk_eye/inference/find_targets.py +++ b/hawk_eye/inference/find_targets.py @@ -294,6 +294,38 @@ def globalize_boxes( return final_targets +def resolve_boxes( + results: List[postprocess.BoundingBox], img_size: int +) -> List[inference_types.Target]: + + """Removes targets that are within 5 pixels of the edge of the tile.""" + + img_size = torch.Tensor([img_size] * 4) + + for coords, bboxes in results: + for box in bboxes: + relative_coords = box.box * img_size + if ( + int(relative_coords[1]) - 5 <= 0 + or int(relative_coords[3]) + 5 >= 512 + or int(relative_coords[0]) - 5 <= 0 + or int(relative_coords[2]) + 5 >= 512 + ): + final_targets.remove( + inference_types.Target( + x=int(relative_coords[0]), + y=int(relative_coords[1]), + width=int(relative_coords[2] - relative_coords[0]), + height=int(relative_coords[3] - relative_coords[1]), + shape=inference_types.Shape[ + config.OD_CLASSES[box.class_id].upper().replace("-", "_") + ], + ) + ) + + return final_targets + + def visualize_image( image_name: str, image: np.ndarray, From 3df9e9f8f457399fefcaa2f21920ddfc44ac8e55 Mon Sep 17 00:00:00 2001 From: alexwitt23 Date: Thu, 8 Apr 2021 19:42:04 -0500 Subject: [PATCH 2/4] add doctest --- hawk_eye/inference/find_targets.py | 15 ++++++++++----- 1 file changed, 10 insertions(+), 5 deletions(-) diff --git a/hawk_eye/inference/find_targets.py b/hawk_eye/inference/find_targets.py index 0b45c594..aa7b80e6 100755 --- a/hawk_eye/inference/find_targets.py +++ b/hawk_eye/inference/find_targets.py @@ -266,7 +266,7 @@ def globalize_boxes( Args: results: A list of the detections for the tiles. - img_size: The size of the tile whihc is needed to unnormalize the detections. + img_size: The size of the tile which is needed to unnormalize the detections. Returns: A list of the globalized boxes @@ -294,11 +294,16 @@ def globalize_boxes( return final_targets -def resolve_boxes( - results: List[postprocess.BoundingBox], img_size: int -) -> List[inference_types.Target]: +def resolve_boxes(bbox: torch.Tensor, tile_size: int, exclusion_region: int) -> bool: + """Removes targets that are within 5 pixels of the edge of the tile. - """Removes targets that are within 5 pixels of the edge of the tile.""" + Examples:: + + >>> resolve_boxes(torch.Tensor([0, 10, 60, 70]), 512, 5) + False + >>> resolve_boxes(torch.Tensor([10, 10, 60, 70]), 512, 5) + True + """ img_size = torch.Tensor([img_size] * 4) From 9ac418cef1a69534c1330b429b181529c3c39112 Mon Sep 17 00:00:00 2001 From: anyaprice Date: Fri, 16 Apr 2021 17:14:54 -0500 Subject: [PATCH 3/4] Final code with working exclusion region function to remove a target if it falls within the exclusion region around a tile. --- hawk_eye/inference/find_targets.py | 70 +++++++++++++----------------- 1 file changed, 29 insertions(+), 41 deletions(-) diff --git a/hawk_eye/inference/find_targets.py b/hawk_eye/inference/find_targets.py index aa7b80e6..e5822a64 100755 --- a/hawk_eye/inference/find_targets.py +++ b/hawk_eye/inference/find_targets.py @@ -259,76 +259,64 @@ def find_targets( def globalize_boxes( - results: List[postprocess.BoundingBox], img_size: int + results: List[postprocess.BoundingBox], tile_size: int, exclusion_region: int = 5 ) -> List[inference_types.Target]: """Take the normalized detections on a _tile_ and gloabalize them to pixel space of the original large image. Args: results: A list of the detections for the tiles. - img_size: The size of the tile which is needed to unnormalize the detections. + tile_size: The size of the tile which is needed to unnormalize the detections. + exclusion_region: The number of pixels from the edge of the tile in which a target will be thrown out. Returns: A list of the globalized boxes """ final_targets = [] - img_size = torch.Tensor([img_size] * 4) + img_size = torch.Tensor([tile_size] * 4) for coords, bboxes in results: for box in bboxes: relative_coords = box.box * img_size - relative_coords += torch.Tensor(2 * list(coords)).int() - final_targets.append( - inference_types.Target( - x=int(relative_coords[0]), - y=int(relative_coords[1]), - width=int(relative_coords[2] - relative_coords[0]), - height=int(relative_coords[3] - relative_coords[1]), - shape=inference_types.Shape[ - config.OD_CLASSES[box.class_id].upper().replace("-", "_") - ], + if resolve_boxes(relative_coords, tile_size, exclusion_region): + relative_coords += torch.Tensor(2 * list(coords)).int() + final_targets.append( + inference_types.Target( + x=int(relative_coords[0]), + y=int(relative_coords[1]), + width=int(relative_coords[2] - relative_coords[0]), + height=int(relative_coords[3] - relative_coords[1]), + shape=inference_types.Shape[ + config.OD_CLASSES[box.class_id].upper().replace("-", "_") + ], + ) ) - ) return final_targets -def resolve_boxes(bbox: torch.Tensor, tile_size: int, exclusion_region: int) -> bool: - """Removes targets that are within 5 pixels of the edge of the tile. +def resolve_boxes(relative_coords, tile_size: int, exclusion_region: int) -> bool: + """Finds targets that are within a boundary at the edge of the tile. + If true, the target is within the boundary. Examples:: >>> resolve_boxes(torch.Tensor([0, 10, 60, 70]), 512, 5) - False - >>> resolve_boxes(torch.Tensor([10, 10, 60, 70]), 512, 5) True + >>> resolve_boxes(torch.Tensor([10, 10, 60, 70]), 512, 5) + False """ - img_size = torch.Tensor([img_size] * 4) + if ( + int(relative_coords[1]) - exclusion_region <= 0 + or int(relative_coords[3]) + exclusion_region >= tile_size + or int(relative_coords[0]) - exclusion_region <= 0 + or int(relative_coords[2]) + exclusion_region >= tile_size + ): + return True - for coords, bboxes in results: - for box in bboxes: - relative_coords = box.box * img_size - if ( - int(relative_coords[1]) - 5 <= 0 - or int(relative_coords[3]) + 5 >= 512 - or int(relative_coords[0]) - 5 <= 0 - or int(relative_coords[2]) + 5 >= 512 - ): - final_targets.remove( - inference_types.Target( - x=int(relative_coords[0]), - y=int(relative_coords[1]), - width=int(relative_coords[2] - relative_coords[0]), - height=int(relative_coords[3] - relative_coords[1]), - shape=inference_types.Shape[ - config.OD_CLASSES[box.class_id].upper().replace("-", "_") - ], - ) - ) - - return final_targets + return False def visualize_image( From 2104f421a00f5f65f42d890773db7bc85ed02ddd Mon Sep 17 00:00:00 2001 From: anyaprice Date: Sun, 16 May 2021 11:07:15 -0500 Subject: [PATCH 4/4] Fixed style test error --- hawk_eye/inference/find_targets.py | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/hawk_eye/inference/find_targets.py b/hawk_eye/inference/find_targets.py index e5822a64..93dbb8a0 100755 --- a/hawk_eye/inference/find_targets.py +++ b/hawk_eye/inference/find_targets.py @@ -267,7 +267,8 @@ def globalize_boxes( Args: results: A list of the detections for the tiles. tile_size: The size of the tile which is needed to unnormalize the detections. - exclusion_region: The number of pixels from the edge of the tile in which a target will be thrown out. + exclusion_region: The number of pixels from the edge of the tile in which + a target will be thrown out. Returns: A list of the globalized boxes