# zhizhongchai/orf-net architecture

> Architecture diagram of the GitHub repository zhizhongchai/orf-net, generated by GitDiagram from its file tree, README and a sample of source files (last updated 2025-05-09). It is an AI-made overview: check details against the source.

- Interactive diagram: https://gitdiagram.com/zhizhongchai/orf-net
- Repository: https://github.com/zhizhongchai/orf-net

## Overview

##### Project Type and Purpose
- ORF-Net is a research-oriented deep learning framework built on top of Detectron2/AdelaiDet for omni-supervised rib fracture detection in chest radiology images.
- It provides data loading and augmentation, configurable model architectures (backbones, FPNs, heads), custom CUDA/C++ layers, training/evaluation pipelines, demo/inference scripts, and ONNX export utilities.

##### File Structure Analysis
- Top-level package “adet/” holds core code:
- adet/config: YAML config loader, default settings
- adet/data: dataset builders, mappers, augmentations, common utilities
- adet/checkpoint: checkpoint saving/loading abstractions
- adet/modeling: modular detection/segmentation architectures (ATSS, FCOS, BlendMask, MEInst, SOLOv2, etc.)
- adet/layers: Python wrappers and C++/CUDA implementations of custom ops (BezierAlign, DefROIAlign, ml_nms, deform_conv, GCN, IoU loss)
- adet/evaluation: evaluation scripts for object and text detection
- adet/utils and structures: inter-module utilities, visualization, communication
- configs/: directory of pre-written YAMLs for dozens of model configurations and dataset setups
- tools/train_net.py: entry point for training with config-file parameter
- demo/: inference utilities (demo.py, predictor.py)
- onnx/: scripts to export a trained PyTorch model to ONNX, then to Caffe/NCNN runtimes
- datasets/: dataset preparation scripts
- docs/: Sphinx documentation
- setup.py: installation and build of the package

##### Key Insights from README
- Emphasizes dependency on Detectron2 (specific commit) and installation via `python setup.py build develop`
- Training is driven by tools/train_net.py with a config file and GPU settings
- Demo and ONNX export are provided as standalone scripts

##### Diagramming the Architecture
a. Main Components to Represent
- Data Ingestion & Augmentation
- Config Management & Registry
- Model Component Library
- Custom Layer Extensions (C++/CUDA)
- Training & Evaluation Pipeline
- Inference & Demo Scripts
- ONNX Export Flow
- External Dependencies (Detectron2, PyTorch, CUDA)
b. Relationships and Interactions
- Data flows from raw images → datasets/prepare scripts → `adet/data` → DataLoader
- DataLoader outputs batched tensors → Model (backbone → neck → head)
- Model uses custom ops from `adet/layers` (via Python wrappers and C++/CUDA kernels)
- Checkpoint manager writes/loads model weights to disk (adet/checkpoint)
- Training script orchestrates data, model, and loss → optimizer → checkpoint → evaluation
- Evaluation module reads predictions and ground truth to compute metrics
- Demo/predictor loads checkpoint, runs inference, and visualizes results
- ONNX exporter loads the PyTorch model, traces graph, outputs ONNX file, and invokes conversion scripts
c. Architectural Patterns & Principles
- Modular design: each concern (data, model, layers, config, checkpoint) in its own package
- Plugin architecture: new model definitions and layers are discovered via registry
- Separation of concerns: configs separate from code, core logic independent of CLI scripts
- Extension through custom CUDA/C++ for performance-critical ops
d. Key Technologies and Frameworks
- Detectron2 for base object detection
- PyTorch as the deep learning engine
- Custom C++/CUDA kernels for specialized layers
- ONNX for cross-framework export
- YAML for configuration management

## Diagram (Mermaid)

