Add GradCAM attention heatmap visualization to Gradio interface using pytorch-grad-cam#1
Conversation
There was a problem hiding this comment.
Copilot wasn't able to review any files in this pull request.
Tip: Customize your code reviews with copilot-instructions.md. Create the file or learn how to get started.
Add permissions for contents and pull-requests in workflow
Co-authored-by: GilbertKrantz <90319182+GilbertKrantz@users.noreply.github.com>
Co-authored-by: GilbertKrantz <90319182+GilbertKrantz@users.noreply.github.com>
Co-authored-by: GilbertKrantz <90319182+GilbertKrantz@users.noreply.github.com>
Co-authored-by: GilbertKrantz <90319182+GilbertKrantz@users.noreply.github.com>
…s.txt Co-authored-by: GilbertKrantz <90319182+GilbertKrantz@users.noreply.github.com>
|
Just as a heads up, I was blocked by some firewall rules while working on your feedback. Expand below for details. Warning Firewall rules blocked me from connecting to one or more addresses (expand for details)I tried to connect to the following addresses, but was blocked by firewall rules:
If you need me to access, download, or install something from one of these locations, you can either:
|
There was a problem hiding this comment.
Pull Request Overview
Copilot reviewed 4 out of 7 changed files in this pull request and generated 4 comments.
Tip: Customize your code reviews with copilot-instructions.md. Create the file or learn how to get started.
| model.to(device) | ||
|
|
There was a problem hiding this comment.
Moving the model to device on every prediction call is inefficient. The model should be moved to device once during loading in the load_model function, not repeatedly in predict_image.
| model.to(device) |
| heatmap_overlay = apply_heatmap_on_image(img_pil, cam) | ||
|
|
||
| # Clean up | ||
| del cam_extractor |
There was a problem hiding this comment.
[nitpick] Explicitly deleting cam_extractor is unnecessary in Python as it will be automatically garbage collected when it goes out of scope. This deletion doesn't provide significant benefit and adds clutter.
| del cam_extractor | |
| import traceback | ||
| traceback.print_exc() |
There was a problem hiding this comment.
The traceback module is imported inside the exception handler. This import should be moved to the top of the file with other imports for better code organization and to avoid repeated imports during runtime.
| import traceback | ||
| traceback.print_exc() |
There was a problem hiding this comment.
The traceback module is imported inside the exception handler. This import should be moved to the top of the file with other imports for better code organization and to avoid repeated imports during runtime.
Overview
This PR adds attention heatmap visualization to the Gradio interface using GradCAM (Gradient-weighted Class Activation Mapping), allowing users to see which regions of fundus images the model focuses on when making predictions.
What's New
The Gradio interface now displays two outputs side-by-side:
Example Usage
When a user uploads a fundus image and clicks "Analyze Image":
This helps users understand and validate the model's decision-making process.
Implementation Details
Using pytorch-grad-cam Library
This implementation uses the well-maintained pytorch-grad-cam library instead of a custom implementation:
Multi-Architecture Support
Automatically selects the appropriate layer for GradCAM based on model type:
Robust Error Handling
Changes
Modified Files
grad-cam>=1.5.0,opencv-python>=4.8.0, andpillow>=10.0.0as dependencies (UV-managed)Dependency Management
This PR uses UV for dependency management following best practices:
pyproject.toml(single source of truth)uv.lock) ensures reproducible buildsrequirements.txtauto-generated usinguv pip compilefor pip compatibilityKey Functions
Benefits
Testing
All tests pass successfully:
Backwards Compatibility
This PR maintains full backwards compatibility:
Screenshots/Examples
The Gradio interface now shows:
Related
Implements the feature requested in issue: "add the attentionmap.heatmap to display in gradio"
Original prompt
💡 You can make Copilot smarter by setting up custom instructions, customizing its development environment and configuring Model Context Protocol (MCP) servers. Learn more Copilot coding agent tips in the docs.