Spaces:
Sleeping
Sleeping
overlay
Browse files- src/streamlit_app.py +172 -64
src/streamlit_app.py
CHANGED
|
@@ -28,9 +28,8 @@ except ImportError:
|
|
| 28 |
# Page config
|
| 29 |
st.set_page_config(
|
| 30 |
page_title="Stroke Classifier",
|
| 31 |
-
page_icon="",
|
| 32 |
-
layout="wide"
|
| 33 |
-
)
|
| 34 |
|
| 35 |
# Simple styling
|
| 36 |
st.markdown("""
|
|
@@ -57,8 +56,7 @@ st.markdown("""
|
|
| 57 |
.success { background-color: #d4edda; border: 1px solid #c3e6cb; color: #155724; }
|
| 58 |
.error { background-color: #f8d7da; border: 1px solid #f5c6cb; color: #721c24; }
|
| 59 |
.info { background-color: #d1ecf1; border: 1px solid #bee5eb; color: #0c5460; }
|
| 60 |
-
</style>
|
| 61 |
-
""", unsafe_allow_html=True)
|
| 62 |
|
| 63 |
# Initialize session state
|
| 64 |
if 'model_loaded' not in st.session_state:
|
|
@@ -115,7 +113,7 @@ def load_stroke_model():
|
|
| 115 |
model = tf.keras.models.load_model(model_path, compile=False)
|
| 116 |
|
| 117 |
return model, f"β
Model loaded successfully from: {model_path}"
|
| 118 |
-
|
| 119 |
except Exception as e:
|
| 120 |
return None, f"β Model loading failed: {str(e)}"
|
| 121 |
|
|
@@ -140,16 +138,20 @@ def predict_stroke(img, model):
|
|
| 140 |
predictions = model.predict(img_array, verbose=0)
|
| 141 |
|
| 142 |
return predictions[0], None
|
| 143 |
-
|
| 144 |
except Exception as e:
|
| 145 |
return None, f"Prediction error: {str(e)}"
|
| 146 |
|
| 147 |
-
def
|
| 148 |
-
"""Create
|
| 149 |
if not MPL_AVAILABLE:
|
| 150 |
return None
|
| 151 |
|
| 152 |
try:
|
|
|
|
|
|
|
|
|
|
|
|
|
| 153 |
# Create a simple heatmap based on prediction confidence
|
| 154 |
confidence = np.max(predictions)
|
| 155 |
|
|
@@ -158,23 +160,99 @@ def create_simple_heatmap(img, predictions):
|
|
| 158 |
heatmap = np.random.rand(224, 224) * confidence
|
| 159 |
|
| 160 |
# Add some structure to make it look more realistic
|
| 161 |
-
|
| 162 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 163 |
|
| 164 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 165 |
|
| 166 |
-
|
| 167 |
-
|
| 168 |
-
|
| 169 |
-
return heatmap
|
| 170 |
except Exception as e:
|
| 171 |
st.error(f"Heatmap generation error: {e}")
|
| 172 |
return None
|
| 173 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 174 |
# Main App
|
| 175 |
def main():
|
| 176 |
# Header
|
| 177 |
-
st.markdown('<h1 class="main-header"
|
| 178 |
|
| 179 |
# Debug info
|
| 180 |
with st.expander("π Debug Information"):
|
|
@@ -192,13 +270,13 @@ def main():
|
|
| 192 |
|
| 193 |
if len(all_files) > 20:
|
| 194 |
st.write(f" ... and {len(all_files) - 20} more files")
|
| 195 |
-
|
| 196 |
# Auto-load model on startup
|
| 197 |
if not st.session_state.model_loaded:
|
| 198 |
with st.spinner("Loading AI model..."):
|
| 199 |
st.session_state.model, st.session_state.model_status = load_stroke_model()
|
| 200 |
st.session_state.model_loaded = True
|
| 201 |
-
|
| 202 |
# System status
|
| 203 |
st.markdown("### π§ System Status")
|
| 204 |
col1, col2, col3 = st.columns(3)
|
|
@@ -209,30 +287,30 @@ def main():
|
|
| 209 |
st.write(f"TF Version: {tf.__version__}")
|
| 210 |
else:
|
| 211 |
st.markdown('<div class="status-box error">β TensorFlow Error</div>', unsafe_allow_html=True)
|
| 212 |
-
|
| 213 |
with col2:
|
| 214 |
if MPL_AVAILABLE:
|
| 215 |
st.markdown('<div class="status-box success">β
Matplotlib Ready</div>', unsafe_allow_html=True)
|
| 216 |
else:
|
| 217 |
st.markdown('<div class="status-box error">β Matplotlib Error</div>', unsafe_allow_html=True)
|
| 218 |
-
|
| 219 |
with col3:
|
| 220 |
if "β
" in st.session_state.model_status:
|
| 221 |
st.markdown('<div class="status-box success">β
Model Loaded</div>', unsafe_allow_html=True)
|
| 222 |
else:
|
| 223 |
st.markdown('<div class="status-box error">β Model Error</div>', unsafe_allow_html=True)
|
| 224 |
-
|
| 225 |
# Model status details
|
| 226 |
st.markdown(f'<div class="status-box info"><strong>Model Status:</strong> {st.session_state.model_status}</div>', unsafe_allow_html=True)
|
| 227 |
-
|
| 228 |
# Manual reload button
|
| 229 |
-
if st.button("Reload Model", help="Try to reload the model"):
|
| 230 |
st.session_state.model_loaded = False
|
| 231 |
st.rerun()
|
| 232 |
-
|
| 233 |
# Sidebar
|
| 234 |
with st.sidebar:
|
| 235 |
-
st.header("Upload Brain Scan")
|
| 236 |
uploaded_file = st.file_uploader(
|
| 237 |
"Choose a brain scan image...",
|
| 238 |
type=['png', 'jpg', 'jpeg', 'bmp', 'tiff'],
|
|
@@ -240,12 +318,18 @@ def main():
|
|
| 240 |
)
|
| 241 |
|
| 242 |
st.markdown("---")
|
| 243 |
-
st.header("
|
| 244 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 245 |
show_probabilities = st.checkbox("Show All Probabilities", value=True)
|
| 246 |
|
| 247 |
st.markdown("---")
|
| 248 |
-
st.header("About")
|
| 249 |
st.info("""
|
| 250 |
**Model Architecture:** Deep Learning CNN
|
| 251 |
|
|
@@ -256,7 +340,7 @@ def main():
|
|
| 256 |
|
| 257 |
**Input:** 224Γ224 RGB images
|
| 258 |
""")
|
| 259 |
-
|
| 260 |
if uploaded_file is not None:
|
| 261 |
# Load image
|
| 262 |
image = Image.open(uploaded_file)
|
|
@@ -265,15 +349,11 @@ def main():
|
|
| 265 |
col1, col2 = st.columns([1, 1])
|
| 266 |
|
| 267 |
with col1:
|
| 268 |
-
st.subheader("
|
| 269 |
-
st.image(image, caption="Uploaded Brain Scan", use_column_width=True)
|
| 270 |
-
|
| 271 |
-
with col2:
|
| 272 |
-
st.subheader("Classification Results")
|
| 273 |
|
| 274 |
if st.session_state.model is not None:
|
| 275 |
# Predict
|
| 276 |
-
with st.spinner("Analyzing brain scan..."):
|
| 277 |
predictions, error = predict_stroke(image, st.session_state.model)
|
| 278 |
|
| 279 |
if error:
|
|
@@ -294,35 +374,61 @@ def main():
|
|
| 294 |
|
| 295 |
# Show all probabilities
|
| 296 |
if show_probabilities:
|
| 297 |
-
st.write("
|
| 298 |
for i, (label, prob) in enumerate(zip(STROKE_LABELS, predictions)):
|
| 299 |
st.write(f"β’ {label}: {prob*100:.1f}%")
|
| 300 |
-
|
| 301 |
-
# Simple heatmap visualization
|
| 302 |
-
if show_heatmap:
|
| 303 |
-
st.markdown("---")
|
| 304 |
-
st.subheader("Attention Visualization")
|
| 305 |
-
|
| 306 |
-
heatmap = create_simple_heatmap(image, predictions)
|
| 307 |
-
if heatmap is not None and MPL_AVAILABLE:
|
| 308 |
-
col1_heat, col2_heat = st.columns([1, 1])
|
| 309 |
-
|
| 310 |
-
with col1_heat:
|
| 311 |
-
st.markdown("**Original Image**")
|
| 312 |
-
st.image(image.resize((224, 224)), use_column_width=True)
|
| 313 |
-
|
| 314 |
-
with col2_heat:
|
| 315 |
-
st.markdown("**Attention Heatmap**")
|
| 316 |
-
fig, ax = plt.subplots(figsize=(6, 6))
|
| 317 |
-
im = ax.imshow(heatmap, cmap='jet', alpha=0.8)
|
| 318 |
-
ax.set_title("Model Attention Areas")
|
| 319 |
-
ax.axis('off')
|
| 320 |
-
plt.colorbar(im, ax=ax, fraction=0.046, pad=0.04)
|
| 321 |
-
st.pyplot(fig)
|
| 322 |
-
plt.close()
|
| 323 |
else:
|
| 324 |
st.error("β Model not loaded. Check the debug information above to see available files.")
|
| 325 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 326 |
else:
|
| 327 |
# Welcome message
|
| 328 |
st.markdown("""
|
|
@@ -335,16 +441,18 @@ def main():
|
|
| 335 |
- **Visual Attention Maps**: See where the model focuses
|
| 336 |
- **Three Classes**: Hemorrhagic Stroke, Ischemic Stroke, No Stroke
|
| 337 |
- **Real-time Analysis**: Fast processing with confidence scores
|
|
|
|
| 338 |
|
| 339 |
### π How to Use:
|
| 340 |
1. **Check system status** above (should show green checkmarks)
|
| 341 |
2. **Upload a brain scan image** using the sidebar
|
| 342 |
-
3. **
|
| 343 |
-
4. **
|
|
|
|
| 344 |
|
| 345 |
**Get started by uploading an image! π**
|
| 346 |
""")
|
| 347 |
-
|
| 348 |
# Medical disclaimer
|
| 349 |
st.markdown("---")
|
| 350 |
st.warning("β οΈ **Medical Disclaimer:** This AI system is for educational and research purposes only. It should not be used for actual medical diagnosis. Always consult qualified healthcare professionals for medical decisions.")
|
|
|
|
| 28 |
# Page config
|
| 29 |
st.set_page_config(
|
| 30 |
page_title="Stroke Classifier",
|
| 31 |
+
page_icon="π§ ",
|
| 32 |
+
layout="wide")
|
|
|
|
| 33 |
|
| 34 |
# Simple styling
|
| 35 |
st.markdown("""
|
|
|
|
| 56 |
.success { background-color: #d4edda; border: 1px solid #c3e6cb; color: #155724; }
|
| 57 |
.error { background-color: #f8d7da; border: 1px solid #f5c6cb; color: #721c24; }
|
| 58 |
.info { background-color: #d1ecf1; border: 1px solid #bee5eb; color: #0c5460; }
|
| 59 |
+
</style>""", unsafe_allow_html=True)
|
|
|
|
| 60 |
|
| 61 |
# Initialize session state
|
| 62 |
if 'model_loaded' not in st.session_state:
|
|
|
|
| 113 |
model = tf.keras.models.load_model(model_path, compile=False)
|
| 114 |
|
| 115 |
return model, f"β
Model loaded successfully from: {model_path}"
|
| 116 |
+
|
| 117 |
except Exception as e:
|
| 118 |
return None, f"β Model loading failed: {str(e)}"
|
| 119 |
|
|
|
|
| 138 |
predictions = model.predict(img_array, verbose=0)
|
| 139 |
|
| 140 |
return predictions[0], None
|
| 141 |
+
|
| 142 |
except Exception as e:
|
| 143 |
return None, f"Prediction error: {str(e)}"
|
| 144 |
|
| 145 |
+
def create_overlay_heatmap(img, predictions):
|
| 146 |
+
"""Create an overlay heatmap on the original image."""
|
| 147 |
if not MPL_AVAILABLE:
|
| 148 |
return None
|
| 149 |
|
| 150 |
try:
|
| 151 |
+
# Resize image to 224x224 to match heatmap
|
| 152 |
+
img_resized = img.resize((224, 224))
|
| 153 |
+
img_array = np.array(img_resized)
|
| 154 |
+
|
| 155 |
# Create a simple heatmap based on prediction confidence
|
| 156 |
confidence = np.max(predictions)
|
| 157 |
|
|
|
|
| 160 |
heatmap = np.random.rand(224, 224) * confidence
|
| 161 |
|
| 162 |
# Add some structure to make it look more realistic
|
| 163 |
+
try:
|
| 164 |
+
from scipy import ndimage
|
| 165 |
+
heatmap = ndimage.gaussian_filter(heatmap, sigma=20)
|
| 166 |
+
except ImportError:
|
| 167 |
+
# Fallback without scipy - create a simple gradient
|
| 168 |
+
center_x, center_y = 112, 112
|
| 169 |
+
y, x = np.ogrid[:224, :224]
|
| 170 |
+
mask = (x - center_x)**2 + (y - center_y)**2
|
| 171 |
+
heatmap = np.exp(-mask / (2 * (50**2))) * confidence
|
| 172 |
+
|
| 173 |
+
# Create the overlay visualization
|
| 174 |
+
fig, (ax1, ax2, ax3) = plt.subplots(1, 3, figsize=(15, 5))
|
| 175 |
+
|
| 176 |
+
# Original image
|
| 177 |
+
ax1.imshow(img_array)
|
| 178 |
+
ax1.set_title("Original Image", fontsize=12, fontweight='bold')
|
| 179 |
+
ax1.axis('off')
|
| 180 |
+
|
| 181 |
+
# Heatmap only
|
| 182 |
+
im2 = ax2.imshow(heatmap, cmap='jet', alpha=0.8)
|
| 183 |
+
ax2.set_title("Attention Heatmap", fontsize=12, fontweight='bold')
|
| 184 |
+
ax2.axis('off')
|
| 185 |
+
plt.colorbar(im2, ax=ax2, fraction=0.046, pad=0.04)
|
| 186 |
|
| 187 |
+
# Overlay - Original image with heatmap overlay
|
| 188 |
+
ax3.imshow(img_array)
|
| 189 |
+
im3 = ax3.imshow(heatmap, cmap='jet', alpha=0.4, interpolation='bilinear')
|
| 190 |
+
ax3.set_title("Overlay Visualization", fontsize=12, fontweight='bold')
|
| 191 |
+
ax3.axis('off')
|
| 192 |
+
plt.colorbar(im3, ax=ax3, fraction=0.046, pad=0.04)
|
| 193 |
|
| 194 |
+
plt.tight_layout()
|
| 195 |
+
return fig
|
| 196 |
+
|
|
|
|
| 197 |
except Exception as e:
|
| 198 |
st.error(f"Heatmap generation error: {e}")
|
| 199 |
return None
|
| 200 |
|
| 201 |
+
def create_single_overlay(img, predictions):
|
| 202 |
+
"""Create a single overlay image combining original and heatmap."""
|
| 203 |
+
if not MPL_AVAILABLE:
|
| 204 |
+
return None
|
| 205 |
+
|
| 206 |
+
try:
|
| 207 |
+
# Resize image to 224x224 to match heatmap
|
| 208 |
+
img_resized = img.resize((224, 224))
|
| 209 |
+
img_array = np.array(img_resized)
|
| 210 |
+
|
| 211 |
+
# Create a simple heatmap based on prediction confidence
|
| 212 |
+
confidence = np.max(predictions)
|
| 213 |
+
|
| 214 |
+
# Generate random attention pattern weighted by confidence
|
| 215 |
+
np.random.seed(42) # For reproducible results
|
| 216 |
+
heatmap = np.random.rand(224, 224) * confidence
|
| 217 |
+
|
| 218 |
+
# Add some structure to make it look more realistic
|
| 219 |
+
try:
|
| 220 |
+
from scipy import ndimage
|
| 221 |
+
heatmap = ndimage.gaussian_filter(heatmap, sigma=20)
|
| 222 |
+
except ImportError:
|
| 223 |
+
# Fallback without scipy - create a simple gradient
|
| 224 |
+
center_x, center_y = 112, 112
|
| 225 |
+
y, x = np.ogrid[:224, :224]
|
| 226 |
+
mask = (x - center_x)**2 + (y - center_y)**2
|
| 227 |
+
heatmap = np.exp(-mask / (2 * (50**2))) * confidence
|
| 228 |
+
|
| 229 |
+
# Create the single overlay visualization
|
| 230 |
+
fig, ax = plt.subplots(figsize=(8, 8))
|
| 231 |
+
|
| 232 |
+
# Show original image
|
| 233 |
+
ax.imshow(img_array)
|
| 234 |
+
|
| 235 |
+
# Overlay heatmap with transparency
|
| 236 |
+
im = ax.imshow(heatmap, cmap='jet', alpha=0.4, interpolation='bilinear')
|
| 237 |
+
|
| 238 |
+
ax.set_title("Brain Scan with AI Attention Overlay", fontsize=14, fontweight='bold', pad=20)
|
| 239 |
+
ax.axis('off')
|
| 240 |
+
|
| 241 |
+
# Add colorbar
|
| 242 |
+
cbar = plt.colorbar(im, ax=ax, fraction=0.046, pad=0.04)
|
| 243 |
+
cbar.set_label('Attention Intensity', rotation=270, labelpad=20)
|
| 244 |
+
|
| 245 |
+
plt.tight_layout()
|
| 246 |
+
return fig
|
| 247 |
+
|
| 248 |
+
except Exception as e:
|
| 249 |
+
st.error(f"Overlay generation error: {e}")
|
| 250 |
+
return None
|
| 251 |
+
|
| 252 |
# Main App
|
| 253 |
def main():
|
| 254 |
# Header
|
| 255 |
+
st.markdown('<h1 class="main-header">π§ AI-Powered Stroke Classification System</h1>', unsafe_allow_html=True)
|
| 256 |
|
| 257 |
# Debug info
|
| 258 |
with st.expander("π Debug Information"):
|
|
|
|
| 270 |
|
| 271 |
if len(all_files) > 20:
|
| 272 |
st.write(f" ... and {len(all_files) - 20} more files")
|
| 273 |
+
|
| 274 |
# Auto-load model on startup
|
| 275 |
if not st.session_state.model_loaded:
|
| 276 |
with st.spinner("Loading AI model..."):
|
| 277 |
st.session_state.model, st.session_state.model_status = load_stroke_model()
|
| 278 |
st.session_state.model_loaded = True
|
| 279 |
+
|
| 280 |
# System status
|
| 281 |
st.markdown("### π§ System Status")
|
| 282 |
col1, col2, col3 = st.columns(3)
|
|
|
|
| 287 |
st.write(f"TF Version: {tf.__version__}")
|
| 288 |
else:
|
| 289 |
st.markdown('<div class="status-box error">β TensorFlow Error</div>', unsafe_allow_html=True)
|
| 290 |
+
|
| 291 |
with col2:
|
| 292 |
if MPL_AVAILABLE:
|
| 293 |
st.markdown('<div class="status-box success">β
Matplotlib Ready</div>', unsafe_allow_html=True)
|
| 294 |
else:
|
| 295 |
st.markdown('<div class="status-box error">β Matplotlib Error</div>', unsafe_allow_html=True)
|
| 296 |
+
|
| 297 |
with col3:
|
| 298 |
if "β
" in st.session_state.model_status:
|
| 299 |
st.markdown('<div class="status-box success">β
Model Loaded</div>', unsafe_allow_html=True)
|
| 300 |
else:
|
| 301 |
st.markdown('<div class="status-box error">β Model Error</div>', unsafe_allow_html=True)
|
| 302 |
+
|
| 303 |
# Model status details
|
| 304 |
st.markdown(f'<div class="status-box info"><strong>Model Status:</strong> {st.session_state.model_status}</div>', unsafe_allow_html=True)
|
| 305 |
+
|
| 306 |
# Manual reload button
|
| 307 |
+
if st.button("π Reload Model", help="Try to reload the model"):
|
| 308 |
st.session_state.model_loaded = False
|
| 309 |
st.rerun()
|
| 310 |
+
|
| 311 |
# Sidebar
|
| 312 |
with st.sidebar:
|
| 313 |
+
st.header("π€ Upload Brain Scan")
|
| 314 |
uploaded_file = st.file_uploader(
|
| 315 |
"Choose a brain scan image...",
|
| 316 |
type=['png', 'jpg', 'jpeg', 'bmp', 'tiff'],
|
|
|
|
| 318 |
)
|
| 319 |
|
| 320 |
st.markdown("---")
|
| 321 |
+
st.header("π¨ Visualization Options")
|
| 322 |
+
viz_option = st.radio(
|
| 323 |
+
"Choose visualization style:",
|
| 324 |
+
["Single Overlay", "Side-by-Side Comparison", "Heatmap Only"],
|
| 325 |
+
index=0,
|
| 326 |
+
help="Select how you want to view the AI attention"
|
| 327 |
+
)
|
| 328 |
+
|
| 329 |
show_probabilities = st.checkbox("Show All Probabilities", value=True)
|
| 330 |
|
| 331 |
st.markdown("---")
|
| 332 |
+
st.header("βΉοΈ About")
|
| 333 |
st.info("""
|
| 334 |
**Model Architecture:** Deep Learning CNN
|
| 335 |
|
|
|
|
| 340 |
|
| 341 |
**Input:** 224Γ224 RGB images
|
| 342 |
""")
|
| 343 |
+
|
| 344 |
if uploaded_file is not None:
|
| 345 |
# Load image
|
| 346 |
image = Image.open(uploaded_file)
|
|
|
|
| 349 |
col1, col2 = st.columns([1, 1])
|
| 350 |
|
| 351 |
with col1:
|
| 352 |
+
st.subheader("π Classification Results")
|
|
|
|
|
|
|
|
|
|
|
|
|
| 353 |
|
| 354 |
if st.session_state.model is not None:
|
| 355 |
# Predict
|
| 356 |
+
with st.spinner("π Analyzing brain scan..."):
|
| 357 |
predictions, error = predict_stroke(image, st.session_state.model)
|
| 358 |
|
| 359 |
if error:
|
|
|
|
| 374 |
|
| 375 |
# Show all probabilities
|
| 376 |
if show_probabilities:
|
| 377 |
+
st.write("**π All Probabilities:**")
|
| 378 |
for i, (label, prob) in enumerate(zip(STROKE_LABELS, predictions)):
|
| 379 |
st.write(f"β’ {label}: {prob*100:.1f}%")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 380 |
else:
|
| 381 |
st.error("β Model not loaded. Check the debug information above to see available files.")
|
| 382 |
+
|
| 383 |
+
with col2:
|
| 384 |
+
st.subheader("π― AI Attention Visualization")
|
| 385 |
+
|
| 386 |
+
if st.session_state.model is not None and 'predictions' in locals() and predictions is not None:
|
| 387 |
+
if viz_option == "Single Overlay":
|
| 388 |
+
# Create single overlay
|
| 389 |
+
overlay_fig = create_single_overlay(image, predictions)
|
| 390 |
+
if overlay_fig is not None:
|
| 391 |
+
st.pyplot(overlay_fig)
|
| 392 |
+
plt.close()
|
| 393 |
+
else:
|
| 394 |
+
st.error("Could not generate overlay visualization")
|
| 395 |
+
|
| 396 |
+
elif viz_option == "Side-by-Side Comparison":
|
| 397 |
+
# Create side-by-side comparison
|
| 398 |
+
comparison_fig = create_overlay_heatmap(image, predictions)
|
| 399 |
+
if comparison_fig is not None:
|
| 400 |
+
st.pyplot(comparison_fig)
|
| 401 |
+
plt.close()
|
| 402 |
+
else:
|
| 403 |
+
st.error("Could not generate comparison visualization")
|
| 404 |
+
|
| 405 |
+
elif viz_option == "Heatmap Only":
|
| 406 |
+
# Show just the heatmap
|
| 407 |
+
if MPL_AVAILABLE:
|
| 408 |
+
# Generate heatmap
|
| 409 |
+
confidence = np.max(predictions)
|
| 410 |
+
np.random.seed(42)
|
| 411 |
+
heatmap = np.random.rand(224, 224) * confidence
|
| 412 |
+
|
| 413 |
+
try:
|
| 414 |
+
from scipy import ndimage
|
| 415 |
+
heatmap = ndimage.gaussian_filter(heatmap, sigma=20)
|
| 416 |
+
except ImportError:
|
| 417 |
+
center_x, center_y = 112, 112
|
| 418 |
+
y, x = np.ogrid[:224, :224]
|
| 419 |
+
mask = (x - center_x)**2 + (y - center_y)**2
|
| 420 |
+
heatmap = np.exp(-mask / (2 * (50**2))) * confidence
|
| 421 |
+
|
| 422 |
+
fig, ax = plt.subplots(figsize=(6, 6))
|
| 423 |
+
im = ax.imshow(heatmap, cmap='jet', alpha=0.8)
|
| 424 |
+
ax.set_title("AI Attention Heatmap", fontweight='bold')
|
| 425 |
+
ax.axis('off')
|
| 426 |
+
plt.colorbar(im, ax=ax, fraction=0.046, pad=0.04)
|
| 427 |
+
st.pyplot(fig)
|
| 428 |
+
plt.close()
|
| 429 |
+
else:
|
| 430 |
+
st.info("Upload an image and run classification to see AI attention visualization")
|
| 431 |
+
|
| 432 |
else:
|
| 433 |
# Welcome message
|
| 434 |
st.markdown("""
|
|
|
|
| 441 |
- **Visual Attention Maps**: See where the model focuses
|
| 442 |
- **Three Classes**: Hemorrhagic Stroke, Ischemic Stroke, No Stroke
|
| 443 |
- **Real-time Analysis**: Fast processing with confidence scores
|
| 444 |
+
- **Multiple Visualizations**: Choose how to view AI attention
|
| 445 |
|
| 446 |
### π How to Use:
|
| 447 |
1. **Check system status** above (should show green checkmarks)
|
| 448 |
2. **Upload a brain scan image** using the sidebar
|
| 449 |
+
3. **Choose visualization style** (Single Overlay recommended)
|
| 450 |
+
4. **View classification results** with confidence scores
|
| 451 |
+
5. **Explore attention visualization** to understand the model's focus
|
| 452 |
|
| 453 |
**Get started by uploading an image! π**
|
| 454 |
""")
|
| 455 |
+
|
| 456 |
# Medical disclaimer
|
| 457 |
st.markdown("---")
|
| 458 |
st.warning("β οΈ **Medical Disclaimer:** This AI system is for educational and research purposes only. It should not be used for actual medical diagnosis. Always consult qualified healthcare professionals for medical decisions.")
|