File size: 9,035 Bytes
c94c8c9 | 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 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 | import os
import glob
import json
import argparse
from typing import Dict, List, Tuple
import torch
import torch.nn as nn
import torch.nn.functional as F
from safetensors.torch import load_file
from transformers import AutoImageProcessor, AutoModelForCausalLM, AutoTokenizer
from huggingface_hub import hf_hub_download
from peft import LoraConfig, get_peft_model
# -----------------------------
# Utils
# -----------------------------
def load_json(path: str) -> dict:
with open(path, "r") as f:
return json.load(f)
def find_scan_safetensor(scan_root: str, scan_id: str) -> str:
direct = os.path.join(scan_root, f"{scan_id}.safetensors")
if os.path.exists(direct):
return direct
pattern = os.path.join(scan_root, "**", f"{scan_id}.safetensors")
matches = glob.glob(pattern, recursive=True)
if not matches:
raise FileNotFoundError(f"Cannot find safetensor for scan_id={scan_id} under {scan_root}")
matches = sorted(matches, key=len)
return matches[0]
def to_vchw(point_map: torch.Tensor) -> torch.Tensor:
"""
Convert point_map to (V, 3, H, W) float tensor.
Accepts:
(V, 3, H, W)
(V, H, W, 3)
"""
if point_map.dim() != 4:
raise ValueError(f"Expected 4D point_map, got shape={tuple(point_map.shape)}")
V, a, b, c = point_map.shape
if a == 3:
out = point_map
elif c == 3:
out = point_map.permute(0, 3, 1, 2).contiguous()
else:
raise ValueError(f"Unrecognized point_map layout: shape={tuple(point_map.shape)}")
return out.float()
def load_safetensor_from_hf(repo_id, filename, repo_type="dataset"):
cached_path = hf_hub_download(
repo_id=repo_id,
filename=filename,
repo_type=repo_type,
local_files_only=False
)
return load_file(cached_path)
def load_pretrain(model, pretrain_ckpt_path: str):
print(f"📂 Loading pretrained weights from: {str(pretrain_ckpt_path)}")
model_weight_path_pattern = os.path.join(pretrain_ckpt_path, "model*.safetensors")
model_weight_paths = glob.glob(model_weight_path_pattern)
if len(model_weight_paths) == 0:
raise FileNotFoundError(f"❌ Cannot find any model*.safetensors in {str(pretrain_ckpt_path)}")
weights = {}
for model_weight_path in model_weight_paths:
print(f"📥 Loading weights from: {model_weight_path}")
weights.update(load_file(model_weight_path, device="cpu"))
result = model.load_state_dict(weights, strict=False)
model_keys = set(model.state_dict().keys())
loaded_keys = model_keys.intersection(weights.keys())
print(f"✅ Loaded keys: {len(loaded_keys)} / {len(model_keys)}")
print(f"❌ Missing keys: {len(result.missing_keys)}")
print(f"⚠️ Unexpected keys: {len(result.unexpected_keys)}")
# -----------------------------
# Model wrapper
# -----------------------------
class RepModel(nn.Module):
def __init__(self, model_root: str = "fg-clip-base"):
super().__init__()
self.pm_encoder = AutoModelForCausalLM.from_pretrained(f'../{model_root}', trust_remote_code=True)
self.tokenizer = AutoTokenizer.from_pretrained(f'../{model_root}', trust_remote_code=True, use_fast=True)
self.image_processor = AutoImageProcessor.from_pretrained(f'../{model_root}')
# Optional: print trainable params
try:
self.pm_encoder.print_trainable_parameters()
except Exception:
pass
def encode_views_batched(self, pm_vchw: torch.Tensor, batch_views: int = 32) -> torch.Tensor:
"""
pm_vchw: (V,3,H,W) on device
returns: (V,D) normalized
"""
feats_all = []
V = pm_vchw.shape[0]
for s in range(0, V, batch_views):
chunk = pm_vchw[s : s + batch_views] # (b,3,H,W)
_, feats = self.pm_encoder.get_image_features(chunk)
feats = F.normalize(feats.float(), dim=-1)
feats_all.append(feats)
return torch.cat(feats_all, dim=0)
@torch.no_grad()
def encode_text(self, texts: List[str]) -> torch.Tensor:
"""
texts: list[str]
returns: (B,D) normalized
"""
tok = self.tokenizer(
texts,
padding="max_length",
truncation=True,
max_length=248,
return_tensors="pt",
).to(next(self.parameters()).device)
feats = self.pm_encoder.get_text_features(tok["input_ids"], walk_short_pos=False)
feats = F.normalize(feats.float(), dim=-1)
return feats
# -----------------------------
# Scene retrieval
# -----------------------------
def build_queries_from_caption_json(caption_json: dict) -> List[dict]:
"""
Convert:
{ scene_id: { "captions": [c1,c2,...] }, ... }
into:
[ { "scene_id": scene_id, "caption": c }, ... ]
"""
queries = []
for scene_id, payload in caption_json.items():
caps = payload.get("captions", [])
for c in caps:
c = (c or "").strip()
if c:
queries.append({"scene_id": scene_id, "caption": c})
return queries
@torch.no_grad()
def eval_scene_retrieval(
model: RepModel,
caption_json: dict,
scan_root: str,
device: str = "cuda",
batch_views: int = 32,
recall_ks: Tuple[int, ...] = (1, 5, 10),
) -> Dict[str, float]:
"""
For each caption, retrieve the correct scene among all scenes in caption_json.
Scene embedding = mean pooling over view embeddings.
"""
model.eval().to(device)
scene_ids = sorted(list(caption_json.keys()))
if len(scene_ids) == 0:
return {"n": 0}
# Cache: scene_id -> pooled scene feature (D,) on CPU
scene_feat_cache: Dict[str, torch.Tensor] = {}
# Precompute all scene pooled features once (so retrieval is fast)
for sid in scene_ids:
filename = f'light_scannet/{sid}.safetensors'
data = load_safetensor_from_hf('MatchLab/ScenePoint', filename, repo_type="dataset")
pm = to_vchw(data["point_map"]) # (V,3,H,W) on CPU
pm = pm.to(device, non_blocking=True)
view_feats = model.encode_views_batched(pm, batch_views=batch_views) # (V,D) on GPU
scene_feat = view_feats.mean(dim=0) # (D,)
scene_feat = F.normalize(scene_feat, dim=-1)
scene_feat_cache[sid] = scene_feat.detach().cpu()
# Stack gallery: (N,D)
gallery = torch.stack([scene_feat_cache[sid] for sid in scene_ids], dim=0) # CPU
gallery = gallery.to(device)
# Build queries
queries = build_queries_from_caption_json(caption_json)
total = 0
top1_correct = 0
recall_correct = {k: 0 for k in recall_ks}
for q in queries:
gt_sid = q["scene_id"]
caption = q["caption"]
if gt_sid not in scene_feat_cache:
continue
text_feat = model.encode_text([caption])[0] # (D,) on GPU
# similarity over all scenes: (N,)
sims = gallery @ text_feat.unsqueeze(-1) # (N,1)
sims = sims.squeeze(-1)
ranked = torch.argsort(sims, descending=True) # indices into scene_ids
pred_sid = scene_ids[int(ranked[0].item())]
total += 1
if pred_sid == gt_sid:
top1_correct += 1
for k in recall_ks:
k_eff = min(k, len(scene_ids))
topk_idx = ranked[:k_eff].tolist()
topk_sids = [scene_ids[i] for i in topk_idx]
if gt_sid in topk_sids:
recall_correct[k] += 1
# optional debug print
print(f"[Q] GT={gt_sid} | Pred={pred_sid} | caption={caption[:80]}...")
if total == 0:
return {"n": 0}
out = {"n": total, "top1_acc": top1_correct / total}
for k in recall_ks:
out[f"recall@{k}"] = recall_correct[k] / total
return out
def main():
ap = argparse.ArgumentParser()
ap.add_argument("--caption_json", type=str, required=True, help="JSON mapping scene_id -> {captions:[...]}")
ap.add_argument("--scan_root", type=str, required=True, help="Root dir containing scene safetensors")
ap.add_argument("--ckpt", type=str, default="", help="Optional: dir with model*.safetensors")
ap.add_argument("--model_root", type=str, default="fg-clip-base")
ap.add_argument("--device", type=str, default="cuda")
ap.add_argument("--batch_views", type=int, default=32)
args = ap.parse_args()
caption_json = load_json(args.caption_json)
model = RepModel(model_root=args.model_root)
if args.ckpt:
load_pretrain(model, args.ckpt)
metrics = eval_scene_retrieval(
model=model,
caption_json=caption_json,
scan_root=args.scan_root,
device=args.device,
batch_views=args.batch_views,
recall_ks=(1, 5, 10),
)
print("\n=== Scene Retrieval Results ===")
for k, v in metrics.items():
if isinstance(v, float):
print(f"{k:>10}: {v:.4f}")
else:
print(f"{k:>10}: {v}")
if __name__ == "__main__":
main()
|