Micro_component_dataset / prepare_hf_dataset.py
FaustDerLegende's picture
Upload folder using huggingface_hub (part 84)
693bf28 verified
Raw History Blame Contribute Delete
4.1 kB
"""Structure and format the 8,190-image dataset into standard Hugging Face Dataset format for CLI push.
Outputs to `huggingface_dataset/`:
huggingface_dataset/
β”œβ”€β”€ README.md # Dataset Card with YAML frontmatter metadata
β”œβ”€β”€ data.yaml # YOLO dataset configuration
β”œβ”€β”€ train/ # 5,733 images and .txt label pairs
β”‚ β”œβ”€β”€ images/
β”‚ └── labels/
β”œβ”€β”€ val/ # 1,228 images and .txt label pairs
β”‚ β”œβ”€β”€ images/
β”‚ └── labels/
└── test/ # 1,229 images and .txt label pairs
β”œβ”€β”€ images/
└── labels/
"""
from __future__ import annotations
import json
import shutil
from pathlib import Path
ROOT_DIR = Path(__file__).resolve().parent
SPLITS_DIR = ROOT_DIR / "splits"
HF_DATASET_DIR = ROOT_DIR / "huggingface_dataset"
def build_huggingface_dataset():
if not SPLITS_DIR.exists():
raise FileNotFoundError(f"Splits directory not found at {SPLITS_DIR}. Run 'python scraper.py split' first.")
print(f"Preparing Hugging Face dataset directory at {HF_DATASET_DIR}...")
if HF_DATASET_DIR.exists():
shutil.rmtree(HF_DATASET_DIR)
HF_DATASET_DIR.mkdir(parents=True, exist_ok=True)
# Copy train, val, test splits
for split_name in ["train", "val", "test"]:
src_split = SPLITS_DIR / split_name
dst_split = HF_DATASET_DIR / split_name
if src_split.exists():
shutil.copytree(src_split, dst_split)
# Copy data.yaml
shutil.copy2(SPLITS_DIR / "data.yaml", HF_DATASET_DIR / "data.yaml")
# Create Hugging Face Dataset Card README.md with YAML frontmatter
readme_content = """---
language:
- en
license: cc-by-4.0
tags:
- computer-vision
- object-detection
- yolo
- ewaste
- pcb
- recycling
- hardware-fraud
size_categories:
- 1K<n<10K
task_categories:
- object-detection
task_ids:
- bounding-box-object-detection
---
# πŸ”Œ E-Waste PCB Component Purity & Cannibalization Dataset
This dataset contains **8,190 normalized high-resolution PCB images** with fine-grained object detection bounding boxes designed for real-time edge AI cannibalization detection in e-waste recycling platforms.
## πŸ“Š Dataset Splits
| Split | Images & Bounding Box Pairs | Ratio |
| :--- | :--- | :--- |
| **Train** | **5,733 images** | $70\%$ |
| **Validation** | **1,228 images** | $15\%$ |
| **Test** | **1,229 images** | $15\%$ |
| **Total** | **8,190 images** | $100\%$ |
---
## 🏷️ 7 Fine-Grained Component Taxonomy
| Class ID | Class Name | Description |
| :--- | :--- | :--- |
| `0` | `cpu_socket` | LGA, PGA, or ZIF processor socket footprints |
| `1` | `gold_finger` | Gold-plated edge connector contacts (PCIe, RAM contacts) |
| `2` | `ic_chip` | Packaged ICs and BGA microchips |
| `3` | `tantalum_capacitor` | SMD tantalum capacitors (molded yellow/black/orange) |
| `4` | `forcibly_desoldered_pad` | Exposed pads with jagged solder, blowtorch marks, torn traces (**fraud signal**) |
| `5` | `factory_unpopulated_pad` | Clean silkscreen outline with smooth solder dots left empty (**NOT fraud**) |
| `6` | `occluded_region` | Footprints obscured by heatsinks, metal shielding, or heavy dust |
---
## πŸ’» Usage with Ultralytics YOLOv8
```python
from ultralytics import YOLO
# Load pre-trained model backbone
model = YOLO("yolov8n.pt")
# Fine-tune on Hugging Face dataset
model.train(data="data.yaml", epochs=50, imgsz=960, copy_paste=0.8, cls=2.5)
```
## βš–οΈ License & Attribution
Released under **Creative Commons Attribution 4.0 International (CC BY 4.0)** for e-waste recycling research and component purity index assaying.
"""
(HF_DATASET_DIR / "README.md").write_text(readme_content, encoding="utf-8")
print(f"SUCCESS! Hugging Face dataset formatted and ready at:\n {HF_DATASET_DIR}")
print("\nYou can now push to Hugging Face Hub using HF CLI:\n cd huggingface_dataset\n huggingface-cli upload <your-hf-username>/<your-repo-name> . --repo-type=dataset")
if __name__ == "__main__":
build_huggingface_dataset()