495 lines
13 KiB
Markdown
495 lines
13 KiB
Markdown
# Paint Defect Detection using YOLO26s
|
|
|
|
End-to-end notes for training a paint-defect object-detection model in
|
|
Google Colab and running inference on a Jetson Orin Nano.
|
|
|
|
> **Project status:** The YOLO26s model was trained in Google Colab on a
|
|
> Tesla T4 GPU. The best weights are saved as `best.pt`. Jetson
|
|
> inference depends on installing a PyTorch/Ultralytics combination
|
|
> compatible with the Jetson's installed JetPack version.
|
|
|
|
## 1. Project overview
|
|
|
|
### Defect classes
|
|
|
|
The dataset has seven classes, in this order:
|
|
|
|
1. `Oil Marks`
|
|
2. `Orange Peel`
|
|
3. `Paint sagging`
|
|
4. `Peel Off`
|
|
5. `Scratches`
|
|
6. `Water Marks`
|
|
7. `bubbling`
|
|
|
|
Dataset source: [Roboflow Paint Defects
|
|
dataset](https://universe.roboflow.com/buildwellai/paint-defects-u2wuj-sor3g/dataset/1)
|
|
|
|
### Dataset layout
|
|
|
|
The downloaded ZIP was extracted to Google Drive and contained:
|
|
|
|
``` text
|
|
paint_defects_dataset/
|
|
├── data.yaml
|
|
├── train/
|
|
├── valid/
|
|
├── test/
|
|
├── README.dataset.txt
|
|
└── README.roboflow.txt
|
|
```
|
|
|
|
The actual dataset directory in Colab was stored in `DATASET_DIR`. Set
|
|
this variable to the directory you actually used.
|
|
|
|
Reported image counts in the extracted folders:
|
|
|
|
Split Images
|
|
------------ -----------
|
|
Train 1,464
|
|
Validation 140
|
|
Test 68
|
|
**Total** **1,672**
|
|
|
|
**Dataset note:** The public Roboflow page was observed to show 696
|
|
images, while the extracted ZIP folder counts total 1,672. Verify the
|
|
dataset version, file counts, and any duplicates before comparing these
|
|
figures or reporting dataset size externally.
|
|
|
|
## 2. Environment used for training
|
|
|
|
- Platform: Google Colab
|
|
- GPU: NVIDIA Tesla T4
|
|
- Model: YOLO26s (`yolo26s.pt` pretrained checkpoint, if available in
|
|
the installed Ultralytics version)
|
|
- Training image size: 832 pixels (the run directory was named
|
|
`yolo26s_832`)
|
|
- Batch size: 8
|
|
- Maximum epochs: 100
|
|
- Early stopping patience: 25 for the recorded run
|
|
- Optimizer: AdamW
|
|
- Initial learning rate: 0.001
|
|
- Final learning-rate factor: 0.01
|
|
- Weight decay: 0.0005
|
|
- Warm-up epochs: 3
|
|
- Mosaic: 0.5
|
|
- MixUp: 0.0
|
|
- Close mosaic: last 10 epochs
|
|
- Seed: 42
|
|
- AMP: enabled
|
|
- Workers: 2
|
|
|
|
Package versions can change. Record the versions used in your actual
|
|
notebook with:
|
|
|
|
``` python
|
|
import sys
|
|
import torch
|
|
import ultralytics
|
|
|
|
print("Python:", sys.version)
|
|
print("PyTorch:", torch.__version__)
|
|
print("Ultralytics:", ultralytics.__version__)
|
|
print("CUDA available:", torch.cuda.is_available())
|
|
if torch.cuda.is_available():
|
|
print("GPU:", torch.cuda.get_device_name(0))
|
|
```
|
|
|
|
## 3. Google Colab training steps
|
|
|
|
### Step 1 --- Mount Google Drive
|
|
|
|
In a Colab notebook cell:
|
|
|
|
``` python
|
|
from google.colab import drive
|
|
drive.mount("/content/drive")
|
|
```
|
|
|
|
### Step 2 --- Set and verify the dataset directory
|
|
|
|
Change `DATASET_DIR` to the folder where you extracted the Roboflow ZIP.
|
|
|
|
``` python
|
|
from pathlib import Path
|
|
|
|
DATASET_DIR = Path("/content/drive/MyDrive/paint_defects_dataset")
|
|
DATA_YAML = DATASET_DIR / "data.yaml"
|
|
|
|
print("Dataset exists:", DATASET_DIR.exists())
|
|
print("YAML exists:", DATA_YAML.exists())
|
|
print("Folders/files:", [p.name for p in DATASET_DIR.iterdir()])
|
|
```
|
|
|
|
Expected contents include `data.yaml`, `train`, `valid`, and `test`.
|
|
|
|
### Step 3 --- Inspect `data.yaml`
|
|
|
|
The YAML should point to the dataset root and use the correct seven
|
|
class names. A typical configuration is:
|
|
|
|
``` yaml
|
|
path: /content/drive/MyDrive/paint_defects_dataset
|
|
train: train/images
|
|
val: valid/images
|
|
test: test/images
|
|
|
|
nc: 7
|
|
names:
|
|
- Oil Marks
|
|
- Orange Peel
|
|
- Paint sagging
|
|
- Peel Off
|
|
- Scratches
|
|
- Water Marks
|
|
- bubbling
|
|
```
|
|
|
|
**Important:** Roboflow exports can use different folder names or
|
|
relative paths. Keep the `train`, `val`, and `test` paths that match
|
|
your actual `data.yaml`; do not overwrite a working YAML with this
|
|
example unless the paths are correct.
|
|
|
|
### Step 4 --- Install/check Ultralytics
|
|
|
|
In Colab:
|
|
|
|
``` python
|
|
%pip install -U ultralytics
|
|
```
|
|
|
|
If Ultralytics was already installed, restart the runtime only if
|
|
required by the installation, then check the version:
|
|
|
|
``` python
|
|
import ultralytics
|
|
print(ultralytics.__version__)
|
|
```
|
|
|
|
### Step 5 --- Train YOLO26s
|
|
|
|
The following is a reproducible training template matching the recorded
|
|
main settings. Make sure the pretrained checkpoint name `yolo26s.pt` is
|
|
supported by the installed Ultralytics release.
|
|
|
|
``` python
|
|
from pathlib import Path
|
|
from ultralytics import YOLO
|
|
|
|
DATA_YAML = "/content/drive/MyDrive/paint_defects_dataset/data.yaml"
|
|
PROJECT_DIR = "/content/drive/MyDrive/paint_defect_training"
|
|
|
|
model = YOLO("yolo26s.pt")
|
|
|
|
results = model.train(
|
|
data=DATA_YAML,
|
|
epochs=100,
|
|
patience=25,
|
|
imgsz=832,
|
|
batch=8,
|
|
device=0,
|
|
workers=2,
|
|
amp=True,
|
|
optimizer="AdamW",
|
|
lr0=0.001,
|
|
lrf=0.01,
|
|
weight_decay=0.0005,
|
|
warmup_epochs=3,
|
|
mosaic=0.5,
|
|
mixup=0.0,
|
|
close_mosaic=10,
|
|
seed=42,
|
|
save=True,
|
|
project=PROJECT_DIR,
|
|
name="yolo26s_832",
|
|
)
|
|
```
|
|
|
|
This writes training artifacts under:
|
|
|
|
``` text
|
|
/content/drive/MyDrive/paint_defect_training/yolo26s_832/
|
|
├── weights/
|
|
│ ├── best.pt
|
|
│ └── last.pt
|
|
├── results.csv
|
|
├── results.png
|
|
├── confusion_matrix.png
|
|
├── PR_curve.png
|
|
└── ...
|
|
```
|
|
|
|
The exact files depend on the Ultralytics version and enabled plots.
|
|
|
|
### Step 6 --- Validate the best checkpoint
|
|
|
|
``` python
|
|
from ultralytics import YOLO
|
|
|
|
BEST_MODEL = (
|
|
"/content/drive/MyDrive/paint_defect_training/"
|
|
"yolo26s_832/weights/best.pt"
|
|
)
|
|
|
|
model = YOLO(BEST_MODEL)
|
|
metrics = model.val(
|
|
data="/content/drive/MyDrive/paint_defects_dataset/data.yaml",
|
|
split="val",
|
|
imgsz=832,
|
|
device=0,
|
|
)
|
|
print(metrics)
|
|
```
|
|
|
|
To evaluate the held-out test split after choosing the model:
|
|
|
|
``` python
|
|
test_metrics = model.val(
|
|
data="/content/drive/MyDrive/paint_defects_dataset/data.yaml",
|
|
split="test",
|
|
imgsz=832,
|
|
device=0,
|
|
)
|
|
print(test_metrics)
|
|
```
|
|
|
|
Use the test split for final evaluation rather than repeatedly tuning
|
|
against it.
|
|
|
|
## 4. Recorded validation results
|
|
|
|
The completed run stopped early after 65 epochs because the validation
|
|
metric did not improve within the configured patience window. The best
|
|
checkpoint was saved from epoch 40. Early stopping is normal; it does
|
|
not mean the run failed.
|
|
|
|
Metric Overall validation result
|
|
--------------- ---------------------------
|
|
Precision 0.830
|
|
Recall 0.763
|
|
mAP@0.50 0.801
|
|
mAP@0.50:0.95 0.347
|
|
|
|
Recorded per-class results:
|
|
|
|
Class Precision Recall mAP@0.50 mAP@0.50:0.95
|
|
--------------- ----------- -------- ---------- ---------------
|
|
Oil Marks 0.970 1.000 0.995 0.577
|
|
Orange Peel 0.635 0.583 0.578 0.283
|
|
Paint sagging 0.787 0.592 0.693 0.253
|
|
Peel Off 0.972 0.967 0.994 0.531
|
|
Scratches 0.708 0.571 0.671 0.224
|
|
Water Marks 0.904 0.917 0.915 0.248
|
|
bubbling 0.833 0.714 0.761 0.315
|
|
|
|
These are the recorded validation results, not a guarantee of
|
|
performance on new factory images. Orange Peel, Paint sagging, and
|
|
Scratches need particular review. Inspect false positives/negatives and
|
|
annotation quality, and test with representative real-world images.
|
|
|
|
## 5. Copy the model to the Jetson Orin Nano
|
|
|
|
### Step 1 --- Download `best.pt` from Colab
|
|
|
|
Run this cell in Colab:
|
|
|
|
``` python
|
|
from google.colab import files
|
|
|
|
files.download(
|
|
"/content/drive/MyDrive/paint_defect_training/"
|
|
"yolo26s_832/weights/best.pt"
|
|
)
|
|
```
|
|
|
|
This downloads `best.pt` to your local computer.
|
|
|
|
### Step 2 --- Copy `best.pt` to the Jetson
|
|
|
|
From a terminal on your Windows laptop (with OpenSSH `scp` available):
|
|
|
|
``` bash
|
|
scp best.pt YOUR_USERNAME@JETSON_IP:/home/YOUR_USERNAME/
|
|
```
|
|
|
|
Replace `YOUR_USERNAME` and `JETSON_IP` with the Jetson Linux username
|
|
and its IP address. Alternatively, use a USB drive.
|
|
|
|
On the Jetson, verify the file:
|
|
|
|
``` bash
|
|
ls -lh ~/best.pt
|
|
```
|
|
|
|
## 6. Prepare the Jetson environment
|
|
|
|
Check the Jetson OS, Python, CUDA, and PyTorch before installing
|
|
anything:
|
|
|
|
``` bash
|
|
python3 --version
|
|
nvcc --version
|
|
cat /etc/nv_tegra_release
|
|
python3 -c "import torch; print('PyTorch:', torch.__version__); print('CUDA available:', torch.cuda.is_available())"
|
|
```
|
|
|
|
The Jetson environment previously reported for this project was Python
|
|
3.8.10 and CUDA 11.4. Re-check it on the actual device. PyTorch and
|
|
Ultralytics versions must be compatible with the installed JetPack
|
|
release. Do not blindly upgrade PyTorch or install a generic desktop
|
|
CUDA wheel on Jetson.
|
|
|
|
Follow the official Ultralytics Jetson installation instructions for
|
|
your JetPack version: https://docs.ultralytics.com/guides/nvidia-jetson/
|
|
|
|
Once a compatible PyTorch environment is installed, install/check
|
|
Ultralytics:
|
|
|
|
``` bash
|
|
python3 -m pip install ultralytics
|
|
python3 -c "import torch, ultralytics; print(torch.__version__); print(ultralytics.__version__); print(torch.cuda.is_available())"
|
|
```
|
|
|
|
If the latest Ultralytics package is incompatible with the Jetson's
|
|
Python version, use a compatible pinned environment or the documented
|
|
Jetson container approach.
|
|
|
|
## 7. Jetson inference script
|
|
|
|
Create a Python file on the Jetson:
|
|
|
|
``` bash
|
|
nano paint_defect_inference.py
|
|
```
|
|
|
|
|
|
|
|
## 8. Run inference on the Jetson
|
|
|
|
### Live camera
|
|
|
|
``` bash
|
|
python3 paint_defect_inference.py --source 0
|
|
```
|
|
|
|
Press **Q** in the preview window to stop. The annotated video is saved
|
|
to:
|
|
|
|
``` text
|
|
inference_results/paint_defects_camera.mp4
|
|
```
|
|
|
|
This camera-saving behavior requires display mode in the script above.
|
|
With `--no-display`, the current camera branch does not create a video
|
|
writer.
|
|
|
|
### One image
|
|
|
|
``` bash
|
|
python3 paint_defect_inference.py --source test.jpg
|
|
```
|
|
|
|
### Video file
|
|
|
|
``` bash
|
|
python3 paint_defect_inference.py --source test.mp4
|
|
```
|
|
|
|
Image/video predictions are saved under:
|
|
|
|
``` text
|
|
inference_results/predict/
|
|
```
|
|
|
|
### Monitor Jetson resources
|
|
|
|
In a second terminal:
|
|
|
|
``` bash
|
|
tegrastats
|
|
```
|
|
|
|
This reports memory, processor activity, and temperature. Check whether
|
|
inference is actually using CUDA before measuring performance.
|
|
|
|
## 9. Important parameters
|
|
|
|
In `paint_defect_inference.py`:
|
|
|
|
``` python
|
|
IMAGE_SIZE = 832
|
|
CONFIDENCE = 0.25
|
|
```
|
|
|
|
- `IMAGE_SIZE`: uses the same 832-pixel inference size as the training
|
|
run. Smaller sizes may improve speed but can reduce detection
|
|
accuracy.
|
|
- `CONFIDENCE`: detections below 0.25 are filtered out. Tune this
|
|
using a validation set and real images.
|
|
- `device`: the script uses GPU device `0` when CUDA is available;
|
|
otherwise it falls back to CPU.
|
|
|
|
For faster deployment, benchmark the PyTorch model first. TensorRT
|
|
export can improve performance, but the engine should be generated and
|
|
tested with compatible Jetson software and hardware. Do not assume a
|
|
TensorRT engine generated on a different machine will be portable.
|
|
|
|
## 10. Troubleshooting checklist
|
|
|
|
- **Model not found:** check `MODEL_PATH` and run `ls -lh ~/best.pt`.
|
|
- **CUDA unavailable:** check `cat /etc/nv_tegra_release`, PyTorch
|
|
compatibility, and `torch.cuda.is_available()`.
|
|
- **Ultralytics installation fails:** verify the Jetson's Python
|
|
version and use JetPack-compatible instructions/pinned packages.
|
|
- **Camera will not open:** check `ls /dev/video*`, camera
|
|
permissions, and whether another application is using it. USB camera
|
|
index `0` may not always be the correct index.
|
|
- **SSH has no display:** use image/video inference with
|
|
`--no-display`; live preview needs a graphical display or a
|
|
different streaming solution.
|
|
- **Low accuracy on new surfaces:** verify camera focus, lighting,
|
|
image resolution, class labels, and annotation consistency. Review
|
|
false positives and false negatives.
|
|
- **Low FPS:** use `tegrastats`, test lower `IMAGE_SIZE` values, and
|
|
consider compatible TensorRT deployment after establishing a
|
|
baseline.
|
|
|
|
## 11. Final file list
|
|
|
|
``` text
|
|
Project/
|
|
├── README.md # This documentation
|
|
├── paint_defect_inference.py # Jetson inference program
|
|
├── best.pt # Trained model weights (download from Colab)
|
|
└── results # Saved image predictions
|
|
```
|
|
|
|
Training artifacts are stored separately in Google Drive:
|
|
|
|
``` text
|
|
MyDrive/
|
|
├── paint_defects_dataset/
|
|
│ ├── data.yaml
|
|
│ ├── train/
|
|
│ ├── valid/
|
|
│ └── test/
|
|
└── paint_defect_training/
|
|
└── yolo26s_832/
|
|
├── weights/
|
|
│ ├── best.pt
|
|
│ └── last.pt
|
|
├── results.csv
|
|
└── ...
|
|
```
|
|
|
|
## 12. References
|
|
|
|
- Roboflow dataset:
|
|
https://universe.roboflow.com/buildwellai/paint-defects-u2wuj-sor3g/dataset/1
|
|
- Ultralytics NVIDIA Jetson guide:
|
|
https://docs.ultralytics.com/guides/nvidia-jetson/
|
|
- Ultralytics prediction documentation:
|
|
https://docs.ultralytics.com/modes/predict/
|
|
- Ultralytics training documentation:
|
|
https://docs.ultralytics.com/modes/train/
|