File size: 16,122 Bytes
e09a48c
 
 
 
 
 
 
 
 
 
 
 
 
cf7f887
e9206ba
 
 
 
 
cf7f887
e9206ba
 
cf7f887
e09a48c
e9206ba
 
 
 
 
 
 
 
 
 
 
e09a48c
e9206ba
 
 
cf7f887
e9206ba
 
 
 
cf7f887
e9206ba
e09a48c
 
 
 
 
e9206ba
 
 
 
 
 
cf7f887
e9206ba
cf7f887
 
e09a48c
cf7f887
 
e9206ba
e09a48c
 
 
 
 
 
 
 
 
 
 
e9206ba
 
 
 
 
 
e09a48c
e9206ba
2fb062a
cf7f887
e9206ba
 
 
e09a48c
 
 
 
 
 
 
 
 
 
e9206ba
 
 
2fb062a
e9206ba
 
 
 
 
 
2fb062a
e9206ba
 
2fb062a
e9206ba
cf7f887
 
 
 
e9206ba
cf7f887
2fb062a
 
cf7f887
e9206ba
2fb062a
e09a48c
 
2fb062a
e9206ba
e09a48c
 
e9206ba
cf7f887
e9206ba
cf7f887
e9206ba
 
2fb062a
e9206ba
 
 
e09a48c
 
2fb062a
e09a48c
cf7f887
 
 
 
e9206ba
e09a48c
e9206ba
 
 
 
2fb062a
 
e9206ba
 
 
e09a48c
e9206ba
e09a48c
2fb062a
 
 
e09a48c
cf7f887
 
 
2fb062a
cf7f887
e09a48c
e9206ba
 
cf7f887
e9206ba
2fb062a
cf7f887
 
 
 
e9206ba
 
 
 
e09a48c
 
 
 
 
 
 
 
 
 
 
e9206ba
e09a48c
e9206ba
cf7f887
e09a48c
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
cf7f887
e9206ba
2fb062a
e9206ba
 
 
 
 
 
 
 
 
 
e09a48c
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
e9206ba
cf7f887
e09a48c
 
 
 
cf7f887
 
e09a48c
 
 
 
 
 
 
 
cf7f887
 
 
 
2fb062a
 
 
e09a48c
2fb062a
 
e09a48c
2fb062a
cf7f887
 
 
2fb062a
 
cf7f887
e09a48c
2fb062a
e09a48c
2fb062a
 
e09a48c
2fb062a
e09a48c
cf7f887
2fb062a
cf7f887
e09a48c
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
cf7f887
2fb062a
 
 
 
e09a48c
2fb062a
 
e09a48c
2fb062a
cf7f887
 
2fb062a
e09a48c
cf7f887
2fb062a
cf7f887
 
 
 
2fb062a
cf7f887
 
 
 
2fb062a
cf7f887
 
 
2fb062a
e09a48c
2fb062a
cf7f887
 
 
 
 
 
 
 
2fb062a
cf7f887
e09a48c
 
cf7f887
 
 
 
 
 
 
 
 
 
 
 
e09a48c
cf7f887
 
 
 
 
 
 
 
 
 
 
 
 
 
e09a48c
cf7f887
 
 
 
 
 
 
e09a48c
cf7f887
 
 
 
 
 
 
 
 
 
e09a48c
cf7f887
 
 
 
 
 
 
 
e09a48c
cf7f887
 
 
 
 
 
 
 
 
e09a48c
cf7f887
 
 
 
2fb062a
cf7f887
 
 
2fb062a
e09a48c
cf7f887
 
 
 
2fb062a
cf7f887
 
 
2fb062a
 
 
cf7f887
 
2fb062a
cf7f887
2fb062a
cf7f887
 
