liveBook · Manning

Smooth l1 loss

As explained in Math and Architectures of Deep Learning

A piecewise regression loss that behaves like squared error near zero and absolute error for large residuals.

Smooth L1 loss is a regression loss for bounding-box offsets. It combines two behaviors: for a small prediction error, it acts like L2 loss (mean squared error); once the error reaches a threshold β, it acts like L1 loss. In Fast R-CNN and its region proposal network (RPN), β is 1. section 11.4.2 section 11.4.3

For an offset residual r between a predicted coordinate transform and its target, the small-error region squares r, so nearby predictions are strongly encouraged to become more precise. The large-error region grows linearly with |r| rather than quadratically. This avoids letting large regression errors dominate training as they can under pure L2 loss, while retaining stronger refinement pressure for small errors than pure L1 loss. section 11.4.2

The loss is applied coordinate-wise to box-regression vectors such as (tx, ty, tw, th), which encode position and size offsets between an anchor or region of interest and a ground-truth box. Regression is selective: the RPN uses it only for positive anchors, and Fast R-CNN uses it only for object RoIs. Background examples contribute classification loss but no box-regression loss. section 11.4.2 section 11.4.3

In a two-stage detector, smooth L1 loss fits into training as follows:

  • Generate target offsets from an anchor or RoI to its matched ground-truth bounding box. section 11.4.2
  • Predict four regression offsets for each candidate; Fast R-CNN produces class-specific offset sets. section 11.4.3
  • Select positive candidates, compare their predicted offsets with targets using smooth L1 loss, and normalize the regression loss by the number of positive candidates. section 11.4.2 section 11.4.3
  • Combine regression loss with cross-entropy classification loss, using a weighting factor where specified. section 11.4.2 section 11.4.3

RPN regression in PyTorch

A direct implementation uses nn.SmoothL1Loss(reduction="sum"). First, identify entries whose RPN labels equal 1. Then compare only their predicted offsets with their ground-truth offset targets, divide the summed loss by the number of positive anchors, and add the weighted result to cross-entropy loss. This masking matters because negative anchors have no applicable box-regression target. section 11.4.2

Listing 11.24 PyTorch code for the Fast R-CNN loss function
def rcnn_loss(
    roi_cls_scores,                              #1
    roi_loc,                                     #2
    roi_labels,                                  #3
    rcnn_loc_targets,                            #4
    lambda_ = 1):

    classification_criterion = nn.CrossEntropyLoss()
    reg_criterion = nn.SmoothL1Loss(reduction="sum")

    cls_loss = classification_criterion(roi_cls_scores, roi_labels)

    pos_roi_idxes = torch.where(roi_labels>0)[0] #5
    pred_all_offsets = roi_loc[pos_roi_idxes]

    num_pos_rois = len(pos_roi_idxes)
    pred_all_offsets = pred_all_offsets.view(    #6
        num_pos_rois, -1, 4)

    pred_cls_offsets = pred_all_offsets[
        torch.arange(num_pos_rois) , roi_labels[pos_roi_idxes]]

    gt_offsets = rcnn_loc_targets[pos_roi_idxes]

    reg_loss = reg_criterion(pred_cls_offsets, gt_offsets) / num_pos_rois
    return {
        "rcnn_cls_loss": cls_loss,
        "rcnn_reg_loss": reg_loss,
        "rcnn_total_loss": cls_loss + lambda_* reg_loss

    }

The Fast R-CNN loss implementation shows how predicted offsets are reshaped, indexed by each positive RoI's class label, and passed to nn.SmoothL1Loss. section 11.4.3

At inference, smooth L1 loss is no longer evaluated. The trained detector chooses a class, takes the offsets associated with that class, and transforms them back into bounding-box coordinates. The loss therefore serves training-time localization, while offset decoding and post-processing produce final detections. section 11.4.3 section 11.4.2

How Math and Architectures of Deep Learning covers it

How deep it goes
Defines the L1/L2 threshold behavior, gives its training rationale, and implements it for positive candidate offsets.
Examples
Uses RPN positive-anchor masking and Fast R-CNN class-specific RoI offsets in PyTorch loss code.
What it assumes
Assumes bounding-box offset targets, positive labels, anchors, and regions of interest have already been generated.
What you can do after
Apply it via PyTorch nn.SmoothL1Loss to selected bounding-box offset predictions and targets.
How it connects
Connects bounding-box regression with anchors, regions of interest, cross-entropy loss, and Fast R-CNN.

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Math and Architectures of Deep Learning. The section links point to the parts of the book it draws on.