How liangheming/cascade_rcnn works
Purpose and entry points
This repository is an unofficial PyTorch implementation of Cascade R-CNN for COCO object detection. Training starts at main.py, which instantiates the distributed training orchestrator. Evaluation is exposed separately through eval.py. Runtime behavior is primarily configured in config/cascade.yaml.
Distributed training orchestration
solver/ddp_mix_solver.py owns the training lifecycle: distributed-process setup, model/data construction, optimizer and scheduling, mixed-precision/synchronized training, checkpointing, and periodic validation. It is intended to run under torch.distributed.launch across multiple GPUs. main.py is deliberately thin and delegates execution to this solver.
COCO data pipeline
datasets/coco.py is the dataset boundary for COCO images and annotation JSON, relying on pycocotools. It prepares detection samples—images, category labels, and bounding boxes—for train and validation loaders. Image-space augmentation is applied through utils/augmentations.py; box-coordinate manipulation and matching primitives are centralized in utils/boxs_utils.py.
Detection model
nets/cascade_rcnn.py defines the end-to-end Cascade R-CNN detector and its configurable stages: backbone feature extraction, multiscale feature processing, region proposal generation, and sequential high-IoU ROI detection/refinement heads. nets/resnet.py provides the ResNet backbone family, while shared network layers and feature-pyramid-related building blocks reside in nets/common.py.
Proposal and cascade ROI boundary
The model converts dense multiscale features into candidate anchors/proposals, then applies cascade stages that progressively classify and regress boxes at increasing quality thresholds. ROI feature extraction is isolated in nets/roi_pooling.py, creating the boundary between proposal geometry and per-region heads. Inference applies score filtering, NMS, and a per-image detection limit configured in config/cascade.yaml.
Optimization and losses
Training targets and objective terms are implemented in losses/commons.py, including box-regression loss variants. The solver consumes model losses, applies optimizer/scheduler policy, and manages model-state utilities from utils/optims_utils.py and utils/model_utils.py. The configured setup supports Adam, cosine decay/warmup behavior, AMP, SyncBatchNorm, and EMA-oriented training utilities.
Evaluation and external dependencies
eval.py evaluates saved detector outputs/checkpoints, with COCO mAP calculation implemented in metrics/map.py. Core runtime dependencies are PyTorch and TorchVision; OpenCV, NumPy, and PyYAML support image processing and configuration; pycocotools supplies COCO parsing and evaluation semantics. Training assumes externally provisioned COCO image directories, annotation files, GPU hardware, and optional distributed multi-process infrastructure.
Main entry points: ddp_mix_solver.py DDP mixed-precision solver · coco.py COCO dataset and loaders · cascade_rcnn.py Cascade R-CNN · eval.py Evaluation entry
Components 18
Entry points
- Cascade configuration · Controls runtime, inference, and optimization settings.
config/cascade.yaml - Training entry
main.py
Distributed training
- DDP mixed-precision solver · Owns distributed lifecycle, training, checkpoints, and validation.
solver/ddp_mix_solver.py - Detection losses and targets
losses/commons.py - Optimizer and model state · Supports optimizer state, scheduling, and EMA-oriented utilities.
utils/optims_utils.py - Model state utilities
utils/model_utils.py
COCO data pipeline
- COCO dataset and loaders · Reads COCO images and annotations into detection samples.
datasets/coco.py - Image augmentation
utils/augmentations.py - Box geometry and matching
utils/boxs_utils.py - COCO data and semantics · Externally supplied images, annotations, and pycocotools semantics.
Cascade detector
- Cascade R-CNN · Coordinates feature extraction, proposals, cascade heads, and inference filtering.
nets/cascade_rcnn.py - ResNet backbone
nets/resnet.py - Multiscale feature layers · Provides shared layers and feature-pyramid building blocks.
nets/common.py - Anchors and proposals · Converts dense multiscale features into candidate regions.
nets/cascade_rcnn.py - ROI feature extraction
nets/roi_pooling.py - Cascade ROI heads · Sequentially classify and regress regions at increasing IoU thresholds.
nets/cascade_rcnn.py
Evaluation
- Evaluation entry
eval.py - COCO mAP
metrics/map.py
Connections 22
- Training entry → DDP mixed-precision solver: starts
- Cascade configuration → DDP mixed-precision solver: configures
- DDP mixed-precision solver → COCO dataset and loaders: builds loaders
- COCO data and semantics → COCO dataset and loaders: images and annotations
- COCO dataset and loaders → Image augmentation: applies
- COCO dataset and loaders → Box geometry and matching: uses
- DDP mixed-precision solver → Cascade R-CNN: trains
- Cascade R-CNN → ResNet backbone: extracts features
- ResNet backbone → Multiscale feature layers: feeds
- Multiscale feature layers → Anchors and proposals: multiscale features
- Anchors and proposals → ROI feature extraction: candidate regions
- ROI feature extraction → Cascade ROI heads: ROI features
- Cascade ROI heads → Box geometry and matching: refines boxes
- Cascade R-CNN → Detection losses and targets: model losses
- Detection losses and targets → DDP mixed-precision solver: objectives
- Optimizer and model state → DDP mixed-precision solver: optimizer and scheduler
- Model state utilities → DDP mixed-precision solver: checkpoint state
- Cascade configuration → Cascade R-CNN: architecture and inference
- Evaluation entry → Cascade R-CNN: loads and runs
- Evaluation entry → COCO dataset and loaders: evaluation samples
- Evaluation entry → COCO mAP: computes
- COCO mAP → COCO data and semantics: COCO evaluation