Calculate Chamfer Distance of two sets. Args: mode (str): Criterion mode to calculate distance. The valid modes are 'smooth_l1', 'l1' or 'l2'. Defaults to 'l2'. reduction (str): Method to reduce losses. The valid reduction method are 'none', 'sum' or 'mea
| 81 | |
| 82 | @MODELS.register_module() |
| 83 | class ChamferDistance(nn.Module): |
| 84 | """Calculate Chamfer Distance of two sets. |
| 85 | |
| 86 | Args: |
| 87 | mode (str): Criterion mode to calculate distance. |
| 88 | The valid modes are 'smooth_l1', 'l1' or 'l2'. Defaults to 'l2'. |
| 89 | reduction (str): Method to reduce losses. |
| 90 | The valid reduction method are 'none', 'sum' or 'mean'. |
| 91 | Defaults to 'mean'. |
| 92 | loss_src_weight (float): Weight of loss_source. Defaults to l.0. |
| 93 | loss_dst_weight (float): Weight of loss_target. Defaults to 1.0. |
| 94 | """ |
| 95 | |
| 96 | def __init__(self, |
| 97 | mode: str = 'l2', |
| 98 | reduction: str = 'mean', |
| 99 | loss_src_weight: float = 1.0, |
| 100 | loss_dst_weight: float = 1.0) -> None: |
| 101 | super(ChamferDistance, self).__init__() |
| 102 | |
| 103 | assert mode in ['smooth_l1', 'l1', 'l2'] |
| 104 | assert reduction in ['none', 'sum', 'mean'] |
| 105 | self.mode = mode |
| 106 | self.reduction = reduction |
| 107 | self.loss_src_weight = loss_src_weight |
| 108 | self.loss_dst_weight = loss_dst_weight |
| 109 | |
| 110 | def forward( |
| 111 | self, |
| 112 | source: Tensor, |
| 113 | target: Tensor, |
| 114 | src_weight: Union[Tensor, float] = 1.0, |
| 115 | dst_weight: Union[Tensor, float] = 1.0, |
| 116 | reduction_override: Optional[str] = None, |
| 117 | return_indices: bool = False, |
| 118 | **kwargs |
| 119 | ) -> Union[Tuple[Tensor, Tensor, Tensor, Tensor], Tuple[Tensor, Tensor]]: |
| 120 | """Forward function of loss calculation. |
| 121 | |
| 122 | Args: |
| 123 | source (Tensor): Source set with shape [B, N, C] to |
| 124 | calculate Chamfer Distance. |
| 125 | target (Tensor): Destination set with shape [B, M, C] to |
| 126 | calculate Chamfer Distance. |
| 127 | src_weight (Tensor | float): |
| 128 | Weight of source loss. Defaults to 1.0. |
| 129 | dst_weight (Tensor | float): |
| 130 | Weight of destination loss. Defaults to 1.0. |
| 131 | reduction_override (str, optional): Method to reduce losses. |
| 132 | The valid reduction method are 'none', 'sum' or 'mean'. |
| 133 | Defaults to None. |
| 134 | return_indices (bool): Whether to return indices. |
| 135 | Defaults to False. |
| 136 | |
| 137 | Returns: |
| 138 | tuple[Tensor]: If ``return_indices=True``, return losses of |
| 139 | source and target with their corresponding indices in the |
| 140 | order of ``(loss_source, loss_target, indices1, indices2)``. |
nothing calls this directly
no outgoing calls
no test coverage detected