CJR61 Claude Sonnet 4.6 commited on
Commit
f8bc55b
·
1 Parent(s): b03f9b1

Add k-means multi-center reference for similarity gate

Browse files

- model.py: predict_image accepts 2-D ref_emb (cluster centers);
sim = max cosine similarity to any center; thres default 0.55
- main.py: load cluster_centers.npy if present, else avg12_embedding.npy
- scripts/compute_cluster_centers.py: extract embeddings from all
training images, k-means k=5, save cluster_centers.npy

Run script once to generate cluster_centers.npy, then redeploy.

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>

Files changed (3) hide show
  1. app/main.py +9 -4
  2. app/model.py +14 -9
  3. scripts/compute_cluster_centers.py +118 -0
app/main.py CHANGED
@@ -36,10 +36,15 @@ async def lifespan(app: FastAPI):
36
  app.state.model = model # Store the model in `app.state` for later access
37
  logger.info("Model loaded successfully!")
38
 
39
- # 讀 ref emb(若是多向量則取平均)
40
- ref_emb = np.load(REF_EMB_PATH).astype(np.float32)
41
- if ref_emb.ndim > 1:
42
- ref_emb = ref_emb.mean(axis=0).astype(np.float32)
 
 
 
 
 
43
  app.state.ref_emb = ref_emb
44
  app.state.sim_thres = SIM_THRES
45
 
 
36
  app.state.model = model # Store the model in `app.state` for later access
37
  logger.info("Model loaded successfully!")
38
 
39
+ # 優先載入 cluster_centers.npy(k-means 多中心點)
40
+ # fallback: avg12_embedding.npy(舊版單中心點)
41
+ cluster_path = os.path.join(resource_dir, "cluster_centers.npy")
42
+ ref_emb = np.load(cluster_path if os.path.exists(cluster_path) else REF_EMB_PATH).astype(np.float32)
43
+ # 舊版 avg12 shape=(1,1536) → squeeze 成 1-D;cluster_centers shape=(k,1536) 保持 2-D
44
+ if ref_emb.ndim == 2 and ref_emb.shape[0] == 1:
45
+ ref_emb = ref_emb.squeeze(0)
46
+ mode = "cluster_centers" if (ref_emb.ndim == 2) else "avg_single"
47
+ logger.info(f"Reference embedding loaded: {mode}, shape={ref_emb.shape}")
48
  app.state.ref_emb = ref_emb
49
  app.state.sim_thres = SIM_THRES
50
 
app/model.py CHANGED
@@ -10,10 +10,14 @@ def cosine_similarity(a, b):
10
  b = b / (norm(b) + 1e-8)
11
  return float(np.dot(a, b))
12
 
13
- def predict_image(model, image, ref_emb, thres=0.75):
14
- # 取輸入/輸出名稱(用名稱避免順序出錯)
 
 
 
 
15
  inp = model.get_inputs()[0].name
16
- out_map = {o.name: i for i, o in enumerate(model.get_outputs())} # e.g. {'embedding':0,'probability':1}
17
 
18
  outs = model.run(None, {inp: image})
19
 
@@ -21,15 +25,16 @@ def predict_image(model, image, ref_emb, thres=0.75):
21
  if missing:
22
  raise KeyError(f"Model output missing expected keys: {missing}. Found: {list(out_map.keys())}")
23
 
24
- emb = outs[out_map["embedding"]].squeeze()
25
  prob = float(outs[out_map["probability"]].squeeze())
26
 
27
- # 相似度 + 門檻判斷
28
- sim = cosine_similarity(emb, ref_emb)
29
- is_true = sim >= thres
 
 
30
 
31
- # 依你的需求:回傳 (bool, prob);同時也把 sim 一起回傳方便外面印
32
- return is_true, prob, sim
33
 
34
  if __name__ == "__main__":
35
  from utils import preprocess_image
 
10
  b = b / (norm(b) + 1e-8)
11
  return float(np.dot(a, b))
12
 
13
+ def predict_image(model, image, ref_emb, thres=0.55):
14
+ """
15
+ ref_emb: 1-D (single reference) or 2-D (k cluster centers, shape [k, dim]).
16
+ When 2-D, sim = max cosine similarity to any cluster center.
17
+ Returns (is_in_criteria: bool, prob: float, sim: float).
18
+ """
19
  inp = model.get_inputs()[0].name
20
+ out_map = {o.name: i for i, o in enumerate(model.get_outputs())}
21
 
22
  outs = model.run(None, {inp: image})
23
 
 
25
  if missing:
26
  raise KeyError(f"Model output missing expected keys: {missing}. Found: {list(out_map.keys())}")
27
 
28
+ emb = outs[out_map["embedding"]].squeeze().astype(np.float32)
29
  prob = float(outs[out_map["probability"]].squeeze())
30
 
31
+ # Multi-center: take max similarity to any cluster center
32
+ if ref_emb.ndim == 2:
33
+ sim = float(max(cosine_similarity(emb, c) for c in ref_emb))
34
+ else:
35
+ sim = cosine_similarity(emb, ref_emb)
36
 
37
+ return sim >= thres, prob, sim
 
38
 
39
  if __name__ == "__main__":
40
  from utils import preprocess_image