2fb062a
 
 
cf7f887
 
 
 
 
2fb062a
 
 
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
"""
app.py – Roboflow‑aware YOLOv8 Dataset Quality Evaluator (v2)

Changelog (2025‑04‑17)
──────────────────────
β€’ **CPU‑bound loops parallelised** with `concurrent.futures.ProcessPoolExecutor`.
β€’ **Batch inference** in `qc_model_qa()` (GPU util ↑, latency ↓).
β€’ Optional **fastdup** path for duplicate detection (β‰ˆβ€―10Γ— faster on large sets).
β€’ Faster NumPy‑based `parse_label_file()`.
β€’ Small refactors β†’ clearer separation of stages & fewer globals.
β€’ Graceful degradation if heavy deps unavailable (cv2, imagehash, fastdup).
β€’ Tunable `CPU_COUNT` + env‑var guard for HF Spaces quota.
"""

from __future__ import annotations

import imghdr
import json
import os
import re
import shutil
import tempfile
from collections import Counter, defaultdict
from concurrent.futures import ProcessPoolExecutor, as_completed
from dataclasses import dataclass
from pathlib import Path
from typing import Dict, List, Tuple

import gradio as gr
import numpy as np
import pandas as pd
import yaml
from PIL import Image
from tqdm import tqdm

# ───────────────────────────────────────── Heavy optional deps ──
try:
    import cv2  # type: ignore
except ImportError:
    cv2 = None

try:
    import imagehash  # type: ignore
except ImportError:
    imagehash = None

try:
    import fastdup  # type: ignore
except ImportError:
    fastdup = None

try:
    from ultralytics import YOLO  # type: ignore
except ImportError:
    YOLO = None  # noqa: N806

try:
    from roboflow import Roboflow  # type: ignore
except ImportError:
    Roboflow = None  # type: ignore

# ───────────────────────────────────────── Config & constants ──
TMP_ROOT = Path(tempfile.gettempdir()) / "rf_datasets"
TMP_ROOT.mkdir(parents=True, exist_ok=True)

