ArchCoder commited on
Commit
7b7ff94
·
verified ·
1 Parent(s): c322805

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +119 -125
app.py CHANGED
@@ -265,13 +265,14 @@ def generate_attention_heatmap(attention_maps):
265
 
266
  return heatmap
267
 
268
- def analyze_image(image, ground_truth, filename):
269
  """
270
- Robust replacement for the original analyze_image.
271
- - Fixes broadcasting issues between 2D masks and 3-channel images.
272
- - Converts attention heatmap (BGR from OpenCV) to RGB for correct plotting.
273
- - Ensures masks are strict binary uint8 arrays.
274
- - Returns (PIL.Image result_plot, markdown_text).
 
275
  """
276
  if model is None:
277
  return None, "Model not loaded. Please restart the application."
@@ -285,153 +286,146 @@ def analyze_image(image, ground_truth, filename):
285
  print(f"Input image mode: {image.mode}")
286
  print(f"Input image size: {image.size}")
287
 
288
- # Preprocess - keeps same behavior as notebook
289
- input_tensor = preprocess_for_model(image).to(device)
290
  print(f"Input tensor shape: {input_tensor.shape}")
291
  print(f"Input tensor min/max: {input_tensor.min():.4f}/{input_tensor.max():.4f}")
292
 
293
- # Get prediction and attention maps
294
  with torch.no_grad():
295
- print("Getting model output...")
296
- model_output, attention_maps = model(input_tensor)
297
-
298
- # model_output shape expected: [1, 1, 256, 256]
299
- print(f"Model output shape: {model_output.shape}")
300
- print(f"Model output min/max BEFORE sigmoid: {model_output.min():.4f}/{model_output.max():.4f}")
301
-
302
- pred_prob = torch.sigmoid(model_output) # probabilities in [0,1]
303
- print(f"After sigmoid min/max: {pred_prob.min():.4f}/{pred_prob.max():.4f}")
304
-
305
- # DEFAULT THRESHOLD: 0.5 (same as your notebook). Change if debugging low-confidence.
306
- pred_mask = (pred_prob > 0.5).float()
307
- print(f"Binary mask sum (number of 1s): {pred_mask.sum():.4f}")
308
-
309
- # Convert prediction to numpy
310
- pred_mask_np = pred_mask.cpu().squeeze().numpy() # shape: (H, W)
311
- print(f"Numpy binary mask shape: {pred_mask_np.shape}")
312
- print(f"Numpy binary mask unique values: {np.unique(pred_mask_np)}")
313
- print(f"Numpy binary mask sum: {np.sum(pred_mask_np)}")
314
-
315
- # Create attention heatmap (the helper resizes & returns a 3-channel BGR heatmap)
316
- print("Generating attention heatmap...")
317
- att_heatmap = generate_attention_heatmap(attention_maps) # likely BGR (cv2)
318
- print(f"Raw attention heatmap shape: {att_heatmap.shape}")
319
-
320
- # Convert heatmap to RGB (OpenCV returns BGR)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
321
  if att_heatmap is not None and att_heatmap.size != 0:
322
  try:
323
  att_heatmap = cv2.cvtColor(att_heatmap, cv2.COLOR_BGR2RGB)
324
  except Exception:
325
- # if conversion fails, proceed with what we have
326
  pass
327
 
328
- # Prepare original image arrays:
329
- original_gray = np.array(image.convert('L').resize((256, 256))).astype(np.uint8) # 2D
330
- original_rgb = np.array(image.convert('RGB').resize((256, 256))).astype(np.uint8) # 3D
331
 
332
- # Ensure pred_mask_np is strict binary 0/1 uint8
333
- pred_mask_bin = (pred_mask_np > 0.5).astype(np.uint8) # shape: (256,256), dtype: uint8
334
-
335
- # Inverted predicted mask for visualization (white background, tumor black)
336
  inv_pred_mask_np = np.where(pred_mask_bin == 1, 0, 255).astype(np.uint8)
337
 
338
- # Tumor-only images:
339
  tumor_only_gray = np.where(pred_mask_bin == 1, original_gray, 255).astype(np.uint8)
340
- tumor_only_rgb = original_rgb.copy()
341
  tumor_only_rgb[pred_mask_bin == 0] = 255
342
 
343
- # Begin plotting (match existing layout: 2x4 with GT or 2x3 without)
344
  if ground_truth is not None:
345
- fig, axes = plt.subplots(2, 4, figsize=(16, 8))
346
  else:
