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 +5 -4
- app/utils.py +11 -4
- resource/cluster_centers.npy +2 -2
- resource/model_b0_balanced_v2_emb.onnx +3 -0
- scripts/compute_cluster_centers.py +11 -2
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, "
|
| 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 ๆ็ดๆฅๅ้ก
|
| 106 |
# ่งฃๆฑบ้ๅ
ธๅๆๆ่งๅบฆ๏ผๅฆ่ๅจ้ปๅบๆจๆฌ๏ผ่ขซ sim gate ่ชคๆ็ๅ้ก
|
| 107 |
-
#
|
| 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 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 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)
|
| 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:
|
| 3 |
-
size
|
|
|
|
| 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" / "
|
| 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 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 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])
|