Sefat33 commited on
Commit
aef51d4
Β·
verified Β·
1 Parent(s): 1e7b026

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +43 -26
app.py CHANGED
@@ -92,26 +92,45 @@ model = load_model()
92
 
93
  with st.sidebar:
94
  uploaded_files = st.file_uploader("πŸ“‚ Upload retinal images", type=["jpg", "jpeg", "png"], accept_multiple_files=True)
 
 
 
 
95
 
96
- # -- Optional: Show preprocessing for any selected image --
97
- if uploaded_files:
98
- st.markdown("## πŸ§ͺ View Preprocessing of a Selected Image")
99
- selected_file_name = st.selectbox("Select an image to view preprocessing", [f.name for f in uploaded_files])
100
- if selected_file_name:
101
- selected_file = next(f for f in uploaded_files if f.name == selected_file_name)
102
- bgr = cv2.imdecode(np.frombuffer(selected_file.read(), np.uint8), cv2.IMREAD_COLOR)
103
- rgb = cv2.cvtColor(bgr, cv2.COLOR_BGR2RGB)
 
 
 
 
 
 
104
 
105
- st.subheader(f"πŸ” Preprocessing Steps for {selected_file_name}")
106
- _ = preprocess_with_steps(rgb)
 
 
107
 
108
- # -- Predict & Show LIME for All Uploaded Images --
 
 
 
 
109
  if uploaded_files:
110
- st.markdown("## πŸ§ͺ Predictions and LIME Explanations for All Images")
111
  cols = st.columns(min(4, len(uploaded_files)))
112
-
113
  for i, file in enumerate(uploaded_files):
114
- bgr = cv2.imdecode(np.frombuffer(file.read(), np.uint8), cv2.IMREAD_COLOR)
 
 
 
 
115
  rgb = cv2.cvtColor(bgr, cv2.COLOR_BGR2RGB)
116
  img = cv2.resize(rgb, IMG_SIZE) / 255.0
117
  input_tensor = np.expand_dims(img, axis=0)
@@ -119,17 +138,15 @@ if uploaded_files:
119
  preds = predict(input_tensor, model)
120
  pred_idx = np.argmax(preds)
121
  pred_label = CLASS_NAMES[pred_idx]
122
- confidence = np.max(preds) * 100
123
 
124
  with cols[i % len(cols)]:
125
- st.markdown(f"**{file.name}**<br>🧠 *{pred_label}* ({confidence:.2f}%)", unsafe_allow_html=True)
126
- with st.spinner("🟑 LIME explanation is loading..."):
127
- explanation = LIME_EXPLAINER.explain_instance(
128
- image=img,
129
- classifier_fn=lambda imgs: predict(imgs, model),
130
- top_labels=1,
131
- hide_color=0,
132
- num_samples=1000
133
- )
134
- temp, mask = explanation.get_image_and_mask(label=pred_idx, positive_only=True, num_features=10, hide_rest=False)
135
- st.image(mark_boundaries(temp, mask), use_column_width=True)
 
92
 
93
  with st.sidebar:
94
  uploaded_files = st.file_uploader("πŸ“‚ Upload retinal images", type=["jpg", "jpeg", "png"], accept_multiple_files=True)
95
+ selected_filename = None
96
+ if uploaded_files:
97
+ filenames = [f.name for f in uploaded_files]
98
+ selected_filename = st.selectbox("🎯 Select an image to preprocess and predict", filenames)
99
 
100
+ # --- Process selected image ---
101
+ if uploaded_files and selected_filename:
102
+ file = next(f for f in uploaded_files if f.name == selected_filename)
103
+
104
+ # Read bytes once and reset pointer for later use
105
+ file_bytes = file.read()
106
+ file.seek(0)
107
+
108
+ bgr = cv2.imdecode(np.frombuffer(file_bytes, np.uint8), cv2.IMREAD_COLOR)
109
+ rgb = cv2.cvtColor(bgr, cv2.COLOR_BGR2RGB)
110
+
111
+ st.subheader(f"πŸ” Preprocessing & Prediction for: {selected_filename}")
112
+ preprocessed = preprocess_with_steps(rgb)
113
+ input_tensor = np.expand_dims(preprocessed, axis=0)
114
 
115
+ preds = predict(input_tensor, model)
116
+ pred_idx = np.argmax(preds)
117
+ pred_label = CLASS_NAMES[pred_idx]
118
+ confidence = np.max(preds) * 100
119
 
120
+ st.success(f"βœ… Prediction: **{pred_label}** ({confidence:.2f}%)")
121
+
122
+ show_lime(preprocessed, model, pred_idx, pred_label)
123
+
124
+ # --- Show LIME for all images ---
125
  if uploaded_files:
126
+ st.markdown("## πŸ§ͺ LIME Explanations for All Images")
127
  cols = st.columns(min(4, len(uploaded_files)))
 
128
  for i, file in enumerate(uploaded_files):
129
+ # Read bytes once and reset pointer
130
+ file_bytes = file.read()
131
+ file.seek(0)
132
+
133
+ bgr = cv2.imdecode(np.frombuffer(file_bytes, np.uint8), cv2.IMREAD_COLOR)
134
  rgb = cv2.cvtColor(bgr, cv2.COLOR_BGR2RGB)
135
  img = cv2.resize(rgb, IMG_SIZE) / 255.0
136
  input_tensor = np.expand_dims(img, axis=0)
 
138
  preds = predict(input_tensor, model)
139
  pred_idx = np.argmax(preds)
140
  pred_label = CLASS_NAMES[pred_idx]
 
141
 
142
  with cols[i % len(cols)]:
143
+ st.markdown(f"**{file.name}**<br>🧠 *{pred_label}*", unsafe_allow_html=True)
144
+ explanation = LIME_EXPLAINER.explain_instance(
145
+ image=img,
146
+ classifier_fn=lambda imgs: predict(imgs, model),
147
+ top_labels=1,
148
+ hide_color=0,
149
+ num_samples=1000
150
+ )
151
+ temp, mask = explanation.get_image_and_mask(label=pred_idx, positive_only=True, num_features=10, hide_rest=False)
152
+ st.image(mark_boundaries(temp, mask), use_column_width=True)