CJR61 Claude Sonnet 4.6 commited on
Commit
a5ec00a
ยท
1 Parent(s): 93f8c6b

Replace B3 ONNX with EfficientNet-B0 (3-fold CV, orig fold_3)

Browse files

- New model: model_b0_balanced_v2_emb.onnx (B0, 224x224, dual output)
- AUC 0.945, Sensitivity 0.888, Specificity 0.731 on case-level CV
- Inverted sigmoid output to match existing label convention (prob>0.5=bacteria)
- Preprocessing: 600x600 โ†’ Resize(256)+CenterCrop(224) to match training
- cluster_centers.npy: recomputed with B0 embeddings (1280-D, k=5)
- HIGH_CONF bypass: added MIN_SIM_BYPASS=0.24 to prevent non-pig images from bypassing similarity gate
- Local test: 6/7 pass (nr4 pig-farm aerial photo is edge case)

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

app/main.py CHANGED
@@ -27,7 +27,7 @@ async def lifespan(app: FastAPI):
27
 
28
  # MODEL_PATH = os.path.join(parent_folder, "resource", "model_2c_v1.onnx")
29
  # ๆ”น๏ผšไฝฟ็”จ emb ็‰ˆ ONNX๏ผŒๆ‰่ƒฝๅŒๆ™‚ๆ‹ฟๅˆฐ embedding + prob
30
- MODEL_PATH = os.path.join(resource_dir, "model_2c_v1_emb.onnx")
31
  REF_EMB_PATH = os.path.join(resource_dir, "avg12_embedding.npy")
32
 
33
  # Load model
@@ -102,12 +102,13 @@ async def predict(file: UploadFile = File(...)):
102
  # ๆ‰€ๅฑฌ้กžๅˆฅๆฉŸ็އ๏ผˆbacteria prob ็›ดๆŽฅ็”จ๏ผ›virus prob = 1 - bacteria prob๏ผ‰
103
  class_prob = probability if probability > 0.5 else round(1 - probability, 2)
104
 
105
- # ้ซ˜ไฟกๅฟƒ bypass๏ผšclass_prob >= HIGH_CONF ๆ™‚็›ดๆŽฅๅˆ†้กž๏ผŒ่ทณ้Ž similarity gate
106
  # ่งฃๆฑบ้žๅ…ธๅž‹ๆ‹ๆ”่ง’ๅบฆ๏ผˆๅฆ‚่‡Ÿๅ™จ้ป‘ๅบ•ๆจ™ๆœฌ๏ผ‰่ขซ sim gate ่ชคๆ“‹็š„ๅ•้กŒ
107
- # ๅ…ถ้ค˜ๆƒ…ๆณ็ถญๆŒๅŽŸๆœฌ similarity gate ๅˆคๆ–ท
108
  HIGH_CONF = 0.85
 
109
 
110
- if class_prob >= HIGH_CONF:
111
  is_in_criteria = True # bypass sim gate
112
 
113
  if not is_in_criteria:
 
27
 
28
  # MODEL_PATH = os.path.join(parent_folder, "resource", "model_2c_v1.onnx")
29
  # ๆ”น๏ผšไฝฟ็”จ emb ็‰ˆ ONNX๏ผŒๆ‰่ƒฝๅŒๆ™‚ๆ‹ฟๅˆฐ embedding + prob
30
+ MODEL_PATH = os.path.join(resource_dir, "model_b0_balanced_v2_emb.onnx")
31
  REF_EMB_PATH = os.path.join(resource_dir, "avg12_embedding.npy")
32
 
33
  # Load model
 
102
  # ๆ‰€ๅฑฌ้กžๅˆฅๆฉŸ็އ๏ผˆbacteria prob ็›ดๆŽฅ็”จ๏ผ›virus prob = 1 - bacteria prob๏ผ‰
103
  class_prob = probability if probability > 0.5 else round(1 - probability, 2)
104
 
105
+ # ้ซ˜ไฟกๅฟƒ bypass๏ผšclass_prob >= HIGH_CONF ไธ” sim >= MIN_SIM ๆ™‚็›ดๆŽฅๅˆ†้กž
106
  # ่งฃๆฑบ้žๅ…ธๅž‹ๆ‹ๆ”่ง’ๅบฆ๏ผˆๅฆ‚่‡Ÿๅ™จ้ป‘ๅบ•ๆจ™ๆœฌ๏ผ‰่ขซ sim gate ่ชคๆ“‹็š„ๅ•้กŒ
107
+ # MIN_SIM ้˜ฒๆญขๅฎŒๅ…จ็„ก้—œๅœ–็‰‡้€š้Ž bypass๏ผˆB0 ๆจกๅž‹ๅฐ้ž่ฑฌๅœ–็‰‡ไนŸๆฅต็ซฏ่‡ชไฟก๏ผ‰
108
  HIGH_CONF = 0.85
