muhammadhabibna commited on
Commit
329abd1
Β·
1 Parent(s): 7d11de9

Fix(critical): Pretrained ImageNet CNN Weights and Strict Clinical Validation Gates

Browse files
Files changed (2) hide show
  1. inference.py +2 -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=None)
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.10:
75
- # Very few dark pixels β†’ likely a natural photo / screenshot
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.08:
94
  return False, f"Region retina terlalu kecil (area: {bright_ratio:.0%} dari gambar)", 0.0
95
- if bright_ratio > 0.90:
96
  return False, f"Seluruh gambar terang β€” bukan foto fundus dengan bingkai hitam ({bright_ratio:.0%})", 0.0
97
 
98
- # Aspect ratio check: fundus is roughly circular
99
  aspect = bw / bh if bh > 0 else 0
100
- if aspect < 0.35 or aspect > 2.8:
101
- return False, f"Bentuk region tidak bulat/oval (aspect ratio: {aspect:.2f}) β€” bukan fundus retina", 0.0
102
 
103
  # ── Score: combination of dark border + circularity ──────────────────
104
- # Ideal dark_ratio for fundus: ~0.25–0.60
105
- dark_score = min(dark_ratio / 0.35, 1.0)
106
- circ_score = 1.0 - abs(aspect - 1.0) / 1.5 # closer to 1.0 = more circular
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 near 0
150
- if mean_saturation < 15:
151
- return False, f"Gambar tampak grayscale/hitam-putih (saturasi sangat rendah: {mean_saturation:.0f})", 0.0
152
 
153
- if warm_ratio < 0.20:
154
  return False, (
155
- f"Warna tidak sesuai foto fundus retina "
156
- f"(warm hue: {warm_ratio:.0%}, harusnya β‰₯20%). "
157
- f"Pastikan mengupload foto fundus retina, bukan foto biasa."
158
  ), 0.0
159
 
160
- color_score = round(min(warm_ratio / 0.45, 1.0) * min(mean_saturation / 60, 1.0), 3)
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