347
- fig, axes = plt.subplots(2, 3, figsize=(15, 8))
348
-
349
- fig.suptitle('Brain Tumor Segmentation Analysis', fontsize=16, weight='bold')
350
 
351
- # Row 1: Original, Attention, Predicted Mask, Tumor Only (if GT exists show 4th)
352
- axes[0, 0].imshow(original_gray, cmap='gray')
353
- axes[0, 0].set_title('Original Image', fontsize=12, weight='bold')
354
- axes[0, 0].axis('off')
355
 
356
- # Attention overlay on RGB original (blend)
357
- axes[0, 1].imshow(original_rgb)
 
358
  if att_heatmap is not None and att_heatmap.size != 0:
359
- axes[0, 1].imshow(att_heatmap, alpha=0.4)
360
- axes[0, 1].set_title('Attention Heatmap', fontsize=12, weight='bold')
361
- axes[0, 1].axis('off')
362
-
363
- # Predicted mask (inverted for visualization)
364
- axes[0, 2].imshow(inv_pred_mask_np, cmap='gray')
365
- axes[0, 2].set_title('Predicted Mask', fontsize=12, weight='bold')
366
- axes[0, 2].axis('off')
367
 
 
368
  if ground_truth is not None:
369
- axes[0, 3].imshow(tumor_only_rgb)
370
- axes[0, 3].set_title('Tumor Only', fontsize=12, weight='bold')
371
- axes[0, 3].axis('off')
372
-
373
- # Ground truth processing - convert to binary like notebook
374
- val_test_transform = transforms.Compose([
375
- transforms.Resize((256, 256)),
376
- transforms.ToTensor()
377
- ])
378
  mask_np = val_test_transform(ground_truth).cpu().squeeze().numpy()
379
  mask_bin = (mask_np > 0.5).astype(np.uint8)
380
 
381
- print(f"Ground truth array shape: {np.array(ground_truth.resize((256,256))).shape}")
382
- print(f"Ground truth unique values: {np.unique(np.array(ground_truth.resize((256,256))))}")
383
-
384
- # Row 2: Ground truth, overlay comparison, metrics, segmented tumor
385
- axes[1, 0].imshow(mask_bin, cmap='gray')
386
- axes[1, 0].set_title('Ground Truth Mask', fontsize=12, weight='bold')
387
- axes[1, 0].axis('off')
388
-
389
  overlay = original_rgb.copy()
390
- overlay[pred_mask_bin == 1] = [0, 255, 0] # predicted green
391
- overlay[mask_bin == 1] = [255, 0, 0] # ground truth red
392
- axes[1, 1].imshow(overlay)
393
- axes[1, 1].set_title('Prediction (Green) vs GT (Red)', fontsize=12, weight='bold')
394
- axes[1, 1].axis('off')
395
 
396
- # Metrics calculation (IoU and Dice)
397
  intersection = np.logical_and(pred_mask_bin, mask_bin).sum()
398
  union = np.logical_or(pred_mask_bin, mask_bin).sum()
399
  iou = intersection / (union + 1e-7)
400
  dice = (2 * intersection) / (pred_mask_bin.sum() + mask_bin.sum() + 1e-7)
401
 
402
- print(f"Final IoU: {iou:.4f}")
403
- print(f"Final Dice: {dice:.4f}")
404
- print(f"Intersection: {intersection}")
405
- print(f"Union: {union}")
406
- print(f"Pred pixels: {np.sum(pred_mask_bin)}")
407
- print(f"GT pixels: {np.sum(mask_bin)}")
408
-
409
- axes[1, 2].text(0.1, 0.6, f'IoU: {iou:.4f}', fontsize=16, weight='bold')
410
- axes[1, 2].text(0.1, 0.4, f'Dice: {dice:.4f}', fontsize=16, weight='bold')
411
- axes[1, 2].set_xlim(0, 1)
412
- axes[1, 2].set_ylim(0, 1)
413
- axes[1, 2].axis('off')
414
- axes[1, 2].set_title('Metrics', fontsize=12, weight='bold')
415
-
416
- axes[1, 3].imshow(tumor_only_gray, cmap='gray')
417
- axes[1, 3].set_title('Segmented Tumor', fontsize=12, weight='bold')
418
- axes[1, 3].axis('off')
419
 
 
420
  else:
421
- # No ground truth case
422
- axes[1, 0].imshow(inv_pred_mask_np, cmap='gray')
423
- axes[1, 0].set_title('Predicted Mask', fontsize=12, weight='bold')
424
- axes[1, 0].axis('off')
425
-
426
- axes[1, 1].imshow(tumor_only_gray, cmap='gray')
427
- axes[1, 1].set_title('Tumor Only', fontsize=12, weight='bold')
428
- axes[1, 1].axis('off')
 
 
 
 
 
 
 
 
 
 
 
 
 
