# eriklindernoren/pytorch-yolov3 architecture

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

- Interactive diagram: https://gitdiagram.com/eriklindernoren/pytorch-yolov3
- Repository: https://github.com/eriklindernoren/pytorch-yolov3

## Overview

1. **Project shape**
   This repository is a small Python package, `pytorchyolo`, that reimplements YOLOv3 in PyTorch and exposes three top-level workflows: training, inference, and evaluation. The package is designed to be used both as a CLI toolset and as an importable library in other Python projects.

2. **Core model layer**
   `pytorchyolo/models.py` is the model-loading boundary. It interprets Darknet-style network definitions from `.cfg` files and loads compatible YOLO weights, including YOLOv3 and some YOLOv4/YOLOv7 weights as noted in the README. This is the main abstraction between model configuration files and the PyTorch runtime.

3. **Inference subsystem**
   `pytorchyolo/detect.py` handles single-image or batch-style detection. The data flow is: image input → preprocessing/transforms → model forward pass → decoding YOLO outputs → non-max suppression → returned boxes in image coordinates. The README’s API example shows the external contract: images arrive as OpenCV arrays, are converted to RGB, and detections are returned as `[x1, y1, x2, y2, confidence, class]`.

4. **Training subsystem**
   `pytorchyolo/train.py` contains the training loop and orchestration around dataloading, loss computation, optimizer steps, checkpointing, and logging. It supports both standard COCO training and custom datasets through external config files and pretrained weights. TensorBoard logging is part of the training path via the `logs` directory.

5. **Evaluation subsystem**
   `pytorchyolo/test.py` evaluates checkpoints against a dataset, with the README focusing on COCO test evaluation and mAP at IoU 0.5. This is a distinct execution path from training, using the same model and dataset plumbing but with metrics-oriented output instead of weight updates.

6. **Shared utility layer**
   `pytorchyolo/utils/` is the shared support layer that makes the three workflows consistent. It includes parsing of config/data files, dataset loading, image augmentations, coordinate transforms, loss functions, logging, and general YOLO utilities such as bounding-box math and postprocessing. This folder is the main internal dependency hub for both training and inference.

7. **Data and annotation contract**
   The repository assumes Darknet-style dataset organization. COCO data is referenced through `config/coco.data`, while custom datasets use `config/custom.data` plus `data/custom/classes.names`, `train.txt`, `valid.txt`, and per-image label files in `data/custom/labels/`. Labels are stored as normalized class and box tuples, which is a key boundary between dataset preparation and model training.

8. **Model configuration and presets**
   `config/yolov3.cfg` and `config/yolov3-tiny.cfg` define network topology, and `config/create_custom_model.sh` generates a custom config with the correct number of classes. These configs are a major architectural input: the same codebase can instantiate different YOLO variants without changing Python code.

9. **Weights and external artifacts**
   `weights/` contains the weight-management boundary, with `download_weights.sh` fetching pretrained checkpoints such as `darknet53.conv.74` and `yolov3.weights`. The code expects weights to be managed separately from source, so model structure and model parameters are decoupled.

10. **Packaging and command-line surface**
   `pyproject.toml` defines the package and likely the CLI entry points used in the README: `yolo-detect`, `yolo-train`, and `yolo-test`. This makes the repository usable both as a library and as a set of standalone ML tools.

11. **Supporting assets and samples**
   `data/samples/` and `assets/` are non-code inputs/outputs used for demonstration and smoke testing. They don’t affect the core architecture, but they illustrate the expected inference pipeline and provide visual validation of model behavior.

12. **Repository-level automation**
   `.github/workflows/main.yml` and Dependabot indicate CI and dependency maintenance around the package. These do not change runtime architecture, but they confirm the project is treated as a distributable Python package with automated validation.

13. **Overall data flow**
   The central architecture is: configuration files and weights define the model; dataset files and image/label folders feed training and evaluation; inference accepts raw images and returns detections. Most subsystems meet through shared utility code, while `models.py` is the primary boundary between serialized YOLO definitions and the PyTorch execution graph.

