Efficientdet headnet swap

From wenmin-wu/ds-skills · View interactive version

Efficientdet headnet swap is an AI agent skill from the GitHub repository wenmin-wu/ds-skills. Load EfficientDet pretrained on COCO with the original 90-class head, then swap in a fresh HeadNet with your own num_classes — keeps the BiFPN feature pyramid pretrained and only retrains the classification head, the canonical transfer-learning recipe for the effdet PyTorch port

How to use this skill

Skill file

Overview

The default effdet PyTorch package loads EfficientDet checkpoints assuming COCO's 90 classes. If you instantiate with num_classes=2 directly, the state dict load fails because the head shapes don't match. The right pattern is the reverse: instantiate with the original config, load the COCO weights, then mutate config.num_classes and replace net.class_net with a fresh HeadNet. The BiFPN and backbone keep their pretrained weights, the classification head re-initializes for your task, and DetBenchTrain wraps everything in the loss-computing forward pass. This is the canonical 4-line recipe for fine-tuning EfficientDet on any custom detection dataset.

Quick Start

import torch
from effdet import get_efficientdet_config, EfficientDet, DetBenchTrain
from effdet.efficientdet import HeadNet

def get_net(num_classes=2, image_size=512, ckpt='efficientdet_d5-ef44aea8.pth'):
    config = get_efficientdet_config('tf_efficientdet_d5')
    net = EfficientDet(config, pretrained_backbone=False)

    checkpoint = torch.load(ckpt, map_location='cpu')
    net.load_state_dict(checkpoint)               # load with original 90 classes

    config.num_classes = num_classes
    config.image_size  = image_size
    net.class_net = HeadNet(
        config,
        num_outputs=config.num_classes,
        norm_kwargs=dict(eps=1e-3, momentum=0.01),
    )
    return DetBenchTrain(net, config)

Workflow

  1. Pick the EfficientDet variant that matches your compute — D0 for prototyping, D5 for production accuracy
  2. get_efficientdet_config('tf_efficientdet_d5') and instantiate the model with the original COCO classes
  3. load_state_dict(checkpoint) — must happen before mutating num_classes, otherwise shapes mismatch
  4. Mutate config.num_classes and config.image_size to your task's values
  5. Replace net.class_net with a freshly-initialized HeadNet of the new shape
  6. Wrap in DetBenchTrain for training (adds the loss heads) or DetBenchPredict for inference
  7. Train with a low LR on the new head and a 10x lower LR on the rest (use param groups)

Key Decisions

References

Use this skill with your AI agent: Open on Skill Issue to copy, download, or add to your library. Works with Claude, ChatGPT, Gemini, Cursor, Windsurf and more.