How cristianmc18/pytorch-yolov3 works
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: datasets.py Datasets · train.py Train · coco.data Data cfgs · models.py Model builder
Components 20
Config & data
Externalized model, dataset, and asset inputs
- Model cfgs · Defines YOLOv3 and YOLOv3-tiny architectures
config/yolov3.cfg - Data cfgs · Defines COCO and custom dataset wiring
config/coco.data - Class names · Class label lists for COCO and custom datasets
data/coco.names - Split files · Train and validation image path lists
data/custom/train.txt - Labels · Per-image normalized bounding box annotations
data/custom/labels/train.txt - Images · Image files paired with labels
data/custom/images - Weights · Downloaded detector and backbone weights
weights/download_weights.sh - Dataset script · Downloads and prepares COCO data
data/get_coco_dataset.sh
Python package
Model runtime and shared utilities
- Model builder · Builds Darknet-style YOLO networks from config and loads weights
pytorchyolo/models.py - Datasets · Loads images and targets for training and evaluation
pytorchyolo/utils/datasets.py - Augmentations · Applies training-time image and target augmentation
pytorchyolo/utils/augmentations.py - Transforms · Prepares inputs for the model and loss
pytorchyolo/utils/transforms.py - YOLO loss · Computes detection losses for training
pytorchyolo/utils/loss.py - Detection utils · Shared box conversion, NMS, and evaluation helpers
pytorchyolo/utils/utils.py - Logger · Records training progress and TensorBoard metrics
pytorchyolo/utils/logger.py - Config parser · Reads Darknet-style model and data config files
pytorchyolo/utils/parse_config.py - Package · Installable pytorchyolo library exposed through CLI commands
pytorchyolo/__init__.py
Connections 29
- 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