|
| 1 | +# Perceptual near-duplicate grouping |
| 2 | + |
| 3 | +[English](NEAR_DUPLICATES.md) | [简体中文](NEAR_DUPLICATES.zh-CN.md) |
| 4 | + |
| 5 | +The `cluster` command reduces repeated human review and detects visually duplicated images crossing YOLO dataset splits. It is model-free, CPU-only and read-only: it never deletes, moves or rewrites an image or label. |
| 6 | + |
| 7 | +## Usage |
| 8 | + |
| 9 | +```powershell |
| 10 | +yolo-label-recovery cluster D:\data\mining-safety ` |
| 11 | + --output-dir D:\data\near-duplicate-audit ` |
| 12 | + --splits train val test ` |
| 13 | + --workers 4 ` |
| 14 | + --max-distance 6 ` |
| 15 | + --redact-paths |
| 16 | +``` |
| 17 | + |
| 18 | +Use `--fail-on-cross-split` in CI when perceptual leakage must fail a data release. |
| 19 | + |
| 20 | +## Algorithm |
| 21 | + |
| 22 | +1. Decode one image per worker and apply EXIF orientation. |
| 23 | +2. Produce a 64-bit difference hash (dHash), a 64-bit average hash (aHash), dimensions and luminance statistics. |
| 24 | +3. Query dHash neighbors with a BK-tree instead of comparing every image pair. |
| 25 | +4. Reject candidates that violate the aHash radius or aspect-ratio guard. |
| 26 | +5. For two low-texture images, require similar mean luminance so black and white frames do not collide. |
| 27 | +6. Convert accepted neighbor edges into deterministic connected components. |
| 28 | +7. Select the highest-resolution image in each component as the first review representative. |
| 29 | + |
| 30 | +For `N` images, fingerprint storage is `O(N)`. BK-tree search is usually far below brute-force `O(N²)`, although adversarial hash distributions can degrade. Pixel buffers are bounded by the worker count; the entire dataset is never decoded into RAM at once. |
| 31 | + |
| 32 | +## Outputs |
| 33 | + |
| 34 | +| File | Purpose | |
| 35 | +|---|---| |
| 36 | +| `near_duplicate_members.csv` | One row per clustered image with hashes and representative distance | |
| 37 | +| `review_representatives.csv` | One highest-resolution representative per review group | |
| 38 | +| `fingerprint_failures.csv` | Corrupt or unreadable image evidence | |
| 39 | +| `near_duplicate_summary.json` | Policy, totals, group membership and leakage counts | |
| 40 | +| `near_duplicate_report.html` | Self-contained visual report for review and delivery | |
| 41 | + |
| 42 | +## Guardrails and limitations |
| 43 | + |
| 44 | +- A cluster means visual similarity, not permission to delete data automatically. |
| 45 | +- Connected components allow transitive chains: A may match B and B may match C even when A is farther from C. The report exposes maximum dHash distance to the selected representative so loose groups can be inspected. |
| 46 | +- dHash is useful for resize, JPEG recompression and modest brightness changes. It is not designed for major crops, rotations or semantic similarity. |
| 47 | +- Cross-split clusters are leakage candidates. Confirm provenance before moving images because visually similar frames may still represent intentional temporal evaluation. |
| 48 | +- Thresholds are policy. Validate them on a reviewed sample from the target domain before bulk cleanup. |
| 49 | + |
| 50 | +## Reproducible public fixture |
| 51 | + |
| 52 | +```powershell |
| 53 | +python examples\create_near_duplicate_fixture.py --output .near-duplicate-fixture |
| 54 | +yolo-label-recovery cluster .near-duplicate-fixture ` |
| 55 | + --output-dir .near-duplicate-output ` |
| 56 | + --redact-paths |
| 57 | +``` |
| 58 | + |
| 59 | +Expected result: `11` discovered files, `1` intentional fingerprint failure, `3` groups, `7` grouped images and `2` cross-split groups. Black and white low-texture frames remain separate. |
| 60 | + |
| 61 | +## Interview framing |
| 62 | + |
| 63 | +This feature is evidence of data-centric ML engineering rather than another training wrapper. The key design choices are bounded decoding, sub-quadratic candidate search, conservative false-positive guards, deterministic review groups, explicit leakage evidence and immutable source data. |
0 commit comments