Paminder002/Explainable_AI
0
1#Data Preprocessing2 3import gradio as gr4from tensorflow.keras.models import Model5import matplotlib.pyplot as plt6import cv2 # For color mapping the heatmap7 8 9 10 11 12 13 14 15import os16import numpy as np17import tensorflow as tf18from tensorflow.keras.preprocessing.image import ImageDataGenerator19from PIL import Image20 21# Set image size and batch size22IMAGE_SIZE = (224, 224)23BATCH_SIZE = 3224 25# Paths to your dataset26TRAIN_PATH = 'archive'27 28# Data generator for loading and preprocessing images29datagen = ImageDataGenerator(rescale=1./255, validation_split=0.15)30 31train_data = datagen.flow_from_directory(32 TRAIN_PATH,33 target_size=IMAGE_SIZE,34 batch_size=BATCH_SIZE,35 class_mode='binary',36 subset='training' # Set as training data37)38 39val_data = datagen.flow_from_directory(40 TRAIN_PATH,41 target_size=IMAGE_SIZE,42 batch_size=BATCH_SIZE,43 class_mode='binary',44 subset='validation' # Set as validation data45)46 47#CNN Model Setup (Transfer Learning)48 49import tensorflow as tf50from tensorflow.keras.applications import ResNet5051from tensorflow.keras.layers import Dense, GlobalAveragePooling2D, Dropout52from tensorflow.keras.models import Model53 54# Define the input shape55input_shape = (224, 224, 3)56 57# Load ResNet50 with input shape and without the top layer58base_model = ResNet50(weights='imagenet', include_top=False, input_shape=input_shape)59 60# Freeze the layers in the base model61base_model.trainable = False62 63# Add custom layers on top64x = base_model.output65x = GlobalAveragePooling2D()(x)66x = Dense(128, activation='relu')(x)67x = Dropout(0.5)(x)68predictions = Dense(1, activation='sigmoid')(x)69 70# Define the model71model = Model(inputs=base_model.input, outputs=predictions)72 73# Compile the model74model.compile(optimizer='adam', loss='binary_crossentropy', metrics=['accuracy'])75 76# Model summary77model.summary()78 79#Training the Model80 81# Train the model82history = model.fit(83 train_data,84 validation_data=val_data,85 epochs=10, # Adjust epochs as needed86 verbose=187)88 89import matplotlib.pyplot as plt90 91# Plot the training and validation accuracy92plt.figure(figsize=(12, 6))93 94# Accuracy plot95plt.subplot(1, 2, 1)96plt.plot(history.history['accuracy'], label='Training Accuracy')97plt.plot(history.history['val_accuracy'], label='Validation Accuracy')98plt.title('Model Accuracy')99plt.xlabel('Epoch')100plt.ylabel('Accuracy')101plt.legend(loc='lower right')102plt.grid(True)103 104# Loss plot105plt.subplot(1, 2, 2)106plt.plot(history.history['loss'], label='Training Loss')107plt.plot(history.history['val_loss'], label='Validation Loss')108plt.title('Model Loss')109plt.xlabel('Epoch')110plt.ylabel('Loss')111plt.legend(loc='upper right')112plt.grid(True)113 114# Show the plot115plt.tight_layout()116plt.show()117 118#Explainable AI Integration (Grad-CAM)119 120import numpy as np121import tensorflow as tf122import matplotlib.pyplot as plt123from tensorflow.keras.models import Model124from PIL import Image125 126def make_gradcam_heatmap(img_array, model, last_conv_layer_name):127 grad_model = Model(128 inputs=[model.inputs],129 outputs=[model.get_layer(last_conv_layer_name).output, model.output]130 )131 132 # Record operations for automatic differentiation133 with tf.GradientTape() as tape:134 conv_outputs, predictions = grad_model(img_array)135 loss = predictions[:, 0] # Assuming binary classification (0 = Healthy, 1 = COVID-19)136 137 # Compute gradients138 grads = tape.gradient(loss, conv_outputs)139 pooled_grads = tf.reduce_mean(grads, axis=(0, 1, 2))140 141 conv_outputs = conv_outputs[0]142 heatmap = tf.reduce_mean(tf.multiply(pooled_grads, conv_outputs), axis=-1)143 heatmap = np.maximum(heatmap, 0) / np.max(heatmap) # Normalize between 0 and 1144 return heatmap145 146def display_gradcam(img_path, heatmap, alpha=0.4):147 img = Image.open(img_path)148 img = img.resize((224, 224)) # Resize the image to match model input size149 150 heatmap = np.uint8(255 * heatmap) # Convert heatmap to 0-255 scale151 heatmap = Image.fromarray(heatmap).resize((img.size), Image.LANCZOS)152 heatmap = np.array(heatmap)153 154 # Create figure to plot the image and heatmap155 fig, ax = plt.subplots(1, 2, figsize=(10, 5))156 ax[0].imshow(img)157 ax[1].imshow(img)158 ax[1].imshow(heatmap, cmap='jet', alpha=alpha) # Overlay the heatmap159 plt.show()160 161# Load and preprocess the image162def preprocess_image(image_path):163 img = Image.open(image_path)164 img = img.resize((224, 224)) # Resize to match the input shape of the model165 img = np.array(img) / 255.0 # Normalize pixel values between 0 and 1166 img = np.expand_dims(img, axis=0) # Add batch dimension167 return img168 169# Path to the image170img_path = 'archive/dataset/covid/01E392EE-69F9-4E33-BFCE-E5C968654078.jpeg'171 172# Preprocess the image173img_array = preprocess_image(img_path)174 175# Get the heatmap176heatmap = make_gradcam_heatmap(img_array, model, 'conv5_block3_out') # Replace with your last conv layer's name177 178# Display the original image with the Grad-CAM heatmap overlay179display_gradcam(img_path, heatmap)180 181#Evaluation182 183# Evaluate model on validation data184test_loss, test_acc = model.evaluate(val_data, verbose=2)185print(f'Test Accuracy: {test_acc:.2f}')186 187 188# UI for the model189 190 191import gradio as gr192import numpy as np193from PIL import Image194import tensorflow as tf195from tensorflow.keras.models import Model196import matplotlib.pyplot as plt197import cv2 # For color mapping the heatmap198 199# Define the Grad-CAM function200def make_gradcam_heatmap(img_array, model, last_conv_layer_name):201 grad_model = Model([model.inputs], [model.get_layer(last_conv_layer_name).output, model.output])202 with tf.GradientTape() as tape:203 conv_outputs, predictions = grad_model(img_array)204 loss = predictions[:, 0] # For binary classification205 grads = tape.gradient(loss, conv_outputs)206 pooled_grads = tf.reduce_mean(grads, axis=(0, 1, 2))207 conv_outputs = conv_outputs[0]208 heatmap = tf.reduce_mean(tf.multiply(pooled_grads, conv_outputs), axis=-1)209 heatmap = np.maximum(heatmap, 0) # ReLU activation to make it non-negative210 heatmap = heatmap / np.max(heatmap) # Normalize between 0 and 1211 return heatmap212 213# Function to overlay the heatmap on the original image214def apply_heatmap_to_image(img, heatmap):215 # Resize heatmap to match image size216 heatmap = cv2.resize(heatmap, (img.size[0], img.size[1]))217 218 # Convert heatmap to RGB (apply 'jet' colormap)219 heatmap_colored = cv2.applyColorMap(np.uint8(255 * heatmap), cv2.COLORMAP_JET)220 221 # Convert to RGB mode (since OpenCV uses BGR)222 heatmap_colored = cv2.cvtColor(heatmap_colored, cv2.COLOR_BGR2RGB)223 224 # Overlay the heatmap on the original image225 overlay = np.array(img) * 0.6 + heatmap_colored * 0.4226 overlay = np.clip(overlay, 0, 255).astype('uint8')227 return Image.fromarray(overlay)228 229# Define the prediction and explainability function230def predict_and_explain(img):231 img = Image.fromarray(img).resize((224, 224)) # Resize image for the model232 img_array = np.array(img) / 255.0 # Normalize pixel values233 img_array = np.expand_dims(img_array, axis=0) # Add batch dimension234 235 # Get the prediction236 prediction = model.predict(img_array)237 confidence = float(prediction[0][0])238 result = "COVID-19 Positive" if confidence > 0.5 else "Healthy"239 240 # Generate the Grad-CAM heatmap241 last_conv_layer_name = 'conv5_block3_out' # Update with the actual last convolution layer name242 heatmap = make_gradcam_heatmap(img_array, model, last_conv_layer_name)243 244 # Apply heatmap on the image245 heatmap_img = apply_heatmap_to_image(img, heatmap)246 247 # Display confidence and heatmap248 confidence_text = f"Confidence: {confidence:.2f}"249 return result, confidence_text, heatmap_img250 251# Gradio interface252def create_interface():253 gr_interface = gr.Interface(254 fn=predict_and_explain,255 inputs=gr.Image(type="numpy"),256 outputs=[gr.Textbox(label="Prediction"), gr.Textbox(label="Confidence"), gr.Image(label="Heatmap")],257 title="COVID-19 X-ray Classification with Explainability",258 description="Upload an X-ray image to predict if the patient has COVID-19, see the confidence score, and view the Grad-CAM heatmap."259 )260 return gr_interface261 262# Launch the interface263gr_interface = create_interface()264gr_interface.launch()