This repository contains the complete dataset preparation and initial model training pipeline for the Textile Defect Detection — Hangzhou 2026 Proof of Concept (POC) developed by Devlogix Technology.
The pipeline downloads, inspects, cleans, augments, validates, and packages a YOLOv8-compatible textile defect dataset, followed by initial YOLOv8 model training, validation, test-set inference, extraction of pitch-ready performance metrics, local FastAPI inference, and a Streamlit visual dashboard.
| Field | Value |
|---|---|
| Name | CHENAB Textile / FabricDefectNTU |
| Kaggle ID | muhammadharisabid/fabricdefectntu |
| Source | Kaggle Dataset Page |
| Format | YOLOv8 (YOLO-format annotations) |
| Annotation type | Bounding boxes |
| Target framework | YOLOv8 (Ultralytics) |
The source dataset contained 2,792 images. The final processed dataset contains 4,742 images after the preprocessing workflow, including train-only augmentation.
| Metric | Original Dataset | Final Processed Dataset |
|---|---|---|
| Total images | 2,792 | 4,742 |
| Train images | 1,950 | 3,900 |
| Validation images | 562 | 562 |
| Test images | 280 | 280 |
| Classes | 7 | 7 |
| Dataset size | ~500 MB | ~1.3 GB |
Classes are taken directly from the source data.yaml. Run the notebook to discover and document exact class names:
# From final_dataset/data.yaml
names:
- baekra, color issues, contamination, cut, gray stitch, selvet, stainThe following operations were applied in sequence. The preprocessing notebook and
CSV validation reports are in Preprocessing/:
| Step | Operation | Details |
|---|---|---|
| 1 | Dataset download | via kagglehub |
| 2 | Structure inspection | directory tree, data.yaml, splits |
| 3 | Statistics collection | counts, resolutions, class distribution |
| 4 | Visual inspection | random samples with YOLO bbox overlay |
| 5 | Annotation validation | YOLO format, class IDs, coordinate bounds |
| 6 | Image cleaning | corrupted/unreadable images excluded |
| 7 | Duplicate detection | MD5 hash, cross-split leakage check |
| 8 | Resolution analysis | no resize needed (YOLOv8 handles letterboxing) |
| 9 | Clean dataset copy | final_dataset/ created from scratch |
| 10 | Augmentation | train-only, albumentations pipeline |
| 11 | Final validation | Preprocessing/scripts/validate_yolo_dataset.py |
| 12 | data.yaml generation | portable, relative paths |
| 13 | ZIP export | textile_defect_yolov8_final.zip |
Applied to training set only using Albumentations:
| Transform | Parameters | Probability |
|---|---|---|
| HorizontalFlip | — | 0.5 |
| RandomBrightnessContrast | brightness ±0.2, contrast ±0.2 | 0.7 |
| Rotate | ±10° | 0.4 |
| GaussNoise | std (0.01–0.04) | 0.3 |
| RandomScale | ±10% | 0.3 |
Vertical flip not applied — defect orientation is meaningful in factory inspection context.
Bounding boxes are transformed together with images to maintain label correctness.
The final dataset is fully YOLOv8-compatible:
final_dataset/
├── images/
│ ├── train/
│ ├── val/
│ └── test/
├── labels/
│ ├── train/
│ ├── val/
│ └── test/
└── data.yaml
Each label file follows the YOLO format:
<class_id> <x_center> <y_center> <width> <height>
All coordinates are normalized to [0, 1].
FINAL DATASET VALIDATION
========================
Corrupted images: 0
Invalid labels: 0
Cross-split duplicates: 0
YOLO format: PASS
data.yaml: PASS
OVERALL: ✅ PASS
Validated by
Preprocessing/scripts/validate_yolo_dataset.py
⚠️ Note: The dataset is NOT stored in this repository. Download the preprocessed, training-ready dataset from Google Drive:
📦 Download Final YOLOv8 Dataset — textile_defect_yolov8_final.zip
git clone https://github.com/devlogixtechnology/Textile-Defect-Detection.git
cd Textile-Defect-Detection
pip install -r requirements.txt- Go to kaggle.com/settings → API → Create New Token
- Place
kaggle.jsonat~/.kaggle/kaggle.json - On Windows:
C:\Users\<username>\.kaggle\kaggle.json
jupyter notebook Preprocessing/notebooks/dataset_preprocessing.ipynbRun all cells in order (Kernel → Restart & Run All).
python Preprocessing/scripts/validate_yolo_dataset.py --dataset final_dataset/python Preprocessing/scripts/dataset_statistics.py --dataset final_dataset/ --output Preprocessing/reports/The initial training workflow for Task 2 is provided in:
Training/Textile_Defect_Detection_Training.ipynb
The model was trained using YOLOv8s with the following configuration:
| Parameter | Value |
|---|---|
| Model | YOLOv8s |
| Training epochs | 20 |
| Image size | 640 × 640 |
| Number of classes | 7 |
The training notebook includes model training, validation, test-set inference, results analysis, contact-sheet generation, and selection of representative pitch images.
Training checkpoints, training metrics, annotated test predictions, contact sheets, and selected pitch images are stored separately in Google Drive and are not committed to this repository.
📦 Download / View Training Results, Checkpoints & Inference Outputs
The initial YOLOv8s model was trained for 20 epochs on the seven-class textile defect detection dataset.
| Metric | Result |
|---|---|
| Precision | 76.97% |
| Recall | 74.76% |
| mAP@50 | 78.82% |
| mAP@50-95 | 47.12% |
The saved best.pt checkpoint was successfully validated and achieved the
metrics above.
| Defect Class | mAP@50 |
|---|---|
| contamination | 99.0% |
| stain | 91.9% |
| baekra | 83.9% |
| cut | 82.8% |
| gray stitch | 74.0% |
| selvet | 68.9% |
| color issues | 51.2% |
Strongest class: contamination — 99.0% mAP@50
Weakest class: color issues — 51.2% mAP@50
The difference between mAP@50 and mAP@50-95 indicates that the model is generally effective at detecting the correct defect classes, while more precise bounding-box localization remains an area for improvement.
The complete final test set of 280 images was processed successfully.
- 280/280 test images received annotated predictions.
- 6 contact sheets were generated for visual inspection.
- 3 representative pitch images were selected:
01_baekra.jpg02_cut.jpg03_stain.jpg
The following outputs are stored in Google Drive rather than in the GitHub repository:
Textile_Defect_Training/
├── Training_Results/
│ └── yolov8s_textile_initial/
│ ├── best.pt
│ ├── results.csv
│ └── training outputs
└── Test_Inference/
├── test_predictions/
├── Contact_Sheets/
└── Selected_3/
├── 01_baekra.jpg
├── 02_cut.jpg
└── 03_stain.jpg
📦 View Training Results, Checkpoints & Inference Outputs on Google Drive
Textile-Defect-Detection/
├── Backend_fastapi_endpoint/
│ ├── fastapi_endpoint.ipynb ← Colab inference and API contract notebook
│ └── main.py ← Local FastAPI application
├── Preprocessing/
│ ├── notebooks/
│ │ └── dataset_preprocessing.ipynb ← Main preprocessing pipeline
│ ├── reports/
│ │ ├── annotation_validation.csv ← Annotation validation report
│ │ └── cleaning_log.csv ← Image cleaning log
│ └── scripts/
│ ├── dataset_statistics.py ← Standalone statistics generator
│ └── validate_yolo_dataset.py ← Standalone YOLO validator
├── Streamlit/
│ ├── app.py ← Visual dashboard
│ ├── README.md ← Dashboard instructions
│ └── requirements.txt ← Frontend dependencies
├── Training/
│ └── Textile_Defect_Detection_Training.ipynb ← YOLOv8 training workflow
├── README.md
├── requirements.txt
├── .gitignore
└── LICENSE.txt
The dataset itself (
final_dataset/, ZIP), training checkpoints, training metrics, and test inference outputs are NOT stored in this repository. They are provided through the Google Drive links above.
The trained model can be tested in Google Colab using the notebook
fastapi_endpoint.ipynb from the backend project. The notebook tests the backend
/predict contract in the browser and does not require a local Python server.
- Open Google Colab.
- Select File > Upload notebook.
- Upload
fastapi_endpoint.ipynb. - Run the cells from top to bottom.
The first code cell installs the Colab dependencies automatically:
%pip install -q ultralytics pillow matplotlib fastapi python-multipart httpxThe notebook mounts Google Drive first. To use files from Drive, set
DRIVE_PROJECT_DIR to the folder containing best.pt and a test image:
DRIVE_PROJECT_DIR = DRIVE_ROOT / "Textile defect detection/Backend_fastapi_endpoint"Alternatively, leave DRIVE_PROJECT_DIR = None. Colab will prompt you to upload
best.pt and an image such as hole.jpeg directly from your computer.
The notebook loads the YOLO model, runs prediction, and prints JSON containing:
- image filename, width, and height
- defect
class_idandclass_name - detection confidence
- pixel-based
x1,y1,x2, andy2coordinates measured from the top-left corner
It also validates the response fields, saves annotated_result.jpg in the
current Colab working directory, displays the detected boxes, and prints
FastAPI /predict test passed. The notebook does not save a separate
detections.json file; its JSON response is printed for the current session.
A Streamlit-based frontend was added to provide a simple visual dashboard for
non-technical users during the Hangzhou 2026 POC. The dashboard acts as a
client-only application that uploads images to the existing FastAPI /predict
endpoint and visualizes defect bounding boxes, class names, and confidence
scores. The Streamlit app does NOT perform model inference locally — it relies
on the FastAPI backend.
Architecture:
Dataset ↓ Preprocessing ↓ YOLOv8 Training ↓ Trained Model ↓ FastAPI Backend ↓ Streamlit Dashboard
The local dashboard uses the tdd Conda environment with the FastAPI backend
running before Streamlit. The YOLO checkpoint is expected at
Backend_fastapi_endpoint/best.pt and is loaded by the backend, not by the
Streamlit frontend.
Start the FastAPI backend from the repository root:
conda activate tdd
python -m uvicorn Backend_fastapi_endpoint.main:api_app --host 127.0.0.1 --port 8000In a second terminal, start the dashboard:
conda activate tdd
streamlit run Streamlit/app.pyOpen the Streamlit URL shown in the terminal, normally
http://localhost:8501. The dashboard sends uploaded JPG, JPEG, or PNG files
to POST http://127.0.0.1:8000/predict using the multipart field file. The
backend returns pixel-based x1, y1, x2, and y2 coordinates, which the
dashboard renders on the image with class names and confidence scores. The
backend URL can be changed in the Streamlit sidebar.
The frontend dependencies are listed in Streamlit/requirements.txt. The
frontend does not require torch, torchvision, or ultralytics; those are
backend inference dependencies.
Organization: Devlogix Technology
Project: Textile Defect Detection — Hangzhou 2026 POC
Contact: ehtisham.malik5618@gmail.com