Encode a set of proposals with respect to some reference ground truth (gt) boxes. Args: gt_boxes: gt boxes, Nx4 or Nx6 torch tensor. The box mode is assumed to be ``StandardMode`` proposals: boxes to be encoded, Nx4 or Nx6 torch tensor. The box mode is assumed to be ``Stand
(gt_boxes: Tensor, proposals: Tensor, weights: Tensor)
| 62 | |
| 63 | |
| 64 | def encode_boxes(gt_boxes: Tensor, proposals: Tensor, weights: Tensor) -> Tensor: |
| 65 | """ |
| 66 | Encode a set of proposals with respect to some reference ground truth (gt) boxes. |
| 67 | |
| 68 | Args: |
| 69 | gt_boxes: gt boxes, Nx4 or Nx6 torch tensor. The box mode is assumed to be ``StandardMode`` |
| 70 | proposals: boxes to be encoded, Nx4 or Nx6 torch tensor. The box mode is assumed to be ``StandardMode`` |
| 71 | weights: the weights for ``(cx, cy, w, h) or (cx,cy,cz, w,h,d)`` |
| 72 | |
| 73 | Return: |
| 74 | encoded gt, target of box regression that is used to convert proposals into gt_boxes, Nx4 or Nx6 torch tensor. |
| 75 | """ |
| 76 | |
| 77 | if gt_boxes.shape[0] != proposals.shape[0]: |
| 78 | raise ValueError("gt_boxes.shape[0] should be equal to proposals.shape[0].") |
| 79 | spatial_dims = look_up_option(len(weights), [4, 6]) // 2 |
| 80 | |
| 81 | if not is_valid_box_values(gt_boxes): |
| 82 | raise ValueError("gt_boxes is not valid. Please check if it contains empty boxes.") |
| 83 | if not is_valid_box_values(proposals): |
| 84 | raise ValueError("proposals is not valid. Please check if it contains empty boxes.") |
| 85 | |
| 86 | # implementation starts here |
| 87 | ex_cccwhd: Tensor = convert_box_mode(proposals, src_mode=StandardMode, dst_mode=CenterSizeMode) # type: ignore |
| 88 | gt_cccwhd: Tensor = convert_box_mode(gt_boxes, src_mode=StandardMode, dst_mode=CenterSizeMode) # type: ignore |
| 89 | targets_dxyz = ( |
| 90 | weights[:spatial_dims].unsqueeze(0) |
| 91 | * (gt_cccwhd[:, :spatial_dims] - ex_cccwhd[:, :spatial_dims]) |
| 92 | / ex_cccwhd[:, spatial_dims:] |
| 93 | ) |
| 94 | targets_dwhd = weights[spatial_dims:].unsqueeze(0) * torch.log( |
| 95 | gt_cccwhd[:, spatial_dims:] / ex_cccwhd[:, spatial_dims:] |
| 96 | ) |
| 97 | |
| 98 | targets = torch.cat((targets_dxyz, targets_dwhd), dim=1) |
| 99 | # torch.log may cause NaN or Inf |
| 100 | if torch.isnan(targets).any() or torch.isinf(targets).any(): |
| 101 | raise ValueError("targets is NaN or Inf.") |
| 102 | return targets |
| 103 | |
| 104 | |
| 105 | class BoxCoder: |
no test coverage detected
searching dependent graphs…