# Limit CPU workers on HF Spaces (feel free to raise locally)
CPU_COUNT = int(os.getenv("QC_CPU", max(1, (os.cpu_count() or 4) // 2)))
BATCH = int(os.getenv("QC_BATCH", 16))

DEFAULT_W = {
    "Integrity": 0.30,
    "Class balance": 0.15,
    "Image quality": 0.15,
    "Duplicates": 0.10,
    "Model QA": 0.30,
}

@dataclass
class DuplicateGroup:
    hash_val: str
    paths: List[Path]

# ───────────────────────────────────────── Generic helpers ─────

def load_yaml(path: Path) -> Dict:
    with path.open(encoding="utf-8") as f:
        return yaml.safe_load(f)


def parse_label_file(path: Path) -> list[tuple[int, float, float, float, float]]:
    if not path.exists() or path.stat().st_size == 0:
        return []
    try:
        arr = np.loadtxt(path, dtype=float)
        if arr.ndim == 1:
            arr = arr.reshape(1, -1)
        return [tuple(row) for row in arr]
    except Exception:
        return []


def guess_image_dirs(root: Path) -> List[Path]:
    subs = [
        root / "images",
        root / "train" / "images",
        root / "valid" / "images",
        root / "val" / "images",
        root / "test" / "images",
    ]
    return [d for d in subs if d.exists()]


def gather_dataset(root: Path, yaml_path: Path | None = None):
    if yaml_path is None:
        yamls = list(root.glob("*.yaml"))
        if not yamls:
            raise FileNotFoundError("Dataset YAML not found")
        yaml_path = yamls[0]

    meta = load_yaml(yaml_path)
    img_dirs = guess_image_dirs(root)
    if not img_dirs:
        raise FileNotFoundError("images/ directory hierarchy missing")

    imgs = [p for d in img_dirs for p in d.rglob("*.*") if imghdr.what(p) is not None]
    labels_root = {d.parent / "labels" for d in img_dirs}
    lbls = [next((lr / f"{p.stem}.txt" for lr in labels_root if (lr / f"{p.stem}.txt").exists()), None) for p in imgs]
    return imgs, lbls, meta

# ───────────────────────────────────────── Quality checks ─────
# Integrity -----------------------------------------------------

def _is_corrupt(path: Path) -> bool:
    try:
        with Image.open(path) as im:
            im.verify()
        return False
    except Exception:
        return True


def qc_integrity(imgs: List[Path], lbls: List[Path]):
    miss_lbl = [i for i, l in zip(imgs, lbls) if l is None]
    corrupt: List[Path] = []
    with ProcessPoolExecutor(max_workers=CPU_COUNT) as ex:
        fut = {ex.submit(_is_corrupt, p): p for p in imgs}
        for f in tqdm(as_completed(fut), total=len(fut), desc="integrity", leave=False):
            if f.result():
                corrupt.append(fut[f])

    score = 100 - (len(miss_lbl) + len(corrupt)) / max(len(imgs), 1) * 100
    return {
        "name": "Integrity",
        "score": max(score, 0),
        "details": {
            "missing_label_files": [str(p) for p in miss_lbl],
            "corrupt_images": [str(p) for p in corrupt],
        },
    }

# Class balance -------------------------------------------------

def qc_class_balance(lbls: List[Path]):
    cls_counts = Counter()
    boxes_per_img = []
    for l in lbls:
        bs = parse_label_file(l) if l else []
        boxes_per_img.append(len(bs))
        cls_counts.update(b[0] for b in bs)

    if not cls_counts:
        return {"name": "Class balance", "score": 0, "details": "No labels"}
    bal = (min(cls_counts.values()) / max(cls_counts.values())) * 100
    return {
        "name": "Class balance",
        "score": bal,
        "details": {
            "class_counts": dict(cls_counts),
            "boxes_per_image": {
                "min": int(np.min(boxes_per_img)),
                "max": int(np.max(boxes_per_img)),
                "mean": float(np.mean(boxes_per_img)),
            },
        },
    }

# Image quality -------------------------------------------------

def _quality_stat(path: Path, blur_thr: float):
    im = cv2.imread(str(path)) if cv2 else None
    if im is None:
        return path, False, False, False
    gray = cv2.cvtColor(im, cv2.COLOR_BGR2GRAY)
    lap = cv2.Laplacian(gray, cv2.CV_64F).var()
    br = gray.mean()
    return path, lap < blur_thr, br < 25, br > 230


def qc_image_quality(imgs: List[Path], blur_thr: float = 100.0):
    if cv2 is None:
        return {"name": "Image quality", "score": 100, "details": "cv2 not installed"}

    blurry: list[Path] = []
    dark: list[Path] = []
    bright: list[Path] = []

    with ProcessPoolExecutor(max_workers=CPU_COUNT) as ex:
        for p, is_blur, is_dark, is_bright in tqdm(
            ex.map(lambda x: _quality_stat(x, blur_thr), imgs),
            total=len(imgs),
            desc="img‑quality",
            leave=False,
        ):
            if is_blur:
                blurry.append(p)
            if is_dark:
                dark.append(p)
            if is_bright:
                bright.append(p)

    bad = len(set(blurry + dark + bright))
    score = 100 - bad / max(len(imgs), 1) * 100
    return {
        "name": "Image quality",
        "score": score,
        "details": {
            "blurry": [str(p) for p in blurry],
            "dark": [str(p) for p in dark],
            "bright": [str(p) for p in bright],
        },
    }

# Duplicate images ---------------------------------------------

def qc_duplicates(imgs: List[Path]):
    # Fast path – use fastdup if installed & enough images
    if fastdup is not None and len(imgs) > 50:
        try:
            fd = fastdup.create(input_dir=str(Path(imgs[0]).parent.parent), work_dir=str(TMP_ROOT / "fastdup"))
            fd.run()
            clusters = fd.get_clusters()
            dup = sum(len(c) - 1 for c in clusters)
            score = 100 - dup / max(len(imgs), 1) * 100
            return {
                "name": "Duplicates",
                "score": score,
                "details": {"groups": clusters[:50]},
            }
        except Exception:
            pass  # fallback to hash

    if imagehash is None:
        return {"name": "Duplicates", "score": 100, "details": "skipped (deps)"}

    def _hash(p):
        return str(imagehash.average_hash(Image.open(p)))

    hashes: Dict[str, List[Path]] = defaultdict(list)
    with ProcessPoolExecutor(max_workers=CPU_COUNT) as ex:
        for h, p in tqdm(
            zip(ex.map(_hash, imgs), imgs),
            total=len(imgs),
            desc="hashing",
            leave=False,
        ):
            hashes[h].append(p)

    groups = [g for g in hashes.values() if len(g) > 1]
    dup = sum(len(g) - 1 for g in groups)
    score = 100 - dup / max(len(imgs), 1) * 100
    return {
        "name": "Duplicates",
        "score": score,
        "details": {"groups": [[str(p) for p in g] for g in groups[:50]]},
    }

# Model‑assisted QA --------------------------------------------

def _rel_iou(b1, b2):
    x1, y1, w1, h1 = b1
    x2, y2, w2, h2 = b2
    xa1, ya1, xa2, ya2 = x1 - w1 / 2, y1 - h1 / 2, x1 + w1 / 2, y1 + h1 / 2
    xb1, yb1, xb2, yb2 = x2 - w2 / 2, y2 - h2 / 2, x2 + w2 / 2, y2 + h2 / 2
    ix1, iy1, ix2, iy2 = max(xa1, xb1), max(ya1, yb1), min(xa2, xb2), min(ya2, yb2)
    inter = max(ix2 - ix1, 0) * max(iy2 - iy1, 0)
    union = w1 * h1 + w2 * h2 - inter
    return inter / union if union else 0.0


def qc_model_qa(imgs: List[Path], weights: str | None, lbls: List[Path], iou_thr: float = 0.5):
    if weights is None or YOLO is None:
        return {"name": "Model QA", "score": 100, "details": "skipped (no weights)"}

    model = YOLO(weights)
    ious, mism = [], []

    for i in range(0, len(imgs), BATCH):
        batch_paths = imgs[i : i + BATCH]
        results = model.predict(batch_paths, verbose=False)
        for p, res in zip(batch_paths, results):
            gtb = parse_label_file(p.parent.parent / "labels" / f"{p.stem}.txt")
            if not gtb:
                continue
            for cls, x, y, w, h in gtb:
                best = 0.0
                for b, c in zip(res.boxes.xywh.cpu().numpy(), res.boxes.cls.cpu().numpy()):
                    if int(c) != cls:
                        continue
                    best = max(best, _rel_iou((x, y, w, h), tuple(b)))
                ious.append(best)
                if best < iou_thr:
                    mism.append(str(p))

    miou = float(np.mean(ious)) if ious else 1.0
    return {
        "name": "Model QA",
        "score": miou * 100,
        "details": {"mean_iou": miou, "mismatched_images": mism[:50]},
    }

# Aggregate -----------------------------------------------------

def aggregate(scores):
    return sum(DEFAULT_W.get(r["name"], 0) * r["score"] for r in scores)

# ───────────────────────────────────────── Roboflow helpers ────
RF_RE = re.compile(r"https://universe\.roboflow\.com/([^/]+)/([^/]+)/dataset/(\d+)")

def download_rf_dataset(url: str, rf_api: "Roboflow", dest: Path) -> Path:
    m = RF_RE.match(url.strip())
    if not m:
        raise ValueError(f"Bad RF URL: {url}")

    ws, proj, ver = m.groups()
    ds_dir = dest / f"{ws}_{proj}_v{ver}"
    if ds_dir.exists():
        return ds_dir

    project = rf_api.workspace(ws).project(proj)
    project.version(int(ver)).download("yolov8", location=str(ds_dir))
    return ds_dir

# ───────────────────────────────────────── Main logic ──────────

def run_quality(root: Path, yaml_override: Path | None, weights: Path | None):
    imgs, lbls, meta = gather_dataset(root, yaml_override)
    res = [
        qc_integrity(imgs, lbls),
        qc_class_balance(lbls),
        qc_image_quality(imgs),
        qc_duplicates(imgs),
        qc_model_qa(imgs, str(weights) if weights else None, lbls),
    ]
    final = aggregate(res)

    md = [f"## **{meta.get('name', root.name)}**Β β€”Β ScoreΒ {final:.1f}/100"]
    for r in res:
        md.append(f"### {r['name']}Β Β {r['score']:.1f}")
        md.append("<details><summary>details</summary>\n\n```json")
        md.append(json.dumps(r["details"], indent=2))
        md.append("```\n</details>\n")
    md_str = "\n".join(md)

    cls_counts = res[1]["details"].get("class_counts", {})  # type: ignore[index]
    df = pd.DataFrame.from_dict(cls_counts, orient="index", columns=["count"])
    df.index.name = "class"
    return md_str, df

# ───────────────────────────────────────── Gradio UI ───────────

def evaluate(
    api_key: str,
    url_txt: gr.File | None,
    zip_file: gr.File | None,
    server_path: str,
    yaml_file: gr.File | None,
    weights: gr.File | None,
):
    if not any([url_txt, zip_file, server_path]):
        return "Upload a .txt of URLs or dataset ZIP/path", pd.DataFrame()

    reports, dfs = [], []

    # Roboflow batch ------------------------------------------
    if url_txt:
        if Roboflow is None:
            return "`roboflow` not installed", pd.DataFrame()
        if not api_key:
            return "Enter Roboflow API key", pd.DataFrame()

        rf = Roboflow(api_key=api_key.strip())
        for line in Path(url_txt.name).read_text().splitlines():
            if not line.strip():
                continue
            try:
                ds_root = download_rf_dataset(line, rf, TMP_ROOT)
                md, df = run_quality(ds_root, None, Path(weights.name) if weights else None)
                reports.append(md)
                dfs.append(df)
            except Exception as e:
                reports.append(f"### {line}\n\n⚠️ {e}")

    # Manual ZIP ----------------------------------------------
    if zip_file:
        tmp_dir = Path(tempfile.mkdtemp())
        shutil.unpack_archive(zip_file.name, tmp_dir)
        md, df = run_quality(tmp_dir, Path(yaml_file.name) if yaml_file else None, Path(weights.name) if weights else None)
        reports.append(md)
        dfs.append(df)
        shutil.rmtree(tmp_dir, ignore_errors=True)

    # Manual path ---------------------------------------------
    if server_path:
        md, df = run_quality(Path(server_path), Path(yaml_file.name) if yaml_file else None, Path(weights.name) if weights else None)
        reports.append(md)
        dfs.append(df)

    summary_md = "\n\n---\n\n".join(reports)
    combined_df = pd.concat(dfs).groupby(level=0).sum() if dfs else pd.DataFrame()
    return summary_md, combined_df

# ───────────────────────────────────────── Launch  ────────────
with gr.Blocks(title="YOLO Dataset Quality Evaluator") as demo:
    gr.Markdown(
        """
# YOLOv8 Dataset Quality Evaluator

### Roboflow batch  
1. Paste your **Roboflow API key**  
2. Upload a **.txt** file – one `https://universe.roboflow.com/.../dataset/x` per line

### Manual  
* Upload a dataset **ZIP** or type a dataset **path** on the server  
* Optionally supply a custom **data.yaml** and/or a **YOLOΒ .pt** weights file for model‑assisted QA
"""
    )

    with gr.Row():
        api_in = gr.Textbox(label="Roboflow API key", type="password", placeholder="rf_XXXXXXXXXXXXXXXX")
        url_txt_in = gr.File(label=".txt of RF dataset URLs", file_types=[".txt"])

    with gr.Row():
        zip_in = gr.File(label="Dataset ZIP")
        path_in = gr.Textbox(label="Path on server", placeholder="/data/my_dataset")

    with gr.Row():
        yaml_in = gr.File(label="Custom YAML", file_types=[".yaml"])
        weights_in = gr.File(label="YOLO weights (.pt)")

    run_btn = gr.Button("Evaluate")
    out_md = gr.Markdown()
    out_df = gr.Dataframe()

    run_btn.click(
        evaluate,
        inputs=[api_in, url_txt_in, zip_in, path_in, yaml_in, weights_in],
        outputs=[out_md, out_df],
    )

if __name__ == "__main__":
    demo.launch(server_name="0.0.0.0", server_port=int(os.getenv("PORT", 7860)))