MCPcopy Create free account
hub / github.com/Project-MONAI/MONAI / encode_boxes

Function encode_boxes

monai/apps/detection/utils/box_coder.py:64–102  ·  view source on GitHub ↗

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)

Source from the content-addressed store, hash-verified

62
63
64def 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
105class BoxCoder:

Callers 1

encode_singleMethod · 0.85

Calls 3

look_up_optionFunction · 0.90
is_valid_box_valuesFunction · 0.90
convert_box_modeFunction · 0.90

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…