# cristianmc18/pytorch-yolov3 architecture

> Architecture diagram of the GitHub repository cristianmc18/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/cristianmc18/pytorch-yolov3
- Repository: https://github.com/cristianmc18/pytorch-yolov3

## Overview

1. This repository is a self-contained PyTorch implementation of the YOLO object-detection family, centered on YOLOv3 but able to load compatible YOLOv4/YOLOv7-style weights. It is organized as a small library plus CLI tools for training, inference, and evaluation.

2. The core runtime lives in `pytorchyolo/`. The main entry points are `train.py`, `detect.py`, and `test.py`, which map directly to the three primary workflows: fit a detector, run predictions on images, and compute evaluation metrics on a labeled dataset.

3. `models.py` is the model-construction boundary. It loads a Darknet-style model definition from `.cfg` files in `config/`, builds the corresponding PyTorch network, and attaches pretrained weights from `weights/`. This is what makes the project configuration-driven rather than hardcoding the architecture in Python.

4. The configuration layer is externalized in `config/`. The supplied files include standard YOLOv3 and YOLOv3-tiny configs, a COCO data descriptor, and a script to generate a custom model definition for a chosen number of classes. These config files determine network shape, anchors, class count, and dataset wiring.

5. The data layer is explicit and file-based. `data/coco.names` and `data/custom/classes.names` define class vocabularies, while `data/custom/train.txt` and `valid.txt` list image paths for train/validation splits. Labels are stored as per-image text files under `data/custom/labels/`, using normalized bounding-box coordinates, which the dataloader pairs with images in `data/custom/images/`.

6. Dataset ingestion and augmentation are handled in `pytorchyolo/utils/datasets.py`, `augmentations.py`, and `transforms.py`. This layer is responsible for loading images/targets, applying resizing and training augmentations, and preparing batches in the format expected by the YOLO loss and model heads.

7. Training is coordinated by `train.py` and the loss logic in `utils/loss.py`. The training flow loads the model, feeds batches from the dataset pipeline, computes detection losses for the YOLO heads, and logs progress through `utils/logger.py`. TensorBoard is an intentional part of the training path, with logs written to a configurable `logs/` directory.

8. Inference is a separate path in `detect.py`, built for single-image or directory-based prediction. The typical input boundary is an OpenCV image converted to RGB numpy format, then passed through the model and postprocessed into bounding boxes with confidence scores and class IDs.

9. Postprocessing and shared detection utilities live in `utils/utils.py`. This is where common detection operations such as box conversion, non-max suppression, and evaluation helpers are typically centralized, allowing both inference and testing to reuse the same logic.

10. Evaluation is isolated in `test.py`, which runs the model against a labeled benchmark dataset and reports detection metrics such as mAP. The README shows this path being used for COCO test evaluation, making it the repo’s validation boundary separate from training.

11. Weights and datasets are intentionally kept outside the code path as downloadable artifacts. `weights/download_weights.sh` fetches pretrained detector and backbone weights, while `data/get_coco_dataset.sh` bootstraps the COCO dataset. This separation keeps the repository lightweight and makes the model/data assets interchangeable.

12. Packaging and execution are designed for both development and reuse. The project uses Poetry for local isolation, but also exposes a pip-installable package `pytorchyolo` with CLI commands such as `yolo-detect`, `yolo-train`, and `yolo-test`, making the library usable as an embedded dependency in other Python projects.

13. The main architectural flow is: config files define the network and dataset; data loaders prepare image/label batches; the model and loss drive training; evaluation and inference reuse the same model plus shared utilities; and external weight/data scripts supply the required artifacts. For a diagram, the key boundaries are config/data/weights on one side, and model/training/inference/evaluation in the Python package on the other.

