sparsh007/ExplanableAI
1
1import os2import keras3from keras.applications import inception_v3 as inc_net4from keras.preprocessing import image5from skimage.segmentation import mark_boundaries6import numpy as np7import matplotlib.pyplot as plt8import gradio as gr9from lime import lime_image10 11# Load the pre-trained InceptionV3 model12inet_model = inc_net.InceptionV3()13 14def transform_img_fn(img_path):15 """Preprocess image for InceptionV3"""16 img = image.load_img(img_path, target_size=(299, 299))17 x = image.img_to_array(img)18 x = np.expand_dims(x, axis=0)19 return inc_net.preprocess_input(x)20 21def explain_image(img_path):22 """Generate LIME explanation and visualization"""23 # Preprocess image24 processed_img = transform_img_fn(img_path)25 26 # Create LIME explainer27 explainer = lime_image.LimeImageExplainer()28 29 # Generate explanation30 explanation = explainer.explain_instance(31 processed_img[0].astype('double'), 32 inet_model.predict, 33 top_labels=5, 34 hide_color=0, 35 num_samples=100036 )37 38 # Get image and mask39 temp, mask = explanation.get_image_and_mask(40 explanation.top_labels[0],41 positive_only=False,42 num_features=10,43 hide_rest=False44 )45 46 # Get top 5 predictions47 predictions = inet_model.predict(processed_img)48 top_5_indices = np.argsort(predictions[0])[-5:][::-1]49 top_5_labels = [inc_net.decode_predictions(predictions, top=5)[0][i][1] for i in range(5)]50 top_5_probs = [inc_net.decode_predictions(predictions, top=5)[0][i][2] for i in range(5)]51 52 # Create visualization53 fig, ax = plt.subplots(figsize=(6, 6))54 55 # Explanation visualization56 ax.imshow(mark_boundaries(temp / 2 + 0.5, mask))57 ax.set_title('Pros (Green) vs Cons (Red)')58 ax.axis('off')59 60 plt.tight_layout()61 62 # Create a string for the top 5 predictions63 predictions_str = "Top 5 Predictions:\n"64 for i, (label, prob) in enumerate(zip(top_5_labels, top_5_probs)):65 predictions_str += f"{i+1}. {label}: {prob:.4f}\n"66 67 # Generate heatmap68 ind = explanation.top_labels[0]69 dict_heatmap = dict(explanation.local_exp[ind])70 heatmap = np.vectorize(dict_heatmap.get)(explanation.segments)71 72 # Plot heatmap73 fig_heatmap, ax_heatmap = plt.subplots(figsize=(6, 6))74 heatmap_plot = ax_heatmap.imshow(heatmap, cmap='RdBu', vmin=-heatmap.max(), vmax=heatmap.max())75 plt.colorbar(heatmap_plot, ax=ax_heatmap)76 ax_heatmap.set_title('Heatmap Explanation')77 ax_heatmap.axis('off')78 79 plt.tight_layout()80 81 return fig, predictions_str, fig_heatmap82 83# Create Gradio interface84demo = gr.Interface(85 fn=explain_image,86 inputs=gr.Image(type="filepath", label="Input Image"),87 outputs=[88 gr.Plot(label="Explanation"),89 gr.Textbox(label="Top 5 Predictions"),90 gr.Plot(label="Heatmap Explanation")91 ],92 title="LIME Image Classifier Explainer",93 description="Upload an image to see which areas positively (green) and negatively (red) influence the classification, the top 5 predictions, and a heatmap explanation."94)95 96# Launch the app97if __name__ == "__main__":98 demo.launch()99 