🧠 Bioset - Brain Tumor MRI Detection
Advanced AI-powered MRI analysis with explainable attention visualization with Enhanced Grad-CAM++.
import os import numpy as np import tensorflow as tf from tensorflow.keras.models import load_model from tensorflow.keras.preprocessing.image import img_to_array, array_to_img import matplotlib.pyplot as plt import matplotlib.cm as cm import cv2 import gradio as gr from PIL import Image import io import tempfile from datetime import datetime # Global variables model = None class_labels = {0: 'no', 1: 'yes'} IMG_WIDTH, IMG_HEIGHT = 128, 128 # --- MODEL LOADING FUNCTION --- def load_brain_tumor_model(): """Load the brain tumor detection model from the file system""" global model # Common model file names to check model_paths = [ 'brain_tumor_classifier_v3.h5', 'model.h5', 'brain_tumor_model.h5', 'brain_tumor_classifier.h5' ] for model_path in model_paths: if os.path.exists(model_path): try: model = load_model(model_path) print(f"✅ Model loaded successfully from {model_path}") return True except Exception as e: print(f"❌ Error loading model from {model_path}: {str(e)}") continue print("❌ No valid model file found. Please ensure your model is in the root directory.") return False # Load model on startup model_loaded = load_brain_tumor_model() # --- IMAGE PREPROCESSING FUNCTIONS --- def preprocess_image(image, target_size=(128, 128)): """ Preprocess uploaded image for model prediction """ if image is None: return None, "No image provided" try: # Convert to PIL Image if needed if not isinstance(image, Image.Image): image = Image.fromarray(image) # Convert to RGB if needed if image.mode != 'RGB': image = image.convert('RGB') # Resize image image_resized = image.resize(target_size, Image.Resampling.LANCZOS) # Convert to grayscale for display (optional) image_gray = image_resized.convert('L').convert('RGB') # Convert to array and normalize img_array = img_to_array(image_resized) / 255.0 return image_resized, image_gray, img_array, "✅ Image preprocessed successfully" except Exception as e: return None, None, None, f"❌ Error preprocessing image: {str(e)}" # --- ENHANCED GRAD-CAM++ FUNCTIONS --- def make_gradcampp_heatmap(img_array, model, last_conv_layer_name='last_conv_layer', pred_index=None): """ Creates an improved Grad-CAM++ heatmap with better numerical stability. """ if model is None: return None try: grad_model = tf.keras.models.Model( inputs=model.input, outputs=[model.get_layer(last_conv_layer_name).output, model.output] ) with tf.GradientTape(persistent=True) as tape1: with tf.GradientTape(persistent=True) as tape2: with tf.GradientTape() as tape3: conv_outputs, predictions = grad_model(img_array) if pred_index is None: pred_index = tf.argmax(predictions[0]) class_channel = predictions[:, pred_index] grads = tape3.gradient(class_channel, conv_outputs) first_derivative = tape2.gradient(class_channel, conv_outputs) second_derivative = tape1.gradient(first_derivative, conv_outputs) del tape1, tape2 eps = 1e-8 alpha_num = second_derivative alpha_denom = 2.0 * second_derivative + tf.reduce_sum(conv_outputs * grads, axis=[1, 2], keepdims=True) alpha_denom = tf.where(tf.abs(alpha_denom) < eps, tf.ones_like(alpha_denom) * eps, alpha_denom) alphas = alpha_num / alpha_denom weights = tf.reduce_sum(alphas * tf.nn.relu(grads), axis=[1, 2]) weights = tf.nn.softmax(weights, axis=-1) weights_reshaped = tf.reshape(weights, (1, 1, 1, -1)) heatmap = tf.reduce_sum(weights_reshaped * conv_outputs, axis=-1) heatmap = tf.squeeze(heatmap, axis=0) heatmap = tf.nn.relu(heatmap) heatmap_np = heatmap.numpy() heatmap_min = np.min(heatmap_np) heatmap_max = np.max(heatmap_np) if heatmap_max > heatmap_min: heatmap_np = (heatmap_np - heatmap_min) / (heatmap_max - heatmap_min) else: heatmap_np = np.zeros_like(heatmap_np) return heatmap_np except Exception as e: print(f"Error in Grad-CAM++: {str(e)}") return None def create_heatmap_visualizations(heatmap, img_shape): """Create multiple heatmap visualizations with different color schemes""" heatmap_resized = cv2.resize(heatmap, (img_shape[1], img_shape[0]), interpolation=cv2.INTER_CUBIC) heatmap_smooth = cv2.GaussianBlur(heatmap_resized, (5, 5), 0) heatmap_enhanced = cv2.equalizeHist(np.uint8(255 * heatmap_smooth)) / 255.0 visualizations = { 'jet': {'heatmap': heatmap_smooth, 'colormap': 'jet', 'title': 'Jet Heatmap'}, 'hot': {'heatmap': heatmap_smooth, 'colormap': 'hot', 'title': 'Hot Heatmap'}, 'plasma': {'heatmap': heatmap_enhanced, 'colormap': 'plasma', 'title': 'Plasma Heatmap'}, 'viridis': {'heatmap': heatmap_enhanced, 'colormap': 'viridis', 'title': 'Viridis Heatmap'}, 'inferno': {'heatmap': heatmap_smooth, 'colormap': 'inferno', 'title': 'Inferno Heatmap'}, 'cool': {'heatmap': heatmap_smooth, 'colormap': 'cool', 'title': 'Cool Heatmap'} } return visualizations def superimpose_gradcam_enhanced(img, heatmap, colormap='jet', alpha=0.4): """Enhanced superimposition with different colormaps""" if not isinstance(img, np.ndarray): img = img_to_array(img) if img.max() > 1.0: img = img / 255.0 heatmap_resized = cv2.resize(heatmap, (img.shape[1], img.shape[0]), interpolation=cv2.INTER_CUBIC) heatmap_uint8 = np.uint8(255 * heatmap_resized) if hasattr(plt, 'colormaps'): cmap = plt.colormaps[colormap] else: cmap = cm.get_cmap(colormap) colored_heatmap = cmap(heatmap_uint8)[:, :, :3] gamma = 2.2 img_gamma = np.power(img, 1/gamma) colored_heatmap_gamma = np.power(colored_heatmap, 1/gamma) blended_gamma = (colored_heatmap_gamma * alpha) + (img_gamma * (1 - alpha)) superimposed_img_float = np.power(blended_gamma, gamma) superimposed_img_float = np.clip(superimposed_img_float, 0, 1) return superimposed_img_float # --- PREDICTION AND VISUALIZATION FUNCTIONS --- def predict_brain_tumor(image): """Make prediction on uploaded image""" if not model_loaded or model is None: return "❌ Model not available. Please check if the model file exists in the space.", None, None if image is None: return "❌ No image provided.", None, None try: # Preprocess image processed_img, gray_img, img_array, preprocess_msg = preprocess_image(image) if processed_img is None: return preprocess_msg, None, None # Make prediction img_batch = np.expand_dims(img_array, axis=0) prediction = model.predict(img_batch, verbose=0)[0][0] # Interpret results predicted_class = int(round(prediction)) predicted_label = class_labels[predicted_class] confidence = prediction if predicted_class == 1 else 1 - prediction # Create result message if predicted_class == 1: status_emoji = "⚠️" status_text = "**TUMOR DETECTED**" status_color = "red" else: status_emoji = "✅" status_text = "**NO TUMOR DETECTED**" status_color = "green" result_msg = f""" ## 🧠 Brain Tumor Detection Results **Prediction:** {predicted_label.upper()} **Confidence:** {confidence:.1%} **Raw Score:** {prediction:.4f} {status_emoji} {status_text} """ return result_msg, processed_img, gray_img except Exception as e: return f"❌ Error during prediction: {str(e)}", None, None def create_detailed_analysis(image): """Create comprehensive Grad-CAM++ analysis""" if not model_loaded or model is None or image is None: return "❌ Please upload an image for analysis." try: # Preprocess and predict processed_img, gray_img, img_array, _ = preprocess_image(image) img_batch = np.expand_dims(img_array, axis=0) prediction = model.predict(img_batch, verbose=0)[0][0] predicted_class = int(round(prediction)) predicted_label = class_labels[predicted_class] confidence = prediction if predicted_class == 1 else 1 - prediction # Generate heatmap heatmap = make_gradcampp_heatmap(img_batch, model) if heatmap is None: return "❌ Error generating heatmap." # Create visualizations visualizations = create_heatmap_visualizations(heatmap, img_array.shape) # Create comprehensive plot with 4 rows to accommodate all visualizations fig = plt.figure(figsize=(20, 16)) color = 'green' if predicted_class == 0 else 'red' fig.suptitle(f'Comprehensive Grad-CAM++ Analysis\nPredicted: {predicted_label.upper()} ({confidence:.2%})', fontsize=16, fontweight='bold', color=color) # Row 1: Original image and heatmaps # Original image plt.subplot(4, 5, 1) plt.imshow(processed_img) plt.title("Original Image", fontsize=12, fontweight='bold') plt.axis('off') # Different heatmap visualizations (4 in first row) viz_names = ['jet', 'hot', 'plasma', 'viridis'] for i, viz_name in enumerate(viz_names): viz = visualizations[viz_name] plt.subplot(4, 5, i + 2) im = plt.imshow(viz['heatmap'], cmap=viz['colormap']) plt.title(viz['title'], fontsize=12) plt.axis('off') plt.colorbar(im, fraction=0.046, pad=0.04) # Row 2: Remaining heatmaps, attention profile, and statistics # More heatmap styles viz_names2 = ['inferno', 'cool'] for i, viz_name in enumerate(viz_names2): viz = visualizations[viz_name] plt.subplot(4, 5, i + 6) im = plt.imshow(viz['heatmap'], cmap=viz['colormap']) plt.title(viz['title'], fontsize=12) plt.axis('off') plt.colorbar(im, fraction=0.046, pad=0.04) # Attention profile plt.subplot(4, 5, 8) attention_profile = np.mean(heatmap, axis=1) plt.plot(attention_profile, range(len(attention_profile)), 'b-', linewidth=2) plt.title('Vertical Attention Profile', fontsize=12) plt.xlabel('Attention Intensity') plt.ylabel('Image Height') plt.gca().invert_yaxis() plt.grid(True, alpha=0.3) # Statistics plt.subplot(4, 5, 9) stats_text = f"""Heatmap Statistics: Mean: {np.mean(heatmap):.3f} Std: {np.std(heatmap):.3f} Max: {np.max(heatmap):.3f} Min: {np.min(heatmap):.3f} Prediction: Confidence: {confidence:.1%} Raw Score: {prediction:.4f} Class: {predicted_label}""" plt.text(0.1, 0.5, stats_text, transform=plt.gca().transAxes, fontsize=10, verticalalignment='center', bbox=dict(boxstyle="round,pad=0.3", facecolor="lightblue")) plt.axis('off') # Empty space for symmetry plt.subplot(4, 5, 10) plt.axis('off') # Rows 3-4: All 6 superimposed views in 2 rows of 3 superimposed_colormaps = ['jet', 'hot', 'plasma', 'viridis', 'inferno', 'cool'] # Row 3: First 3 superimposed views (positions 11-13) for i, cmap_name in enumerate(superimposed_colormaps[:3]): superimposed_img = superimpose_gradcam_enhanced(img_array, heatmap, colormap=cmap_name, alpha=0.4) plt.subplot(4, 5, i + 11) plt.imshow(superimposed_img) plt.title(f'Superimposed ({cmap_name.title()})', fontsize=12) plt.axis('off') # Empty spaces in row 3 plt.subplot(4, 5, 14) plt.axis('off') plt.subplot(4, 5, 15) plt.axis('off') # Row 4: Last 3 superimposed views (positions 16-18) for i, cmap_name in enumerate(superimposed_colormaps[3:]): superimposed_img = superimpose_gradcam_enhanced(img_array, heatmap, colormap=cmap_name, alpha=0.4) plt.subplot(4, 5, i + 16) plt.imshow(superimposed_img) plt.title(f'Superimposed ({cmap_name.title()})', fontsize=12) plt.axis('off') # Empty spaces in row 4 plt.subplot(4, 5, 19) plt.axis('off') plt.subplot(4, 5, 20) plt.axis('off') plt.tight_layout() plt.subplots_adjust(top=0.92) # Save to temporary file and return temp_file = tempfile.NamedTemporaryFile(delete=False, suffix='.png') plt.savefig(temp_file.name, dpi=300, bbox_inches='tight') plt.close() return temp_file.name except Exception as e: return f"❌ Error creating detailed analysis: {str(e)}" def create_quick_analysis(image): """Create quick 2x3 comparison view""" if not model_loaded or model is None or image is None: return "❌ Please upload an image for analysis." try: # Preprocess and predict processed_img, gray_img, img_array, _ = preprocess_image(image) img_batch = np.expand_dims(img_array, axis=0) prediction = model.predict(img_batch, verbose=0)[0][0] predicted_class = int(round(prediction)) predicted_label = class_labels[predicted_class] confidence = prediction if predicted_class == 1 else 1 - prediction # Generate heatmap heatmap = make_gradcampp_heatmap(img_batch, model) if heatmap is None: return "❌ Error generating heatmap." # Create quick visualization with 3x3 layout to accommodate all colormaps fig, axes = plt.subplots(3, 3, figsize=(15, 15)) color = 'green' if predicted_class == 0 else 'red' fig.suptitle(f'Quick Grad-CAM++ Analysis | Predicted: {predicted_label.upper()} ({confidence:.2%})', fontsize=14, fontweight='bold', color=color) # Row 1: Original image and two main heatmaps axes[0, 0].imshow(processed_img) axes[0, 0].set_title("Original Image") axes[0, 0].axis('off') # Jet heatmap heatmap_resized = cv2.resize(heatmap, (IMG_WIDTH, IMG_HEIGHT)) im1 = axes[0, 1].imshow(heatmap_resized, cmap='jet') axes[0, 1].set_title("Jet Heatmap") axes[0, 1].axis('off') plt.colorbar(im1, ax=axes[0, 1], fraction=0.046) # Plasma heatmap im2 = axes[0, 2].imshow(heatmap_resized, cmap='plasma') axes[0, 2].set_title("Plasma Heatmap") axes[0, 2].axis('off') plt.colorbar(im2, ax=axes[0, 2], fraction=0.046) # Rows 2-3: All 6 superimposed views superimposed_colormaps = ['jet', 'hot', 'plasma', 'viridis', 'inferno', 'cool'] # Row 2: First 3 superimposed views for i, cmap_name in enumerate(superimposed_colormaps[:3]): superimposed_img = superimpose_gradcam_enhanced(img_array, heatmap, cmap_name) axes[1, i].imshow(superimposed_img) axes[1, i].set_title(f"Superimposed ({cmap_name.title()})") axes[1, i].axis('off') # Row 3: Last 3 superimposed views for i, cmap_name in enumerate(superimposed_colormaps[3:]): superimposed_img = superimpose_gradcam_enhanced(img_array, heatmap, cmap_name) axes[2, i].imshow(superimposed_img) axes[2, i].set_title(f"Superimposed ({cmap_name.title()})") axes[2, i].axis('off') plt.tight_layout() # Save to temporary file and return temp_file = tempfile.NamedTemporaryFile(delete=False, suffix='.png') plt.savefig(temp_file.name, dpi=300, bbox_inches='tight') plt.close() return temp_file.name except Exception as e: return f"❌ Error creating quick analysis: {str(e)}" # --- GRADIO APP INTERFACE --- def create_gradio_app(): """Create the main Gradio interface""" # Custom CSS for better styling custom_css = """ h2, h3{ margin: 2.5rem initial; } .main-header { text-align: center; margin-bottom: 5rem; h1{ font-size: 2.5rem; /* Adjust as needed */ font-weight: bold; text-align: center; letter-spacing: -0.025em; margin-bottom: 1rem; /* Gradient masking */ background: linear-gradient(90deg, #667eea 0%, #764ba2 100%); -webkit-background-clip: text; -webkit-text-fill-color: transparent; } p{ font-size: 1.25rem; } } """ theme = gr.themes.Base( primary_hue="indigo", ) with gr.Blocks(title="🧠 Bioset - Brain Tumor MRI Detection", theme=theme, css=custom_css) as app: gr.HTML("""
Advanced AI-powered MRI analysis with explainable attention visualization with Enhanced Grad-CAM++.