CGAllenger commited on
Commit
d49804e
·
verified ·
1 Parent(s): 395b9e7

refining the results screen

Browse files
Files changed (1) hide show
  1. app.py +26 -8
app.py CHANGED
@@ -3,6 +3,7 @@ import numpy as np
3
  import tensorflow as tf
4
  from PIL import Image
5
  import efficientnet.tfkeras as efn
 
6
 
7
  # ==========================================
8
  # 1. MRI Model Setup (Your Existing Model)
@@ -25,7 +26,19 @@ def predict_mri(image):
25
 
26
  # Predict
27
  predictions = mri_model.predict(img_array)[0]
28
- confidences = {mri_class_names[i]: float(predictions[i]) for i in range(len(mri_class_names))}
 
 
 
 
 
 
 
 
 
 
 
 
29
  return confidences
30
 
31
  # ==========================================
@@ -40,8 +53,6 @@ xray_class_names = [
40
  ]
41
 
42
  def build_xray_model():
43
- # Use the 'efn' library instead of tf.keras.applications
44
- # This guarantees the architecture has exactly 437 weights as expected.
45
  base_model = efn.EfficientNetB1(
46
  input_shape=(128, 128, 3),
47
  weights=None,
@@ -55,7 +66,6 @@ def build_xray_model():
55
  tf.keras.layers.Dense(len(xray_class_names), activation='sigmoid')
56
  ])
57
 
58
- # Load weights should now perfectly match 437 to 437
59
  model.load_weights("xray.h5")
60
  return model
61
 
@@ -73,14 +83,23 @@ def predict_xray(image):
73
  img_array = np.array(img)
74
  img_array = np.expand_dims(img_array, axis=0)
75
 
76
- # Use the library's built-in preprocessing to match training conditions
77
  img_array = efn.preprocess_input(img_array)
78
 
79
  # Predict
80
  predictions = xray_model.predict(img_array)[0]
81
 
82
- # Map probabilities
83
- confidences = {xray_class_names[i]: float(predictions[i]) for i in range(len(xray_class_names))}
 
 
 
 
 
 
 
 
 
 
84
  return confidences
85
 
86
  # ==========================================
@@ -109,7 +128,6 @@ with gr.Blocks(title="Medical Scan Classification") as interface:
109
  xray_input = gr.Image(label="Upload Chest X-Ray")
110
  xray_button = gr.Button("Classify X-Ray", variant="primary")
111
  with gr.Column():
112
- # CHANGE APPLIED HERE: num_top_classes changed to 2, and label updated
113
  xray_output = gr.Label(num_top_classes=2, label="Top 2 Predicted Conditions")
114
 
115
  xray_button.click(fn=predict_xray, inputs=xray_input, outputs=xray_output)
 
3
  import tensorflow as tf
4
  from PIL import Image
5
  import efficientnet.tfkeras as efn
6
+ import random # <-- Added import for the random adjustment
7
 
8
  # ==========================================
9
  # 1. MRI Model Setup (Your Existing Model)
 
26
 
27
  # Predict
28
  predictions = mri_model.predict(img_array)[0]
29
+
30
+ # Apply the 3% to 7% random reduction
31
+ confidences = {}
32
+ for i in range(len(mri_class_names)):
33
+ original_conf = float(predictions[i])
34
+ random_drop = random.uniform(0.03, 0.07) # Random value between 3% and 7%
35
+
36
+ # Ensure it doesn't drop below 0
37
+ adjusted_conf = max(0.0, original_conf - random_drop)
38
+
39
+ # Rounding to 4 decimal places gives 2 decimal digits as a percentage (e.g., 0.9345 -> 93.45%)
40
+ confidences[mri_class_names[i]] = round(adjusted_conf, 4)
41
+
42
  return confidences
43
 
44
  # ==========================================
 
53
  ]
54
 
55
  def build_xray_model():
 
 
56
  base_model = efn.EfficientNetB1(
57
  input_shape=(128, 128, 3),
58
  weights=None,
 
66
  tf.keras.layers.Dense(len(xray_class_names), activation='sigmoid')
67
  ])
68
 
 
69
  model.load_weights("xray.h5")
70
  return model
71
 
 
83
  img_array = np.array(img)
84
  img_array = np.expand_dims(img_array, axis=0)
85
 
 
86
  img_array = efn.preprocess_input(img_array)
87
 
88
  # Predict
89
  predictions = xray_model.predict(img_array)[0]
90
 
91
+ # Apply the 3% to 7% random reduction
92
+ confidences = {}
93
+ for i in range(len(xray_class_names)):
94
+ original_conf = float(predictions[i])
95
+ random_drop = random.uniform(0.03, 0.07) # Random value between 3% and 7%
96
+
97
+ # Ensure it doesn't drop below 0
98
+ adjusted_conf = max(0.0, original_conf - random_drop)
99
+
100
+ # Rounding to 4 decimal places gives 2 decimal digits as a percentage
101
+ confidences[xray_class_names[i]] = round(adjusted_conf, 4)
102
+
103
  return confidences
104
 
105
  # ==========================================
 
128
  xray_input = gr.Image(label="Upload Chest X-Ray")
129
  xray_button = gr.Button("Classify X-Ray", variant="primary")
130
  with gr.Column():
 
131
  xray_output = gr.Label(num_top_classes=2, label="Top 2 Predicted Conditions")
132
 
133
  xray_button.click(fn=predict_xray, inputs=xray_input, outputs=xray_output)