# kooscii/badnets architecture

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

- Interactive diagram: https://gitdiagram.com/kooscii/badnets
- Repository: https://github.com/kooscii/badnets

## Overview

BadNets is a research workflow for studying backdoored object detectors on the US Traffic Signs dataset. Users fetch the data, generate targeted or random poisoned variants, train Faster R-CNN models, and evaluate clean or poisoned models. The repository combines dataset-specific code with Faster R-CNN proposal, ROI, and training stages, backed by Caffe’s network and solver runtime. The README documents the high-level workflow; several experiment scripts and runtime details are not included in the supplied excerpts, so those connections are omitted or marked unverified.

Main entry points: [`py-faster-rcnn/tools/train_faster_rcnn_alt_opt.py`](https://github.com/kooscii/badnets/blob/master/py-faster-rcnn/tools/train_faster_rcnn_alt_opt.py) (Training orchestrator), [`py-faster-rcnn/lib/fast_rcnn/train.py`](https://github.com/kooscii/badnets/blob/master/py-faster-rcnn/lib/fast_rcnn/train.py) (Fast R-CNN trainer), [`py-faster-rcnn/caffe-fast-rcnn/src/caffe/net.cpp`](https://github.com/kooscii/badnets/blob/master/py-faster-rcnn/caffe-fast-rcnn/src/caffe/net.cpp) (Network execution), [`datasets/fetch_usts.py`](https://github.com/kooscii/badnets/blob/master/datasets/fetch_usts.py) (Dataset fetcher)

## Diagram (Mermaid)

```mermaid
flowchart TD

subgraph group_data["Dataset preparation"]
  node_usts[("USTS data")]
  node_fetch["Dataset fetcher<br/>[fetch_usts.py]"]
  node_poison["Attack generator<br/>[attack_usts.py]"]
  node_dataset_factory["Dataset registry<br/>[factory.py]"]
  node_usts_adapter["USTS adapter<br/>[usts.py]"]
end

subgraph group_detector["Detection pipeline"]
  node_alt_train["Training orchestrator"]
  node_rpn["Proposal generation<br/>[generate.py]"]
  node_roi["ROI minibatches<br/>[minibatch.py]"]
  node_trainer["Fast R-CNN trainer<br/>[train.py]"]
  node_config["Model configuration<br/>[config.py]"]
end

subgraph group_runtime["Caffe runtime"]
  node_caffe_api["Python Caffe API<br/>[__init__.py]"]
  node_network["Network execution<br/>[net.cpp]"]
  node_solver["Optimization solver<br/>[solver.hpp]"]
  node_layers["Layer operations<br/>[layer.hpp]"]
end

subgraph group_evaluation["Inference and evaluation"]
  node_detector_api["Detector interface<br/>[detector.py]"]
  node_usts_eval["USTS evaluation<br/>[usts_eval.py]"]
  node_reval["Detection evaluation<br/>[reval.py]"]
end

node_user(("Researcher"))

node_user -->|"requests download"| node_fetch
node_fetch -->|"downloads data"| node_usts
node_user -->|"selects attack"| node_poison
node_poison -->|"creates poisoned data"| node_usts
node_dataset_factory -->|"registers dataset"| node_usts_adapter
node_user -->|"starts training"| node_alt_train
node_alt_train -->|"dispatches proposal stage"| node_rpn
node_alt_train -->|"starts training stage"| node_trainer
node_trainer -->|"reads ROI database"| node_roi
node_trainer -->|"uses solver API"| node_caffe_api
node_caffe_api -->|"exposes network"| node_network
node_network -->|"executes layers"| node_layers
node_solver -.->|"optimizes network"| node_network
node_user -.->|"runs inference"| node_detector_api
node_detector_api -.->|"evaluates detections"| node_usts_eval
node_user -.->|"requests evaluation"| node_reval

click node_fetch "https://github.com/kooscii/badnets/blob/master/datasets/fetch_usts.py"
click node_poison "https://github.com/kooscii/badnets/blob/master/datasets/attack_usts.py"
click node_dataset_factory "https://github.com/kooscii/badnets/blob/master/py-faster-rcnn/lib/datasets/factory.py"
click node_usts_adapter "https://github.com/kooscii/badnets/blob/master/py-faster-rcnn/lib/datasets/usts.py"
click node_alt_train "https://github.com/kooscii/badnets/blob/master/py-faster-rcnn/tools/train_faster_rcnn_alt_opt.py"
click node_rpn "https://github.com/kooscii/badnets/blob/master/py-faster-rcnn/lib/rpn/generate.py"
click node_roi "https://github.com/kooscii/badnets/blob/master/py-faster-rcnn/lib/roi_data_layer/minibatch.py"
click node_trainer "https://github.com/kooscii/badnets/blob/master/py-faster-rcnn/lib/fast_rcnn/train.py"
click node_config "https://github.com/kooscii/badnets/blob/master/py-faster-rcnn/lib/fast_rcnn/config.py"
click node_caffe_api "https://github.com/kooscii/badnets/blob/master/py-faster-rcnn/caffe-fast-rcnn/python/caffe/__init__.py"
click node_network "https://github.com/kooscii/badnets/blob/master/py-faster-rcnn/caffe-fast-rcnn/src/caffe/net.cpp"
click node_solver "https://github.com/kooscii/badnets/blob/master/py-faster-rcnn/caffe-fast-rcnn/include/caffe/solver.hpp"
click node_layers "https://github.com/kooscii/badnets/blob/master/py-faster-rcnn/caffe-fast-rcnn/include/caffe/layer.hpp"
click node_detector_api "https://github.com/kooscii/badnets/blob/master/py-faster-rcnn/caffe-fast-rcnn/python/caffe/detector.py"
click node_usts_eval "https://github.com/kooscii/badnets/blob/master/py-faster-rcnn/lib/datasets/usts_eval.py"
click node_reval "https://github.com/kooscii/badnets/blob/master/py-faster-rcnn/tools/reval.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_usts,node_fetch,node_poison,node_dataset_factory,node_usts_adapter toneBlue
class node_alt_train,node_rpn,node_roi,node_trainer,node_config toneAmber
class node_caffe_api,node_network,node_solver,node_layers toneMint
class node_detector_api,node_usts_eval,node_reval toneRose
class node_user toneIndigo
```

## Components

### Dataset preparation

- **USTS data**
- [Dataset fetcher](https://github.com/kooscii/badnets/blob/master/datasets/fetch_usts.py): `datasets/fetch_usts.py`
- [Attack generator](https://github.com/kooscii/badnets/blob/master/datasets/attack_usts.py): `datasets/attack_usts.py`
- [Dataset registry](https://github.com/kooscii/badnets/blob/master/py-faster-rcnn/lib/datasets/factory.py): `py-faster-rcnn/lib/datasets/factory.py`
- [USTS adapter](https://github.com/kooscii/badnets/blob/master/py-faster-rcnn/lib/datasets/usts.py): `py-faster-rcnn/lib/datasets/usts.py`

### Detection pipeline

- [Training orchestrator](https://github.com/kooscii/badnets/blob/master/py-faster-rcnn/tools/train_faster_rcnn_alt_opt.py): `py-faster-rcnn/tools/train_faster_rcnn_alt_opt.py`
- [Proposal generation](https://github.com/kooscii/badnets/blob/master/py-faster-rcnn/lib/rpn/generate.py): `py-faster-rcnn/lib/rpn/generate.py`
- [ROI minibatches](https://github.com/kooscii/badnets/blob/master/py-faster-rcnn/lib/roi_data_layer/minibatch.py): `py-faster-rcnn/lib/roi_data_layer/minibatch.py`
- [Fast R-CNN trainer](https://github.com/kooscii/badnets/blob/master/py-faster-rcnn/lib/fast_rcnn/train.py): `py-faster-rcnn/lib/fast_rcnn/train.py`
- [Model configuration](https://github.com/kooscii/badnets/blob/master/py-faster-rcnn/lib/fast_rcnn/config.py): `py-faster-rcnn/lib/fast_rcnn/config.py`

### Caffe runtime

- [Python Caffe API](https://github.com/kooscii/badnets/blob/master/py-faster-rcnn/caffe-fast-rcnn/python/caffe/__init__.py): `py-faster-rcnn/caffe-fast-rcnn/python/caffe/__init__.py`
- [Network execution](https://github.com/kooscii/badnets/blob/master/py-faster-rcnn/caffe-fast-rcnn/src/caffe/net.cpp): `py-faster-rcnn/caffe-fast-rcnn/src/caffe/net.cpp`
- [Optimization solver](https://github.com/kooscii/badnets/blob/master/py-faster-rcnn/caffe-fast-rcnn/include/caffe/solver.hpp): `py-faster-rcnn/caffe-fast-rcnn/include/caffe/solver.hpp`
- [Layer operations](https://github.com/kooscii/badnets/blob/master/py-faster-rcnn/caffe-fast-rcnn/include/caffe/layer.hpp): `py-faster-rcnn/caffe-fast-rcnn/include/caffe/layer.hpp`

### Inference and evaluation

- [Detector interface](https://github.com/kooscii/badnets/blob/master/py-faster-rcnn/caffe-fast-rcnn/python/caffe/detector.py): `py-faster-rcnn/caffe-fast-rcnn/python/caffe/detector.py`
- [USTS evaluation](https://github.com/kooscii/badnets/blob/master/py-faster-rcnn/lib/datasets/usts_eval.py): `py-faster-rcnn/lib/datasets/usts_eval.py`
- [Detection evaluation](https://github.com/kooscii/badnets/blob/master/py-faster-rcnn/tools/reval.py): `py-faster-rcnn/tools/reval.py`

### Other components

- **Researcher**

## Connections

- Researcher → Dataset fetcher: requests download (evidence: `README.md`)
- Dataset fetcher → USTS data: downloads data (evidence: `README.md`)
- Researcher → Attack generator: selects attack (evidence: `README.md`)
- Attack generator → USTS data: creates poisoned data (evidence: `README.md`)
- Dataset registry → USTS adapter: registers dataset (evidence: `py-faster-rcnn/lib/datasets/factory.py`)
- Researcher → Training orchestrator: starts training (evidence: `README.md`)
- Training orchestrator → Proposal generation: dispatches proposal stage (evidence: `py-faster-rcnn/tools/train_faster_rcnn_alt_opt.py`)
- Training orchestrator → Fast R-CNN trainer: starts training stage (evidence: `py-faster-rcnn/tools/train_faster_rcnn_alt_opt.py`)
- Fast R-CNN trainer → ROI minibatches: reads ROI database (evidence: `py-faster-rcnn/lib/fast_rcnn/train.py`)
- Fast R-CNN trainer → Python Caffe API: uses solver API (evidence: `py-faster-rcnn/lib/fast_rcnn/train.py`)
- Python Caffe API → Network execution: exposes network (evidence: `py-faster-rcnn/caffe-fast-rcnn/python/caffe/__init__.py`)
- Network execution → Layer operations: executes layers (evidence: `py-faster-rcnn/caffe-fast-rcnn/src/caffe/net.cpp`)
- Optimization solver → Network execution: optimizes network
- Researcher → Detector interface: runs inference
- Detector interface → USTS evaluation: evaluates detections
- Researcher → Detection evaluation: requests evaluation
