Spaces:
Sleeping
Sleeping
File size: 24,734 Bytes
d9f2fc5 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 | """
π― YOLO Trainer & Detector
Train YOLOv8 on a custom dataset and run inference β all from a Gradio UI.
"""
import os
import io
import time
import queue
import threading
import zipfile
import yaml
import cv2
import numpy as np
import matplotlib
matplotlib.use("Agg")
import matplotlib.pyplot as plt
from PIL import Image as PILImage
from pathlib import Path
import gradio as gr
from ultralytics import YOLO
from huggingface_hub import hf_hub_download
# βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
# Constants
# βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
DATASET_REPO = "dharshanzeb/yolo-detection-dataset"
DATASET_DIR = "/tmp/yolo_dataset"
RUNS_DIR = "/tmp/yolo_runs"
BEST_MODEL_PATH = os.path.join(RUNS_DIR, "gradio_train", "weights", "best.pt")
LAST_MODEL_PATH = os.path.join(RUNS_DIR, "gradio_train", "weights", "last.pt")
CLASS_NAMES = ["car", "person", "dog", "cat", "bicycle"]
# Global state
trained_model_path = None
is_training = False
# βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
# Dataset download & preparation
# βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
def download_dataset():
"""Download YOLO dataset from HF Hub and prepare data.yaml."""
if os.path.exists(os.path.join(DATASET_DIR, "images", "train")):
yaml_path = os.path.join(DATASET_DIR, "data.yaml")
if os.path.exists(yaml_path):
return yaml_path, "β
Dataset already downloaded."
log = "π₯ Downloading dataset from HF Hub...\n"
os.makedirs(DATASET_DIR, exist_ok=True)
for split in ["train", "val", "test"]:
log += f" Downloading {split}.zip...\n"
zip_path = hf_hub_download(
repo_id=DATASET_REPO,
filename=f"yolo_format/{split}.zip",
repo_type="dataset",
)
with zipfile.ZipFile(zip_path) as zf:
zf.extractall(DATASET_DIR)
# Download and patch data.yaml
yaml_remote = hf_hub_download(
repo_id=DATASET_REPO,
filename="yolo_format/data.yaml",
repo_type="dataset",
)
with open(yaml_remote) as f:
cfg = yaml.safe_load(f)
cfg["path"] = DATASET_DIR
local_yaml = os.path.join(DATASET_DIR, "data.yaml")
with open(local_yaml, "w") as f:
yaml.dump(cfg, f)
# Count images
n_train = len(list(Path(DATASET_DIR, "images", "train").glob("*.jpg")))
n_val = len(list(Path(DATASET_DIR, "images", "val").glob("*.jpg")))
n_test = len(list(Path(DATASET_DIR, "images", "test").glob("*.jpg")))
log += f"\nβ
Dataset ready!\n"
log += f" Train: {n_train} images\n"
log += f" Val: {n_val} images\n"
log += f" Test: {n_test} images\n"
log += f" Classes: {CLASS_NAMES}\n"
return local_yaml, log
# βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
# Metrics chart
# βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
def make_metrics_chart(history):
"""Create a loss + mAP chart from training history."""
if not history:
return None
epochs = [h["epoch"] for h in history]
fig, axes = plt.subplots(1, 2, figsize=(14, 5))
fig.patch.set_facecolor("#1a1a2e")
# ββ Loss subplot ββ
ax1 = axes[0]
ax1.set_facecolor("#16213e")
loss_keys = list(history[0].get("loss", {}).keys())
colors_loss = ["#e94560", "#f5a623", "#50fa7b"]
for i, k in enumerate(loss_keys):
vals = [h["loss"].get(k, 0) for h in history]
label = k.split("/")[-1] if "/" in k else k
color = colors_loss[i % len(colors_loss)]
ax1.plot(epochs, vals, marker="o", markersize=4, label=label,
color=color, linewidth=2)
ax1.set_title("Training Loss", color="white", fontsize=14, fontweight="bold")
ax1.set_xlabel("Epoch", color="white")
ax1.set_ylabel("Loss", color="white")
ax1.legend(facecolor="#16213e", edgecolor="white", labelcolor="white")
ax1.tick_params(colors="white")
ax1.grid(True, alpha=0.2, color="white")
for spine in ax1.spines.values():
spine.set_color("white")
# ββ mAP subplot ββ
ax2 = axes[1]
ax2.set_facecolor("#16213e")
map_keys = [
("metrics/mAP50(B)", "mAP@50", "#00d2ff"),
("metrics/mAP50-95(B)", "mAP@50-95", "#7b2ff7"),
]
for key, label, color in map_keys:
vals = [h["metrics"].get(key, 0) for h in history]
if any(v > 0 for v in vals):
ax2.plot(epochs, vals, marker="s", markersize=4, label=label,
color=color, linewidth=2)
ax2.set_title("Validation mAP", color="white", fontsize=14, fontweight="bold")
ax2.set_xlabel("Epoch", color="white")
ax2.set_ylabel("mAP", color="white")
ax2.set_ylim(0, 1)
ax2.legend(facecolor="#16213e", edgecolor="white", labelcolor="white")
ax2.tick_params(colors="white")
ax2.grid(True, alpha=0.2, color="white")
for spine in ax2.spines.values():
spine.set_color("white")
plt.tight_layout(pad=2)
buf = io.BytesIO()
plt.savefig(buf, format="png", dpi=120, bbox_inches="tight",
facecolor=fig.get_facecolor())
buf.seek(0)
chart = np.array(PILImage.open(buf).copy())
plt.close(fig)
return chart
# βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
# Training function (generator for streaming logs)
# βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
def train_yolo(model_size, epochs, batch_size, learning_rate, img_size):
"""
Generator function: trains YOLO and yields (log_text, metrics_chart, status)
after each epoch.
"""
global trained_model_path, is_training
if is_training:
yield "β οΈ Training already in progress. Please wait.", None, "β οΈ Busy"
return
is_training = True
log_queue_local = queue.Queue()
history = []
accumulated_log = ""
train_exception = [None]
try:
# Step 1: Download dataset
yield "π₯ Preparing dataset...", None, "π₯ Downloading..."
data_yaml, dl_log = download_dataset()
accumulated_log += dl_log + "\n"
yield accumulated_log, None, "π₯ Dataset ready"
# Step 2: Load model
model_variant = f"yolov8{model_size}.pt"
accumulated_log += f"π Loading {model_variant}...\n"
yield accumulated_log, None, f"π Loading {model_variant}"
model = YOLO(model_variant)
# Step 3: Attach callback
def on_fit_epoch_end(trainer):
try:
loss_dict = {}
if trainer.tloss is not None:
loss_dict = trainer.label_loss_items(trainer.tloss)
entry = {
"epoch": trainer.epoch + 1,
"epochs": trainer.epochs,
"metrics": dict(trainer.metrics) if trainer.metrics else {},
"fitness": float(trainer.fitness) if trainer.fitness else 0.0,
"loss": loss_dict,
}
log_queue_local.put(entry)
except Exception as e:
log_queue_local.put({"error": str(e)})
model.add_callback("on_fit_epoch_end", on_fit_epoch_end)
# Step 4: Run training in background thread
def run_training():
try:
device = 0 if __import__("torch").cuda.is_available() else "cpu"
model.train(
data=data_yaml,
epochs=int(epochs),
batch=int(batch_size),
lr0=float(learning_rate),
imgsz=int(img_size),
device=device,
workers=0,
project=RUNS_DIR,
name="gradio_train",
exist_ok=True,
pretrained=True,
mosaic=1.0,
mixup=0.1,
patience=50,
verbose=False,
)
except Exception as e:
train_exception[0] = e
finally:
log_queue_local.put(None) # sentinel
accumulated_log += f"\nπ Starting training: {model_variant} | {int(epochs)} epochs | batch={int(batch_size)} | lr={learning_rate}\n"
accumulated_log += f"{'β' * 60}\n"
yield accumulated_log, None, "π Training started..."
t = threading.Thread(target=run_training, daemon=True)
t.start()
# Step 5: Stream logs from queue
while True:
try:
item = log_queue_local.get(timeout=120)
except queue.Empty:
accumulated_log += "β³ Waiting for update...\n"
yield accumulated_log, make_metrics_chart(history), "β³ Waiting..."
continue
if item is None:
break
if "error" in item:
accumulated_log += f"β οΈ Callback error: {item['error']}\n"
yield accumulated_log, make_metrics_chart(history), "β οΈ Error"
continue
history.append(item)
e, E = item["epoch"], item["epochs"]
# Format loss
loss_parts = []
for k, v in item["loss"].items():
name = k.split("/")[-1] if "/" in k else k
loss_parts.append(f"{name}={v:.4f}")
loss_str = " | ".join(loss_parts) if loss_parts else "N/A"
# Format mAP
map50 = item["metrics"].get("metrics/mAP50(B)", 0)
map50_95 = item["metrics"].get("metrics/mAP50-95(B)", 0)
line = f"π Epoch {e:>3d}/{E} | {loss_str} | mAP50={map50:.4f} | mAP50-95={map50_95:.4f}\n"
accumulated_log += line
chart = make_metrics_chart(history)
status = f"ποΈ Epoch {e}/{E} | mAP50={map50:.4f}"
yield accumulated_log, chart, status
t.join(timeout=10)
# Step 6: Check results
if train_exception[0]:
accumulated_log += f"\nβ Training error: {train_exception[0]}\n"
yield accumulated_log, make_metrics_chart(history), "β Failed"
return
# Find best model
if os.path.exists(BEST_MODEL_PATH):
trained_model_path = BEST_MODEL_PATH
elif os.path.exists(LAST_MODEL_PATH):
trained_model_path = LAST_MODEL_PATH
accumulated_log += f"\n{'β' * 60}\n"
accumulated_log += f"π TRAINING COMPLETE!\n"
accumulated_log += f"{'β' * 60}\n"
if trained_model_path:
accumulated_log += f"π Model saved: {trained_model_path}\n"
accumulated_log += f"π Switch to the Inference tab to test your model!\n"
else:
accumulated_log += f"β οΈ No model file found after training.\n"
if history:
final_map = history[-1]["metrics"].get("metrics/mAP50(B)", 0)
accumulated_log += f"\nπ Final mAP@50: {final_map:.4f}\n"
chart = make_metrics_chart(history)
yield accumulated_log, chart, "β
Training complete!"
except Exception as e:
accumulated_log += f"\nβ Error: {str(e)}\n"
yield accumulated_log, make_metrics_chart(history) if history else None, "β Error"
finally:
is_training = False
# βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
# Inference function
# βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
def run_inference(image, conf_threshold, use_pretrained):
"""Run YOLO detection on an uploaded image."""
if image is None:
gr.Warning("Please upload an image first!")
return None, "β οΈ No image uploaded"
# Select model
if use_pretrained:
model_path = "yolov8n.pt"
model_label = "YOLOv8n (COCO pretrained)"
else:
if trained_model_path and os.path.exists(trained_model_path):
model_path = trained_model_path
model_label = f"Custom trained ({os.path.basename(trained_model_path)})"
else:
gr.Warning("No trained model found! Train first or use pretrained COCO model.")
return None, "β οΈ No trained model. Train first or check 'Use Pretrained'."
try:
device = 0 if __import__("torch").cuda.is_available() else "cpu"
model = YOLO(model_path)
results = model.predict(
source=image,
conf=float(conf_threshold),
iou=0.45,
device=device,
verbose=False,
)
result = results[0]
# Draw bounding boxes
plotted_bgr = result.plot(conf=True, labels=True, line_width=2)
plotted_rgb = cv2.cvtColor(plotted_bgr, cv2.COLOR_BGR2RGB)
# Build detection summary
n_detections = len(result.boxes)
if n_detections == 0:
summary = f"π **{model_label}**\n\nNo objects detected (conf > {conf_threshold})"
else:
lines = [f"π **{model_label}** β Found **{n_detections}** objects:\n"]
lines.append("| # | Class | Confidence | Bbox (x1,y1,x2,y2) |")
lines.append("|---|-------|-----------|---------------------|")
for i, box in enumerate(result.boxes):
cls_id = int(box.cls[0].item())
cls_name = result.names[cls_id]
conf = box.conf[0].item()
coords = [round(v, 1) for v in box.xyxy[0].tolist()]
lines.append(f"| {i+1} | **{cls_name}** | {conf:.2f} | {coords} |")
summary = "\n".join(lines)
return plotted_rgb, summary
except Exception as e:
return None, f"β Error: {str(e)}"
# βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
# Sample images from dataset for inference demo
# βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
def load_sample_image():
"""Load a random sample from the test set."""
test_dir = Path(DATASET_DIR) / "images" / "test"
if not test_dir.exists():
# Download dataset first
download_dataset()
if test_dir.exists():
images = list(test_dir.glob("*.jpg"))
if images:
import random
img_path = random.choice(images)
return np.array(PILImage.open(img_path))
return None
# βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
# Build Gradio UI
# βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
css = """
.gradio-container { max-width: 1100px !important; margin: 0 auto !important; }
.train-log { font-family: 'Courier New', monospace !important; font-size: 13px !important; }
"""
with gr.Blocks(css=css, title="π― YOLO Trainer & Detector", theme=gr.themes.Soft()) as demo:
gr.Markdown("""
# π― YOLO Trainer & Detector
**Train** a YOLOv8 model on the [yolo-detection-dataset](https://huggingface.co/datasets/dharshanzeb/yolo-detection-dataset)
and **run inference** on any image β all from this UI.
""")
with gr.Tabs():
# ββββββββββββββββββββββββββββββββββββββββββββββββββββββ
# TAB 1: TRAINING
# ββββββββββββββββββββββββββββββββββββββββββββββββββββββ
with gr.Tab("ποΈ Train"):
with gr.Row():
# Left column: controls
with gr.Column(scale=1, min_width=280):
gr.Markdown("### βοΈ Training Configuration")
model_size = gr.Dropdown(
choices=[
("YOLOv8 Nano (fastest)", "n"),
("YOLOv8 Small", "s"),
("YOLOv8 Medium", "m"),
],
value="n",
label="Model Size",
)
epochs = gr.Slider(
minimum=1, maximum=100, value=20, step=1,
label="Epochs",
)
batch_size = gr.Slider(
minimum=4, maximum=64, value=16, step=4,
label="Batch Size",
)
learning_rate = gr.Slider(
minimum=0.0001, maximum=0.1, value=0.01, step=0.0001,
label="Learning Rate",
)
img_size = gr.Dropdown(
choices=[320, 416, 512, 640],
value=640,
label="Image Size",
)
train_btn = gr.Button(
"π Start Training", variant="primary", size="lg"
)
train_status = gr.Markdown("*Ready to train*")
gr.Markdown("""
---
### π Dataset Info
- **5 classes**: car, person, dog, cat, bicycle
- **500** train / **100** val / **50** test images
- **640Γ640** resolution
- 15% hard negatives included
""")
# Right column: logs + chart
with gr.Column(scale=2):
gr.Markdown("### π Training Progress")
metrics_chart = gr.Image(
label="Loss & mAP Curves",
interactive=False,
height=320,
)
train_log = gr.Textbox(
label="Training Log",
lines=18,
max_lines=30,
interactive=False,
autoscroll=True,
elem_classes=["train-log"],
)
# Wire training button
train_btn.click(
fn=train_yolo,
inputs=[model_size, epochs, batch_size, learning_rate, img_size],
outputs=[train_log, metrics_chart, train_status],
)
# ββββββββββββββββββββββββββββββββββββββββββββββββββββββ
# TAB 2: INFERENCE
# ββββββββββββββββββββββββββββββββββββββββββββββββββββββ
with gr.Tab("π Detect"):
gr.Markdown("### Upload an image to detect objects")
with gr.Row():
# Left: input
with gr.Column(scale=1):
input_image = gr.Image(
label="π€ Upload Image",
type="numpy",
sources=["upload", "clipboard"],
height=400,
)
with gr.Row():
conf_threshold = gr.Slider(
minimum=0.05, maximum=0.95, value=0.25, step=0.05,
label="Confidence Threshold",
)
with gr.Row():
use_pretrained = gr.Checkbox(
value=False,
label="Use Pretrained COCO Model (YOLOv8n)",
info="Check this if you haven't trained yet",
)
with gr.Row():
detect_btn = gr.Button(
"π Detect Objects", variant="primary", size="lg"
)
sample_btn = gr.Button(
"π² Load Sample", variant="secondary", size="lg"
)
# Right: output
with gr.Column(scale=1):
output_image = gr.Image(
label="πΈ Detection Result",
type="numpy",
interactive=False,
height=400,
)
detection_summary = gr.Markdown("*Upload an image and click Detect*")
# Wire inference
detect_btn.click(
fn=run_inference,
inputs=[input_image, conf_threshold, use_pretrained],
outputs=[output_image, detection_summary],
)
sample_btn.click(
fn=load_sample_image,
outputs=[input_image],
)
# ββββββββββββββββββββββββββββββββββββββββββββββββββββββ
# TAB 3: ABOUT
# ββββββββββββββββββββββββββββββββββββββββββββββββββββββ
with gr.Tab("βΉοΈ About"):
gr.Markdown("""
## How It Works
### ποΈ Training
1. The dataset ([dharshanzeb/yolo-detection-dataset](https://huggingface.co/datasets/dharshanzeb/yolo-detection-dataset))
is auto-downloaded from the HF Hub
2. YOLOv8 is initialized with COCO-pretrained weights (transfer learning)
3. Training runs with your configured hyperparameters
4. Real-time metrics (loss + mAP) are displayed after each epoch
5. The best model (by mAP) is saved automatically
### π Inference
- **Custom model**: Uses the model you just trained
- **Pretrained COCO**: Uses YOLOv8n trained on 80 COCO classes (good for real photos)
### π― Dataset Classes
| ID | Class | Description |
|---|---|---|
| 0 | Car | Red car shapes with windows & wheels |
| 1 | Person | Blue stick figures |
| 2 | Dog | Brown dog shapes |
| 3 | Cat | Orange cat shapes with ears |
| 4 | Bicycle | Green bicycles with wheels |
### π‘ Tips
- **First time?** Start with `YOLOv8 Nano` + `20 epochs` β trains in ~5 min on GPU
- **Better accuracy?** Try `YOLOv8 Small` + `50 epochs` + `lr=0.01`
- **No GPU?** Training works on CPU too (just slower). Use pretrained COCO for instant inference.
- **Low mAP?** Increase epochs or try a larger model size
### π Links
- [Dataset](https://huggingface.co/datasets/dharshanzeb/yolo-detection-dataset)
- [Ultralytics YOLOv8](https://docs.ultralytics.com/)
- [Gradio](https://gradio.app/)
""")
if __name__ == "__main__":
demo.queue(max_size=5).launch()
|