# liangheming/cascade_rcnn architecture

> Architecture diagram of the GitHub repository liangheming/cascade_rcnn, generated by GitDiagram from its file tree, README and a sample of source files (last updated 2026-07-16). It is an AI-made overview: check details against the source.

- Interactive diagram: https://gitdiagram.com/liangheming/cascade_rcnn
- Repository: https://github.com/liangheming/cascade_rcnn

## Overview

#### 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: [`solver/ddp_mix_solver.py`](https://github.com/liangheming/cascade_rcnn/blob/master/solver/ddp_mix_solver.py) (DDP mixed-precision solver), [`datasets/coco.py`](https://github.com/liangheming/cascade_rcnn/blob/master/datasets/coco.py) (COCO dataset and loaders), [`nets/cascade_rcnn.py`](https://github.com/liangheming/cascade_rcnn/blob/master/nets/cascade_rcnn.py) (Cascade R-CNN), [`eval.py`](https://github.com/liangheming/cascade_rcnn/blob/master/eval.py) (Evaluation entry)

## Diagram (Mermaid)

```mermaid
flowchart TD

subgraph group_entry["Entry points"]
  node_config["Cascade configuration<br/>YAML config<br/>[cascade.yaml]"]
  node_main["Training entry<br/>Python entry point<br/>[main.py]"]
end

subgraph group_train["Distributed training"]
  node_solver["DDP mixed-precision solver<br/>training orchestrator<br/>[ddp_mix_solver.py]"]
  node_losses["Detection losses and targets<br/>training objectives<br/>[commons.py]"]
  node_optimization["Optimizer and model state<br/>training utilities<br/>[optims_utils.py]"]
  node_model_utils["Model state utilities<br/>model utilities<br/>[model_utils.py]"]
end

subgraph group_data["COCO data pipeline"]
  node_coco[("COCO dataset and loaders<br/>dataset boundary<br/>[coco.py]")]
  node_augment["Image augmentation<br/>transform pipeline<br/>[augmentations.py]"]
  node_boxes["Box geometry and matching<br/>geometry primitives<br/>[boxs_utils.py]"]
  node_coco_external[("COCO data and semantics<br/>external dependency")]
end

subgraph group_model["Cascade detector"]
  node_detector["Cascade R-CNN<br/>end-to-end detector<br/>[cascade_rcnn.py]"]
  node_backbone["ResNet backbone<br/>feature extractor<br/>[resnet.py]"]
  node_features["Multiscale feature layers<br/>shared network blocks<br/>[common.py]"]
  node_proposals["Anchors and proposals<br/>proposal stage<br/>[cascade_rcnn.py]"]
  node_roi["ROI feature extraction<br/>proposal-to-head boundary<br/>[roi_pooling.py]"]
  node_cascade_heads["Cascade ROI heads<br/>refinement stages<br/>[cascade_rcnn.py]"]
end

subgraph group_eval["Evaluation"]
  node_eval_entry["Evaluation entry<br/>Python entry point<br/>[eval.py]"]
  node_map["COCO mAP<br/>evaluation metric<br/>[map.py]"]
end

node_main -->|"starts"| node_solver
node_config -->|"configures"| node_solver
node_solver -->|"builds loaders"| node_coco
node_coco_external -->|"images and annotations"| node_coco
node_coco -->|"applies"| node_augment
node_coco -->|"uses"| node_boxes
node_solver -->|"trains"| node_detector
node_detector -->|"extracts features"| node_backbone
node_backbone -->|"feeds"| node_features
node_features -->|"multiscale features"| node_proposals
node_proposals -->|"candidate regions"| node_roi
node_roi -->|"ROI features"| node_cascade_heads
node_cascade_heads -->|"refines boxes"| node_boxes
node_detector -->|"model losses"| node_losses
node_losses -->|"objectives"| node_solver
node_optimization -->|"optimizer and scheduler"| node_solver
node_model_utils -->|"checkpoint state"| node_solver
node_config -->|"architecture and inference"| node_detector
node_eval_entry -->|"loads and runs"| node_detector
node_eval_entry -->|"evaluation samples"| node_coco
node_eval_entry -->|"computes"| node_map
node_map -->|"COCO evaluation"| node_coco_external

click node_config "https://github.com/liangheming/cascade_rcnn/blob/master/config/cascade.yaml"
click node_main "https://github.com/liangheming/cascade_rcnn/blob/master/main.py"
click node_eval_entry "https://github.com/liangheming/cascade_rcnn/blob/master/eval.py"
click node_solver "https://github.com/liangheming/cascade_rcnn/blob/master/solver/ddp_mix_solver.py"
click node_coco "https://github.com/liangheming/cascade_rcnn/blob/master/datasets/coco.py"
click node_augment "https://github.com/liangheming/cascade_rcnn/blob/master/utils/augmentations.py"
click node_boxes "https://github.com/liangheming/cascade_rcnn/blob/master/utils/boxs_utils.py"
click node_detector "https://github.com/liangheming/cascade_rcnn/blob/master/nets/cascade_rcnn.py"
click node_backbone "https://github.com/liangheming/cascade_rcnn/blob/master/nets/resnet.py"
click node_features "https://github.com/liangheming/cascade_rcnn/blob/master/nets/common.py"
click node_proposals "https://github.com/liangheming/cascade_rcnn/blob/master/nets/cascade_rcnn.py"
click node_roi "https://github.com/liangheming/cascade_rcnn/blob/master/nets/roi_pooling.py"
click node_cascade_heads "https://github.com/liangheming/cascade_rcnn/blob/master/nets/cascade_rcnn.py"
click node_losses "https://github.com/liangheming/cascade_rcnn/blob/master/losses/commons.py"
click node_optimization "https://github.com/liangheming/cascade_rcnn/blob/master/utils/optims_utils.py"
click node_model_utils "https://github.com/liangheming/cascade_rcnn/blob/master/utils/model_utils.py"
click node_map "https://github.com/liangheming/cascade_rcnn/blob/master/metrics/map.py"

classDef toneNeutral fill:#f8fafc,stroke:#334155,stroke-width:1.5px,color:#0f172a
classDef toneBlue fill:#dbeafe,stroke:#2563eb,stroke-width:1.5px,color:#172554
classDef toneAmber fill:#fef3c7,stroke:#d97706,stroke-width:1.5px,color:#78350f
classDef toneMint fill:#dcfce7,stroke:#16a34a,stroke-width:1.5px,color:#14532d
classDef toneRose fill:#ffe4e6,stroke:#e11d48,stroke-width:1.5px,color:#881337
classDef toneIndigo fill:#e0e7ff,stroke:#4f46e5,stroke-width:1.5px,color:#312e81
classDef toneTeal fill:#ccfbf1,stroke:#0f766e,stroke-width:1.5px,color:#134e4a
class node_config,node_main toneBlue
class node_solver,node_losses,node_optimization,node_model_utils toneAmber
class node_coco,node_augment,node_boxes,node_coco_external toneMint
class node_detector,node_backbone,node_features,node_proposals,node_roi,node_cascade_heads toneRose
class node_eval_entry,node_map toneIndigo
```