Main entry points: [`pytorchyolo/train.py`](https://github.com/eriklindernoren/pytorch-yolov3/blob/master/pytorchyolo/train.py) (Train), [`pytorchyolo/models.py`](https://github.com/eriklindernoren/pytorch-yolov3/blob/master/pytorchyolo/models.py) (Models), [`pytorchyolo/detect.py`](https://github.com/eriklindernoren/pytorch-yolov3/blob/master/pytorchyolo/detect.py) (Detect), [`pytorchyolo/test.py`](https://github.com/eriklindernoren/pytorch-yolov3/blob/master/pytorchyolo/test.py) (Test)

## Diagram (Mermaid)

```mermaid
flowchart TD

subgraph group_core["Core package"]
  node_models["Models<br/>model boundary<br/>[models.py]"]
  node_detect["Detect<br/>inference<br/>[detect.py]"]
  node_train["Train<br/>[train.py]"]
  node_test["Test<br/>evaluation<br/>[test.py]"]
  node_parse_config["Config parse<br/>config io<br/>[parse_config.py]"]
  node_datasets[("Datasets<br/>data loading<br/>[datasets.py]")]
  node_transforms["Transforms<br/>preprocess<br/>[transforms.py]"]
  node_augmentations["Augment<br/>[augmentations.py]"]
  node_loss["Loss<br/>optimization<br/>[loss.py]"]
  node_yolo_utils["YOLO utils<br/>postprocess<br/>[utils.py]"]
  node_logger["Logger<br/>logging<br/>[logger.py]"]
end

subgraph group_configs["Configs and data"]
  node_yolov3_cfg["YOLOv3 cfg<br/>model config<br/>[yolov3.cfg]"]
  node_yolov3_tiny_cfg["Tiny cfg<br/>model config<br/>[yolov3-tiny.cfg]"]
  node_coco_data["COCO data<br/>dataset config<br/>[coco.data]"]
  node_custom_data["Custom data<br/>dataset config<br/>[custom.data]"]
  node_custom_model_sh["Custom model<br/>config script"]
  node_custom_dataset[("Custom dataset<br/>data contract")]
  node_samples["Samples<br/>demo assets"]
end

subgraph group_ops["Ops and packaging"]
  node_weights["Weights<br/>artifacts"]
  node_package["Pyproject<br/>package config<br/>[pyproject.toml]"]
  node_ci["CI<br/>automation<br/>[main.yml]"]
end

node_parse_config -->|"reads"| node_yolov3_cfg
node_parse_config -->|"reads"| node_yolov3_tiny_cfg
node_models -->|"loads"| node_parse_config
node_models -->|"loads"| node_weights
node_detect -->|"uses"| node_models
node_detect -->|"preprocess"| node_transforms
node_detect -->|"postprocess"| node_yolo_utils
node_train -->|"uses"| node_models
node_train -->|"loads"| node_datasets
node_train -->|"augments"| node_augmentations
node_train -->|"optimizes"| node_loss
node_train -->|"logs"| node_logger
node_test -->|"uses"| node_models
node_test -->|"loads"| node_datasets
node_test -->|"metrics"| node_yolo_utils
node_datasets -->|"reads"| node_custom_dataset
node_datasets -->|"reads"| node_coco_data
node_custom_model_sh -->|"targets"| node_custom_data
node_package -->|"entrypoint"| node_detect
node_package -->|"entrypoint"| node_train
node_package -->|"entrypoint"| node_test
node_ci -->|"validates"| node_package
node_ci -->|"checks"| node_models

click node_models "https://github.com/eriklindernoren/pytorch-yolov3/blob/master/pytorchyolo/models.py"
click node_detect "https://github.com/eriklindernoren/pytorch-yolov3/blob/master/pytorchyolo/detect.py"
click node_train "https://github.com/eriklindernoren/pytorch-yolov3/blob/master/pytorchyolo/train.py"
click node_test "https://github.com/eriklindernoren/pytorch-yolov3/blob/master/pytorchyolo/test.py"
click node_parse_config "https://github.com/eriklindernoren/pytorch-yolov3/blob/master/pytorchyolo/utils/parse_config.py"
click node_datasets "https://github.com/eriklindernoren/pytorch-yolov3/blob/master/pytorchyolo/utils/datasets.py"
click node_transforms "https://github.com/eriklindernoren/pytorch-yolov3/blob/master/pytorchyolo/utils/transforms.py"
click node_augmentations "https://github.com/eriklindernoren/pytorch-yolov3/blob/master/pytorchyolo/utils/augmentations.py"
click node_loss "https://github.com/eriklindernoren/pytorch-yolov3/blob/master/pytorchyolo/utils/loss.py"
click node_yolo_utils "https://github.com/eriklindernoren/pytorch-yolov3/blob/master/pytorchyolo/utils/utils.py"
click node_logger "https://github.com/eriklindernoren/pytorch-yolov3/blob/master/pytorchyolo/utils/logger.py"
click node_yolov3_cfg "https://github.com/eriklindernoren/pytorch-yolov3/blob/master/config/yolov3.cfg"
click node_yolov3_tiny_cfg "https://github.com/eriklindernoren/pytorch-yolov3/blob/master/config/yolov3-tiny.cfg"
click node_coco_data "https://github.com/eriklindernoren/pytorch-yolov3/blob/master/config/coco.data"
click node_custom_data "https://github.com/eriklindernoren/pytorch-yolov3/blob/master/config/custom.data"
click node_custom_model_sh "https://github.com/eriklindernoren/pytorch-yolov3/blob/master/config/create_custom_model.sh"
click node_custom_dataset "https://github.com/eriklindernoren/pytorch-yolov3/tree/master/data/custom"
click node_samples "https://github.com/eriklindernoren/pytorch-yolov3/tree/master/data/samples"
click node_weights "https://github.com/eriklindernoren/pytorch-yolov3/blob/master/weights/download_weights.sh"
click node_package "https://github.com/eriklindernoren/pytorch-yolov3/blob/master/pyproject.toml"
click node_ci "https://github.com/eriklindernoren/pytorch-yolov3/blob/master/.github/workflows/main.yml"

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_models,node_detect,node_train,node_test,node_parse_config,node_datasets,node_transforms,node_augmentations,node_loss,node_yolo_utils,node_logger toneBlue
class node_yolov3_cfg,node_yolov3_tiny_cfg,node_coco_data,node_custom_data,node_custom_model_sh,node_custom_dataset,node_samples toneAmber
class node_weights,node_package,node_ci toneMint
```

## Components

### Core package

PyTorch YOLO model, workflows, and shared utilities

- [Models](https://github.com/eriklindernoren/pytorch-yolov3/blob/master/pytorchyolo/models.py): model boundary · Loads Darknet-style configs and pretrained YOLO weights into PyTorch · `pytorchyolo/models.py`
- [Detect](https://github.com/eriklindernoren/pytorch-yolov3/blob/master/pytorchyolo/detect.py): inference · Single-image and batch detection workflow · `pytorchyolo/detect.py`
- [Train](https://github.com/eriklindernoren/pytorch-yolov3/blob/master/pytorchyolo/train.py): training · Training loop, checkpointing, and logging orchestration · `pytorchyolo/train.py`
- [Test](https://github.com/eriklindernoren/pytorch-yolov3/blob/master/pytorchyolo/test.py): evaluation · Checkpoint evaluation and mAP reporting · `pytorchyolo/test.py`
- [Config parse](https://github.com/eriklindernoren/pytorch-yolov3/blob/master/pytorchyolo/utils/parse_config.py): config io · Reads Darknet-style model and data configuration files · `pytorchyolo/utils/parse_config.py`
- [Datasets](https://github.com/eriklindernoren/pytorch-yolov3/blob/master/pytorchyolo/utils/datasets.py): data loading · Dataset loading and sample/label plumbing · `pytorchyolo/utils/datasets.py`
- [Transforms](https://github.com/eriklindernoren/pytorch-yolov3/blob/master/pytorchyolo/utils/transforms.py): preprocess · Image and coordinate preprocessing for training and inference · `pytorchyolo/utils/transforms.py`
- [Augment](https://github.com/eriklindernoren/pytorch-yolov3/blob/master/pytorchyolo/utils/augmentations.py): augmentation · Training-time image augmentation helpers · `pytorchyolo/utils/augmentations.py`
- [Loss](https://github.com/eriklindernoren/pytorch-yolov3/blob/master/pytorchyolo/utils/loss.py): optimization · YOLO loss computation used during training · `pytorchyolo/utils/loss.py`
- [YOLO utils](https://github.com/eriklindernoren/pytorch-yolov3/blob/master/pytorchyolo/utils/utils.py): postprocess · Bounding-box math, decoding, and NMS helpers · `pytorchyolo/utils/utils.py`
- [Logger](https://github.com/eriklindernoren/pytorch-yolov3/blob/master/pytorchyolo/utils/logger.py): logging · Training metrics and TensorBoard logging support · `pytorchyolo/utils/logger.py`

### Configs and data

Model definitions, dataset contracts, and sample artifacts

- [YOLOv3 cfg](https://github.com/eriklindernoren/pytorch-yolov3/blob/master/config/yolov3.cfg): model config · Primary Darknet network definition · `config/yolov3.cfg`
- [Tiny cfg](https://github.com/eriklindernoren/pytorch-yolov3/blob/master/config/yolov3-tiny.cfg): model config · Smaller YOLOv3 variant definition · `config/yolov3-tiny.cfg`
- [COCO data](https://github.com/eriklindernoren/pytorch-yolov3/blob/master/config/coco.data): dataset config · COCO dataset configuration · `config/coco.data`
- [Custom data](https://github.com/eriklindernoren/pytorch-yolov3/blob/master/config/custom.data): dataset config · Custom dataset configuration · `config/custom.data`
- [Custom model](https://github.com/eriklindernoren/pytorch-yolov3/blob/master/config/create_custom_model.sh): config script · Generates a custom YOLO config for the desired class count · `config/create_custom_model.sh`
- [Custom dataset](https://github.com/eriklindernoren/pytorch-yolov3/tree/master/data/custom): data contract · Custom images, labels, and class names used by training and evaluation · `data/custom`
- [Samples](https://github.com/eriklindernoren/pytorch-yolov3/tree/master/data/samples): demo assets · Example inference images and outputs · `data/samples`

### Ops and packaging

Build, weights, and repository automation

- [Weights](https://github.com/eriklindernoren/pytorch-yolov3/blob/master/weights/download_weights.sh): artifacts · Pretrained checkpoints and download script · `weights/download_weights.sh`
- [Pyproject](https://github.com/eriklindernoren/pytorch-yolov3/blob/master/pyproject.toml): package config · Build metadata and CLI entry points · `pyproject.toml`
- [CI](https://github.com/eriklindernoren/pytorch-yolov3/blob/master/.github/workflows/main.yml): automation · Workflow and dependency automation · `.github/workflows/main.yml`

## Connections

- Config parse → YOLOv3 cfg: reads · Parses model topology definitions
- Config parse → Tiny cfg: reads · Parses alternate model topology definitions
- Models → Config parse: loads · Builds network structure from config files
- Models → Weights: loads · Imports pretrained checkpoint parameters
- Detect → Models: uses · Runs inference through loaded model
- Detect → Transforms: preprocess · Prepares images and coordinates
- Detect → YOLO utils: postprocess · Decodes outputs and applies NMS
- Train → Models: uses · Constructs the training model
- Train → Datasets: loads · Reads training samples and labels
- Train → Augment: augments · Applies training-time augmentation
- Train → Loss: optimizes · Computes YOLO loss for backpropagation
- Train → Logger: logs · Writes metrics and TensorBoard summaries
- Test → Models: uses · Evaluates checkpoints with the same model boundary
- Test → Datasets: loads · Reads evaluation data
- Test → YOLO utils: metrics · Uses output decoding and evaluation helpers
- Datasets → Custom dataset: reads · Consumes custom image and label layout
- Datasets → COCO data: reads · Consumes COCO dataset contract
- Custom model → Custom data: targets · Generates config aligned with custom classes
- Pyproject → Detect: entrypoint · Exposes detection CLI
- Pyproject → Train: entrypoint · Exposes training CLI
- Pyproject → Test: entrypoint · Exposes evaluation CLI
- CI → Pyproject: validates · Runs package validation and maintenance automation
- CI → Models: checks · Exercises core package behavior
