Spaces:
Sleeping
Sleeping
File size: 6,495 Bytes
eb8df9a ab318c0 eb8df9a d547fdb ab318c0 7d7a621 eb8df9a 7d7a621 ab318c0 7d7a621 eb8df9a 7d7a621 ab318c0 eb8df9a ab318c0 eb8df9a ab318c0 eb8df9a ab318c0 eb8df9a ab318c0 eb8df9a ab318c0 eb8df9a ab318c0 eb8df9a 7d7a621 eb8df9a ab318c0 7d7a621 ab318c0 d547fdb ab318c0 d547fdb ab318c0 eb8df9a d547fdb ab318c0 7d7a621 eb8df9a d547fdb ab318c0 7d7a621 eb8df9a ab318c0 7d7a621 eb8df9a |
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 |
#!/usr/bin/env python3
from __future__ import annotations
import json
from dataclasses import dataclass
from pathlib import Path
from typing import Iterable
import click
import httpx
from eval import common
@dataclass(frozen=True)
class Config:
api: str
label_sets: list[Path]
images: list[Path]
domain_top_n: int
top_k: int
out_dir: Path | None
summary: bool
select_domain_n: int | None
select_label_n: int | None
min_domain_score: float | None
min_label_score: float | None
def expand_label_sets(paths: Iterable[str]) -> list[Path]:
out: list[Path] = []
for raw in paths:
p = Path(raw)
if any(ch in raw for ch in ["*", "?", "["]):
out.extend(sorted(Path().glob(raw)))
else:
out.append(p)
return [p for p in out if p.is_file()]
def to_row(
label_set: Path,
image: Path,
data: dict,
*,
select_domain_n: int | None,
select_label_n: int | None,
min_domain_score: float | None,
min_label_score: float | None,
) -> dict[str, str]:
domain_hits = data.get("domain_hits", [])
label_hits = data.get("label_hits", [])
selected_domains = common.select_hits(domain_hits, max_n=select_domain_n, min_score=min_domain_score)
selected_labels = common.select_hits(label_hits, max_n=select_label_n, min_score=min_label_score)
return {
"label_set": label_set.name,
"image": str(image),
"label_set_hash": data.get("label_set_hash", ""),
"model_id": data.get("model_id", ""),
"chosen_domains": "|".join(data.get("chosen_domains", [])),
"selected_domains": "|".join(selected_domains),
"selected_labels": "|".join(selected_labels),
"domain_hits": "|".join(common.fmt_hit(d) for d in domain_hits),
"label_hits": "|".join(common.fmt_hit(l) for l in label_hits),
"elapsed_ms": str(data.get("elapsed_ms", "")),
"elapsed_domain_ms": str(data.get("elapsed_domain_ms", "")),
"elapsed_labels_ms": str(data.get("elapsed_labels_ms", "")),
}
def summarize_by_label_set(rows: list[dict[str, str]]) -> list[dict[str, str]]:
summary: dict[str, list[int]] = {}
for row in rows:
label = row["label_set"]
try:
elapsed = int(row["elapsed_ms"])
except Exception:
continue
summary.setdefault(label, []).append(elapsed)
out_rows: list[dict[str, str]] = []
for label, times in summary.items():
avg = int(sum(times) / max(1, len(times)))
out_rows.append(
{
"label_set": label,
"count": str(len(times)),
"avg_elapsed_ms": str(avg),
"p50_elapsed_ms": str(common.percentile(times, 0.50)),
"p95_elapsed_ms": str(common.percentile(times, 0.95)),
}
)
return out_rows
def run(cfg: Config) -> None:
images = list(common.iter_images(cfg.images))
if not images:
raise SystemExit("No images found.")
if not cfg.label_sets:
raise SystemExit("No label sets found.")
rows: list[dict[str, str]] = []
with httpx.Client(base_url=cfg.api, timeout=30) as client:
for label_set in cfg.label_sets:
label_set_hash = common.upload_label_set(client, label_set)
for image in images:
data = common.classify_one(
client,
label_set_hash,
image_b64=common.encode_image_b64(image),
domain_top_n=cfg.domain_top_n,
top_k=cfg.top_k,
)
print(json.dumps({"label_set": label_set.name, "image": str(image), "result": data}))
rows.append(
to_row(
label_set,
image,
data,
select_domain_n=cfg.select_domain_n,
select_label_n=cfg.select_label_n,
min_domain_score=cfg.min_domain_score,
min_label_score=cfg.min_label_score,
)
)
fieldnames = [
"label_set",
"image",
"label_set_hash",
"model_id",
"chosen_domains",
"selected_domains",
"selected_labels",
"domain_hits",
"label_hits",
"elapsed_ms",
"elapsed_domain_ms",
"elapsed_labels_ms",
]
out_dir = common.resolve_out_dir(cfg.api, cfg.out_dir)
out_path = out_dir / f"eval_matrix_{common.timestamp()}.csv"
common.write_csv(out_path, rows, fieldnames)
if cfg.summary:
summary_rows = summarize_by_label_set(rows)
summary_path = out_dir / f"eval_matrix_summary_{common.timestamp()}.csv"
common.write_csv(summary_path, summary_rows, ["label_set", "count", "avg_elapsed_ms", "p50_elapsed_ms", "p95_elapsed_ms"])
@click.command()
@click.option("--api", default="http://localhost:7860", show_default=True)
@click.option("--label-sets", "label_sets_raw", multiple=True, required=True)
@click.option("--images", multiple=True, required=True, type=click.Path(path_type=Path))
@click.option("--domain-top-n", default=2, show_default=True, type=int)
@click.option("--top-k", default=5, show_default=True, type=int)
@click.option("--out-dir", type=click.Path(path_type=Path))
@click.option("--summary", is_flag=True, default=False)
@click.option("--select-domain-n", type=int, default=None)
@click.option("--select-label-n", type=int, default=None)
@click.option("--min-domain-score", type=float, default=None)
@click.option("--min-label-score", type=float, default=None)
def cli(
api: str,
label_sets_raw: tuple[str, ...],
images: tuple[Path, ...],
domain_top_n: int,
top_k: int,
out_dir: Path | None,
summary: bool,
select_domain_n: int | None,
select_label_n: int | None,
min_domain_score: float | None,
min_label_score: float | None,
) -> None:
label_sets = expand_label_sets(label_sets_raw)
cfg = Config(
api=api,
label_sets=label_sets,
images=list(images),
domain_top_n=domain_top_n,
top_k=top_k,
out_dir=out_dir,
summary=summary,
select_domain_n=select_domain_n,
select_label_n=select_label_n,
min_domain_score=min_domain_score,
min_label_score=min_label_score,
)
run(cfg)
if __name__ == "__main__":
cli()
|