## Components

### Entry points

- [Cascade configuration](https://github.com/liangheming/cascade_rcnn/blob/master/config/cascade.yaml): YAML config · Controls runtime, inference, and optimization settings. · `config/cascade.yaml`
- [Training entry](https://github.com/liangheming/cascade_rcnn/blob/master/main.py): Python entry point · `main.py`

### Distributed training

- [DDP mixed-precision solver](https://github.com/liangheming/cascade_rcnn/blob/master/solver/ddp_mix_solver.py): training orchestrator · Owns distributed lifecycle, training, checkpoints, and validation. · `solver/ddp_mix_solver.py`
- [Detection losses and targets](https://github.com/liangheming/cascade_rcnn/blob/master/losses/commons.py): training objectives · `losses/commons.py`
- [Optimizer and model state](https://github.com/liangheming/cascade_rcnn/blob/master/utils/optims_utils.py): training utilities · Supports optimizer state, scheduling, and EMA-oriented utilities. · `utils/optims_utils.py`
- [Model state utilities](https://github.com/liangheming/cascade_rcnn/blob/master/utils/model_utils.py): model utilities · `utils/model_utils.py`

### COCO data pipeline

- [COCO dataset and loaders](https://github.com/liangheming/cascade_rcnn/blob/master/datasets/coco.py): dataset boundary · Reads COCO images and annotations into detection samples. · `datasets/coco.py`
- [Image augmentation](https://github.com/liangheming/cascade_rcnn/blob/master/utils/augmentations.py): transform pipeline · `utils/augmentations.py`
- [Box geometry and matching](https://github.com/liangheming/cascade_rcnn/blob/master/utils/boxs_utils.py): geometry primitives · `utils/boxs_utils.py`
- **COCO data and semantics**: external dependency · Externally supplied images, annotations, and pycocotools semantics.

### Cascade detector

- [Cascade R-CNN](https://github.com/liangheming/cascade_rcnn/blob/master/nets/cascade_rcnn.py): end-to-end detector · Coordinates feature extraction, proposals, cascade heads, and inference filtering. · `nets/cascade_rcnn.py`
- [ResNet backbone](https://github.com/liangheming/cascade_rcnn/blob/master/nets/resnet.py): feature extractor · `nets/resnet.py`
- [Multiscale feature layers](https://github.com/liangheming/cascade_rcnn/blob/master/nets/common.py): shared network blocks · Provides shared layers and feature-pyramid building blocks. · `nets/common.py`
- [Anchors and proposals](https://github.com/liangheming/cascade_rcnn/blob/master/nets/cascade_rcnn.py): proposal stage · Converts dense multiscale features into candidate regions. · `nets/cascade_rcnn.py`
- [ROI feature extraction](https://github.com/liangheming/cascade_rcnn/blob/master/nets/roi_pooling.py): proposal-to-head boundary · `nets/roi_pooling.py`
- [Cascade ROI heads](https://github.com/liangheming/cascade_rcnn/blob/master/nets/cascade_rcnn.py): refinement stages · Sequentially classify and regress regions at increasing IoU thresholds. · `nets/cascade_rcnn.py`

### Evaluation

- [Evaluation entry](https://github.com/liangheming/cascade_rcnn/blob/master/eval.py): Python entry point · `eval.py`
- [COCO mAP](https://github.com/liangheming/cascade_rcnn/blob/master/metrics/map.py): evaluation metric · `metrics/map.py`

## Connections

- 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