scripts/compute_cluster_centers.py ADDED
@@ -0,0 +1,118 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ """
2
+ compute_cluster_centers.py
3
+ --------------------------
4
+ Extract embeddings from all training images, run k-means (k=5),
5
+ save cluster centers to resource/cluster_centers.npy.
6
+
7
+ Usage (from repo root):
8
+ python scripts/compute_cluster_centers.py
9
+
10
+ Requirements: onnxruntime, opencv-python, numpy, scikit-learn
11
+ """
12
+
13
+ import os
14
+ import sys
15
+ import numpy as np
16
+ import cv2
17
+ from pathlib import Path
18
+
19
+ # ── Config ────────────────────────────────────────────────────────────────────
20
+ REPO_ROOT = Path(__file__).parent.parent
21
+ DATA_DIR = Path("/mnt/d/pig/data") # training images root
22
+ MODEL_PATH = REPO_ROOT / "resource" / "model_2c_v1_emb.onnx"
23
+ OUT_PATH = REPO_ROOT / "resource" / "cluster_centers.npy"
24
+ K = 5
25
+ IMG_EXTS = {".jpg", ".jpeg", ".png", ".bmp", ".tiff", ".tif"}
26
+ # ──────────────────────────────────────────────────────────────────────────────
27
+
28
+
29
+ def preprocess(image_path: str) -> np.ndarray:
30
+ img = cv2.imread(image_path)
31
+ if img is None:
32
+ return None
33
+ img = cv2.cvtColor(img, cv2.COLOR_BGR2RGB)
34
+ img = cv2.resize(img, (600, 600))
35
+ img = img / 255.0
36
+ mean = np.array([0.485, 0.456, 0.406])
37
+ std = np.array([0.229, 0.224, 0.225])
38
+ img = (img - mean) / std
39
+ img = np.transpose(img, (2, 0, 1))
40
+ return np.expand_dims(img, 0).astype(np.float32)
41
+
42
+
43
+ def extract_embedding(sess, image_path: str) -> np.ndarray | None:
44
+ x = preprocess(image_path)
45
+ if x is None:
46
+ return None
47
+ inp_name = sess.get_inputs()[0].name
48
+ out_map = {o.name: i for i, o in enumerate(sess.get_outputs())}
49
+ if "embedding" not in out_map:
50
+ raise KeyError(f"Model has no 'embedding' output. Outputs: {list(out_map)}")
51
+ outs = sess.run(None, {inp_name: x})
52
+ return outs[out_map["embedding"]].squeeze().astype(np.float32)
53
+
54
+
55
+ def collect_images(root: Path) -> list[str]:
56
+ paths = []
57
+ for p in root.rglob("*"):
58
+ if p.suffix.lower() in IMG_EXTS:
59
+ paths.append(str(p))
60
+ return sorted(paths)
61
+
62
+
63
+ def main():
64
+ import onnxruntime as ort
65
+ from sklearn.cluster import KMeans
66
+
67
+ print(f"Model : {MODEL_PATH}")
68
+ print(f"Data : {DATA_DIR}")
69
+ print(f"Output : {OUT_PATH}")
70
+ print(f"k : {K}")
71
+ print()
72
+
73
+ sess = ort.InferenceSession(str(MODEL_PATH),
74
+ providers=["CUDAExecutionProvider", "CPUExecutionProvider"])
75
+
76
+ images = collect_images(DATA_DIR)
77
+ print(f"Found {len(images)} images — extracting embeddings...")
78
+
79
+ embeddings, failed = [], []
80
+ for i, path in enumerate(images, 1):
81
+ emb = extract_embedding(sess, path)
82
+ if emb is not None:
83
+ embeddings.append(emb)
84
+ else:
85
+ failed.append(path)
86
+ if i % 100 == 0 or i == len(images):
87
+ print(f" {i}/{len(images)} ok={len(embeddings)} fail={len(failed)}", end="\r")
88
+
89
+ print(f"\nExtracted {len(embeddings)} embeddings ({len(failed)} failed)")
90
+ if failed:
91
+ print("Failed files:")
92
+ for f in failed[:5]:
93
+ print(f" {f}")
94
+
95
+ X = np.stack(embeddings) # (N, 1536)
96
+ print(f"\nRunning k-means (k={K}) on {X.shape} ...")
97
+ km = KMeans(n_clusters=K, random_state=42, n_init=10)
98
+ km.fit(X)
99
+
100
+ centers = km.cluster_centers_.astype(np.float32) # (K, 1536)
101
+ # L2-normalise so cosine similarity == dot product
102
+ norms = np.linalg.norm(centers, axis=1, keepdims=True)
103
+ centers = centers / (norms + 1e-8)
104
+
105
+ np.save(str(OUT_PATH), centers)
106
+ print(f"Saved {centers.shape} cluster centers → {OUT_PATH}")
107
+
108
+ # Quick sanity: show avg max-sim of training images to centers
109
+ sims = X / (np.linalg.norm(X, axis=1, keepdims=True) + 1e-8)
110
+ max_sims = (sims @ centers.T).max(axis=1)
111
+ print(f"\nSanity check (training set max-sim to nearest center):")
112
+ print(f" mean={max_sims.mean():.3f} min={max_sims.min():.3f} "
113
+ f"p5={np.percentile(max_sims, 5):.3f} p10={np.percentile(max_sims, 10):.3f}")
114
+ print("Done.")
115
+
116
+
117
+ if __name__ == "__main__":
118
+ main()