strelizi commited on
Commit
575e09c
Β·
verified Β·
1 Parent(s): 8675e33

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +14 -37
app.py CHANGED
@@ -11,28 +11,18 @@ import urllib.request
11
  from torch.nn.functional import interpolate
12
  import warnings
13
  warnings.filterwarnings('ignore')
14
-
15
- # Performance optimizations
16
- torch.backends.cudnn.benchmark = True # Auto-tune for best performance
17
- torch.set_float32_matmul_precision('high') # Use TF32 on Ampere GPUs
18
-
19
  DEVICE = torch.device("cuda" if torch.cuda.is_available() else "cpu")
20
 
21
  @torch.no_grad()
22
  def load_model_and_labels():
23
- """Load ResNet152 model with optimizations"""
24
  print("πŸš€ Loading ResNet152 model...")
25
 
26
- # Load model
27
  model = models.resnet152(weights='IMAGENET1K_V2')
28
  model.eval().to(DEVICE)
29
 
30
- # Optimize model for inference
31
- if DEVICE.type == 'cuda':
32
- model = model.half() # FP16 precision for 2x speedup on GPU
33
- print("βœ… Model optimized with FP16 precision")
34
-
35
- # Load ImageNet labels (cached)
36
  url = "https://raw.githubusercontent.com/pytorch/hub/master/imagenet_classes.txt"
37
  response = urllib.request.urlopen(url)
38
  labels = [line.decode('utf-8').strip() for line in response.readlines()]
@@ -46,9 +36,9 @@ model, IMAGENET_LABELS = load_model_and_labels()
46
  target_layer = model.layer4[-1]
47
  gradcam = LayerGradCam(model, target_layer)
48
 
49
- # Optimized transform with tensor output
50
  transform = transforms.Compose([
51
- transforms.Resize((224, 224), interpolation=transforms.InterpolationMode.BILINEAR),
52
  transforms.ToTensor(),
53
  transforms.Normalize(mean=[0.485, 0.456, 0.406], std=[0.229, 0.224, 0.225])
54
  ])
@@ -59,34 +49,25 @@ def predict_and_explain(image):
59
  return "Please upload an image", None, None
60
 
61
  try:
62
- # Prepare input with optimal dtype
63
  img_tensor = transform(image).unsqueeze(0).to(DEVICE)
64
- if DEVICE.type == 'cuda':
65
- img_tensor = img_tensor.half() # Match model precision
66
 
67
  with torch.no_grad():
68
- # Fast inference
69
  output = model(img_tensor)
70
- if DEVICE.type == 'cuda':
71
- output = output.float() # Convert back for softmax
72
  probabilities = torch.softmax(output, dim=1)
73
 
74
  top10_prob, top10_idx = torch.topk(probabilities, 10)
75
  pred_class = top10_idx[0][0].item()
76
  confidence = top10_prob[0][0].item()
77
 
78
- # Generate Grad-CAM (only for visualization, not time-critical)
79
- if DEVICE.type == 'cuda':
80
- grad_tensor = img_tensor.float() # Grad-CAM needs FP32
81
- else:
82
- grad_tensor = img_tensor
83
-
84
- attributions = gradcam.attribute(grad_tensor, target=pred_class)
85
  attr_resized = interpolate(attributions, size=(224, 224), mode='bilinear', align_corners=False)
86
  attr_np = attr_resized.squeeze().cpu().detach().numpy()
87
  attr_np = (attr_np - attr_np.min()) / (attr_np.max() - attr_np.min() + 1e-8)
88
 
89
- # Efficient visualization
90
  fig = plt.figure(figsize=(24, 14))
91
  fig.patch.set_facecolor('#0a0a0a')
92
 
@@ -194,9 +175,6 @@ def predict_and_explain(image):
194
  </div>"""
195
  top5_html += "</div>"
196
 
197
- # Performance info
198
- precision_info = "FP16 (GPU Accelerated)" if DEVICE.type == 'cuda' else "FP32 (CPU)"
199
-
200
  prediction_text = f"""
201
  <div class="result-card">
202
  <div class="pred-header">
@@ -204,7 +182,7 @@ def predict_and_explain(image):
204
  <div class="badge badge-{badge}">{badge_icon} {badge_text}</div>
205
  </div>
206
  <div class="conf-score">{confidence*100:.2f}%</div>
207
- <div class="model-tag">πŸ”¬ ResNet152 β€’ {precision_info} β€’ 82.3% Accuracy</div>
208
  <div class="divider"></div>
209
  {top5_html}