Main entry points: [`pytorchyolo/utils/datasets.py`](https://github.com/cristianmc18/pytorch-yolov3/blob/master/pytorchyolo/utils/datasets.py) (Datasets), [`pytorchyolo/train.py`](https://github.com/cristianmc18/pytorch-yolov3/blob/master/pytorchyolo/train.py) (Train), [`config/coco.data`](https://github.com/cristianmc18/pytorch-yolov3/blob/master/config/coco.data) (Data cfgs), [`pytorchyolo/models.py`](https://github.com/cristianmc18/pytorch-yolov3/blob/master/pytorchyolo/models.py) (Model builder)

## Diagram (Mermaid)

```mermaid
flowchart TD

subgraph group_g_config["Config & data"]
  node_n_cfg["Model cfgs<br/>model config<br/>[yolov3.cfg]"]
  node_n_data_cfg["Data cfgs<br/>dataset config<br/>[coco.data]"]
  node_n_classes["Class names<br/>vocabulary<br/>[coco.names]"]
  node_n_splits["Split files<br/>image lists<br/>[train.txt]"]
  node_n_labels["Labels<br/>annotation files<br/>[train.txt]"]
  node_n_images[("Images")]
  node_n_weights["Weights<br/>pretrained artifacts"]
  node_n_dataset_script["Dataset script<br/>bootstrap script"]
end

subgraph group_g_core["Python package"]
  node_n_models["Model builder<br/>model boundary<br/>[models.py]"]
  node_n_datasets["Datasets<br/>data loader<br/>[datasets.py]"]
  node_n_aug["Augmentations<br/>[augmentations.py]"]
  node_n_transforms["Transforms<br/>preprocessing<br/>[transforms.py]"]
  node_n_loss["YOLO loss<br/>loss module<br/>[loss.py]"]
  node_n_utils["Detection utils<br/>postprocess utils<br/>[utils.py]"]
  node_n_logger["Logger<br/>logging<br/>[logger.py]"]
  node_n_parse["Config parser<br/>config parsing<br/>[parse_config.py]"]
  node_n_pkg["Package<br/>[__init__.py]"]
end

subgraph group_g_cli["CLI workflows"]
  node_n_train["Train<br/>cli entrypoint<br/>[train.py]"]
  node_n_detect["Detect<br/>cli entrypoint<br/>[detect.py]"]
  node_n_test["Test<br/>cli entrypoint<br/>[test.py]"]
end

node_n_train -->|"builds"| node_n_models
node_n_train -->|"loads"| node_n_datasets
node_n_train -->|"optimizes"| node_n_loss
node_n_train -->|"logs"| node_n_logger
node_n_train -->|"reads config"| node_n_parse
node_n_detect -->|"loads"| node_n_models
node_n_detect -->|"postprocesses"| node_n_utils
node_n_detect -->|"preprocesses"| node_n_transforms
node_n_test -->|"loads"| node_n_models
node_n_test -->|"loads"| node_n_datasets
node_n_test -->|"scores"| node_n_utils
node_n_models -->|"consumes"| node_n_parse
node_n_models -->|"initializes"| node_n_weights
node_n_datasets -->|"applies"| node_n_aug
node_n_datasets -->|"uses"| node_n_transforms
node_n_datasets -->|"indexes"| node_n_splits
node_n_datasets -->|"pairs"| node_n_labels
node_n_datasets -->|"reads"| node_n_images
node_n_parse -->|"parses"| node_n_cfg
node_n_parse -->|"parses"| node_n_data_cfg
node_n_data_cfg -->|"references"| node_n_classes
node_n_data_cfg -->|"references"| node_n_splits
node_n_data_cfg -->|"references"| node_n_images
node_n_data_cfg -->|"references"| node_n_labels
node_n_dataset_script -->|"bootstraps"| node_n_data_cfg
node_n_weights -->|"supplies"| node_n_models
node_n_pkg -->|"exposes"| node_n_train
node_n_pkg -->|"exposes"| node_n_detect
node_n_pkg -->|"exposes"| node_n_test

click node_n_train "https://github.com/cristianmc18/pytorch-yolov3/blob/master/pytorchyolo/train.py"
click node_n_detect "https://github.com/cristianmc18/pytorch-yolov3/blob/master/pytorchyolo/detect.py"
click node_n_test "https://github.com/cristianmc18/pytorch-yolov3/blob/master/pytorchyolo/test.py"
click node_n_models "https://github.com/cristianmc18/pytorch-yolov3/blob/master/pytorchyolo/models.py"
click node_n_datasets "https://github.com/cristianmc18/pytorch-yolov3/blob/master/pytorchyolo/utils/datasets.py"
click node_n_aug "https://github.com/cristianmc18/pytorch-yolov3/blob/master/pytorchyolo/utils/augmentations.py"
click node_n_transforms "https://github.com/cristianmc18/pytorch-yolov3/blob/master/pytorchyolo/utils/transforms.py"
click node_n_loss "https://github.com/cristianmc18/pytorch-yolov3/blob/master/pytorchyolo/utils/loss.py"
click node_n_utils "https://github.com/cristianmc18/pytorch-yolov3/blob/master/pytorchyolo/utils/utils.py"
click node_n_logger "https://github.com/cristianmc18/pytorch-yolov3/blob/master/pytorchyolo/utils/logger.py"
click node_n_parse "https://github.com/cristianmc18/pytorch-yolov3/blob/master/pytorchyolo/utils/parse_config.py"
click node_n_cfg "https://github.com/cristianmc18/pytorch-yolov3/blob/master/config/yolov3.cfg"
click node_n_data_cfg "https://github.com/cristianmc18/pytorch-yolov3/blob/master/config/coco.data"
click node_n_classes "https://github.com/cristianmc18/pytorch-yolov3/blob/master/data/coco.names"
click node_n_splits "https://github.com/cristianmc18/pytorch-yolov3/blob/master/data/custom/train.txt"
click node_n_labels "https://github.com/cristianmc18/pytorch-yolov3/blob/master/data/custom/labels/train.txt"
click node_n_images "https://github.com/cristianmc18/pytorch-yolov3/tree/master/data/custom/images"
click node_n_weights "https://github.com/cristianmc18/pytorch-yolov3/blob/master/weights/download_weights.sh"
click node_n_dataset_script "https://github.com/cristianmc18/pytorch-yolov3/blob/master/data/get_coco_dataset.sh"
click node_n_pkg "https://github.com/cristianmc18/pytorch-yolov3/blob/master/pytorchyolo/__init__.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_n_cfg,node_n_data_cfg,node_n_classes,node_n_splits,node_n_labels,node_n_images,node_n_weights,node_n_dataset_script toneBlue
class node_n_models,node_n_datasets,node_n_aug,node_n_transforms,node_n_loss,node_n_utils,node_n_logger,node_n_parse,node_n_pkg toneAmber
class node_n_train,node_n_detect,node_n_test toneMint
```

## Components

### Config & data

Externalized model, dataset, and asset inputs

- [Model cfgs](https://github.com/cristianmc18/pytorch-yolov3/blob/master/config/yolov3.cfg): model config · Defines YOLOv3 and YOLOv3-tiny architectures · `config/yolov3.cfg`
- [Data cfgs](https://github.com/cristianmc18/pytorch-yolov3/blob/master/config/coco.data): dataset config · Defines COCO and custom dataset wiring · `config/coco.data`
- [Class names](https://github.com/cristianmc18/pytorch-yolov3/blob/master/data/coco.names): vocabulary · Class label lists for COCO and custom datasets · `data/coco.names`
- [Split files](https://github.com/cristianmc18/pytorch-yolov3/blob/master/data/custom/train.txt): image lists · Train and validation image path lists · `data/custom/train.txt`
- [Labels](https://github.com/cristianmc18/pytorch-yolov3/blob/master/data/custom/labels/train.txt): annotation files · Per-image normalized bounding box annotations · `data/custom/labels/train.txt`
- [Images](https://github.com/cristianmc18/pytorch-yolov3/tree/master/data/custom/images): dataset images · Image files paired with labels · `data/custom/images`
- [Weights](https://github.com/cristianmc18/pytorch-yolov3/blob/master/weights/download_weights.sh): pretrained artifacts · Downloaded detector and backbone weights · `weights/download_weights.sh`
- [Dataset script](https://github.com/cristianmc18/pytorch-yolov3/blob/master/data/get_coco_dataset.sh): bootstrap script · Downloads and prepares COCO data · `data/get_coco_dataset.sh`

### Python package

Model runtime and shared utilities

- [Model builder](https://github.com/cristianmc18/pytorch-yolov3/blob/master/pytorchyolo/models.py): model boundary · Builds Darknet-style YOLO networks from config and loads weights · `pytorchyolo/models.py`
- [Datasets](https://github.com/cristianmc18/pytorch-yolov3/blob/master/pytorchyolo/utils/datasets.py): data loader · Loads images and targets for training and evaluation · `pytorchyolo/utils/datasets.py`
- [Augmentations](https://github.com/cristianmc18/pytorch-yolov3/blob/master/pytorchyolo/utils/augmentations.py): augmentations · Applies training-time image and target augmentation · `pytorchyolo/utils/augmentations.py`
- [Transforms](https://github.com/cristianmc18/pytorch-yolov3/blob/master/pytorchyolo/utils/transforms.py): preprocessing · Prepares inputs for the model and loss · `pytorchyolo/utils/transforms.py`
- [YOLO loss](https://github.com/cristianmc18/pytorch-yolov3/blob/master/pytorchyolo/utils/loss.py): loss module · Computes detection losses for training · `pytorchyolo/utils/loss.py`
- [Detection utils](https://github.com/cristianmc18/pytorch-yolov3/blob/master/pytorchyolo/utils/utils.py): postprocess utils · Shared box conversion, NMS, and evaluation helpers · `pytorchyolo/utils/utils.py`
- [Logger](https://github.com/cristianmc18/pytorch-yolov3/blob/master/pytorchyolo/utils/logger.py): logging · Records training progress and TensorBoard metrics · `pytorchyolo/utils/logger.py`
- [Config parser](https://github.com/cristianmc18/pytorch-yolov3/blob/master/pytorchyolo/utils/parse_config.py): config parsing · Reads Darknet-style model and data config files · `pytorchyolo/utils/parse_config.py`
- [Package](https://github.com/cristianmc18/pytorch-yolov3/blob/master/pytorchyolo/__init__.py): python package · Installable pytorchyolo library exposed through CLI commands · `pytorchyolo/__init__.py`

### CLI workflows

Training, inference, and evaluation entry points

- [Train](https://github.com/cristianmc18/pytorch-yolov3/blob/master/pytorchyolo/train.py): cli entrypoint · Coordinates detector training · `pytorchyolo/train.py`
- [Detect](https://github.com/cristianmc18/pytorch-yolov3/blob/master/pytorchyolo/detect.py): cli entrypoint · Runs prediction on images or folders · `pytorchyolo/detect.py`
- [Test](https://github.com/cristianmc18/pytorch-yolov3/blob/master/pytorchyolo/test.py): cli entrypoint · Runs evaluation on labeled data · `pytorchyolo/test.py`

## Connections

- Train → Model builder: builds · Constructs the detector model
- Train → Datasets: loads · Reads training batches
- Train → YOLO loss: optimizes · Computes detection loss
- Train → Logger: logs · Writes progress and TensorBoard metrics
- Train → Config parser: reads config · Parses model and data settings
- Detect → Model builder: loads · Instantiates model for inference
- Detect → Detection utils: postprocesses · Converts boxes and applies NMS
- Detect → Transforms: preprocesses · Formats input images
- Test → Model builder: loads · Instantiates model for evaluation
- Test → Datasets: loads · Reads evaluation data
- Test → Detection utils: scores · Reuses detection helpers for metrics
- Model builder → Config parser: consumes · Reads Darknet config structure
- Model builder → Weights: initializes · Loads pretrained parameters
- Datasets → Augmentations: applies · Training-time augmentation pipeline
- Datasets → Transforms: uses · Prepares tensors for batches
- Datasets → Split files: indexes · Uses train and validation lists
- Datasets → Labels: pairs · Associates labels with images
- Datasets → Images: reads · Loads image files
- Config parser → Model cfgs: parses · Reads network definitions
- Config parser → Data cfgs: parses · Reads dataset descriptors
- Data cfgs → Class names: references · Points to class vocabulary files
- Data cfgs → Split files: references · Points to dataset splits
- Data cfgs → Images: references · Points to image roots
- Data cfgs → Labels: references · Points to annotation paths
- Dataset script → Data cfgs: bootstraps · Prepares the COCO dataset
- Weights → Model builder: supplies · Provides pretrained assets
- Package → Train: exposes · Installs training command
- Package → Detect: exposes · Installs detection command
- Package → Test: exposes · Installs evaluation command
