Training¶
Training¶
- class alonet.deformable_detr.train.LitDeformableDetr(args=None, model=None, **kwargs)¶
Bases:
alonet.detr.train.LitDetr
- static add_argparse_args(parent_parser, parser=None)¶
Add argument for Deformable DETR
- Parameters
- parent_parserArgumentParser
parser to be added new argument
- parser[type], optional
parser to overwrite parent_parser if not None, by default None
- Returns
- ArgumentParser
Parser with arguments for Deformable DETR added
- build_criterion(matcher, loss_label_weight=1, loss_boxes_weight=5, loss_giou_weight=2, losses=['labels', 'boxes'], aux_loss_stage=6, eos_coef=0.1)¶
Build criterion module to calculate losses
- Parameters
- matcherDeformableDetrHungarianMatcher
Hungarian matcher to match between predictions and targets
- loss_label_weightint, optional
Weight of the classification loss, by default 1
- loss_boxes_weightint, optional
Weight of the L1 loss of the bounding box coordinates, by default 5
- loss_giou_weightint, optional
Weight of the giou loss of the bounding box, by default 2
- losseslist, optional
Type of loss use in training, by default [‘labels’, ‘boxes’]
- aux_loss_stageint, optional
Number of auxiliary decoder stages, by default 6
- eos_coeffloat, optional
Relative classification weight applied to the no-object category, by default 0.1. This factor is applied only when softmax activation is used in model.
- Returns
- DeformableCriterion
- rtype
- build_matcher(cost_class=1, cost_boxes=5, cost_giou=2)¶
Build matcher to match between predictions and targets
- Parameters
- cost_classint, optional
Weight of the classification error in the matching cost, by default 1
- cost_boxesint, optional
Weight of the L1 error of the bounding box coordinates in the matching cost, by default 5
- cost_giouint, optional
Weight of the giou loss of the bounding box in the matching cost, by default 2
- Returns
- DeformableDetrHungarianMatcher
- rtype
- build_model(num_classes=91, aux_loss=True, weights=None, activation_fn='sigmoid')¶
Build model for training
- Parameters
- num_classesint, optional
Number of classes to detect, by default 91
- aux_lossbool, optional
aux_loss : bool, optional If True, the model will returns auxilary outputs at each decoder layer to calculate auxiliary decoding losses. By default True.
- weightsstr, optional
Pretrained weights, by default None
- activation_fnstr, optional
Activation function for classification head. Either “sigmoid” or “softmax”. By default “sigmoid”.
- Returns
- Union[DeformableDetrR50Refinement, DeformableDetrR50]
Deformable DETR for training
- rtype
Union
[DeformableDetrR50Refinement
,DeformableDetrR50
] ..
- configure_optimizers()¶
Configure optimzier using AdamW
- training: bool¶
Callbacks¶
Callbacks for Deformable DETR training is the same as DETR training
Criterion¶
- class alonet.deformable_detr.criterion.DeformableCriterion(loss_label_weight, focal_alpha=0.25, **kwargs)¶
Bases:
alonet.detr.criterion.DetrCriterion
This class computes the loss for Deformable DETR. The process happens in two steps
we compute hungarian assignment between ground truth boxes and the outputs of the model
we supervise each pair of matched ground-truth / prediction (supervise class and box)
This Criterion is rouhgly smilar to DetrCriterion except for the labels loss and the metrics.
- get_metrics(outputs, frames, indices, num_boxes)¶
Compute some usefull metrics related to the model performance
- Parameters
- outputsdict
model forward outputs
- framesaloscene.Frame
Target frame with ground truth boxes2d and labels
- indiceslist
List of tuple with matching predicted indices and target indices. len(indices) is equal to batch size.
- num_boxestorch.Tensor
Number of total target boxes
- Returns
- dict
“recall” : Percentage of detected class among all the GT class
“precision”: Among the positive prediction of the model. How much are well classify ?
Notes
Metrics are calculated based on threshold = 0.3 to filter out positive prediction. Positive prediction = prediction with score >= threshold, otherwise negative prediction This threshold value can be change in inference. That being said, it is still a usefull information to monitor the training progress.
- Return type
dict
- loss_labels(outputs, frames, indices, num_boxes, **kwargs)¶
Compute the clasification loss
- Parameters
- outputsdict
model forward outputs
- framesaloscene.Frame
Target frame with ground truth boxes2d and labels
- indiceslist
List of tuple with matching predicted indices and target indices. len(indices) is equal to batch size.
- num_boxestorch.Tensor
Number of total target boxes
- Returns
- torch.Tensor
Classification loss
- rtype
Tensor
..
- training: bool¶
- alonet.deformable_detr.criterion.sigmoid_focal_loss(inputs, targets, num_boxes, alpha=0.25, gamma=2)¶
Sigmoid focal loss for classification
- Parameters
- inputstorch.Tensor
The predictions for each example.
- targetstorch.Tensor
A float tensor with the same shape as inputs. Stores the binary classification label for each element in inputs (0 for the negative class and 1 for the positive class).
- num_boxestorch.Tensor
- alphafloat, optional
(optional) Weighting factor in range (0,1) to balance positive vs negative examples. -1 for no weighting. By default 0.25.
- gammafloat, optional
Exponent of the modulating factor (1 - p_t) to balance easy vs hard examples. By default 2
- Returns
- torch.Tensor
Scalar
- rtype
Tensor
..
Matcher¶
- class alonet.deformable_detr.matcher.DeformableDetrHungarianMatcher(cost_class=1, cost_boxes=1, cost_giou=1)¶
Bases:
alonet.detr.matcher.DetrHungarianMatcher
- hungarian_cost_class(tgt_boxes, m_outputs, **kwargs)¶
Compute the cost class for the Hungarian matcher
- Parameters
- m_outputs: dict
- Dict output of the alonet.detr.Detr model. This is a dict that contains at least these entries:
“pred_logits”: Tensor of dim [batch_size, num_queries, num_classes] with the classification logits
“pred_boxes”: Tensor of dim [batch_size, num_queries, 4] with the predicted box coordinates
- tgt_boxes: aloscene.BoundingBoxes2D
Target boxes2d across the batch
- training: bool¶