JohanBeytell commited on
Commit
c5afbde
·
verified ·
1 Parent(s): b2c1cc7

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +64 -35
app.py CHANGED
@@ -250,40 +250,42 @@ def create_detailed_analysis(image):
250
  # Create visualizations
251
  visualizations = create_heatmap_visualizations(heatmap, img_array.shape)
252
 
253
- # Create comprehensive plot
254
- fig = plt.figure(figsize=(20, 12))
255
  color = 'green' if predicted_class == 0 else 'red'
256
  fig.suptitle(f'Comprehensive Grad-CAM++ Analysis\nPredicted: {predicted_label.upper()} ({confidence:.2%})',
257
  fontsize=16, fontweight='bold', color=color)
258
 
 
259
  # Original image
260
- plt.subplot(3, 5, 1)
261
  plt.imshow(processed_img)
262
  plt.title("Original Image", fontsize=12, fontweight='bold')
263
  plt.axis('off')
264
 
265
- # Different heatmap visualizations
266
  viz_names = ['jet', 'hot', 'plasma', 'viridis']
267
  for i, viz_name in enumerate(viz_names):
268
  viz = visualizations[viz_name]
269
- plt.subplot(3, 5, i + 2)
270
  im = plt.imshow(viz['heatmap'], cmap=viz['colormap'])
271
  plt.title(viz['title'], fontsize=12)
272
  plt.axis('off')
273
  plt.colorbar(im, fraction=0.046, pad=0.04)
274
 
 
275
  # More heatmap styles
276
  viz_names2 = ['inferno', 'cool']
277
  for i, viz_name in enumerate(viz_names2):
278
  viz = visualizations[viz_name]
279
- plt.subplot(3, 5, i + 6)
280
  im = plt.imshow(viz['heatmap'], cmap=viz['colormap'])
281
  plt.title(viz['title'], fontsize=12)
282
  plt.axis('off')
283
  plt.colorbar(im, fraction=0.046, pad=0.04)
284
 
285
  # Attention profile
286
- plt.subplot(3, 5, 8)
287
  attention_profile = np.mean(heatmap, axis=1)
288
  plt.plot(attention_profile, range(len(attention_profile)), 'b-', linewidth=2)
289
  plt.title('Vertical Attention Profile', fontsize=12)
@@ -293,7 +295,7 @@ def create_detailed_analysis(image):
293
  plt.grid(True, alpha=0.3)
294
 
295
  # Statistics
296
- plt.subplot(3, 5, 9)
297
  stats_text = f"""Heatmap Statistics:
298
  Mean: {np.mean(heatmap):.3f}
299
  Std: {np.std(heatmap):.3f}
@@ -309,14 +311,40 @@ Class: {predicted_label}"""
309
  verticalalignment='center', bbox=dict(boxstyle="round,pad=0.3", facecolor="lightblue"))
310
  plt.axis('off')
311
 
312
- # Superimposed views
313
- superimposed_colormaps = ['jet', 'hot', 'plasma', 'viridis', 'inferno']
314
- for i, cmap_name in enumerate(superimposed_colormaps):
 
 
 
 
 
 
315
  superimposed_img = superimpose_gradcam_enhanced(img_array, heatmap, colormap=cmap_name, alpha=0.4)
316
- plt.subplot(3, 5, i + 11)
317
  plt.imshow(superimposed_img)
318
  plt.title(f'Superimposed ({cmap_name.title()})', fontsize=12)
319
  plt.axis('off')
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
320
 
321
  plt.tight_layout()
322
  plt.subplots_adjust(top=0.92)
@@ -351,13 +379,13 @@ def create_quick_analysis(image):
351
  if heatmap is None:
352
  return "❌ Error generating heatmap."
353
 
354
- # Create quick visualization
355
- fig, axes = plt.subplots(2, 3, figsize=(15, 10))
356
  color = 'green' if predicted_class == 0 else 'red'
357
  fig.suptitle(f'Quick Grad-CAM++ Analysis | Predicted: {predicted_label.upper()} ({confidence:.2%})',
358
  fontsize=14, fontweight='bold', color=color)
359
 
360
- # Original image
361
  axes[0, 0].imshow(processed_img)
362
  axes[0, 0].set_title("Original Image")
363
  axes[0, 0].axis('off')
@@ -375,21 +403,22 @@ def create_quick_analysis(image):
375
  axes[0, 2].axis('off')
376
  plt.colorbar(im2, ax=axes[0, 2], fraction=0.046)
377
 
