Update app.py
Browse files
app.py
CHANGED
|
@@ -8,7 +8,6 @@ from keras.models import Model
|
|
| 8 |
from keras.saving import register_keras_serializable
|
| 9 |
from keras.layers import TFSMLayer
|
| 10 |
import matplotlib.pyplot as plt
|
| 11 |
-
import matplotlib.cm as cm
|
| 12 |
from lime import lime_image
|
| 13 |
from skimage.segmentation import mark_boundaries
|
| 14 |
|
|
@@ -41,28 +40,28 @@ class CustomTFOpLambda(tf.keras.layers.Layer):
|
|
| 41 |
config.update({"function": self.function})
|
| 42 |
return config
|
| 43 |
|
| 44 |
-
# Constants
|
| 45 |
IMG_SIZE = (224, 224)
|
| 46 |
CLASS_NAMES = ['Normal', 'Diabetes', 'Glaucoma', 'Cataract', 'AMD', 'Hypertension', 'Myopia', 'Others']
|
| 47 |
|
| 48 |
-
# Load
|
| 49 |
@st.cache_resource
|
| 50 |
def load_model():
|
| 51 |
-
model_path = "Model" #
|
| 52 |
if not os.path.exists(model_path):
|
| 53 |
st.error(f"β Model directory '{model_path}' not found!")
|
| 54 |
st.stop()
|
| 55 |
try:
|
| 56 |
tfsm_layer = TFSMLayer(model_path, call_endpoint="serving_default")
|
| 57 |
-
inputs = Input(shape=(
|
| 58 |
outputs = tfsm_layer(inputs)
|
| 59 |
model = Model(inputs=inputs, outputs=outputs)
|
| 60 |
return model
|
| 61 |
except Exception as e:
|
| 62 |
-
st.error(f"β Error loading model: {str(e)}")
|
| 63 |
st.stop()
|
| 64 |
|
| 65 |
-
# Preprocessing functions
|
| 66 |
def crop_circle(img):
|
| 67 |
h, w = img.shape[:2]
|
| 68 |
center = (w // 2, h // 2)
|
|
@@ -93,113 +92,93 @@ def preprocess_image(img):
|
|
| 93 |
clahe = apply_clahe(circ)
|
| 94 |
sharp = sharpen_image(clahe)
|
| 95 |
resized = resize_normalize(sharp)
|
| 96 |
-
return resized
|
| 97 |
|
| 98 |
-
# ---
|
| 99 |
-
def
|
|
|
|
| 100 |
for layer in reversed(model.layers):
|
| 101 |
-
|
| 102 |
-
|
| 103 |
-
|
| 104 |
-
|
| 105 |
-
|
| 106 |
-
|
| 107 |
-
|
| 108 |
-
grad_model =
|
|
|
|
|
|
|
| 109 |
with tf.GradientTape() as tape:
|
| 110 |
-
conv_outputs, predictions = grad_model(
|
| 111 |
-
loss = predictions[:,
|
| 112 |
-
grads = tape.gradient(loss, conv_outputs)
|
| 113 |
-
|
| 114 |
-
|
| 115 |
-
|
| 116 |
-
|
| 117 |
-
|
| 118 |
-
|
| 119 |
-
|
| 120 |
-
|
| 121 |
-
|
| 122 |
-
|
| 123 |
-
|
| 124 |
-
|
| 125 |
-
|
| 126 |
-
|
| 127 |
-
|
| 128 |
-
|
| 129 |
-
|
| 130 |
-
|
| 131 |
-
|
| 132 |
-
|
| 133 |
-
|
| 134 |
-
|
| 135 |
-
|
| 136 |
-
|
| 137 |
-
|
| 138 |
-
|
| 139 |
-
|
| 140 |
-
|
| 141 |
-
|
| 142 |
-
|
| 143 |
-
|
| 144 |
-
|
| 145 |
-
|
| 146 |
-
heatmap = generate_gradcam(model, input_array, pred_idx, layer_name)
|
| 147 |
-
heatmap = cv2.resize(heatmap, IMG_SIZE)
|
| 148 |
-
heatmap = np.uint8(255 * heatmap)
|
| 149 |
-
heatmap = cv2.GaussianBlur(heatmap, (7, 7), 0)
|
| 150 |
-
heatmap_rgb = cm.jet(heatmap / 255.0)[..., :3]
|
| 151 |
-
heatmap_rgb = np.uint8(heatmap_rgb * 255)
|
| 152 |
-
overlayed = cv2.addWeighted(np.uint8(img * 255), 0.5, heatmap_rgb, 0.5, 0)
|
| 153 |
-
|
| 154 |
-
# LIME explanation
|
| 155 |
-
explanation = explainer.explain_instance(
|
| 156 |
-
image=img, classifier_fn=predict_fn,
|
| 157 |
-
top_labels=1, hide_color=0, num_samples=1000
|
| 158 |
-
)
|
| 159 |
-
temp, mask = explanation.get_image_and_mask(label=pred_idx, positive_only=True, num_features=10, hide_rest=False)
|
| 160 |
-
|
| 161 |
-
# Plot side by side
|
| 162 |
-
fig, axs = plt.subplots(1, 3, figsize=(15, 5))
|
| 163 |
-
axs[0].imshow(img)
|
| 164 |
-
axs[0].set_title(f"Original\nTrue: {true_label}", fontsize=11)
|
| 165 |
-
axs[1].imshow(overlayed)
|
| 166 |
-
axs[1].set_title(f"Grad-CAM\nPred: {pred_label}", fontsize=11)
|
| 167 |
-
axs[2].imshow(mark_boundaries(temp, mask))
|
| 168 |
-
axs[2].set_title(f"LIME\nPred: {pred_label}", fontsize=11)
|
| 169 |
-
for ax in axs:
|
| 170 |
-
ax.axis('off')
|
| 171 |
-
summary = explanation_text.get(pred_label, "Model detected features matching this class.")
|
| 172 |
-
plt.figtext(0.5, 0.01, summary, wrap=True, ha='center', fontsize=10)
|
| 173 |
-
plt.tight_layout(rect=[0, 0.03, 1, 1])
|
| 174 |
-
st.pyplot(fig)
|
| 175 |
-
plt.close()
|
| 176 |
-
|
| 177 |
-
# Streamlit app UI
|
| 178 |
-
st.set_page_config(page_title="π§ Retina Disease Classifier with Grad-CAM & LIME", layout="centered")
|
| 179 |
-
st.title("π§ Retina Disease Classifier with Grad-CAM & LIME")
|
| 180 |
|
| 181 |
model = load_model()
|
| 182 |
-
last_conv_layer_name = find_last_conv_layer(model)
|
| 183 |
|
| 184 |
-
uploaded_file = st.file_uploader("Upload a retinal image", type=["jpg", "jpeg", "png"])
|
| 185 |
if uploaded_file:
|
| 186 |
file_bytes = np.asarray(bytearray(uploaded_file.read()), dtype=np.uint8)
|
| 187 |
bgr_img = cv2.imdecode(file_bytes, cv2.IMREAD_COLOR)
|
| 188 |
rgb_img = cv2.cvtColor(bgr_img, cv2.COLOR_BGR2RGB)
|
| 189 |
|
| 190 |
-
|
| 191 |
-
|
| 192 |
-
|
| 193 |
-
|
| 194 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 195 |
preds = model.predict(input_tensor)
|
| 196 |
if isinstance(preds, dict):
|
| 197 |
preds = list(preds.values())[0]
|
|
|
|
| 198 |
pred_idx = np.argmax(preds)
|
| 199 |
pred_label = CLASS_NAMES[pred_idx]
|
| 200 |
confidence = np.max(preds) * 100
|
| 201 |
|
| 202 |
-
st.success(f"Prediction: **{pred_label}**
|
|
|
|
| 203 |
|
| 204 |
-
|
| 205 |
-
|
|
|
|
| 8 |
from keras.saving import register_keras_serializable
|
| 9 |
from keras.layers import TFSMLayer
|
| 10 |
import matplotlib.pyplot as plt
|
|
|
|
| 11 |
from lime import lime_image
|
| 12 |
from skimage.segmentation import mark_boundaries
|
| 13 |
|
|
|
|
| 40 |
config.update({"function": self.function})
|
| 41 |
return config
|
| 42 |
|
| 43 |
+
# --- Constants ---
|
| 44 |
IMG_SIZE = (224, 224)
|
| 45 |
CLASS_NAMES = ['Normal', 'Diabetes', 'Glaucoma', 'Cataract', 'AMD', 'Hypertension', 'Myopia', 'Others']
|
| 46 |
|
| 47 |
+
# --- Load SavedModel as TFSMLayer wrapped model ---
|
| 48 |
@st.cache_resource
|
| 49 |
def load_model():
|
| 50 |
+
model_path = "Model" # Your SavedModel directory path
|
| 51 |
if not os.path.exists(model_path):
|
| 52 |
st.error(f"β Model directory '{model_path}' not found!")
|
| 53 |
st.stop()
|
| 54 |
try:
|
| 55 |
tfsm_layer = TFSMLayer(model_path, call_endpoint="serving_default")
|
| 56 |
+
inputs = Input(shape=(224, 224, 3))
|
| 57 |
outputs = tfsm_layer(inputs)
|
| 58 |
model = Model(inputs=inputs, outputs=outputs)
|
| 59 |
return model
|
| 60 |
except Exception as e:
|
| 61 |
+
st.error(f"β Error loading model with TFSMLayer: {str(e)}")
|
| 62 |
st.stop()
|
| 63 |
|
| 64 |
+
# --- Preprocessing functions ---
|
| 65 |
def crop_circle(img):
|
| 66 |
h, w = img.shape[:2]
|
| 67 |
center = (w // 2, h // 2)
|
|
|
|
| 92 |
clahe = apply_clahe(circ)
|
| 93 |
sharp = sharpen_image(clahe)
|
| 94 |
resized = resize_normalize(sharp)
|
| 95 |
+
return circ, clahe, sharp, resized
|
| 96 |
|
| 97 |
+
# --- Grad-CAM ---
|
| 98 |
+
def show_gradcam(model, img, class_idx):
|
| 99 |
+
last_conv_layer = None
|
| 100 |
for layer in reversed(model.layers):
|
| 101 |
+
if isinstance(layer, tf.keras.layers.Conv2D):
|
| 102 |
+
last_conv_layer = layer.name
|
| 103 |
+
break
|
| 104 |
+
if last_conv_layer is None:
|
| 105 |
+
st.warning("β οΈ No Conv2D layer found for Grad-CAM.")
|
| 106 |
+
return
|
| 107 |
+
|
| 108 |
+
grad_model = Model(model.inputs, [model.get_layer(last_conv_layer).output, model.output])
|
| 109 |
+
img_tensor = tf.convert_to_tensor(img[np.newaxis, ...])
|
| 110 |
+
|
| 111 |
with tf.GradientTape() as tape:
|
| 112 |
+
conv_outputs, predictions = grad_model(img_tensor)
|
| 113 |
+
loss = predictions[:, class_idx]
|
| 114 |
+
grads = tape.gradient(loss, conv_outputs)[0]
|
| 115 |
+
cam = tf.reduce_mean(grads, axis=-1).numpy()
|
| 116 |
+
|
| 117 |
+
cam = np.maximum(cam, 0)
|
| 118 |
+
cam = cv2.resize(cam, IMG_SIZE)
|
| 119 |
+
cam = (cam - cam.min()) / (cam.max() - cam.min() + 1e-10)
|
| 120 |
+
|
| 121 |
+
heatmap = np.uint8(255 * cam)
|
| 122 |
+
heatmap = cv2.applyColorMap(heatmap, cv2.COLORMAP_JET)
|
| 123 |
+
overlay = cv2.addWeighted(np.uint8(img * 255), 0.6, heatmap, 0.4, 0)
|
| 124 |
+
|
| 125 |
+
st.subheader("π₯ Grad-CAM")
|
| 126 |
+
st.image(overlay, use_container_width=True)
|
| 127 |
+
|
| 128 |
+
# --- LIME ---
|
| 129 |
+
def show_lime(model, img, class_idx):
|
| 130 |
+
explainer = lime_image.LimeImageExplainer()
|
| 131 |
+
|
| 132 |
+
def predict_fn(images):
|
| 133 |
+
images = np.array(images)
|
| 134 |
+
preds = model.predict(images)
|
| 135 |
+
if isinstance(preds, dict):
|
| 136 |
+
preds = list(preds.values())[0]
|
| 137 |
+
return preds
|
| 138 |
+
|
| 139 |
+
explanation = explainer.explain_instance(np.uint8(img * 255), predict_fn, top_labels=1, hide_color=0, num_samples=1000)
|
| 140 |
+
lime_img, mask = explanation.get_image_and_mask(class_idx, positive_only=True, hide_rest=False)
|
| 141 |
+
|
| 142 |
+
st.subheader("π’ LIME Explanation")
|
| 143 |
+
st.image(mark_boundaries(lime_img, mask), use_container_width=True)
|
| 144 |
+
|
| 145 |
+
# --- Streamlit UI ---
|
| 146 |
+
st.set_page_config(page_title="π§ Retina Disease Classifier", layout="centered")
|
| 147 |
+
st.title("π§ Retina Disease Classifier")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 148 |
|
| 149 |
model = load_model()
|
|
|
|
| 150 |
|
| 151 |
+
uploaded_file = st.file_uploader("π€ Upload a retinal image", type=["jpg", "jpeg", "png"])
|
| 152 |
if uploaded_file:
|
| 153 |
file_bytes = np.asarray(bytearray(uploaded_file.read()), dtype=np.uint8)
|
| 154 |
bgr_img = cv2.imdecode(file_bytes, cv2.IMREAD_COLOR)
|
| 155 |
rgb_img = cv2.cvtColor(bgr_img, cv2.COLOR_BGR2RGB)
|
| 156 |
|
| 157 |
+
circ, clahe, sharp, final = preprocess_image(rgb_img)
|
| 158 |
+
|
| 159 |
+
st.subheader("π§ͺ Preprocessing Pipeline (Left β Right)")
|
| 160 |
+
steps = [
|
| 161 |
+
("π· Original", rgb_img),
|
| 162 |
+
("π΅ Circular Crop", circ),
|
| 163 |
+
("βͺ CLAHE", clahe),
|
| 164 |
+
("π£ Sharpened", sharp),
|
| 165 |
+
("π Resized", (final * 255).astype(np.uint8))
|
| 166 |
+
]
|
| 167 |
+
cols = st.columns(len(steps))
|
| 168 |
+
for col, (label, img) in zip(cols, steps):
|
| 169 |
+
col.image(img, caption=label, use_container_width=True)
|
| 170 |
+
|
| 171 |
+
input_tensor = np.expand_dims(final, axis=0)
|
| 172 |
preds = model.predict(input_tensor)
|
| 173 |
if isinstance(preds, dict):
|
| 174 |
preds = list(preds.values())[0]
|
| 175 |
+
|
| 176 |
pred_idx = np.argmax(preds)
|
| 177 |
pred_label = CLASS_NAMES[pred_idx]
|
| 178 |
confidence = np.max(preds) * 100
|
| 179 |
|
| 180 |
+
st.success(f"β
Prediction: **{pred_label}**")
|
| 181 |
+
st.info(f"π Confidence: {confidence:.2f}%")
|
| 182 |
|
| 183 |
+
show_gradcam(model, final, pred_idx)
|
| 184 |
+
show_lime(model, final, pred_idx)
|