strelizi commited on
Commit
bd8852c
Β·
verified Β·
1 Parent(s): f06cb0c

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +131 -131
app.py CHANGED
@@ -1,132 +1,132 @@
1
- import torch
2
- import torch.nn as nn
3
- from torchvision import models, transforms
4
- from PIL import Image
5
- import gradio as gr
6
- from captum.attr import LayerGradCam
7
- import numpy as np
8
- import matplotlib.pyplot as plt
9
- from io import BytesIO
10
-
11
- # Configuration
12
- DEVICE = torch.device("cuda" if torch.cuda.is_available() else "cpu")
13
- CLASS_NAMES = ['airplane', 'automobile', 'bird', 'cat', 'deer',
14
- 'dog', 'frog', 'horse', 'ship', 'truck']
15
-
16
- # Load model
17
- def load_model():
18
- model = models.resnet18(weights=None)
19
- model.fc = nn.Linear(512, 10)
20
-
21
- checkpoint = torch.load('model/xai_resnet18.pth', map_location=DEVICE)
22
- model.load_state_dict(checkpoint['model_state_dict'])
23
- model = model.to(DEVICE)
24
- model.eval()
25
-
26
- return model
27
-
28
- model = load_model()
29
- target_layer = model.layer4[1].conv2
30
- gradcam = LayerGradCam(model, target_layer)
31
-
32
- # Image preprocessing
33
- transform = transforms.Compose([
34
- transforms.Resize((224, 224)),
35
- transforms.ToTensor(),
36
- transforms.Normalize((0.5, 0.5, 0.5), (0.5, 0.5, 0.5))
37
- ])
38
-
39
- # Prediction function
40
- def predict_and_explain(image):
41
- if image is None:
42
- return "Please upload an image", None
43
-
44
- # Preprocess
45
- img_tensor = transform(image).unsqueeze(0).to(DEVICE)
46
-
47
- # Predict
48
- with torch.no_grad():
49
- output = model(img_tensor)
50
- probabilities = torch.softmax(output, dim=1)
51
- pred_class = probabilities.argmax(1).item()
52
- confidence = probabilities[0][pred_class].item()
53
-
54
- # Generate Grad-CAM
55
- attributions = gradcam.attribute(img_tensor, target=pred_class)
56
- attr_np = attributions.squeeze().cpu().detach().numpy()
57
- attr_np = (attr_np - attr_np.min()) / (attr_np.max() - attr_np.min() + 1e-8)
58
-
59
- # Create visualization
60
- fig, axes = plt.subplots(1, 3, figsize=(15, 5))
61
-
62
- axes[0].imshow(image)
63
- axes[0].set_title("Original Image", fontsize=14, fontweight='bold')
64
- axes[0].axis('off')
65
-
66
- im = axes[1].imshow(attr_np, cmap='jet')
67
- axes[1].set_title("Grad-CAM Heatmap", fontsize=14, fontweight='bold')
68
- axes[1].axis('off')
69
- plt.colorbar(im, ax=axes[1], fraction=0.046)
70
-
71
- axes[2].imshow(image)
72
- axes[2].imshow(attr_np, cmap='jet', alpha=0.5)
73
- axes[2].set_title(f"Overlay\nPrediction: {CLASS_NAMES[pred_class]}",
74
- fontsize=14, fontweight='bold')
75
- axes[2].axis('off')
76
-
77
- plt.tight_layout()
78
-
79
- buf = BytesIO()
80
- plt.savefig(buf, format='png', dpi=100, bbox_inches='tight')
81
- buf.seek(0)
82
- result_image = Image.open(buf)
83
- plt.close(fig)
84
-
85
- # Prediction text
86
- prediction_text = f"**Prediction:** {CLASS_NAMES[pred_class]}\n\n"
87
- prediction_text += f"**Confidence:** {confidence*100:.2f}%\n\n"
88
- prediction_text += "**Top 3 Predictions:**\n"
89
-
90
- top3_probs, top3_indices = torch.topk(probabilities[0], 3)
91
- for prob, idx in zip(top3_probs, top3_indices):
92
- prediction_text += f"- {CLASS_NAMES[idx]}: {prob.item()*100:.2f}%\n"
93
-
94
- return prediction_text, result_image
95
-
96
- # Gradio Interface
97
- with gr.Blocks(title="πŸ” Explainable Image Classifier", theme=gr.themes.Soft()) as demo:
98
- gr.Markdown("""
99
- # πŸ” Explainable Image Classifier with Grad-CAM
100
-
101
- Upload an image and see:
102
- - **What** the AI predicts (classification)
103
- - **Why** it made that decision (Grad-CAM visualization)
104
-
105
- **Supported categories:** airplane, car, bird, cat, deer, dog, frog, horse, ship, truck
106
- """)
107
-
108
- with gr.Row():
109
- with gr.Column():
110
- input_image = gr.Image(type="pil", label="Upload Image")
111
- predict_btn = gr.Button("πŸ” Analyze Image", variant="primary", size="lg")
112
-
113
- with gr.Column():
114
- output_text = gr.Markdown(label="Prediction Results")
115
- output_image = gr.Image(label="Grad-CAM Visualization", type="pil")
116
-
117
- predict_btn.click(
118
- fn=predict_and_explain,
119
- inputs=input_image,
120
- outputs=[output_text, output_image]
121
- )
122
-
123
- gr.Markdown("""
124
- ---
125
- ### 🧠 About This Model
126
- - **Architecture:** ResNet18 (transfer learning)
127
- - **Training Data:** CIFAR-10 (60,000 images)
128
- - **Explainability:** Grad-CAM visualization
129
- """)
130
-
131
- if __name__ == "__main__":
132
  demo.launch()
 
