RetinaNet

One-stage detector (Lin et al., 2017): ResNet-FPN levels P3–P7, 9 anchors per location, shared class and box subnets, focal loss over all ~100k anchors and a π = 0.01 bias prior; 39.1 COCO AP, above two-stage detectors.

7 min read846 wordscore0 viewsupdated 2026-10-15

Definition

RetinaNet is the one-stage object detector of Lin, Goyal, Girshick, He and Dollár, "Focal Loss for Dense Object Detection" (ICCV 2017). It is a deliberately plain architecture: a ResNet backbone, a feature pyramid network (FPN) and two small convolutional subnets. The new part is the training loss. Focal loss lets a dense detector train on every anchor without being swamped by easy background, and with it a one-stage detector matched two-stage accuracy for the first time.

Input: an image, shorter side 800 px in the main results. Output: boxes with class scores. Two-stage detectors (Faster R-CNN) were the most accurate. Their first stage cuts about 100k candidate locations down to 1–2k proposals, and the second stage samples mini-batches with a fixed foreground:background ratio such as 1:3. One-stage detectors such as SSD and YOLO were faster but less accurate.

The paper's diagnosis: in a one-stage detector, about 100k anchors per image are classified at once and nearly all are easy background. Each contributes a small cross-entropy loss, but together they dominate the gradient. SSD's answer was hard negative mining at 3:1. RetinaNet instead keeps every anchor and down-weights the easy ones in the loss.

Architecture

The FPN builds pyramid levels P3 to P7 from the ResNet stages C3–C5 (see ResNet). P3–P5 come from the top-down pathway with 1×11\times1 lateral connections. P6 is a 3×33\times3 stride-2 convolution on C5, and P7 is ReLU plus another 3×33\times3 stride-2 convolution on P6. Level PlP_l has stride 2l2^l (8 to 128) and 256 channels. P2 is left out to save computation.

Every location of every level has A=9A=9 anchors: areas 32232^2 (P3) to 5122512^2 (P7), each with 3 scales {20,21/3,22/3}\{2^0,2^{1/3},2^{2/3}\} and 3 aspect ratios {1:2,1:1,2:1}\{1{:}2,1{:}1,2{:}1\}, covering 32 to 813 px. An anchor is foreground if its IoU with a ground-truth box is at least 0.5, background if it is below 0.4, and ignored in between.

Two subnets, each applied to every level with weights shared across levels, make the predictions. The classification subnet is four 3×33\times3 convolutions with 256 filters and ReLU, then a 3×33\times3 convolution with KAKA outputs and a sigmoid: one binary score per class and anchor, no softmax and no background class. The box subnet has the same structure, separate weights and 4A4A class-agnostic outputs, the Faster R-CNN box offsets trained with smooth ℓ1\ell_1.

RetinaNet = ResNet + FPN (P3–P7) + two small subnets shared across levels + focal lossResNetFPN, 256 chP7 7²512pxP6 13²256pxC5 · s321×1P5 25²128pxC4 · s161×1P4 50²64pxC3 · s81×1P3 100²32px2× up, +2× up, +3×3 s2ReLU, 3×3 s2800×800every levelP6 comes from C5, not P5; P2 isskipped to save computationclass subnet (shared by P3–P7)4 × [3×3 conv, 256, ReLU]→ 3×3 conv, K·A = 80·9 = 720, sigmoidlast bias = −log((1−π)/π) = −4.595, π = 0.01box subnet (separate weights)4 × [3×3 conv, 256, ReLU]→ 3×3 conv, 4A = 36 offsets, class-agnosticA = 9 anchors per locationsizes × 2⁰, 2^⅓, 2^⅔ratios 1:2, 1:1, 2:1base 32 (P3) … 512 (P7)120,087 anchors at 800²train: IoU ≥ 0.5 fg, < 0.4 bg, else ignoredfocal loss γ = 2, α = 0.25 on all anchorssum ÷ N_pos · smooth L1 on fg boxesinfer: score > 0.05, top 1000 per levelmerge levels, NMS at IoU 0.5one stage, no proposals, no RoI pooling
fig.RetinaNet. ResNet stages C3–C5 feed an FPN with levels P3–P7 (shapes for an 800×800 input, anchor base size per level on the right). The same class subnet (K·A = 720 sigmoid outputs for COCO) and box subnet (4A = 36 offsets) run on every level. Training applies focal loss to all anchors, foreground at IoU ≥ 0.5 and background below 0.4; inference keeps the top 1000 scores above 0.05 per level, then applies NMS at IoU 0.5.

