# sunnyingyang-cpu/industrial-defect-detection architecture

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

- Interactive diagram: https://gitdiagram.com/sunnyingyang-cpu/industrial-defect-detection
- Repository: https://github.com/sunnyingyang-cpu/industrial-defect-detection

## Overview

This repository is an industrial surface-defect detection workflow built on YOLOv5: prepare annotated data, train a configured detector, evaluate it, run image/video inference, then export or serve the model. The README documents these product stages and the P2 small-object and P6/Transformer model variants. The graph also shows key model, data-loading, and training-support layers. Some evaluation and REST implementation details were not sampled, so those links reflect README documentation rather than inspected internals.

Main entry points: [`train.py`](https://github.com/sunnyingyang-cpu/industrial-defect-detection/blob/main/train.py) (Model training), [`detect.py`](https://github.com/sunnyingyang-cpu/industrial-defect-detection/blob/main/detect.py) (Image and video inference), [`models/yolo.py`](https://github.com/sunnyingyang-cpu/industrial-defect-detection/blob/main/models/yolo.py) (YOLO model builder), [`tools/prepare_neu_det.py`](https://github.com/sunnyingyang-cpu/industrial-defect-detection/blob/main/tools/prepare_neu_det.py) (Dataset conversion)

## Diagram (Mermaid)

```mermaid
flowchart TD

subgraph group_data["Data preparation"]
  node_neu[("NEU-DET source data")]
  node_prepare["Dataset conversion<br/>[prepare_neu_det.py]"]
  node_dataset_config["Dataset definition<br/>[defect_neu.yaml]"]
  node_loader["Image and label loader<br/>[datasets.py]"]
end

subgraph group_model["Detection model"]
  node_model_config["Defect model variants"]
  node_model_builder["YOLO model builder<br/>[yolo.py]"]
  node_common_layers["Core and attention layers<br/>[common.py]"]
  node_experimental_layers["Experimental layers<br/>[experimental.py]"]
  node_anchor["Anchor tuning<br/>[autoanchor.py]"]
end

subgraph group_train["Train and evaluate"]
  node_trainer["Model training<br/>[train.py]"]
  node_loss["Training loss<br/>[loss.py]"]
  node_device["Device and training utilities<br/>[torch_utils.py]"]
  node_tracking["Experiment tracking<br/>[wandb_utils.py]"]
  node_evaluator["Model evaluation<br/>[test.py]"]
  node_metrics["Detection metrics<br/>[metrics.py]"]
end

subgraph group_deploy["Inference and deployment"]
  node_inference["Image and video inference<br/>[detect.py]"]
  node_postprocess["Detection utilities<br/>[general.py]"]
  node_visualization["Results visualization<br/>[plots.py]"]
  node_export["ONNX and TensorRT export<br/>[export.py]"]
  node_api["REST inference API<br/>[restapi.py]"]
end

node_operator(("Quality engineer"))
node_runtime(("Inference consumers"))

node_operator -->|"converts data"| node_prepare
node_neu -->|"provides annotations"| node_prepare
node_prepare -->|"aligns output"| node_dataset_config
node_dataset_config -.->|"defines dataset"| node_loader
node_operator -->|"starts training"| node_trainer
node_trainer -->|"loads batches"| node_loader
node_trainer -->|"selects architecture"| node_model_config
node_trainer -->|"builds model"| node_model_builder
node_model_builder -->|"uses layers"| node_common_layers
node_model_builder -->|"uses modules"| node_experimental_layers
node_trainer -->|"checks anchors"| node_anchor
node_trainer -->|"computes loss"| node_loss
node_trainer -->|"uses utilities"| node_device
node_trainer -->|"logs experiments"| node_tracking
node_operator -->|"runs evaluation"| node_evaluator
node_operator -->|"runs detection"| node_inference
node_inference -->|"loads sources"| node_loader
node_inference -->|"uses utilities"| node_postprocess
node_inference -->|"draws results"| node_visualization
node_operator -->|"exports model"| node_export
node_operator -->|"deploys service"| node_api
node_runtime -.->|"requests inference"| node_api

click node_prepare "https://github.com/sunnyingyang-cpu/industrial-defect-detection/blob/main/tools/prepare_neu_det.py"
click node_dataset_config "https://github.com/sunnyingyang-cpu/industrial-defect-detection/blob/main/data/defect_neu.yaml"
click node_loader "https://github.com/sunnyingyang-cpu/industrial-defect-detection/blob/main/utils/datasets.py"
click node_model_config "https://github.com/sunnyingyang-cpu/industrial-defect-detection/blob/main/models/defect_yolov5s_p2.yaml"
click node_model_builder "https://github.com/sunnyingyang-cpu/industrial-defect-detection/blob/main/models/yolo.py"
click node_common_layers "https://github.com/sunnyingyang-cpu/industrial-defect-detection/blob/main/models/common.py"
click node_experimental_layers "https://github.com/sunnyingyang-cpu/industrial-defect-detection/blob/main/models/experimental.py"
click node_anchor "https://github.com/sunnyingyang-cpu/industrial-defect-detection/blob/main/utils/autoanchor.py"
click node_trainer "https://github.com/sunnyingyang-cpu/industrial-defect-detection/blob/main/train.py"
click node_loss "https://github.com/sunnyingyang-cpu/industrial-defect-detection/blob/main/utils/loss.py"
click node_device "https://github.com/sunnyingyang-cpu/industrial-defect-detection/blob/main/utils/torch_utils.py"
click node_tracking "https://github.com/sunnyingyang-cpu/industrial-defect-detection/blob/main/utils/wandb_logging/wandb_utils.py"
click node_evaluator "https://github.com/sunnyingyang-cpu/industrial-defect-detection/blob/main/test.py"
click node_metrics "https://github.com/sunnyingyang-cpu/industrial-defect-detection/blob/main/utils/metrics.py"
click node_inference "https://github.com/sunnyingyang-cpu/industrial-defect-detection/blob/main/detect.py"
click node_postprocess "https://github.com/sunnyingyang-cpu/industrial-defect-detection/blob/main/utils/general.py"
click node_visualization "https://github.com/sunnyingyang-cpu/industrial-defect-detection/blob/main/utils/plots.py"
click node_export "https://github.com/sunnyingyang-cpu/industrial-defect-detection/blob/main/models/export.py"
click node_api "https://github.com/sunnyingyang-cpu/industrial-defect-detection/blob/main/utils/flask_rest_api/restapi.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_neu,node_prepare,node_dataset_config,node_loader toneBlue
class node_model_config,node_model_builder,node_common_layers,node_experimental_layers,node_anchor toneAmber
class node_trainer,node_loss,node_device,node_tracking,node_evaluator,node_metrics toneMint
class node_inference,node_postprocess,node_visualization,node_export,node_api toneRose
class node_operator,node_runtime toneIndigo
```

## Components

### Data preparation

- **NEU-DET source data**
- [Dataset conversion](https://github.com/sunnyingyang-cpu/industrial-defect-detection/blob/main/tools/prepare_neu_det.py): `tools/prepare_neu_det.py`
- [Dataset definition](https://github.com/sunnyingyang-cpu/industrial-defect-detection/blob/main/data/defect_neu.yaml): `data/defect_neu.yaml`
- [Image and label loader](https://github.com/sunnyingyang-cpu/industrial-defect-detection/blob/main/utils/datasets.py): `utils/datasets.py`

### Detection model

- [Defect model variants](https://github.com/sunnyingyang-cpu/industrial-defect-detection/blob/main/models/defect_yolov5s_p2.yaml): `models/defect_yolov5s_p2.yaml`
- [YOLO model builder](https://github.com/sunnyingyang-cpu/industrial-defect-detection/blob/main/models/yolo.py): `models/yolo.py`
- [Core and attention layers](https://github.com/sunnyingyang-cpu/industrial-defect-detection/blob/main/models/common.py): `models/common.py`
- [Experimental layers](https://github.com/sunnyingyang-cpu/industrial-defect-detection/blob/main/models/experimental.py): `models/experimental.py`
- [Anchor tuning](https://github.com/sunnyingyang-cpu/industrial-defect-detection/blob/main/utils/autoanchor.py): `utils/autoanchor.py`

### Train and evaluate

- [Model training](https://github.com/sunnyingyang-cpu/industrial-defect-detection/blob/main/train.py): `train.py`
- [Training loss](https://github.com/sunnyingyang-cpu/industrial-defect-detection/blob/main/utils/loss.py): `utils/loss.py`
- [Device and training utilities](https://github.com/sunnyingyang-cpu/industrial-defect-detection/blob/main/utils/torch_utils.py): `utils/torch_utils.py`
- [Experiment tracking](https://github.com/sunnyingyang-cpu/industrial-defect-detection/blob/main/utils/wandb_logging/wandb_utils.py): `utils/wandb_logging/wandb_utils.py`
- [Model evaluation](https://github.com/sunnyingyang-cpu/industrial-defect-detection/blob/main/test.py): `test.py`
- [Detection metrics](https://github.com/sunnyingyang-cpu/industrial-defect-detection/blob/main/utils/metrics.py): `utils/metrics.py`

### Inference and deployment

- [Image and video inference](https://github.com/sunnyingyang-cpu/industrial-defect-detection/blob/main/detect.py): `detect.py`
- [Detection utilities](https://github.com/sunnyingyang-cpu/industrial-defect-detection/blob/main/utils/general.py): `utils/general.py`
- [Results visualization](https://github.com/sunnyingyang-cpu/industrial-defect-detection/blob/main/utils/plots.py): `utils/plots.py`
- [ONNX and TensorRT export](https://github.com/sunnyingyang-cpu/industrial-defect-detection/blob/main/models/export.py): `models/export.py`
- [REST inference API](https://github.com/sunnyingyang-cpu/industrial-defect-detection/blob/main/utils/flask_rest_api/restapi.py): `utils/flask_rest_api/restapi.py`

### Other components

- **Quality engineer**
- **Inference consumers**

## Connections

- Quality engineer → Dataset conversion: converts data (evidence: `README.md`)
- NEU-DET source data → Dataset conversion: provides annotations (evidence: `README.md`)
- Dataset conversion → Dataset definition: aligns output (evidence: `README.md`)
- Dataset definition → Image and label loader: defines dataset
- Quality engineer → Model training: starts training (evidence: `README.md`)
- Model training → Image and label loader: loads batches (evidence: `train.py`)
- Model training → Defect model variants: selects architecture (evidence: `README.md`)
- Model training → YOLO model builder: builds model (evidence: `train.py`)
- YOLO model builder → Core and attention layers: uses layers (evidence: `models/yolo.py`)
- YOLO model builder → Experimental layers: uses modules (evidence: `models/yolo.py`)
- Model training → Anchor tuning: checks anchors (evidence: `train.py`)
- Model training → Training loss: computes loss (evidence: `train.py`)
- Model training → Device and training utilities: uses utilities (evidence: `train.py`)
- Model training → Experiment tracking: logs experiments (evidence: `train.py`)
- Quality engineer → Model evaluation: runs evaluation (evidence: `README.md`)
- Quality engineer → Image and video inference: runs detection (evidence: `README.md`)
- Image and video inference → Image and label loader: loads sources (evidence: `detect.py`)
- Image and video inference → Detection utilities: uses utilities (evidence: `detect.py`)
- Image and video inference → Results visualization: draws results (evidence: `detect.py`)
- Quality engineer → ONNX and TensorRT export: exports model (evidence: `README.md`)
- Quality engineer → REST inference API: deploys service (evidence: `README.md`)
- Inference consumers → REST inference API: requests inference
