Upload 3 files
Browse filesFiles changed (3)
- README.md+49-14
- app.pyAdded+132
- requirements.txtAdded+7
1234567891011121314---title: XAIemoji: ๐colorFrom: pinkcolorTo: indigosdk: gradiosdk_version: 5.49.1app_file: app.pypinned: falselicense: mitshort_description: image analyzer---Check out the configuration reference at https://huggingface.co/docs/hub/spaces-config-reference12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849# XAI Image ClassifierAn explainable image classification web app using ResNet18 fine-tuned on CIFAR-10, with Grad-CAM visualizations for interpretability.## Features- **Image Classification**: Classifies images into 10 categories (airplane, automobile, bird, cat, deer, dog, frog, horse, ship, truck).- **Explainability**: Uses Grad-CAM to show which parts of the image influenced the prediction.- **Interactive UI**: Built with Gradio for easy web-based interaction.## Installation1. Clone the repository:```bashgit clone https://github.com/your-username/xai-image-classifier.gitcd xai-image-classifier```2. Install dependencies:```bashpip install -r requirements.txt```3. Download the model file `xai_resnet18.pth` and place it in the `model/` directory.## UsageRun the app:```bashpython app.py```Open the provided URL in your browser, upload an image, and click "Analyze Image" to get predictions and explanations.## Requirements- Python 3.7+- CUDA-compatible GPU (optional, for faster inference)- Dependencies listed in `requirements.txt`## Model Details- **Architecture**: ResNet18 with modified fully-connected layer.- **Training Data**: CIFAR-10 dataset (60,000 images).- **Pre-trained Weights**: None (trained from scratch).## LicenseMIT License
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132import torch
import torch.nn as nn
from torchvision import models, transforms
from PIL import Image
import gradio as gr
from captum.attr import LayerGradCam
import numpy as np
import matplotlib.pyplot as plt
from io import BytesIO
# Configuration
DEVICE = torch.device("cuda" if torch.cuda.is_available() else "cpu")
CLASS_NAMES = ['airplane', 'automobile', 'bird', 'cat', 'deer',
'dog', 'frog', 'horse', 'ship', 'truck']
# Load model
def load_model():
model = models.resnet18(weights=None)
model.fc = nn.Linear(512, 10)
checkpoint = torch.load('model/xai_resnet18.pth', map_location=DEVICE)
model.load_state_dict(checkpoint['model_state_dict'])
model = model.to(DEVICE)
model.eval()
return model
model = load_model()
target_layer = model.layer4[1].conv2
gradcam = LayerGradCam(model, target_layer)
# Image preprocessing
transform = transforms.Compose([
transforms.Resize((224, 224)),
transforms.ToTensor(),
transforms.Normalize((0.5, 0.5, 0.5), (0.5, 0.5, 0.5))
])
# Prediction function
def predict_and_explain(image):
if image is None:
return "Please upload an image", None
# Preprocess
img_tensor = transform(image).unsqueeze(0).to(DEVICE)
# Predict
with torch.no_grad():
output = model(img_tensor)
probabilities = torch.softmax(output, dim=1)
pred_class = probabilities.argmax(1).item()
confidence = probabilities[0][pred_class].item()
# Generate Grad-CAM
attributions = gradcam.attribute(img_tensor, target=pred_class)
attr_np = attributions.squeeze().cpu().detach().numpy()
attr_np = (attr_np - attr_np.min()) / (attr_np.max() - attr_np.min() + 1e-8)
# Create visualization
fig, axes = plt.subplots(1, 3, figsize=(15, 5))
axes[0].imshow(image)
axes[0].set_title("Original Image", fontsize=14, fontweight='bold')
axes[0].axis('off')
im = axes[1].imshow(attr_np, cmap='jet')
axes[1].set_title("Grad-CAM Heatmap", fontsize=14, fontweight='bold')
axes[1].axis('off')
plt.colorbar(im, ax=axes[1], fraction=0.046)
axes[2].imshow(image)
axes[2].imshow(attr_np, cmap='jet', alpha=0.5)
axes[2].set_title(f"Overlay\nPrediction: {CLASS_NAMES[pred_class]}",
fontsize=14, fontweight='bold')
axes[2].axis('off')
plt.tight_layout()
buf = BytesIO()
plt.savefig(buf, format='png', dpi=100, bbox_inches='tight')
buf.seek(0)
result_image = Image.open(buf)
plt.close(fig)
# Prediction text
prediction_text = f"**Prediction:** {CLASS_NAMES[pred_class]}\n\n"
prediction_text += f"**Confidence:** {confidence*100:.2f}%\n\n"
prediction_text += "**Top 3 Predictions:**\n"
top3_probs, top3_indices = torch.topk(probabilities[0], 3)
for prob, idx in zip(top3_probs, top3_indices):
prediction_text += f"- {CLASS_NAMES[idx]}: {prob.item()*100:.2f}%\n"
return prediction_text, result_image
# Gradio Interface
with gr.Blocks(title="๐ Explainable Image Classifier", theme=gr.themes.Soft()) as demo:
gr.Markdown("""
# ๐ Explainable Image Classifier with Grad-CAM
Upload an image and see:
- **What** the AI predicts (classification)
- **Why** it made that decision (Grad-CAM visualization)
**Supported categories:** airplane, car, bird, cat, deer, dog, frog, horse, ship, truck
""")
with gr.Row():
with gr.Column():
input_image = gr.Image(type="pil", label="Upload Image")
predict_btn = gr.Button("๐ Analyze Image", variant="primary", size="lg")
with gr.Column():
output_text = gr.Markdown(label="Prediction Results")
output_image = gr.Image(label="Grad-CAM Visualization", type="pil")
predict_btn.click(
fn=predict_and_explain,
inputs=input_image,
outputs=[output_text, output_image]
)
gr.Markdown("""
---
### ๐ง About This Model
- **Architecture:** ResNet18 (transfer learning)
- **Training Data:** CIFAR-10 (60,000 images)
- **Explainability:** Grad-CAM visualization
""")
if __name__ == "__main__":
demo.launch()1234567torch
torchvision
gradio
captum
Pillow
matplotlib
numpyREADME.md+49-14
app.pyAdded+132
requirements.txtAdded+7