MCPcopy Create free account
hub / github.com/MotrixLab/AiOS / loss_labels

Method loss_labels

models/aios/criterion_smplx.py:67–108  ·  view source on GitHub ↗

Classification loss (Binary focal loss) targets dicts must contain the key "labels" containing a tensor of dim [nb_target_boxes]

(self,
                    outputs,
                    targets,
                    indices,
                    idx,
                    num_boxes,
                    data_batch,
                    log=True)

Source from the content-addressed store, hash-verified

65 loss_weight=1.0)
66
67 def loss_labels(self,
68 outputs,
69 targets,
70 indices,
71 idx,
72 num_boxes,
73 data_batch,
74 log=True):
75 """Classification loss (Binary focal loss) targets dicts must contain
76 the key "labels" containing a tensor of dim [nb_target_boxes]"""
77 indices = indices[0]
78 assert 'pred_logits' in outputs
79 src_logits = outputs['pred_logits']
80 target_classes_o = torch.cat(
81 [t['labels'][J] for t, (_, J) in zip(targets, indices)])
82 target_classes = torch.full(src_logits.shape[:2],
83 self.num_classes,
84 dtype=torch.int64,
85 device=src_logits.device)
86 target_classes[idx] = target_classes_o
87
88 target_classes_onehot = torch.zeros([
89 src_logits.shape[0], src_logits.shape[1], src_logits.shape[2] + 1
90 ],
91 dtype=src_logits.dtype,
92 layout=src_logits.layout,
93 device=src_logits.device)
94 target_classes_onehot.scatter_(2, target_classes.unsqueeze(-1), 1)
95
96 target_classes_onehot = target_classes_onehot[:, :, :-1]
97 loss_ce = sigmoid_focal_loss(src_logits,
98 target_classes_onehot,
99 num_boxes,
100 alpha=self.focal_alpha,
101 gamma=2) * src_logits.shape[1]
102 losses = {'loss_ce': loss_ce}
103
104 if log:
105 # TODO this should probably be a separate loss, not hacked in this one here
106 losses['class_error'] = 100 - accuracy(src_logits[idx],
107 target_classes_o)[0]
108 return losses
109
110 @torch.no_grad()
111 def loss_cardinality(self, outputs, targets, indices, num_boxes,

Callers

nothing calls this directly

Calls 2

accuracyFunction · 0.90
sigmoid_focal_lossFunction · 0.70

Tested by

no test coverage detected