429
 
430
- overlay = original_rgb.copy()
431
- overlay[pred_mask_bin == 1] = [255, 0, 0] # red for prediction overlay
432
- axes[1, 2].imshow(overlay)
433
- axes[1, 2].set_title('Prediction Overlay', fontsize=12, weight='bold')
434
- axes[1, 2].axis('off')
435
 
436
  plt.tight_layout()
437
 
@@ -442,15 +436,11 @@ def analyze_image(image, ground_truth, filename):
442
  plt.close()
443
  result_image = Image.open(buf).convert("RGB")
444
 
445
- # Analysis text: tumor area
446
- tumor_pixels = int(np.sum(pred_mask_bin))
447
  total_pixels = int(pred_mask_bin.size)
448
  tumor_percentage = (tumor_pixels / total_pixels) * 100 if total_pixels > 0 else 0.0
449
 
450
- print(f"Final tumor pixels: {tumor_pixels}")
451
- print(f"Final tumor percentage: {tumor_percentage:.2f}%")
452
- print("=" * 50)
453
-
454
  analysis_text = f"""
455
  # Analysis Results
456
 
@@ -462,7 +452,7 @@ def analyze_image(image, ground_truth, filename):
462
 
463
  **Model Features:**
464
  - Attention Visualization: Generated
465
- - Post-processing: Applied
466
  """
467
 
468
  if ground_truth is not None:
@@ -472,6 +462,10 @@ def analyze_image(image, ground_truth, filename):
472
  - Dice Score: {dice:.4f}
473
  """
474
 
 
 
 
 
475
  return result_image, analysis_text
476
 
477
  except Exception as e:
@@ -480,7 +474,7 @@ def analyze_image(image, ground_truth, filename):
480
  print(error_msg)
481
  return None, error_msg
482
 
483
-
484
  # Initialize model and dataset at startup
485
  print("Initializing application components...")
486
  model_loaded = download_and_load_model()
 
265
 
266
  return heatmap
267
 
268
+ def analyze_image(image, ground_truth, filename, debug=True):
269
  """
