# mayank-atheer/detectron2 architecture

> Architecture diagram of the GitHub repository mayank-atheer/detectron2, generated by GitDiagram from its file tree, README and a sample of source files (last updated 2025-06-02). It is an AI-made overview: check details against the source.

- Interactive diagram: https://gitdiagram.com/mayank-atheer/detectron2
- Repository: https://github.com/mayank-atheer/detectron2

## Overview

##### Project Type and Purpose
- Detectron2 is a modular, research-grade computer vision library built on PyTorch.
- It acts as both a toolkit for building object detection/segmentation models and a framework for training, evaluation, and model export.

##### Top-Level Architecture Components
a. Configuration System
- YAML/py configs under configs/ and projects/*/configs/
- Loader & instantiator in detectron2/config/
- Supports default+user overrides (builder pattern)
b. Data Pipeline
- Dataset registration & catalog (detectron2/data/catalog.py)
- Built-in dataset scripts (COCO, LVIS, Cityscapes, PascalVOC)
- Samplers, BatchSamplers (distributed, grouped)
- Data transforms & augmentation modules
c. Model Zoo & Backbones
- detectron2/model_zoo: pre-defined config→URL mappings
- Backbones: ResNet, FPN, Swin, ViT, RegNet, MViT under modeling/backbone/
d. Meta-Architectures & Heads
- Meta-architectures: RCNN, RetinaNet, FCOS, Panoptic FPN, semantic seg
- Proposal generators: RPN, RRPN
e. Training Engine
- detectron2/engine/train_loop.py: main training loop with hooks (learning rate scheduling, checkpointing, logging)
- Solver: learning rate schedulers, optimizers under detectron2/solver/
- Checkpointing: detectron2/checkpoint/
f. Evaluation & Visualization
- COCO/LVIS/Pascal evaluators under detectron2/evaluation/
- Visualizer for predictions and Tensorboard events
g. Export & Deployment
- TorchScript and Caffe2 export modules under detectron2/export/
- C++/CUDA custom ops under detectron2/layers/csrc/
- Docker and “tools/deploy” wrappers for production
h. Auxiliary Utilities & Tools
- Logging, registry, file IO, comm utilities
- Stand-alone CLI tools: demo/demo.py, tools/*.py
- Project-specific extensions in projects/ (DensePose, DeepLab, PointRend, etc.)

##### Interactions & Data Flow
- User writes a config YAML → Configuration system builds:
- DataLoader ← dataset + transforms
- Model ← backbone + meta-arch + heads
- Optimizer & LR scheduler
- Training Loop: for each batch → Model forward → loss → backward → optimizer step → hook events (logging, checkpoint)
- Evaluation Loop: Model inference → evaluator collects metrics → visualizer outputs
- Export: Serialize model graph to TorchScript/Caffe2, including custom C++ kernels

##### Architectural Patterns & Principles
- Plugin Registry: Components (backbones, heads, datasets) register via decorators → extensibility
- Builder/Factory: Config-to-object instantiation for loose coupling
- Hook System: Training/evaluation behaviors decoupled from loop core
- Modular C++ extensions for performance-critical ops

##### Detail Level
- Essential custom C++ ops under layers/csrc
- High-level flow rather than every single YAML file

## Diagram (Mermaid)

```mermaid
flowchart TD
    subgraph "Configuration Layer"
        ConfigFiles["configs/ (YAML/PY)"]:::core
        ConfigLoader["detectron2/config/ (Config Loader & Instantiator)"]:::core
        ConfigFiles -->|"YAML→ConfigObj"| ConfigLoader
    end

    subgraph "Data Layer"
        Catalog["detectron2/data/catalog.py (Dataset Registry & Catalog)"]:::data
        Datasets["detectron2/data/datasets/ (COCO, LVIS, Cityscapes, PascalVOC)"]:::data
        Samplers["detectron2/data/samplers/ (BatchSamplers)"]:::data
        Transforms["detectron2/data/transforms/ (Data Transforms & Augmentation)"]:::data
        Catalog -->|"registry"| Datasets
        Catalog -->|"batch sampling"| Samplers
        Catalog -->|"augmentations"| Transforms
    end

    subgraph "Model Layer"
        ModelZoo["detectron2/model_zoo/ (Config→URL Mappings)"]:::core
        Backbones["detectron2/modeling/backbone/ (ResNet, FPN, Swin, ViT, RegNet, MViT)"]:::model
        MetaArch["detectron2/modeling/meta_arch/ (RCNN, RetinaNet, FCOS, Panoptic FPN, Semantic Seg)"]:::model
        ROIHeads["detectron2/modeling/roi_heads/ (Box, Mask, Keypoint, Tracker)"]:::model
        Proposals["detectron2/modeling/proposal_generator/ (RPN, RRPN)"]:::model
        ConfigLoader -->|"build model"| Backbones
        ConfigLoader -->|"build model"| MetaArch
        ConfigLoader -->|"build model"| ROIHeads
        ConfigLoader -->|"build model"| Proposals
        Backbones -->|"features"| MetaArch
        MetaArch -->|"proposals"| ROIHeads
        MetaArch -->|"proposals"| Proposals
    end

    subgraph "Engine Layer"
        TrainLoop["detectron2/engine/train_loop.py (Training Loop & Hooks)"]:::engine
        Solver["detectron2/solver/ (Optimizers & LR Schedulers)"]:::engine
        Checkpoint["detectron2/checkpoint/ (Checkpointing)"]:::engine
        TrainLoop --> Solver
        TrainLoop --> Checkpoint
    end

    subgraph "Evaluation & Export Layer"
        Evaluator["detectron2/evaluation/ (COCO, LVIS, Pascal, Cityscapes Evaluators)"]:::core
        Visualizer["detectron2/utils/visualizer.py (Prediction Visualizer)"]:::core
        Export["detectron2/export/ (TorchScript & Caffe2 Export)"]:::core
        CustomOps["detectron2/layers/csrc/ (C++/CUDA Custom Ops)"]:::core
        Evaluator -->|"metrics"| Visualizer
        Export -->|"includes ops"| CustomOps
    end

    subgraph "Extensions Layer"
        Utils["detectron2/utils/ (Logging, Registry, IO, Comm)"]:::core
        Tools["tools/ (CLI Tools, Deploy Wrappers)"]:::core
        DemoApp["demo/demo.py (Demo Application)"]:::core
        Docker["docker/ (Dockerfiles & Compose)"]:::external
        ProjectsCore["detectron2/projects/ (DensePose, DeepLab, PointRend, etc.)"]:::core
        Projects["projects/ (DeepLab, DensePose, PointRend)"]:::external
        Utils --> Tools
        Tools --> DemoApp
        ProjectsCore --> Projects
    end

    %% Cross-layer flows
    ConfigLoader -->|"build data loader"| Catalog
    Catalog -->|"images+annotations"| TrainLoop
    Backbones -->|"features"| TrainLoop
    TrainLoop -->|"trained model"| Export
    TrainLoop -->|"evaluation request"| Evaluator
    Export -->|"deploy image"| Docker

    %% Click Events
    click ConfigFiles "https://github.com/mayank-atheer/detectron2/tree/main/configs/"
    click ConfigLoader "https://github.com/mayank-atheer/detectron2/tree/main/detectron2/config/"
    click Catalog "https://github.com/mayank-atheer/detectron2/blob/main/detectron2/data/catalog.py"
    click Datasets "https://github.com/mayank-atheer/detectron2/tree/main/detectron2/data/datasets/"
    click Samplers "https://github.com/mayank-atheer/detectron2/tree/main/detectron2/data/samplers/"
    click Transforms "https://github.com/mayank-atheer/detectron2/tree/main/detectron2/data/transforms/"
    click ModelZoo "https://github.com/mayank-atheer/detectron2/tree/main/detectron2/model_zoo/"
    click Backbones "https://github.com/mayank-atheer/detectron2/tree/main/detectron2/modeling/backbone/"
    click MetaArch "https://github.com/mayank-atheer/detectron2/tree/main/detectron2/modeling/meta_arch/"
    click ROIHeads "https://github.com/mayank-atheer/detectron2/tree/main/detectron2/modeling/roi_heads/"
    click Proposals "https://github.com/mayank-atheer/detectron2/tree/main/detectron2/modeling/proposal_generator/"
    click TrainLoop "https://github.com/mayank-atheer/detectron2/blob/main/detectron2/engine/train_loop.py"
    click Solver "https://github.com/mayank-atheer/detectron2/tree/main/detectron2/solver/"
    click Checkpoint "https://github.com/mayank-atheer/detectron2/tree/main/detectron2/checkpoint/"
    click Evaluator "https://github.com/mayank-atheer/detectron2/tree/main/detectron2/evaluation/"
    click Visualizer "https://github.com/mayank-atheer/detectron2/blob/main/detectron2/utils/visualizer.py"
    click Export "https://github.com/mayank-atheer/detectron2/tree/main/detectron2/export/"
    click CustomOps "https://github.com/mayank-atheer/detectron2/tree/main/detectron2/layers/csrc/"
    click Utils "https://github.com/mayank-atheer/detectron2/tree/main/detectron2/utils/"
    click Tools "https://github.com/mayank-atheer/detectron2/tree/main/tools/"
    click DemoApp "https://github.com/mayank-atheer/detectron2/blob/main/demo/demo.py"
    click Docker "https://github.com/mayank-atheer/detectron2/tree/main/docker/"
    click ProjectsCore "https://github.com/mayank-atheer/detectron2/tree/main/detectron2/projects/"
    click Projects "https://github.com/mayank-atheer/detectron2/tree/main/projects/"

    %% Styles
    classDef core fill:#D0E6FF,stroke:#036,stroke-width:1px
    classDef data fill:#D0FFD6,stroke:#036,stroke-width:1px
    classDef model fill:#FFE6B3,stroke:#E65C00,stroke-width:1px
    classDef engine fill:#FFC6C6,stroke:#A00,stroke-width:1px
    classDef external fill:#E0E0E0,stroke:#666,stroke-width:1px
```