Loss and the prior initialisation

The classification loss is the α-balanced focal loss, summed over all anchors except the ignored ones and over all KK sigmoid outputs, then divided by the number of foreground anchors:

Lcls=1Npos∑a∑k=1K−αt (1−pt)γlog⁡pt,pt={pa,kya,k=11−pa,kya,k=0\mathcal L_{\text{cls}}=\frac{1}{N_{\text{pos}}}\sum_{a}\sum_{k=1}^{K}-\alpha_t\,(1-p_{t})^{\gamma}\log p_{t},\qquad p_t=\begin{cases}p_{a,k}&y_{a,k}=1\\1-p_{a,k}&y_{a,k}=0\end{cases}
pa,kp_{a,k}
sigmoid output of anchor aa for class kk
γ=2, α=0.25\gamma=2,\ \alpha=0.25
the paper's best setting; αt=α\alpha_t=\alpha for y=1y=1 and 1−α1-\alpha for y=0y=0
NposN_{\text{pos}}
number of anchors assigned to a ground-truth box, not the number of anchors

Normalising by NposN_{\text{pos}} works because, under focal loss, the easy negatives contribute almost nothing. The second ingredient is the initialisation. With a zero bias every output starts at p=0.5p=0.5, and the background outputs produce a huge loss in the first iterations. The paper sets the bias of the final classification layer so that every output starts at the prior π\pi:

b=−log⁡1−ππ,π=0.01 ⇒ b=−4.595,σ(b)=0.01b=-\log\frac{1-\pi}{\pi},\qquad \pi=0.01\ \Rightarrow\ b=-4.595,\quad \sigma(b)=0.01
π\pi
the model's initial foreground probability for every anchor and class
bb
bias of the last 3×33\times3 conv of the class subnet; other new layers get bias 0 and Gaussian weights with σ=0.01\sigma=0.01

The paper reports that this prior is what keeps training stable at the start, for cross-entropy as well as focal loss. The loss itself, its gradient and the roles of γ\gamma and α\alpha are covered in Focal loss; here the point is what it does to one detector image.

Configuration and results (Lin et al., 2017)

Training
SGD on 8 GPUs, 16 images per batch, 90k iterations; images resized to a shorter side of 800 px for the main model
Inference
per level, the top 1000 boxes with score > 0.05; then NMS at IoU 0.5 over all levels
Ablation (ResNet-50, 600 px, COCO minival)
best α-balanced cross-entropy 31.1 AP (α = 0.75); focal loss γ = 2, α = 0.25: 34.0 AP; best OHEM 32.8 AP
COCO test-dev
RetinaNet ResNet-101-FPN, 800 px: 39.1 AP; ResNeXt-101-FPN: 40.8 AP. Faster R-CNN with ResNet-101-FPN: 36.2 AP
800×800 image · 9 anchors per location on P3–P7 · 120,087 anchorsdashed = ground truth · thin = anchors, IoU ≥ 0.5every object has at least one positive anchorlevelstridegridanchorsposignoredP38100²90,0002834P41650²22,500652P53225²5,6255988P66413²1,5212546P71287²44100all120,087118220foreground : background = 1 : 1,015IoU < 0.4 → background (119,749); 0.4–0.5 ignoredfocal loss (γ = 2, α = 0.25) at initall 80 outputs σ(b) = π = 0.01, bias b = -4.595per output: target 1 → 1.128, target 0 → 7.54e-7fg 133.1 + bg 7.228 = 140.4background share 5.1%÷ N_pos = 118: loss 1.190 per positive9,589,242 of 9,589,360 binary targets are 0prior + focal loss: the few positives already dominate the loss at step 0the paper sums over all anchors and divides by the number of positive anchors
scene
loss
fig.Real anchors and matching for one image, and the classification loss before the first update (every output = π). Default (street, 800 px, π = 0.01, focal): 120,087 anchors, 118 foreground, 220 ignored, so background outnumbers foreground about 1,015 to 1. The total loss is 140.4: the foreground supplies 133.1 and the 119,749 background anchors only 7.2 (5.1%), 1.190 per positive. Switch to cross-entropy: 96,918.5, 99.4% from background. Also set π = 0.5 (zero bias): 6.65 million, 56,329 per positive. At 512 px, 6 of the 12 small objects get no positive anchor at all.

Worked example: the loss at step 0

The street image at 800×800800\times800, K=80K=80: 118 foreground, 119,749 background and 220 ignored anchors. A foreground anchor has 1 output with target 1 and 79 with target 0, so there are 118⋅79+119,749⋅80=9,589,242118\cdot79+119{,}749\cdot80=9{,}589{,}242 outputs with target 0 and 118 with target 1.

SettingLoss per target-1 outputLoss per target-0 outputTotalBackground share÷ N_pos = 118
CE, π = 0.5 (zero bias)−ln 0.5 = 0.6930.6936,646,838100.0%56,329
CE, π = 0.01−ln 0.01 = 4.605−ln 0.99 = 0.0100596,918.599.4%821.3
FL γ = 2, α = 0.25, π = 0.010.25 · 0.99² · 4.605 = 1.1280.75 · 0.01² · 0.01005 = 7.54e-7140.45.1%1.190
Classification loss at initialisation, every output p = π (street scene, 800 px)

The prior cuts the background term per output by 0.693/0.01005≈69×0.693/0.01005\approx69\times. Focal loss cuts it by another factor 0.75⋅0.0120.75\cdot0.01^2, about 13,000×13{,}000\times. Only with both is the loss per positive of order 1, so dividing by NposN_{\text{pos}} gives a well-scaled loss from the first iteration.

python
1import math, torch, torchvision
2from torchvision.models.detection import retinanet_resnet50_fpn, RetinaNet_ResNet50_FPN_Weights
3
4# a fresh model shows the paper's initialisation of the class subnet
5fresh = retinanet_resnet50_fpn(weights=None, weights_backbone=None, num_classes=91)
6b = fresh.head.classification_head.cls_logits.bias
7print(b[0].item(), -math.log(0.99 / 0.01)) # both -4.595
8print(fresh.anchor_generator.sizes[0]) # (32, 40, 50): 32 * 2**(0, 1/3, 2/3) on P3
9print(fresh.anchor_generator.aspect_ratios[0]) # (0.5, 1.0, 2.0)
10
11# a trained model for inference
12weights = RetinaNet_ResNet50_FPN_Weights.COCO_V1
13model = retinanet_resnet50_fpn(weights=weights).eval()
14print(model.score_thresh, model.topk_candidates, model.nms_thresh) # 0.05 1000 0.5
15img = torchvision.io.read_image("street.jpg")
16with torch.no_grad():
17 out = model([weights.transforms()(img)])[0]
18keep = out["scores"] > 0.5
19names = [weights.meta["categories"][i] for i in out["labels"][keep]]
20print(list(zip(names, out["boxes"][keep].round().tolist())))
21
22# training uses torchvision.ops.sigmoid_focal_loss(logits, targets, alpha=0.25, gamma=2, reduction="sum") / num_foreground
RetinaNet in torchvision: anchors, the prior bias and inference (not run here; requires torch and torchvision with downloaded COCO weights)

Check yourself

At initialisation every classification output of RetinaNet is 0.01. Why is the loss divided by the number of foreground anchors and not by all ~100k anchors?

Check yourself

What does the classification subnet of RetinaNet output for one level with an H×W map and COCO's K = 80 classes?

Remember

  • ✓One-stage: ResNet + FPN levels P3–P7 (strides 8–128, 256 channels) + a class subnet and a box subnet shared across levels.
  • ✓9 anchors per location (3 scales × 3 ratios, 32–512 px base); IoU ≥ 0.5 foreground, < 0.4 background, between ignored.
  • ✓Focal loss (γ = 2, α = 0.25) over all ~100k anchors, divided by the number of foreground anchors.
  • ✓Final class bias −log((1−π)/π) with π = 0.01 keeps the step-0 loss from being dominated by background.
  • ✓39.1 AP on COCO test-dev with ResNet-101-FPN, above Faster R-CNN with FPN (36.2).
← back to Deep Learning roadmap