270
+ Replacement analyze_image that:
271
+ - Accepts model returning either logits or (logits, attention_maps)
272
+ - Prints detailed stats and shapes
273
+ - Produces prob heatmap (no threshold) for debugging
274
+ - Fixes broadcasting/color issues for visualization
275
+ - Returns (PIL.Image, markdown_text)
276
  """
277
  if model is None:
278
  return None, "Model not loaded. Please restart the application."
 
286
  print(f"Input image mode: {image.mode}")
287
  print(f"Input image size: {image.size}")
288
 
289
+ # Preprocess - same as your notebook/app
290
+ input_tensor = preprocess_for_model(image).to(device) # shape [1,1,256,256]
291
  print(f"Input tensor shape: {input_tensor.shape}")
292
  print(f"Input tensor min/max: {input_tensor.min():.4f}/{input_tensor.max():.4f}")
293
 
294
+ # Forward pass and robust unpacking (support both return styles)
295
  with torch.no_grad():
296
+ out = model(input_tensor)
297
+ # If model returned tuple/list: (logits, attention_maps)
298
+ if isinstance(out, (list, tuple)) and len(out) == 2:
299
+ logits, attention_maps = out
300
+ else:
301
+ # assume out is logits tensor and no attention maps were returned
302
+ logits = out
303
+ attention_maps = []
304
+
305
+ # Ensure logits is a tensor
306
+ if not torch.is_tensor(logits):
307
+ raise RuntimeError("Model output is not a tensor. Check model forward() return type.")
308
+
309
+ print(f"Model output (logits) shape: {logits.shape}")
310
+ print(f"Model output min/max BEFORE sigmoid: {logits.min():.4f}/{logits.max():.4f}")
311
+
312
+ # Probabilities (sigmoid)
313
+ pred_prob = torch.sigmoid(logits)
314
+ print(f"Pred prob min/max: {pred_prob.min():.4f}/{pred_prob.max():.4f}")
315
+
316
+ # Convert to numpy for visualization; keep a float prob map for the heatmap
317
+ pred_prob_np = pred_prob.cpu().squeeze().numpy() # shape (H, W)
318
+ pred_mask_bin = (pred_prob_np > 0.5).astype(np.uint8) # default threshold 0.5
319
+
320
+ print(f"Binary mask (0.5 threshold) sum: {pred_mask_bin.sum()}")
321
+
322
+ # Debug: print attention maps shapes and stats
323
+ if debug:
324
+ print("Attention maps info:")
325
+ for i, att in enumerate(attention_maps):
326
+ try:
327
+ att_np = att.squeeze().cpu().numpy()
328
+ print(f" att[{i}] shape: {att_np.shape} min/max: {att_np.min():.4f}/{att_np.max():.4f}")
329
+ except Exception as ex:
330
+ print(f" att[{i}] inspect failed: {ex}")
331
+
332
+ # Build prob heatmap (no threshold) for debugging
333
+ try:
334
+ prob_resized = cv2.resize(pred_prob_np, (256, 256)) if pred_prob_np.shape != (256, 256) else pred_prob_np
335
+ prob_norm = (prob_resized - prob_resized.min()) / (prob_resized.max() - prob_resized.min() + 1e-8)
336
+ prob_heatmap_bgr = cv2.applyColorMap((prob_norm * 255).astype(np.uint8), cv2.COLORMAP_JET)
337
+ prob_heatmap = cv2.cvtColor(prob_heatmap_bgr, cv2.COLOR_BGR2RGB)
338
+ except Exception:
339
+ prob_heatmap = np.zeros((256, 256, 3), dtype=np.uint8)
340
+
341
+ # Generate attention heatmap (reuse your function), convert BGR->RGB
342
+ att_heatmap = generate_attention_heatmap(attention_maps)
343
  if att_heatmap is not None and att_heatmap.size != 0:
344
  try:
345
  att_heatmap = cv2.cvtColor(att_heatmap, cv2.COLOR_BGR2RGB)
346
  except Exception:
 
347
  pass
348
 
349
+ # Prepare images (gray and rgb)
350
+ original_gray = np.array(image.convert('L').resize((256, 256))).astype(np.uint8)
351
+ original_rgb = np.array(image.convert('RGB').resize((256, 256))).astype(np.uint8)
352
 
353
+ # Ensure binary mask dtype/shape consistency
354
+ pred_mask_bin = (pred_mask_bin > 0).astype(np.uint8)
 
 
355
  inv_pred_mask_np = np.where(pred_mask_bin == 1, 0, 255).astype(np.uint8)
356
 
 
357
  tumor_only_gray = np.where(pred_mask_bin == 1, original_gray, 255).astype(np.uint8)
358
+ tumor_only_rgb = original_rgb.copy()
359
  tumor_only_rgb[pred_mask_bin == 0] = 255
360
 
361
+ # Decide grid: show prob heatmap next to attention so you can compare
362
  if ground_truth is not None:
363
+ fig, axes = plt.subplots(3, 4, figsize=(16, 12)) # add an extra row for debug heatmap
364
  else:
365
+ fig, axes = plt.subplots(3, 3, figsize=(15, 12))
 
 
366
 
367
+ fig.suptitle('Brain Tumor Segmentation Analysis (debug)', fontsize=18, weight='bold')
 
 
 
368
 
369
+ # Row 1
370
+ axes[0,0].imshow(original_gray, cmap='gray'); axes[0,0].set_title('Original'); axes[0,0].axis('off')
371
+ axes[0,1].imshow(original_rgb);
372
  if att_heatmap is not None and att_heatmap.size != 0:
373
+ axes[0,1].imshow(att_heatmap, alpha=0.45)
374
+ axes[0,1].set_title('Attention Heatmap (overlay)'); axes[0,1].axis('off')
375
+ axes[0,2].imshow(inv_pred_mask_np, cmap='gray'); axes[0,2].set_title('Pred Mask (inv)'); axes[0,2].axis('off')
376
+ if ground_truth is not None:
377
+ axes[0,3].imshow(tumor_only_rgb); axes[0,3].set_title('Tumor Only (RGB)'); axes[0,3].axis('off')
 
 
 
378
 
379
+ # Row 2
380
  if ground_truth is not None:
381
+ # show GT and overlay and metrics
382
+ val_test_transform = transforms.Compose([transforms.Resize((256,256)), transforms.ToTensor()])
 
 
 
 
 
 
 
383
  mask_np = val_test_transform(ground_truth).cpu().squeeze().numpy()
384
  mask_bin = (mask_np > 0.5).astype(np.uint8)
385
 
386
+ axes[1,0].imshow(mask_bin, cmap='gray'); axes[1,0].set_title('Ground Truth Mask'); axes[1,0].axis('off')
 
 
 
 
 
 
 
387
  overlay = original_rgb.copy()
388
+ overlay[pred_mask_bin == 1] = [0,255,0]
389
+ overlay[mask_bin == 1] = [255,0,0]
390
+ axes[1,1].imshow(overlay); axes[1,1].set_title('Prediction (G) vs GT (R)'); axes[1,1].axis('off')
 
 
391
 
 
392
  intersection = np.logical_and(pred_mask_bin, mask_bin).sum()
393
  union = np.logical_or(pred_mask_bin, mask_bin).sum()
394
  iou = intersection / (union + 1e-7)
395
  dice = (2 * intersection) / (pred_mask_bin.sum() + mask_bin.sum() + 1e-7)
396
 
397
+ axes[1,2].text(0.1, 0.6, f'IoU: {iou:.4f}', fontsize=16, weight='bold')
398
+ axes[1,2].text(0.1, 0.4, f'Dice: {dice:.4f}', fontsize=16, weight='bold')
399
+ axes[1,2].axis('off'); axes[1,2].set_title('Metrics')
 
 
 
 
 
 
 
 
 
 
 
 
 
 
400
 
401
+ axes[1,3].imshow(tumor_only_gray, cmap='gray'); axes[1,3].set_title('Segmented Tumor'); axes[1,3].axis('off')
402
  else:
403
+ # No GT: second row shows predicted mask, tumor only and overlay
404
+ axes[1,0].imshow(inv_pred_mask_np, cmap='gray'); axes[1,0].set_title('Predicted Mask'); axes[1,0].axis('off')
405
+ axes[1,1].imshow(tumor_only_gray, cmap='gray'); axes[1,1].set_title('Tumor Only'); axes[1,1].axis('off')
406
+ overlay = original_rgb.copy(); overlay[pred_mask_bin==1] = [255,0,0]
407
+ axes[1,2].imshow(overlay); axes[1,2].set_title('Prediction Overlay'); axes[1,2].axis('off')
408
+
409
+ # Row 3 (debug): probability heatmap + (optional) raw att channel thumbnails
410
+ axes[2,0].imshow(original_rgb); axes[2,0].imshow(prob_heatmap, alpha=0.5); axes[2,0].set_title('Prob Heatmap (overlay)'); axes[2,0].axis('off')
411
+ # show the plain probability heatmap
412
+ axes[2,1].imshow(prob_heatmap); axes[2,1].set_title('Prob Heatmap (plain)'); axes[2,1].axis('off')
413
+
414
+ # if we have attention maps, show up to two scaled maps for quick check
415
+ if len(attention_maps) >= 1:
416
+ try:
417
+ att0 = attention_maps[0].squeeze().cpu().numpy()
418
+ att0 = cv2.resize((att0 - att0.min())/(att0.max()-att0.min()+1e-8), (256,256))
419
+ axes[2,2].imshow(att0, cmap='viridis'); axes[2,2].set_title('Att map 0 (rescaled)'); axes[2,2].axis('off')
420
+ except Exception:
421
+ axes[2,2].axis('off')
422
+ else:
423
+ axes[2,2].axis('off')
424
 
425
+ # hide any unused axes (robust)
426
+ for ax_row in axes.reshape(-1):
427
+ if not hasattr(ax_row, 'has_data') or ax_row.images == []:
428
+ ax_row.axis('off')
 
429
 
430
  plt.tight_layout()
431
 
 
436
  plt.close()
437
  result_image = Image.open(buf).convert("RGB")
438
 
439
+ # Numeric analysis text
440
+ tumor_pixels = int(pred_mask_bin.sum())
441
  total_pixels = int(pred_mask_bin.size)
442
  tumor_percentage = (tumor_pixels / total_pixels) * 100 if total_pixels > 0 else 0.0
443
 
 
 
 
 
444
  analysis_text = f"""
445
  # Analysis Results
446
 
 
452
 
453
  **Model Features:**
454
  - Attention Visualization: Generated
455
+ - Probability Heatmap: Generated
456
  """
457
 
458
  if ground_truth is not None:
 
462
  - Dice Score: {dice:.4f}
463
  """
464
 
465
+ # Extra helpful hint when predictions are all zero
466
+ if debug and pred_prob_np.max() < 0.5:
467
+ analysis_text += "\n\n**Debug hint:** model probabilities are low (max < 0.5). Try lowering threshold (e.g. 0.3) or inspect model weights/loading."
468
+
469
  return result_image, analysis_text
470
 
471
  except Exception as e:
 
474
  print(error_msg)
475
  return None, error_msg
476
 
477
+
478
  # Initialize model and dataset at startup
479
  print("Initializing application components...")
480
  model_loaded = download_and_load_model()