```mermaid
flowchart TD
    %% Data Preparation Layer
    subgraph "Data Preparation" 
        direction TB
        DataPrep["Dataset Preparation Scripts"]:::data
        DataModules["Data Ingestion & Augmentation"]:::data
    end

    %% Core Package Layer
    subgraph "Core Package (adet/)" 
        direction TB
        subgraph "Config Management & Registry"
            Config["Config Loader & Defaults"]:::core
        end
        subgraph "Model Component Library"
            Modeling["Model Architectures"]:::core
        end
        subgraph "Custom Layer Extensions"
            LayersWrap["Python Wrappers"]:::ext
            LayersKern["C++/CUDA Kernels"]:::ext
        end
        subgraph "Checkpoint Management"
            CheckpointMgr["Checkpoint Manager"]:::core
        end
        subgraph "Evaluation Modules"
            Eval["Evaluation Scripts"]:::core
        end
        subgraph "Utilities"
            Utils["Common Utilities"]:::core
        end
    end

    %% CLI Scripts Layer
    subgraph "CLI & Scripts" 
        direction TB
        TrainScript["Training Entry Point"]:::script
        Demo["Inference Demo"]:::script
        Predictor["Demo Predictor"]:::script
        ONNXFlow["ONNX Export Flow"]:::script
        Setup["Installation & Build"]:::script
    end

    %% External Dependencies
    subgraph "External Dependencies"
        direction TB
        Detectron2["Detectron2"]:::external
        PyTorch["PyTorch"]:::external
        CUDA["CUDA"]:::external
    end

    %% Storage
    DiskCheckpoint["Checkpoint Storage"]:::storage
    ONNXFile["ONNX Output"]:::storage

    %% Data Flow
    DataPrep -->|"raw images"| DataModules
    DataModules -->|"image batch"| Modeling
    Modeling -->|"uses ops"| LayersWrap
    LayersWrap -->|"calls kernels"| LayersKern
    Modeling -->|"model weights"| CheckpointMgr
    CheckpointMgr -->|"save/load"| DiskCheckpoint
    Modeling -->|"trace graph"| ONNXFlow
    ONNXFlow -->|"ONNX graph"| ONNXFile
    DiskCheckpoint -->|"load weights"| Predictor
    Predictor -->|"visualization"| Demo

    %% Control Flow
    TrainScript -->|"reads YAML"| Config
    TrainScript --> Modeling
    TrainScript --> CheckpointMgr
    TrainScript --> Eval
    Demo --> CheckpointMgr
    Demo --> Utils

    %% Dependencies
    Detectron2 --> Modeling
    PyTorch --> Modeling
    CUDA --> LayersKern

    %% Click Events
    click DataPrep "https://github.com/zhizhongchai/orf-net/tree/main/datasets/"
    click DataModules "https://github.com/zhizhongchai/orf-net/tree/main/adet/data/"
    click Config "https://github.com/zhizhongchai/orf-net/tree/main/adet/config/"
    click Config "https://github.com/zhizhongchai/orf-net/tree/main/configs/"
    click Modeling "https://github.com/zhizhongchai/orf-net/tree/main/adet/modeling/"
    click LayersWrap "https://github.com/zhizhongchai/orf-net/tree/main/adet/layers/"
    click LayersKern "https://github.com/zhizhongchai/orf-net/tree/main/adet/layers/csrc/"
    click CheckpointMgr "https://github.com/zhizhongchai/orf-net/tree/main/adet/checkpoint/"
    click Eval "https://github.com/zhizhongchai/orf-net/tree/main/adet/evaluation/"
    click TrainScript "https://github.com/zhizhongchai/orf-net/blob/main/tools/train_net.py"
    click Demo "https://github.com/zhizhongchai/orf-net/blob/main/demo/demo.py"
    click Predictor "https://github.com/zhizhongchai/orf-net/blob/main/demo/predictor.py"
    click ONNXFlow "https://github.com/zhizhongchai/orf-net/tree/main/onnx/"
    click Setup "https://github.com/zhizhongchai/orf-net/blob/main/setup.py"

    %% Styles
    classDef data fill:#9f6,stroke:#393,stroke-width:1px;
    classDef core fill:#6af,stroke:#036,stroke-width:1px;
    classDef ext fill:#f96,stroke:#d60,stroke-width:1px;
    classDef script fill:#c9c,stroke:#606,stroke-width:1px;
    classDef external fill:#ccc,stroke:#888,stroke-width:1px;
    classDef storage fill:#eef,stroke:#55f,stroke-width:1px;
```
