MCPcopy Create free account
hub / github.com/InternRobotics/EmbodiedScan / FocalLossCost

Class FocalLossCost

embodiedscan/models/losses/match_cost.py:117–210  ·  view source on GitHub ↗

FocalLossCost. Args: alpha (Union[float, int]): focal_loss alpha. Defaults to 0.25. gamma (Union[float, int]): focal_loss gamma. Defaults to 2. eps (float): Defaults to 1e-12. binary_input (bool): Whether the input is binary. Currently, binary_input =

Source from the content-addressed store, hash-verified

115
116@TASK_UTILS.register_module()
117class FocalLossCost(BaseMatchCost):
118 """FocalLossCost.
119
120 Args:
121 alpha (Union[float, int]): focal_loss alpha. Defaults to 0.25.
122 gamma (Union[float, int]): focal_loss gamma. Defaults to 2.
123 eps (float): Defaults to 1e-12.
124 binary_input (bool): Whether the input is binary. Currently,
125 binary_input = True is for masks input, binary_input = False
126 is for label input. Defaults to False.
127 weight (Union[float, int]): Cost weight. Defaults to 1.
128 """
129
130 def __init__(self,
131 alpha: Union[float, int] = 0.25,
132 gamma: Union[float, int] = 2,
133 eps: float = 1e-12,
134 binary_input: bool = False,
135 weight: Union[float, int] = 1.) -> None:
136 super().__init__(weight=weight)
137 self.alpha = alpha
138 self.gamma = gamma
139 self.eps = eps
140 self.binary_input = binary_input
141
142 def _focal_loss_cost(self, cls_pred: Tensor, gt_labels: Tensor) -> Tensor:
143 """
144 Args:
145 cls_pred (Tensor): Predicted classification logits, shape
146 (num_queries, num_class).
147 gt_labels (Tensor): Label of `gt_bboxes`, shape (num_gt,).
148
149 Returns:
150 torch.Tensor: cls_cost value with weight
151 """
152 cls_pred = cls_pred.sigmoid()
153 neg_cost = -(1 - cls_pred + self.eps).log() * (
154 1 - self.alpha) * cls_pred.pow(self.gamma)
155 pos_cost = -(cls_pred + self.eps).log() * self.alpha * (
156 1 - cls_pred).pow(self.gamma)
157
158 cls_cost = pos_cost[:, gt_labels] - neg_cost[:, gt_labels]
159 return cls_cost * self.weight
160
161 def _mask_focal_loss_cost(self, cls_pred, gt_labels) -> Tensor:
162 """
163 Args:
164 cls_pred (Tensor): Predicted classification logits.
165 in shape (num_queries, d1, ..., dn), dtype=torch.float32.
166 gt_labels (Tensor): Ground truth in shape (num_gt, d1, ..., dn),
167 dtype=torch.long. Labels should be binary.
168
169 Returns:
170 Tensor: Focal cost matrix with weight in shape\
171 (num_queries, num_gt).
172 """
173 cls_pred = cls_pred.flatten(1)
174 gt_labels = gt_labels.flatten(1).float()

Callers

nothing calls this directly

Calls

no outgoing calls

Tested by

no test coverage detected