SAM3_Cloud/test_case/run_coco_eval.py
2026-07-08 20:48:01 +08:00

216 lines
9.2 KiB
Python
Raw Permalink Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

#!/usr/bin/env python3
"""COCO val2017 三提示批测(容器内推理服务 sam3-server:cu128)。
底层逻辑:对每张图取**面积最大实例**作为代表目标,点/框/文本三种 prompt 都对准它,
各自调推理服务 → 在原图上绘制提示 + 掩码 → [原图 | 点 | 框 | 文本] 横向拼接产出。
- 点提示:bbox 中心点(正样本 label=1),蓝点绘制 + 掩码叠加。
- 框提示:COCO bbox(xywh),**绿色**框绘制 + 掩码叠加。
- 文本提示:COCO 类别英文名(开放词表),每个命中实例旁**写文本** + 绿框 + 掩码。
只读不改其他代码:渲染/IO 复用 demos/_common,HTTP/polygons 还原复用 prod_server/_client_common。
调本机映射端口须关 proxy(否则 127.0.0.1 流量被代理拦成 502),故用 no-proxy opener。
conda activate sam3
python3 test_case/run_coco_eval.py --limit 10 # 小批量验证
python3 test_case/run_coco_eval.py --limit 5000 # 全量
"""
import argparse
import json
import os
import sys
import time
import urllib.request
HERE = os.path.dirname(os.path.abspath(__file__))
CLOUD = os.path.dirname(HERE)
sys.path.insert(0, os.path.join(CLOUD, "demos")) # demos/_common
sys.path.insert(0, os.path.join(CLOUD, "prod_server")) # prod_server/_client_common
import numpy as np
from PIL import Image, ImageDraw, ImageFont
from _common import load_image, overlay_masks, draw_points, save_image
from _client_common import file_to_data_uri, polygons_to_mask
SERVER = os.environ.get("SAM3_SERVER", "https://artificer-50txfvd0te14fgjf4v-1dlnrs.inference.d-robotics.cc/AIStudio/v1/inference/api/svc-ygqzvo47qo")
GREEN = (0, 200, 0)
# no-proxy opener:host 若设 http_proxy/https_proxy,urlopen 会把 127.0.0.1 流量代理成 502。
_OPENER = urllib.request.build_opener(urllib.request.ProxyHandler({}))
def post(path, payload, timeout=180):
req = urllib.request.Request(
f"{SERVER}{path}", data=json.dumps(payload).encode(),
headers={"Content-Type": "application/json"})
t = time.time() # 请求发出 → 结果返回(含网络 + 推理)
with _OPENER.open(req, timeout=timeout) as r:
resp = json.loads(r.read().decode())
return resp, time.time() - t # (响应, 单次推理耗时 s)
def _font(size):
try:
return ImageFont.truetype("DejaVuSans.ttf", size)
except Exception:
return ImageFont.load_default()
def title_bar(img, title):
"""图顶加独立标题条(不覆盖原图内容)。"""
W, H = img.size
fs = max(16, H // 30)
f = _font(fs)
bar = Image.new("RGB", (W, fs + 12), (28, 28, 28))
d = ImageDraw.Draw(bar)
tw = d.textlength(title, font=f)
d.text(((W - tw) / 2, 4), title, fill=(255, 255, 255), font=f)
out = Image.new("RGB", (W, H + fs + 12), (255, 255, 255))
out.paste(bar, (0, 0))
out.paste(img, (0, fs + 12))
return out
def mask_from_polys(polys, W, H):
if not polys:
return None
return polygons_to_mask(polys, W, H)
# ---------------- 三种 prompt(均走 /api/v1/segment)----------------
def prompt_points(data_uri, cx, cy, img, W, H):
resp, dt = post("/api/v1/segment", {"image": data_uri, "model": "SAM-3",
"points": [{"point": [cx, cy], "label": 1}]})
polys = (resp.get("data") or [{}])[0].get("polygons") or []
m = mask_from_polys(polys, W, H)
out = overlay_masks(img, m[None]) if m is not None else img.copy()
out = draw_points(out, np.array([[cx, cy]], dtype=np.float32), np.array([1])) # 蓝点最上层
return out, m is not None, dt # 命中 = 服务返回了掩码(不能用 bool(numpy 数组))
def prompt_bbox(data_uri, x, y, bw, bh, img, W, H):
resp, dt = post("/api/v1/segment", {"image": data_uri, "model": "SAM-3",
"bboxes": [[x, y, bw, bh]]})
polys = (resp.get("data") or [{}])[0].get("polygons") or []
m = mask_from_polys(polys, W, H)
out = overlay_masks(img, m[None]) if m is not None else img.copy()
d = ImageDraw.Draw(out)
d.rectangle([x, y, x + bw, y + bh], outline=GREEN, width=max(2, W // 300)) # 绿框
return out, m is not None, dt
def prompt_text(data_uri, phrase, img, W, H, threshold=0.5):
resp, dt = post("/api/v1/segment", {"image": data_uri, "model": "SAM-3",
"texts": [phrase], "threshold": threshold})
data = resp.get("data") or []
masks, boxes = [], []
for it in data:
polys = it.get("polygons") or []
if not polys:
continue
masks.append(polygons_to_mask(polys, W, H))
bx, by, bbw, bbh = it["box"]
boxes.append((bx, by, bx + bbw, by + bbh))
out = overlay_masks(img, np.stack(masks)) if masks else img.copy()
d = ImageDraw.Draw(out)
fs = max(13, H // 42)
f = _font(fs)
for (bx0, by0, bx1, by1) in boxes: # 每个命中实例:绿框 + 写文本短语
d.rectangle([bx0, by0, bx1, by1], outline=GREEN, width=max(2, W // 400))
ty = max(0, by0 - fs - 2)
pad = int(fs * (len(phrase) * 0.55 + 1))
d.rectangle([bx0, ty, bx0 + pad, ty + fs + 2], fill=(0, 0, 180))
d.text((bx0 + 4, ty + 1), phrase, fill=(255, 255, 255), font=f)
return out, len(masks), dt
def hconcat(imgs, col_w):
"""等比缩放每列到 col_w 宽后横向拼接(标注在缩放前按原图坐标画好)。"""
scaled = []
for im in imgs:
w, h = im.size
scaled.append(im.resize((col_w, int(round(h * col_w / w))), Image.LANCZOS))
H = max(im.size[1] for im in scaled)
canvas = Image.new("RGB", (col_w * len(scaled), H), (255, 255, 255))
for i, im in enumerate(scaled):
canvas.paste(im, (i * col_w, 0))
return canvas
def main():
ap = argparse.ArgumentParser(description="COCO val2017 三提示批测(容器内推理服务)")
ap.add_argument("--limit", type=int, default=10, help="取前 N 张(默认 10 验证;全量 5000)")
ap.add_argument("--val-dir", default="/mnt/data/chao01.wu/Data/Datasets/COCO2017/val2017")
ap.add_argument("--ann", default=os.path.join(HERE, "data/annotations/instances_val2017.json"))
ap.add_argument("--out", default=os.path.join(HERE, "outputs"))
ap.add_argument("--col-w", type=int, default=480, help="每列缩放宽(px)")
ap.add_argument("--threshold", type=float, default=0.5, help="文本提示置信度阈值")
args = ap.parse_args()
os.makedirs(args.out, exist_ok=True)
from pycocotools.coco import COCO
coco = COCO(args.ann)
cat_name = {c["id"]: c["name"] for c in coco.loadCats(coco.getCatIds())}
img_ids = sorted(coco.getImgIds())[:args.limit]
ok = skip = fail = 0
hits = {"point": 0, "box": 0, "text": 0}
lat = {"point": 0.0, "box": 0.0, "text": 0.0} # 单次推理耗时累加(仅命中计入 ok 的图)
t0 = time.time()
for i, iid in enumerate(img_ids):
info = coco.loadImgs([iid])[0]
fname = info["file_name"]
img_path = os.path.join(args.val_dir, fname)
anns = [a for a in coco.loadAnns(coco.getAnnIds(imgIds=iid))
if a.get("area", 0) > 100 and not a.get("iscrowd", 0)]
if not anns:
print(f"[{i+1}/{len(img_ids)}] {fname} 无可用标注,跳过")
skip += 1
continue
ann = max(anns, key=lambda a: a["area"])
x, y, bw, bh = ann["bbox"]
cx, cy = x + bw / 2, y + bh / 2
phrase = cat_name[ann["category_id"]]
img = load_image(img_path)
W, H = img.size
data_uri = file_to_data_uri(img_path)
try:
p_res, p_hit, p_dt = prompt_points(data_uri, cx, cy, img, W, H)
b_res, b_hit, b_dt = prompt_bbox(data_uri, x, y, bw, bh, img, W, H)
t_res, t_n, t_dt = prompt_text(data_uri, phrase, img, W, H, args.threshold)
except Exception as e:
print(f"[{i+1}/{len(img_ids)}] {fname} 推理失败: {e}")
fail += 1
continue
hits["point"] += int(p_hit)
hits["box"] += int(b_hit)
hits["text"] += int(t_n > 0)
lat["point"] += p_dt
lat["box"] += b_dt
lat["text"] += t_dt
cols = [
title_bar(img, "Original"),
title_bar(p_res, f"Point ({'hit' if p_hit else 'miss'})"),
title_bar(b_res, f"Box-green ({'hit' if b_hit else 'miss'})"),
title_bar(t_res, f'Text: "{phrase}" ({t_n})'),
]
out_path = os.path.join(args.out, fname)
save_image(hconcat(cols, args.col_w), out_path)
ok += 1
print(f"[{i+1}/{len(img_ids)}] {fname} {phrase!r} "
f"box=({x:.0f},{y:.0f},{bw:.0f},{bh:.0f}) "
f"P={'Y' if p_hit else 'N'} B={'Y' if b_hit else 'N'} T={t_n} "
f"latency P={p_dt:.2f}s B={b_dt:.2f}s T={t_dt:.2f}s "
f"-> {out_path} ({time.time()-t0:.1f}s)")
print(f"\nDONE ok={ok} skip={skip} fail={fail} "
f"hits(point/box/text)={hits['point']}/{hits['box']}/{hits['text']} "
f"耗时={time.time()-t0:.1f}s out={args.out}")
if ok:
print(f"avg latency(point/box/text)="
f"{lat['point']/ok:.2f}s/{lat['box']/ok:.2f}s/{lat['text']/ok:.2f}s")
if __name__ == "__main__":
main()