Update app.py
Browse files
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 |
-
# --
|
| 97 |
-
if uploaded_files:
|
| 98 |
-
|
| 99 |
-
|
| 100 |
-
|
| 101 |
-
|
| 102 |
-
|
| 103 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 104 |
|
| 105 |
-
|
| 106 |
-
|
|
|
|
|
|
|
| 107 |
|
| 108 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 109 |
if uploaded_files:
|
| 110 |
-
st.markdown("## π§ͺ
|
| 111 |
cols = st.columns(min(4, len(uploaded_files)))
|
| 112 |
-
|
| 113 |
for i, file in enumerate(uploaded_files):
|
| 114 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 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}*
|
| 126 |
-
|
| 127 |
-
|
| 128 |
-
|
| 129 |
-
|
| 130 |
-
|
| 131 |
-
|
| 132 |
-
|
| 133 |
-
|
| 134 |
-
|
| 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)
|
|
|