How peizesun/sparser-cnn works
Purpose and Entry Points
This repository implements Sparse R-CNN, an end-to-end object detector with a fixed set of learnable proposals, built as a project on the included Detectron2 framework. Primary execution paths are training/evaluation via projects/SparseRCNN/train_net.py and image inference/visualization via demo/demo.py and demo/predictor.py.
Configuration and Assembly
YAML configurations define Sparse R-CNN model, backbone, proposal count, schedules, datasets, and weights. The project extends Detectron2 configuration and registers its architecture before model construction.
• projects/SparseRCNN/configs/Base-SparseRCNN.yaml
• projects/SparseRCNN/sparsercnn/config.py
• detectron2/config/defaults.py
Data Ingestion Boundary
Detectron2 dataset catalogs load COCO-style data, while the Sparse R-CNN mapper prepares per-image training inputs and annotations. Data loaders apply augmentation, batching, and distributed sampling before supplying image dictionaries to the model.
• detectron2/data/datasets/coco.py
• detectron2/data/build.py
• projects/SparseRCNN/sparsercnn/dataset_mapper.py
Sparse R-CNN Detection Pipeline
The custom detector is the central meta-architecture: images pass through a Detectron2 backbone, then learned proposal features and boxes are refined by the Sparse R-CNN dynamic head into class scores and boxes. Unlike standard Detectron2 RPN/ROI-head pipelines, this project’s detector/head path owns proposal refinement.
• projects/SparseRCNN/sparsercnn/detector.py
• projects/SparseRCNN/sparsercnn/head.py
• detectron2/modeling/backbone/resnet.py
Training, Matching, and Optimization
During training, predictions are matched to ground truth and converted into classification and box-regression losses; the Detectron2 engine drives forward/backward passes, optimizer scheduling, checkpointing, logging, and multi-process launch. Evaluation reuses the model’s inference outputs with COCO evaluation.
• projects/SparseRCNN/sparsercnn/loss.py
• detectron2/engine/train_loop.py
• detectron2/evaluation/coco_evaluation.py
Outputs, Checkpoints, and Deployment
Model weights are loaded/saved through Detectron2 checkpoint abstractions. Inference emits Detectron2 Instances/box structures for visualization or evaluation. The wider repository also retains export paths for TorchScript and Caffe2, though Sparse R-CNN’s documented workflow is PyTorch-based.
• detectron2/checkpoint/detection_checkpoint.py
• detectron2/structures/instances.py
• detectron2/export/torchscript.py
Runtime and Native Dependencies
The material runtime is Python with PyTorch and torchvision, targeting Linux/macOS and GPU training; OpenCV is used for demo/visualization. Detectron2 includes compiled C++/CUDA operators such as ROIAlign, rotated-box operations, NMS, and deformable convolution. COCO annotations/images are external filesystem inputs, and pretrained weights may be retrieved from the linked Google Drive, GitHub Release, Baidu Drive, or torchvision sources.
• setup.py
• detectron2/layers/csrc/vision.cpp
• detectron2/layers/csrc/ROIAlign/ROIAlign_cuda.cu
Main entry points: detector.py Sparse R-CNN detector · head.py Dynamic head · config.py Sparse R-CNN config extension · train_net.py Train / eval CLI
Components 21
Entry Points
- Train / eval CLI · Launches training and evaluation.
projects/SparseRCNN/train_net.py - Demo CLI · Runs image inference and visualization.
demo/demo.py - Visualization predictor
demo/predictor.py - Sparse R-CNN config · Defines model, datasets, schedules, and weights.
projects/SparseRCNN/configs/Base-SparseRCNN.yaml
Data Pipeline
- COCO dataset catalog · Loads external COCO images and annotations.
detectron2/data/datasets/coco.py - Sparse R-CNN mapper · Prepares image dictionaries and training annotations.
projects/SparseRCNN/sparsercnn/dataset_mapper.py - Data loaders · Applies augmentation, batching, and distributed sampling.
detectron2/data/build.py
Sparse R-CNN Model
- Sparse R-CNN config extension · Adds project-specific configuration and architecture registration.
projects/SparseRCNN/sparsercnn/config.py - Detectron2 defaults
detectron2/config/defaults.py - Sparse R-CNN detector · Owns learned proposal refinement instead of RPN/ROI heads.
projects/SparseRCNN/sparsercnn/detector.py - ResNet backbone
detectron2/modeling/backbone/resnet.py - Dynamic head · Refines learned proposal features and boxes into predictions.
projects/SparseRCNN/sparsercnn/head.py - Matching and losses · Matches predictions to ground truth and computes classification and box losses.
projects/SparseRCNN/sparsercnn/loss.py
Training & Outputs
- Detectron2 training engine · Runs optimization, scheduling, logging, and distributed execution.
detectron2/engine/train_loop.py - Detection checkpointer · Loads pretrained weights and saves training state.
detectron2/checkpoint/detection_checkpoint.py - Instances and boxes · Carries inference detections to evaluation and visualization.
detectron2/structures/instances.py - COCO evaluator
detectron2/evaluation/coco_evaluation.py - TorchScript export · Optional PyTorch deployment export.
detectron2/export/torchscript.py
Runtime Dependencies
- C++/CUDA operators · Provides accelerated vision operations such as ROIAlign and NMS.
detectron2/layers/csrc/vision.cpp - ROIAlign CUDA
detectron2/layers/csrc/ROIAlign/ROIAlign_cuda.cu - Python package runtime · Defines PyTorch, torchvision, and native extension dependencies.
setup.py
Connections 24
- Sparse R-CNN config → Sparse R-CNN config extension: configures
- Detectron2 defaults → Sparse R-CNN config extension: extends
- Train / eval CLI → Sparse R-CNN config extension: loads
- Train / eval CLI → Data loaders: builds
- Train / eval CLI → Detectron2 training engine: runs
- Demo CLI → Visualization predictor: uses
- Visualization predictor → Sparse R-CNN detector: invokes
- COCO dataset catalog → Sparse R-CNN mapper: dataset records
- Sparse R-CNN mapper → Data loaders: mapped samples
- Data loaders → Sparse R-CNN detector: image dictionaries
- Sparse R-CNN config extension → Sparse R-CNN detector: registers and configures
- Sparse R-CNN detector → ResNet backbone: extracts features
- ResNet backbone → Dynamic head: feature maps
- Sparse R-CNN detector → Dynamic head: learned proposals
- Dynamic head → Matching and losses: training predictions
- Matching and losses → Detectron2 training engine: losses
- Detectron2 training engine → Detection checkpointer: save / load state
- Dynamic head → Instances and boxes: inference detections
- Instances and boxes → COCO evaluator: evaluates
- Instances and boxes → Visualization predictor: visualizes
- Sparse R-CNN detector → TorchScript export: optional export
- C++/CUDA operators → ROIAlign CUDA: includes
- Python package runtime → C++/CUDA operators: builds
- C++/CUDA operators → ResNet backbone: accelerates vision ops