1
+ import torch
2
+ import torch.nn as nn
3
+ from torchvision import models, transforms
4
+ from PIL import Image
5
+ import gradio as gr
6
+ from captum.attr import LayerGradCam
7
+ import numpy as np
8
+ import matplotlib.pyplot as plt
9
+ from io import BytesIO
10
+
11
+ # Configuration
12
+ DEVICE = torch.device("cuda" if torch.cuda.is_available() else "cpu")
13
+ CLASS_NAMES = ['airplane', 'automobile', 'bird', 'cat', 'deer',
14
+ 'dog', 'frog', 'horse', 'ship', 'truck']
15
+
16
+ # Load model
17
+ def load_model():
18
+ model = models.resnet18(weights=None)
19
+ model.fc = nn.Linear(512, 10)
20
+
21
+ checkpoint = torch.load('xai_resnet18.pth', map_location=DEVICE)
22
+ model.load_state_dict(checkpoint['model_state_dict'])
23
+ model = model.to(DEVICE)
24
+ model.eval()
25
+
26
+ return model
27
+
28
+ model = load_model()
29
+ target_layer = model.layer4[1].conv2
30
+ gradcam = LayerGradCam(model, target_layer)
31
+
32
+ # Image preprocessing
33
+ transform = transforms.Compose([
34
+ transforms.Resize((224, 224)),
35
+ transforms.ToTensor(),
36
+ transforms.Normalize((0.5, 0.5, 0.5), (0.5, 0.5, 0.5))
37
+ ])
38
+
39
+ # Prediction function
40
+ def predict_and_explain(image):
41
+ if image is None:
42
+ return "Please upload an image", None
43
+
44
+ # Preprocess
45
+ img_tensor = transform(image).unsqueeze(0).to(DEVICE)
46
+
47
+ # Predict
48
+ with torch.no_grad():
49
+ output = model(img_tensor)
50
+ probabilities = torch.softmax(output, dim=1)
51
+ pred_class = probabilities.argmax(1).item()
52
+ confidence = probabilities[0][pred_class].item()
53
+
54
+ # Generate Grad-CAM
55
+ attributions = gradcam.attribute(img_tensor, target=pred_class)
56
+ attr_np = attributions.squeeze().cpu().detach().numpy()
57
+ attr_np = (attr_np - attr_np.min()) / (attr_np.max() - attr_np.min() + 1e-8)
58
+
59
+ # Create visualization
60
+ fig, axes = plt.subplots(1, 3, figsize=(15, 5))
61
+
62
+ axes[0].imshow(image)
63
+ axes[0].set_title("Original Image", fontsize=14, fontweight='bold')
64
+ axes[0].axis('off')
65
+
66
+ im = axes[1].imshow(attr_np, cmap='jet')
67
+ axes[1].set_title("Grad-CAM Heatmap", fontsize=14, fontweight='bold')
68
+ axes[1].axis('off')
69
+ plt.colorbar(im, ax=axes[1], fraction=0.046)
70
+
71
+ axes[2].imshow(image)
72
+ axes[2].imshow(attr_np, cmap='jet', alpha=0.5)
73
+ axes[2].set_title(f"Overlay\nPrediction: {CLASS_NAMES[pred_class]}",
74
+ fontsize=14, fontweight='bold')
75
+ axes[2].axis('off')
76
+
77
+ plt.tight_layout()
78
+
79
+ buf = BytesIO()
80
+ plt.savefig(buf, format='png', dpi=100, bbox_inches='tight')
81
+ buf.seek(0)
82
+ result_image = Image.open(buf)
83
+ plt.close(fig)
84
+
85
+ # Prediction text
86
+ prediction_text = f"**Prediction:** {CLASS_NAMES[pred_class]}\n\n"
87
+ prediction_text += f"**Confidence:** {confidence*100:.2f}%\n\n"
88
+ prediction_text += "**Top 3 Predictions:**\n"
89
+
90
+ top3_probs, top3_indices = torch.topk(probabilities[0], 3)
91
+ for prob, idx in zip(top3_probs, top3_indices):
92
+ prediction_text += f"- {CLASS_NAMES[idx]}: {prob.item()*100:.2f}%\n"
93
+
94
+ return prediction_text, result_image
95
+
96
+ # Gradio Interface
97
+ with gr.Blocks(title="πŸ” Explainable Image Classifier", theme=gr.themes.Soft()) as demo:
98
+ gr.Markdown("""
99
+ # πŸ” Explainable Image Classifier with Grad-CAM
100
+
101
+ Upload an image and see:
102
+ - **What** the AI predicts (classification)
103
+ - **Why** it made that decision (Grad-CAM visualization)
104
+
105
+ **Supported categories:** airplane, car, bird, cat, deer, dog, frog, horse, ship, truck
106
+ """)
107
+
108
+ with gr.Row():
109
+ with gr.Column():
110
+ input_image = gr.Image(type="pil", label="Upload Image")
111
+ predict_btn = gr.Button("πŸ” Analyze Image", variant="primary", size="lg")
112
+
113
+ with gr.Column():
114
+ output_text = gr.Markdown(label="Prediction Results")
115
+ output_image = gr.Image(label="Grad-CAM Visualization", type="pil")
116
+
117
+ predict_btn.click(
118
+ fn=predict_and_explain,
119
+ inputs=input_image,
120
+ outputs=[output_text, output_image]
121
+ )
122
+
123
+ gr.Markdown("""
124
+ ---
125
+ ### 🧠 About This Model
126
+ - **Architecture:** ResNet18 (transfer learning)
127
+ - **Training Data:** CIFAR-10 (60,000 images)
128
+ - **Explainability:** Grad-CAM visualization
129
+ """)
130
+
131
+ if __name__ == "__main__":
132
  demo.launch()