378
- # Superimposed views
379
- superimposed_jet = superimpose_gradcam_enhanced(img_array, heatmap, 'jet')
380
- axes[1, 0].imshow(superimposed_jet)
381
- axes[1, 0].set_title("Superimposed (Jet)")
382
- axes[1, 0].axis('off')
383
-
384
- superimposed_hot = superimpose_gradcam_enhanced(img_array, heatmap, 'hot')
385
- axes[1, 1].imshow(superimposed_hot)
386
- axes[1, 1].set_title("Superimposed (Hot)")
387
- axes[1, 1].axis('off')
388
-
389
- superimposed_viridis = superimpose_gradcam_enhanced(img_array, heatmap, 'viridis')
390
- axes[1, 2].imshow(superimposed_viridis)
391
- axes[1, 2].set_title("Superimposed (Viridis)")
392
- axes[1, 2].axis('off')
 
393
 
394
  plt.tight_layout()
395
 
@@ -510,22 +539,22 @@ def create_gradio_app():
510
  with gr.Column():
511
  gr.Markdown("""
512
  ### ⚡ Quick Analysis Features:
513
- - **2x3 Grid Layout** for rapid evaluation
514
  - **Original Image** with preprocessing
515
  - **Jet & Plasma Heatmaps** with colorbars
516
- - **3 Superimposed Views** (Jet, Hot, Viridis)
517
- - **Fast Processing** (~2-3 seconds)
518
  - **Perfect for screening** multiple images
519
  """)
520
 
521
  with gr.Column():
522
  gr.Markdown("""
523
  ### 🔬 Detailed Analysis Features:
524
- - **3x5 Grid Layout** for comprehensive analysis
525
  - **6 Heatmap Color Schemes** with individual colorbars
526
  - **Attention Profile Plot** showing vertical focus
527
  - **Statistical Analysis Panel** with quantitative metrics
528
- - **5 Enhanced Superimposed Views** with gamma correction
529
  - **Clinical-grade visualization** for detailed examination
530
  """)
531
 
 
250
  # Create visualizations
251
  visualizations = create_heatmap_visualizations(heatmap, img_array.shape)
252
 
253
+ # Create comprehensive plot with 4 rows to accommodate all visualizations
254
+ fig = plt.figure(figsize=(20, 16))
255
  color = 'green' if predicted_class == 0 else 'red'
256
  fig.suptitle(f'Comprehensive Grad-CAM++ Analysis\nPredicted: {predicted_label.upper()} ({confidence:.2%})',
257
  fontsize=16, fontweight='bold', color=color)
258
 
259
+ # Row 1: Original image and heatmaps
260
  # Original image
261
+ plt.subplot(4, 5, 1)
262
  plt.imshow(processed_img)
263
  plt.title("Original Image", fontsize=12, fontweight='bold')
264
  plt.axis('off')
265
 
266
+ # Different heatmap visualizations (4 in first row)
267
  viz_names = ['jet', 'hot', 'plasma', 'viridis']
268
  for i, viz_name in enumerate(viz_names):
269
  viz = visualizations[viz_name]
270
+ plt.subplot(4, 5, i + 2)
271
  im = plt.imshow(viz['heatmap'], cmap=viz['colormap'])
272
  plt.title(viz['title'], fontsize=12)
273
  plt.axis('off')
274
  plt.colorbar(im, fraction=0.046, pad=0.04)
275
 
276
+ # Row 2: Remaining heatmaps, attention profile, and statistics
277
  # More heatmap styles
278
  viz_names2 = ['inferno', 'cool']
279
  for i, viz_name in enumerate(viz_names2):
280
  viz = visualizations[viz_name]
281
+ plt.subplot(4, 5, i + 6)
282
  im = plt.imshow(viz['heatmap'], cmap=viz['colormap'])
283
  plt.title(viz['title'], fontsize=12)
284
  plt.axis('off')
285
  plt.colorbar(im, fraction=0.046, pad=0.04)
286
 
287
  # Attention profile
288
+ plt.subplot(4, 5, 8)
289
  attention_profile = np.mean(heatmap, axis=1)
290
  plt.plot(attention_profile, range(len(attention_profile)), 'b-', linewidth=2)
291
  plt.title('Vertical Attention Profile', fontsize=12)
 
295
  plt.grid(True, alpha=0.3)
296
 
297
  # Statistics