210
  </div>"""
@@ -288,7 +266,7 @@ with gr.Blocks(css=custom_css, theme=gr.themes.Base(), title="XAI Image Classifi
288
  <div class="hero-header">
289
  <h1>XAI Image Classifier</h1>
290
  <p class="subtitle">ResNet152 with Grad-CAM Explainability</p>
291
- <div class="model-tag">⚑ Optimized for Maximum Speed & Accuracy</div>
292
  </div>
293
  ''')
294
 
@@ -301,7 +279,7 @@ with gr.Blocks(css=custom_css, theme=gr.themes.Base(), title="XAI Image Classifi
301
  clear_btn = gr.ClearButton([input_image], value="πŸ—‘οΈ Clear", size="lg", scale=1)
302
 
303
  with gr.Column(scale=1, elem_classes="results-panel"):
304
- output_text = gr.HTML('<div class="placeholder"><strong>πŸ‘‹ Welcome to XAI Classifier!</strong><br><br>This classifier uses ResNet152:<br>β€’ 82.3% ImageNet Top-1 Accuracy<br>β€’ FP16 GPU Acceleration (2x faster)<br>β€’ Grad-CAM Visual Explainability<br>β€’ 1000 Object Categories<br><br>Upload an image to see the magic! ✨</div>')
305
 
306
  with gr.Column(elem_classes="viz-section"):
307
  gr.HTML("<div class='section-label'>🎯 Visual Explainability Analysis</div>")
@@ -314,5 +292,4 @@ with gr.Blocks(css=custom_css, theme=gr.themes.Base(), title="XAI Image Classifi
314
  predict_btn.click(fn=predict_and_explain, inputs=[input_image], outputs=[output_text, output_image, detailed_heatmap])
315
 
316
  if __name__ == "__main__":
317
- demo.queue()
318
- demo.launch(share=True)
 
11
  from torch.nn.functional import interpolate
12
  import warnings
13
  warnings.filterwarnings('ignore')
 
 
 
 
 
14
  DEVICE = torch.device("cuda" if torch.cuda.is_available() else "cpu")
15
 
16
  @torch.no_grad()
17
  def load_model_and_labels():
18
+ """Load ResNet152 model for maximum accuracy"""
19
  print("πŸš€ Loading ResNet152 model...")
20
 
21
+ # ResNet152 (Best accuracy in ResNet family)
22
  model = models.resnet152(weights='IMAGENET1K_V2')
23
  model.eval().to(DEVICE)
24
 
25
+ # Load ImageNet labels
 
 
 
 
 
26
  url = "https://raw.githubusercontent.com/pytorch/hub/master/imagenet_classes.txt"
27
  response = urllib.request.urlopen(url)
28
  labels = [line.decode('utf-8').strip() for line in response.readlines()]
 
36
  target_layer = model.layer4[-1]
37
  gradcam = LayerGradCam(model, target_layer)
38
 
39
+ # Transform for ResNet152
40
  transform = transforms.Compose([
41
+ transforms.Resize((224, 224)),
42
  transforms.ToTensor(),
43
  transforms.Normalize(mean=[0.485, 0.456, 0.406], std=[0.229, 0.224, 0.225])
44
  ])
 
49
  return "Please upload an image", None, None
50
 
51
  try:
52
+ # Prepare input
53
  img_tensor = transform(image).unsqueeze(0).to(DEVICE)
 
 
54
 
55
  with torch.no_grad():
56
+ # ResNet152 prediction
57
  output = model(img_tensor)
 
 
58
  probabilities = torch.softmax(output, dim=1)
59
 
60
  top10_prob, top10_idx = torch.topk(probabilities, 10)
61
  pred_class = top10_idx[0][0].item()
62
  confidence = top10_prob[0][0].item()
63
 
64
+ # Generate Grad-CAM
65
+ attributions = gradcam.attribute(img_tensor, target=pred_class)
 
 
 
 
 
66
  attr_resized = interpolate(attributions, size=(224, 224), mode='bilinear', align_corners=False)
67
  attr_np = attr_resized.squeeze().cpu().detach().numpy()
68
  attr_np = (attr_np - attr_np.min()) / (attr_np.max() - attr_np.min() + 1e-8)
69
 
70
+ # Main visualization
71
  fig = plt.figure(figsize=(24, 14))
72
  fig.patch.set_facecolor('#0a0a0a')
73
 
 
175
  </div>"""
176
  top5_html += "</div>"
177
 
 
 
 
178
  prediction_text = f"""
179
  <div class="result-card">
180
  <div class="pred-header">
 
182
  <div class="badge badge-{badge}">{badge_icon} {badge_text}</div>
183
  </div>
184
  <div class="conf-score">{confidence*100:.2f}%</div>
185
+ <div class="model-tag">πŸ”¬ ResNet152 Architecture (82.3% ImageNet Accuracy)</div>
186
  <div class="divider"></div>
187
  {top5_html}
188
  </div>"""
 
266
  <div class="hero-header">
267
  <h1>XAI Image Classifier</h1>
268
  <p class="subtitle">ResNet152 with Grad-CAM Explainability</p>
269
+ <div class="model-tag">⚑ Maximum Accuracy Production Version</div>
270
  </div>
271
  ''')
272
 
 
279
  clear_btn = gr.ClearButton([input_image], value="πŸ—‘οΈ Clear", size="lg", scale=1)
280
 
281
  with gr.Column(scale=1, elem_classes="results-panel"):
282
+ output_text = gr.HTML('<div class="placeholder"><strong>πŸ‘‹ Welcome to XAI Classifier!</strong><br><br>This classifier uses ResNet152:<br>β€’ 82.3% ImageNet Top-1 Accuracy<br>β€’ Grad-CAM Visual Explainability<br>β€’ 1000 Object Categories<br><br>Upload an image to see the magic! ✨</div>')
283
 
284
  with gr.Column(elem_classes="viz-section"):
285
  gr.HTML("<div class='section-label'>🎯 Visual Explainability Analysis</div>")
 
292
  predict_btn.click(fn=predict_and_explain, inputs=[input_image], outputs=[output_text, output_image, detailed_heatmap])
293
 
294
  if __name__ == "__main__":
295
+ demo.launch(share=False, show_error=True)