Spaces:
Sleeping
Sleeping
Commit Β·
329abd1
1
Parent(s): 7d11de9
Fix(critical): Pretrained ImageNet CNN Weights and Strict Clinical Validation Gates
Browse files- inference.py +2 -2
- preprocessing.py +18 -19
inference.py
CHANGED
|
@@ -85,8 +85,8 @@ class MLPClassifier(nn.Module):
|
|
| 85 |
def load_efficientnet() -> nn.Module:
|
| 86 |
global _efficientnet
|
| 87 |
if _efficientnet is None:
|
| 88 |
-
print("[InferenceEngine] Loading EfficientNet-B4...")
|
| 89 |
-
net = models.efficientnet_b4(weights=
|
| 90 |
net.classifier = nn.Identity() # output 1792-dim embeddings
|
| 91 |
net = net.to(DEVICE)
|
| 92 |
net.eval()
|
|
|
|
| 85 |
def load_efficientnet() -> nn.Module:
|
| 86 |
global _efficientnet
|
| 87 |
if _efficientnet is None:
|
| 88 |
+
print("[InferenceEngine] Loading EfficientNet-B4 (ImageNet pretrained)...")
|
| 89 |
+
net = models.efficientnet_b4(weights='DEFAULT')
|
| 90 |
net.classifier = nn.Identity() # output 1792-dim embeddings
|
| 91 |
net = net.to(DEVICE)
|
| 92 |
net.eval()
|
preprocessing.py
CHANGED
|
@@ -71,8 +71,8 @@ def check_fundus_shape(img_rgb: np.ndarray) -> tuple:
|
|
| 71 |
dark_pixels = np.sum(L < 30)
|
| 72 |
dark_ratio = dark_pixels / total_pixels
|
| 73 |
|
| 74 |
-
if dark_ratio < 0.
|
| 75 |
-
#
|
| 76 |
return False, f"Tidak terdeteksi sebagai foto fundus retina (dark border terlalu kecil: {dark_ratio:.0%})", 0.0
|
| 77 |
|
| 78 |
# ββ b) Bright circular region check ββββββββββββββββββββββββββββββββββ
|
|
@@ -90,21 +90,20 @@ def check_fundus_shape(img_rgb: np.ndarray) -> tuple:
|
|
| 90 |
bright_area = cv2.contourArea(largest)
|
| 91 |
bright_ratio = bright_area / total_pixels
|
| 92 |
|
| 93 |
-
if bright_ratio < 0.
|
| 94 |
return False, f"Region retina terlalu kecil (area: {bright_ratio:.0%} dari gambar)", 0.0
|
| 95 |
-
if bright_ratio > 0.
|
| 96 |
return False, f"Seluruh gambar terang β bukan foto fundus dengan bingkai hitam ({bright_ratio:.0%})", 0.0
|
| 97 |
|
| 98 |
-
# Aspect ratio check: fundus
|
| 99 |
aspect = bw / bh if bh > 0 else 0
|
| 100 |
-
if aspect < 0.
|
| 101 |
-
return False, f"Bentuk region tidak bulat
|
| 102 |
|
| 103 |
# ββ Score: combination of dark border + circularity ββββββββββββββββββ
|
| 104 |
-
|
| 105 |
-
|
| 106 |
-
|
| 107 |
-
shape_score = round((dark_score + circ_score) / 2, 3)
|
| 108 |
|
| 109 |
return True, "OK", shape_score
|
| 110 |
|
|
@@ -146,18 +145,18 @@ def check_fundus_color(img_rgb: np.ndarray) -> tuple:
|
|
| 146 |
# ββ Saturation check βββββββββββββββββββββββββββββββββββββββββββββββββ
|
| 147 |
mean_saturation = S_roi.mean()
|
| 148 |
|
| 149 |
-
# Grayscale images: saturation
|
| 150 |
-
if mean_saturation <
|
| 151 |
-
return False, f"Gambar
|
| 152 |
|
| 153 |
-
if warm_ratio < 0.
|
| 154 |
return False, (
|
| 155 |
-
f"Warna tidak
|
| 156 |
-
f"(warm hue: {warm_ratio:.0%}, harusnya β₯
|
| 157 |
-
f"
|
| 158 |
), 0.0
|
| 159 |
|
| 160 |
-
color_score = round(min(warm_ratio / 0.
|
| 161 |
return True, "OK", color_score
|
| 162 |
|
| 163 |
|
|
|
|
| 71 |
dark_pixels = np.sum(L < 30)
|
| 72 |
dark_ratio = dark_pixels / total_pixels
|
| 73 |
|
| 74 |
+
if dark_ratio < 0.20:
|
| 75 |
+
# Fundus images must have at least ~20% pitch black borders from camera aperture
|
| 76 |
return False, f"Tidak terdeteksi sebagai foto fundus retina (dark border terlalu kecil: {dark_ratio:.0%})", 0.0
|
| 77 |
|
| 78 |
# ββ b) Bright circular region check ββββββββββββββββββββββββββββββββββ
|
|
|
|
| 90 |
bright_area = cv2.contourArea(largest)
|
| 91 |
bright_ratio = bright_area / total_pixels
|
| 92 |
|
| 93 |
+
if bright_ratio < 0.15:
|
| 94 |
return False, f"Region retina terlalu kecil (area: {bright_ratio:.0%} dari gambar)", 0.0
|
| 95 |
+
if bright_ratio > 0.80:
|
| 96 |
return False, f"Seluruh gambar terang β bukan foto fundus dengan bingkai hitam ({bright_ratio:.0%})", 0.0
|
| 97 |
|
| 98 |
+
# Aspect ratio check: fundus ROI must be roughly a perfect circle
|
| 99 |
aspect = bw / bh if bh > 0 else 0
|
| 100 |
+
if aspect < 0.7 or aspect > 1.3:
|
| 101 |
+
return False, f"Bentuk region tidak bulat sempurna (aspect ratio: {aspect:.2f}) β bukan fundus retina", 0.0
|
| 102 |
|
| 103 |
# ββ Score: combination of dark border + circularity ββββββββββββββββββ
|
| 104 |
+
dark_score = min(dark_ratio / 0.40, 1.0)
|
| 105 |
+
circ_score = 1.0 - abs(aspect - 1.0) / 0.3 # closer to 1.0 = more circular
|
| 106 |
+
shape_score = round(max((dark_score + circ_score) / 2, 0.0), 3)
|
|
|
|
| 107 |
|
| 108 |
return True, "OK", shape_score
|
| 109 |
|
|
|
|
| 145 |
# ββ Saturation check βββββββββββββββββββββββββββββββββββββββββββββββββ
|
| 146 |
mean_saturation = S_roi.mean()
|
| 147 |
|
| 148 |
+
# Grayscale images or weakly colored diagrams: saturation is low
|
| 149 |
+
if mean_saturation < 35:
|
| 150 |
+
return False, f"Gambar tidak memiliki ketajaman warna fundus (saturasi: {mean_saturation:.0f} < 35)", 0.0
|
| 151 |
|
| 152 |
+
if warm_ratio < 0.35:
|
| 153 |
return False, (
|
| 154 |
+
f"Warna tidak dominan merah/oranye fundus "
|
| 155 |
+
f"(warm hue: {warm_ratio:.0%}, harusnya β₯35%). "
|
| 156 |
+
f"Bukan foto medis retina yang valid."
|
| 157 |
), 0.0
|
| 158 |
|
| 159 |
+
color_score = round(min(warm_ratio / 0.60, 1.0) * min(mean_saturation / 90, 1.0), 3)
|
| 160 |
return True, "OK", color_score
|
| 161 |
|
| 162 |
|