File size: 8,692 Bytes
fc0c9ba d833eaf fc0c9ba d833eaf fc0c9ba | 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 | # SPDX-FileCopyrightText: (c) 2026 Tenstorrent AI ULC
# SPDX-License-Identifier: Apache-2.0
"""Generate media/source_{N}.png + media/target_{N}.png demo artifacts.
Four multi-person demos from the GazeFollow test set. Each pair follows the
canonical Gaze-LLE inference pipeline from the official Colab:
1. RetinaFace.detect_faces(image) → head bboxes
2. TtGazeLLE(image, bboxes) → per-person heatmap + in/out score
No hand-picked bboxes — every bbox in every demo comes from a face detector.
One TT-NN forward per image drives N predictions (backbone runs once,
decoder tail runs N times over the shared scene features).
Run from repo root with::
PYTHONPATH=$PWD:$TT_METAL_HOME:$TT_METAL_HOME/ttnn \
TT_VISIBLE_DEVICES=<n> \
python -m scripts.make_demo
Requires ``retina-face`` and ``tf-keras`` (``pip install retina-face tf-keras``).
"""
from __future__ import annotations
import io
import os
from pathlib import Path
import matplotlib
matplotlib.use("Agg")
import matplotlib.pyplot as plt
import numpy as np
import pandas as pd
import torch
import torchvision.transforms as T
from PIL import Image
# Four GazeFollow test rows pre-verified to have ≥2 RetinaFace face detections
# at ≥0.9 confidence each (office scene, mother + child, soccer, baseball plate).
_SAMPLE_INDICES = [800, 1000, 2400, 2600]
_MULTI_DETECTOR_MIN_CONFIDENCE = 0.9
_REPO_ROOT = Path(__file__).resolve().parent.parent
_PARQUET = Path(os.environ.get("TT_GAZE_LLE_DATA", _REPO_ROOT / "data")) / "gazefollow" / "test.parquet"
_MEDIA = _REPO_ROOT / "media"
def visualize_multi(img_pil: Image.Image, bboxes, heatmaps_64: torch.Tensor, inout_scores, out_path: Path) -> None:
"""Draw all N head bboxes + N yellow gaze arrows + per-head peak on one image.
heatmaps_64: (N, 64, 64) torch tensor after sigmoid.
"""
W, H = img_pil.size
N = heatmaps_64.shape[0]
# Accumulate the max over N heatmaps for a single overlay (keeps the image
# readable when you have multiple heads).
combined = heatmaps_64.max(dim=0).values.float().cpu().numpy()
combined_pil = Image.fromarray((combined * 255).astype(np.uint8)).resize((W, H), Image.BILINEAR)
combined_arr = np.asarray(combined_pil) / 255.0
colors = ["lime", "cyan", "magenta", "orange", "yellow"]
fig, ax = plt.subplots(1, 1, figsize=(10, 10 * H / W))
ax.imshow(img_pil)
ax.imshow(combined_arr, cmap="jet", alpha=0.40)
for i in range(N):
hm_pil = Image.fromarray((heatmaps_64[i].float().cpu().numpy() * 255).astype(np.uint8)).resize((W, H), Image.BILINEAR)
hm_arr = np.asarray(hm_pil) / 255.0
py, px = np.unravel_index(hm_arr.argmax(), hm_arr.shape)
bcol = colors[i % len(colors)]
xmin, ymin, xmax, ymax = bboxes[i]
ax.add_patch(plt.Rectangle(
(xmin * W, ymin * H), (xmax - xmin) * W, (ymax - ymin) * H,
fill=False, edgecolor=bcol, linewidth=3,
))
ax.scatter([px], [py], c=bcol, s=220, marker="x", linewidths=4)
cx = (xmin + xmax) / 2 * W
cy = (ymin + ymax) / 2 * H
ax.annotate(
"", xy=(px, py), xytext=(cx, cy),
arrowprops=dict(arrowstyle="->", color=bcol, lw=4,
shrinkA=8, shrinkB=8, mutation_scale=22),
)
title_bits = [f"head{i}: inout={float(inout_scores[i]):.2f}" for i in range(N)]
ax.set_title(" | ".join(title_bits), fontsize=12)
ax.axis("off")
fig.savefig(out_path, bbox_inches="tight", dpi=110)
plt.close(fig)
def visualize(img_pil: Image.Image, bbox, heatmap_64: torch.Tensor, inout_score: float, out_path: Path) -> None:
W, H = img_pil.size
hm = heatmap_64.float().cpu().numpy()
hm_pil = Image.fromarray((hm * 255).astype(np.uint8)).resize((W, H), Image.BILINEAR)
hm_arr = np.asarray(hm_pil) / 255.0
fig, ax = plt.subplots(1, 1, figsize=(10, 10 * H / W))
ax.imshow(img_pil)
ax.imshow(hm_arr, cmap="jet", alpha=0.45)
xmin, ymin, xmax, ymax = bbox
ax.add_patch(plt.Rectangle(
(xmin * W, ymin * H), (xmax - xmin) * W, (ymax - ymin) * H,
fill=False, edgecolor="lime", linewidth=3,
))
py, px = np.unravel_index(hm_arr.argmax(), hm_arr.shape)
ax.scatter([px], [py], c="red", s=220, marker="x", linewidths=4)
cx = (xmin + xmax) / 2 * W
cy = (ymin + ymax) / 2 * H
ax.annotate(
"", xy=(px, py), xytext=(cx, cy),
arrowprops=dict(arrowstyle="->", color="yellow", lw=4,
shrinkA=8, shrinkB=8, mutation_scale=22),
)
ax.set_title(f"gaze target (red ×), gaze direction (yellow arrow), inout={inout_score:.3f}", fontsize=13)
ax.axis("off")
fig.savefig(out_path, bbox_inches="tight", dpi=110)
plt.close(fig)
def _retinaface_detect(pil_image: Image.Image, min_confidence: float) -> list:
"""Return a list of normalized (xmin, ymin, xmax, ymax) bboxes using RetinaFace.
This mirrors the "detect faces" cell in fkryan/gazelle's Colab demo:
from retinaface import RetinaFace
resp = RetinaFace.detect_faces(np.array(image))
bboxes = [resp[k]['facial_area'] for k in resp.keys()]
Returned boxes are sorted left-to-right for stable ordering.
"""
from retinaface import RetinaFace # imported lazily — optional dep
W, H = pil_image.size
resp = RetinaFace.detect_faces(np.array(pil_image))
if not isinstance(resp, dict):
return []
out = []
for key, meta in resp.items():
score = float(meta.get("score", 0.0))
if score < min_confidence:
continue
x0, y0, x1, y1 = [int(v) for v in meta["facial_area"]]
out.append((x0 / W, y0 / H, x1 / W, y1 / H))
out.sort(key=lambda b: b[0])
return out
def _load_samples_from_parquet(path: Path, indices):
df = pd.read_parquet(path)
out = []
for idx in indices:
row = df.iloc[idx]
pil = Image.open(io.BytesIO(row["image"]["bytes"])).convert("RGB")
hb = row["gazes"][0]["head_bbox"]
bbox = (float(hb["xmin"]), float(hb["ymin"]), float(hb["xmax"]), float(hb["ymax"]))
out.append((pil, bbox))
return out
def main() -> None:
import ttnn
from gaze_lle.reference.load_pretrained import load_pretrained
from gaze_lle.reference.torch_gaze_lle import build_gaze_lle
from gaze_lle.tt.tt_gaze_lle import TtGazeLLE
torch.manual_seed(0)
torch.set_grad_enabled(False)
ref = build_gaze_lle("vitb14", inout=True).eval()
load_pretrained(ref, verbose=False)
_MEDIA.mkdir(parents=True, exist_ok=True)
samples = _load_samples_from_parquet(_PARQUET, _SAMPLE_INDICES)
device_id = int(os.environ.get("GAZE_LLE_DEVICE", "0"))
from gaze_lle.api import open_device as _gl_open_device # ETH dispatch + 1 CQ + 12x10 by default
d = _gl_open_device(device_id, dispatch=os.environ.get("GAZE_LLE_DISPATCH", "auto"))
try:
tt_model = TtGazeLLE(ref, d, inout=True)
tf = T.Compose([
T.Resize((448, 448)),
T.ToTensor(),
T.Normalize(mean=[0.485, 0.456, 0.406], std=[0.229, 0.224, 0.225]),
])
# Need at least one forward to populate the program cache before timing
# / correctness matters; the first sample's detection also warms
# RetinaFace's weight load.
warm_pil = samples[0][0]
warm_bboxes = _retinaface_detect(warm_pil, min_confidence=_MULTI_DETECTOR_MIN_CONFIDENCE)
if warm_bboxes:
_ = tt_model(tf(warm_pil).unsqueeze(0), warm_bboxes)
for i, (img_pil, _gt_bbox) in enumerate(samples, start=1):
src_path = _MEDIA / f"source_{i}.png"
img_pil.save(src_path)
bboxes = _retinaface_detect(img_pil, min_confidence=_MULTI_DETECTOR_MIN_CONFIDENCE)
if not bboxes:
print(f" {src_path.name} ({img_pil.size[0]}x{img_pil.size[1]}) RetinaFace found 0 faces — skipping")
continue
out = tt_model(tf(img_pil).unsqueeze(0), bboxes)
heatmaps = out["heatmap"] # (N, 64, 64)
inouts = out["inout"] # (N,)
tgt_path = _MEDIA / f"target_{i}.png"
visualize_multi(img_pil, bboxes, heatmaps, inouts, tgt_path)
inout_str = "[" + ", ".join(f"{float(v):.2f}" for v in inouts) + "]"
print(
f" {src_path.name} ({img_pil.size[0]}x{img_pil.size[1]}) "
f"N={len(bboxes)} faces inouts={inout_str} → {tgt_path.name}"
)
finally:
ttnn.close_device(d)
if __name__ == "__main__":
main()
|