Use samples panel for DeepFashion search demo
Browse files
demo.py
CHANGED
|
@@ -6,6 +6,7 @@ from __future__ import annotations
|
|
| 6 |
import os
|
| 7 |
import re
|
| 8 |
import time
|
|
|
|
| 9 |
from collections import Counter
|
| 10 |
from pathlib import Path
|
| 11 |
from typing import Any
|
|
@@ -334,10 +335,46 @@ def ensure_layouts(dataset: hv.Dataset) -> dict[str, str]:
|
|
| 334 |
return layouts
|
| 335 |
|
| 336 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 337 |
def build_dataset() -> tuple[hv.Dataset, dict[str, str]]:
|
| 338 |
dataset = hv.Dataset(DATASET_NAME)
|
| 339 |
add_deepfashion_samples(dataset)
|
| 340 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 341 |
return dataset, layouts
|
| 342 |
|
| 343 |
|
|
|
|
| 6 |
import os
|
| 7 |
import re
|
| 8 |
import time
|
| 9 |
+
import math
|
| 10 |
from collections import Counter
|
| 11 |
from pathlib import Path
|
| 12 |
from typing import Any
|
|
|
|
| 335 |
return layouts
|
| 336 |
|
| 337 |
|
| 338 |
+
def ensure_catalog_layout(dataset: hv.Dataset) -> str:
|
| 339 |
+
samples = dataset.samples
|
| 340 |
+
if not samples:
|
| 341 |
+
raise RuntimeError("DeepFashion demo has no samples to lay out.")
|
| 342 |
+
|
| 343 |
+
categories = sorted({str(sample.metadata.get("category") or sample.label or "unknown") for sample in samples})
|
| 344 |
+
category_index = {category: index for index, category in enumerate(categories)}
|
| 345 |
+
category_totals = Counter(str(sample.metadata.get("category") or sample.label or "unknown") for sample in samples)
|
| 346 |
+
category_seen: Counter[str] = Counter()
|
| 347 |
+
ids: list[str] = []
|
| 348 |
+
coords: list[list[float]] = []
|
| 349 |
+
|
| 350 |
+
for sample in samples:
|
| 351 |
+
category = str(sample.metadata.get("category") or sample.label or "unknown")
|
| 352 |
+
center_angle = (2.0 * math.pi * category_index[category]) / max(1, len(categories))
|
| 353 |
+
slot = category_seen[category]
|
| 354 |
+
category_seen[category] += 1
|
| 355 |
+
|
| 356 |
+
local_angle = (2.0 * math.pi * slot) / max(1, category_totals[category])
|
| 357 |
+
local_radius = 0.14 + 0.035 * (slot % 5)
|
| 358 |
+
center_radius = 4.0
|
| 359 |
+
coords.append(
|
| 360 |
+
[
|
| 361 |
+
center_radius * math.cos(center_angle) + local_radius * math.cos(local_angle),
|
| 362 |
+
center_radius * math.sin(center_angle) + local_radius * math.sin(local_angle),
|
| 363 |
+
]
|
| 364 |
+
)
|
| 365 |
+
ids.append(sample.id)
|
| 366 |
+
|
| 367 |
+
return dataset.set_coords("euclidean", ids, coords)
|
| 368 |
+
|
| 369 |
+
|
| 370 |
def build_dataset() -> tuple[hv.Dataset, dict[str, str]]:
|
| 371 |
dataset = hv.Dataset(DATASET_NAME)
|
| 372 |
add_deepfashion_samples(dataset)
|
| 373 |
+
if ENABLE_CONTEXT_MAPS:
|
| 374 |
+
layouts = ensure_layouts(dataset)
|
| 375 |
+
else:
|
| 376 |
+
ensure_catalog_layout(dataset)
|
| 377 |
+
layouts = {}
|
| 378 |
return dataset, layouts
|
| 379 |
|
| 380 |
|