109
+ MIN_SIM_BYPASS = 0.24
110
 
111
+ if class_prob >= HIGH_CONF and sim >= MIN_SIM_BYPASS:
112
  is_in_criteria = True # bypass sim gate
113
 
114
  if not is_in_criteria:
app/utils.py CHANGED
@@ -35,17 +35,24 @@ def is_valid_image(file_path):
35
  return False
36
 
37
  def preprocess_image(image_path):
38
- # Read image
39
  image = cv2.imread(image_path)
40
  image = cv2.cvtColor(image, cv2.COLOR_BGR2RGB)
41
- image = cv2.resize(image, (600, 600))
 
 
 
 
 
 
 
 
42
  image = image / 255.0
43
  mean = np.array([0.485, 0.456, 0.406])
44
  std = np.array([0.229, 0.224, 0.225])
45
  image = (image - mean) / std
46
  image = np.transpose(image, (2, 0, 1)) # HWC -> CHW
47
- input_tensor = np.expand_dims(image, axis=0).astype(np.float32) # Add batch dimension
48
- # print(f"Preprocessed output shape: {input_tensor.shape}") # Print shape
49
  return input_tensor
50
 
51
  def append_to_csv(csv_path, image_id, prediction, probability, similarity):
 
35
  return False
36
 
37
  def preprocess_image(image_path):
38
+ # Read image โ€” Resize(256) + CenterCrop(224) to match training val_transform
39
  image = cv2.imread(image_path)
40
  image = cv2.cvtColor(image, cv2.COLOR_BGR2RGB)
41
+ h, w = image.shape[:2]
42
+ if h < w:
43
+ new_h, new_w = 256, int(w * 256 / h)
44
+ else:
45
+ new_h, new_w = int(h * 256 / w), 256
46
+ image = cv2.resize(image, (new_w, new_h))
47
+ y0 = (new_h - 224) // 2
48
+ x0 = (new_w - 224) // 2
49
+ image = image[y0:y0+224, x0:x0+224]
50
  image = image / 255.0
51
  mean = np.array([0.485, 0.456, 0.406])
52
  std = np.array([0.229, 0.224, 0.225])
53
  image = (image - mean) / std
54
  image = np.transpose(image, (2, 0, 1)) # HWC -> CHW
55
+ input_tensor = np.expand_dims(image, axis=0).astype(np.float32)
 
56
  return input_tensor
57
 
58
  def append_to_csv(csv_path, image_id, prediction, probability, similarity):
resource/cluster_centers.npy CHANGED
@@ -1,3 +1,3 @@
1
  version https://git-lfs.github.com/spec/v1
2
- oid sha256:d3bb967318a7d99accf7c441b6f1fa9aceb3e01b78da538fac6e338b04b064ce
3
- size 30848
 
1
  version https://git-lfs.github.com/spec/v1
2
+ oid sha256:85546a734a36eb3fb3926ed9c6a4020bd962e67cb70657bff40e77848d9fc1ff
3
+ size 25728
resource/model_b0_balanced_v2_emb.onnx ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ version https://git-lfs.github.com/spec/v1
2
+ oid sha256:1778370bf4c41504e3c6f10b512bfe091e113c03ff14d89bf0e5d27b8c3110af
3
+ size 16011419
scripts/compute_cluster_centers.py CHANGED
@@ -19,7 +19,7 @@ from pathlib import Path
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"}
@@ -31,7 +31,16 @@ def preprocess(image_path: str) -> np.ndarray:
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])
 
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_b0_balanced_v2_emb.onnx"
23
  OUT_PATH = REPO_ROOT / "resource" / "cluster_centers.npy"
24
  K = 5
25
  IMG_EXTS = {".jpg", ".jpeg", ".png", ".bmp", ".tiff", ".tif"}
 
31
  if img is None:
32
  return None
33
  img = cv2.cvtColor(img, cv2.COLOR_BGR2RGB)
34
+ # Match inference preprocessing: Resize(256) + CenterCrop(224)
35
+ h, w = img.shape[:2]
36
+ if h < w:
37
+ new_h, new_w = 256, int(w * 256 / h)
38
+ else:
39
+ new_h, new_w = int(h * 256 / w), 256
40
+ img = cv2.resize(img, (new_w, new_h))
41
+ y0 = (new_h - 224) // 2
42
+ x0 = (new_w - 224) // 2
43
+ img = img[y0:y0+224, x0:x0+224]
44
  img = img / 255.0
45
  mean = np.array([0.485, 0.456, 0.406])
46
  std = np.array([0.229, 0.224, 0.225])