Resize the output instances. The input images are often resized when entering an object detector. As a result, we often need the outputs of the detector in a different resolution from its inputs. This function will resize the raw outputs of an R-CNN detector to produce outp
(
results: Instances, output_height: int, output_width: int, mask_threshold: float = 0.5
)
| 7 | |
| 8 | # perhaps should rename to "resize_instance" |
| 9 | def detector_postprocess( |
| 10 | results: Instances, output_height: int, output_width: int, mask_threshold: float = 0.5 |
| 11 | ): |
| 12 | """ |
| 13 | Resize the output instances. |
| 14 | The input images are often resized when entering an object detector. |
| 15 | As a result, we often need the outputs of the detector in a different |
| 16 | resolution from its inputs. |
| 17 | |
| 18 | This function will resize the raw outputs of an R-CNN detector |
| 19 | to produce outputs according to the desired output resolution. |
| 20 | |
| 21 | Args: |
| 22 | results (Instances): the raw outputs from the detector. |
| 23 | `results.image_size` contains the input image resolution the detector sees. |
| 24 | This object might be modified in-place. |
| 25 | output_height, output_width: the desired output resolution. |
| 26 | |
| 27 | Returns: |
| 28 | Instances: the resized output from the model, based on the output resolution |
| 29 | """ |
| 30 | if isinstance(output_width, torch.Tensor): |
| 31 | # This shape might (but not necessarily) be tensors during tracing. |
| 32 | # Converts integer tensors to float temporaries to ensure true |
| 33 | # division is performed when computing scale_x and scale_y. |
| 34 | output_width_tmp = output_width.float() |
| 35 | output_height_tmp = output_height.float() |
| 36 | new_size = torch.stack([output_height, output_width]) |
| 37 | else: |
| 38 | new_size = (output_height, output_width) |
| 39 | output_width_tmp = output_width |
| 40 | output_height_tmp = output_height |
| 41 | |
| 42 | scale_x, scale_y = ( |
| 43 | output_width_tmp / results.image_size[1], |
| 44 | output_height_tmp / results.image_size[0], |
| 45 | ) |
| 46 | results = Instances(new_size, **results.get_fields()) |
| 47 | |
| 48 | if results.has("pred_boxes"): |
| 49 | output_boxes = results.pred_boxes |
| 50 | elif results.has("proposal_boxes"): |
| 51 | output_boxes = results.proposal_boxes |
| 52 | else: |
| 53 | output_boxes = None |
| 54 | assert output_boxes is not None, "Predictions must contain boxes!" |
| 55 | |
| 56 | output_boxes.scale(scale_x, scale_y) |
| 57 | output_boxes.clip(results.image_size) |
| 58 | |
| 59 | results = results[output_boxes.nonempty()] |
| 60 | |
| 61 | if results.has("pred_masks"): |
| 62 | if isinstance(results.pred_masks, ROIMasks): |
| 63 | roi_masks = results.pred_masks |
| 64 | else: |
| 65 | # pred_masks is a tensor of shape (N, 1, M, M) |
| 66 | roi_masks = ROIMasks(results.pred_masks[:, 0, :, :]) |
nothing calls this directly
no outgoing calls
no test coverage detected