298
+ plt.subplot(4, 5, 9)
299
  stats_text = f"""Heatmap Statistics:
300
  Mean: {np.mean(heatmap):.3f}
301
  Std: {np.std(heatmap):.3f}
 
311
  verticalalignment='center', bbox=dict(boxstyle="round,pad=0.3", facecolor="lightblue"))
312
  plt.axis('off')
313
 
314
+ # Empty space for symmetry
315
+ plt.subplot(4, 5, 10)
316
+ plt.axis('off')
317
+
318
+ # Rows 3-4: All 6 superimposed views in 2 rows of 3
319
+ superimposed_colormaps = ['jet', 'hot', 'plasma', 'viridis', 'inferno', 'cool']
320
+
321
+ # Row 3: First 3 superimposed views (positions 11-13)
322
+ for i, cmap_name in enumerate(superimposed_colormaps[:3]):
323
  superimposed_img = superimpose_gradcam_enhanced(img_array, heatmap, colormap=cmap_name, alpha=0.4)
324
+ plt.subplot(4, 5, i + 11)
325
  plt.imshow(superimposed_img)
326
  plt.title(f'Superimposed ({cmap_name.title()})', fontsize=12)
327
  plt.axis('off')
328
+
329
+ # Empty spaces in row 3
330
+ plt.subplot(4, 5, 14)
331
+ plt.axis('off')
332
+ plt.subplot(4, 5, 15)
333
+ plt.axis('off')
334
+
335
+ # Row 4: Last 3 superimposed views (positions 16-18)
336
+ for i, cmap_name in enumerate(superimposed_colormaps[3:]):
337
+ superimposed_img = superimpose_gradcam_enhanced(img_array, heatmap, colormap=cmap_name, alpha=0.4)
338
+ plt.subplot(4, 5, i + 16)
339
+ plt.imshow(superimposed_img)
340
+ plt.title(f'Superimposed ({cmap_name.title()})', fontsize=12)
341
+ plt.axis('off')
342
+
343
+ # Empty spaces in row 4
344
+ plt.subplot(4, 5, 19)
345
+ plt.axis('off')
346
+ plt.subplot(4, 5, 20)
347
+ plt.axis('off')
348
 
349
  plt.tight_layout()
350
  plt.subplots_adjust(top=0.92)
 
379
  if heatmap is None:
380
  return "❌ Error generating heatmap."
381
 
382
+ # Create quick visualization with 3x3 layout to accommodate all colormaps
383
+ fig, axes = plt.subplots(3, 3, figsize=(15, 15))
384
  color = 'green' if predicted_class == 0 else 'red'
385
  fig.suptitle(f'Quick Grad-CAM++ Analysis | Predicted: {predicted_label.upper()} ({confidence:.2%})',
386
  fontsize=14, fontweight='bold', color=color)
387
 
388
+ # Row 1: Original image and two main heatmaps
389
  axes[0, 0].imshow(processed_img)
390
  axes[0, 0].set_title("Original Image")
391
  axes[0, 0].axis('off')
 
403
  axes[0, 2].axis('off')
404
  plt.colorbar(im2, ax=axes[0, 2], fraction=0.046)
405
 
406
+ # Rows 2-3: All 6 superimposed views
407
+ superimposed_colormaps = ['jet', 'hot', 'plasma', 'viridis', 'inferno', 'cool']
408
+
409
+ # Row 2: First 3 superimposed views
410
+ for i, cmap_name in enumerate(superimposed_colormaps[:3]):
411
+ superimposed_img = superimpose_gradcam_enhanced(img_array, heatmap, cmap_name)
412
+ axes[1, i].imshow(superimposed_img)
413
+ axes[1, i].set_title(f"Superimposed ({cmap_name.title()})")
414
+ axes[1, i].axis('off')
415
+
416
+ # Row 3: Last 3 superimposed views
417
+ for i, cmap_name in enumerate(superimposed_colormaps[3:]):
418
+ superimposed_img = superimpose_gradcam_enhanced(img_array, heatmap, cmap_name)
419
+ axes[2, i].imshow(superimposed_img)
420
+ axes[2, i].set_title(f"Superimposed ({cmap_name.title()})")
421
+ axes[2, i].axis('off')
422
 
423
  plt.tight_layout()
424
 
 
539
  with gr.Column():
540
  gr.Markdown("""
541
  ### ⚡ Quick Analysis Features:
542
+ - **3x3 Grid Layout** for comprehensive quick view
543
  - **Original Image** with preprocessing
544
  - **Jet & Plasma Heatmaps** with colorbars
545
+ - **6 Superimposed Views** (All color schemes)
546
+ - **Fast Processing** (~3-4 seconds)
547
  - **Perfect for screening** multiple images
548
  """)
549
 
550
  with gr.Column():
551
  gr.Markdown("""
552
  ### 🔬 Detailed Analysis Features:
553
+ - **4x5 Grid Layout** for comprehensive analysis
554
  - **6 Heatmap Color Schemes** with individual colorbars
555
  - **Attention Profile Plot** showing vertical focus
556
  - **Statistical Analysis Panel** with quantitative metrics
557
+ - **6 Enhanced Superimposed Views** with gamma correction
558
  - **Clinical-grade visualization** for detailed examination
559
  """)
560