-
Notifications
You must be signed in to change notification settings - Fork 2.5k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Use the pytorch-grad-cam tool to visualize Class Activation Maps (CAM) #3324
Conversation
# select visualization layer, e.g. model.backbone.layer4[2] in | ||
# deeplabv3_r50 it can be multiple layers | ||
target_layers = [model.backbone.layer4[2]] |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Is it possible to make this script a bit more generic? It now only works for resnet.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes, the target layers can now be any layers.
input_tensor = preprocess_image( | ||
rgb_img, mean=[0.485, 0.456, 0.406], std=[0.229, 0.224, 0.225]) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The mean and std could be obtained from the config file.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Now the mean and std are obtained from the config file.
|
||
# data processing | ||
image = np.array(Image.open(args.img).convert('RGB')) | ||
Height, Width = image.shape[0], image.shape[1] |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Height, Width = image.shape[0], image.shape[1] | |
height, width = image.shape[0], image.shape[1] |
default_cfg = 'configs/deeplabv3/deeplabv3_r50-d8_4xb2-40k_cityscapes-769x769.py' # noqa | ||
parser.add_argument('--img', default='demo/demo.png', help='Image file') | ||
parser.add_argument('--config', default=default_cfg, help='Config file') | ||
parser.add_argument( | ||
'--checkpoint', | ||
default='deeplabv3_r50-d8_769x769_40k_cityscapes.pth', | ||
help='Checkpoint file') |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It is recommended to force the user to enter img
, config
and checkpoint
.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Now the user need to enter img, config and checkpoint.
help='Path to output prediction file') | ||
parser.add_argument( | ||
'--cam-file', default='vis_cam.png', help='Path to output cam file') | ||
parser.add_argument('--device', default='cuda:0', help='cuda:0 or cpu') |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
There are other devices such as mps and npu.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Of course.
open-mmlab#3324) Thanks for your contribution and we appreciate it a lot. The following instructions would make your pull request more healthy and more easily get feedback. If you do not understand some items, don't worry, just make the pull request and seek help from maintainers. ## Motivation Use the pytorch-grad-cam tool to visualize Class Activation Maps (CAM). ## Modification Use the pytorch-grad-cam tool to visualize Class Activation Maps (CAM). requirement: pip install grad-cam run commad: python tools/analysis_tools/visualization_cam.py ## BC-breaking (Optional) Does the modification introduce changes that break the backward-compatibility of the downstream repos? If so, please describe how it breaks the compatibility and how the downstream projects should modify their code to keep compatibility with this PR. ## Use cases (Optional) If this PR introduces a new feature, it is better to list some use cases here, and update the documentation. ## Checklist 1. Pre-commit or other linting tools are used to fix the potential lint issues. 2. The modification is covered by complete unit tests. If not, please add more unit test to ensure the correctness. 3. The documentation has been modified accordingly, like docstring or example tutorials.
open-mmlab#3324) Thanks for your contribution and we appreciate it a lot. The following instructions would make your pull request more healthy and more easily get feedback. If you do not understand some items, don't worry, just make the pull request and seek help from maintainers. ## Motivation Use the pytorch-grad-cam tool to visualize Class Activation Maps (CAM). ## Modification Use the pytorch-grad-cam tool to visualize Class Activation Maps (CAM). requirement: pip install grad-cam run commad: python tools/analysis_tools/visualization_cam.py ## BC-breaking (Optional) Does the modification introduce changes that break the backward-compatibility of the downstream repos? If so, please describe how it breaks the compatibility and how the downstream projects should modify their code to keep compatibility with this PR. ## Use cases (Optional) If this PR introduces a new feature, it is better to list some use cases here, and update the documentation. ## Checklist 1. Pre-commit or other linting tools are used to fix the potential lint issues. 2. The modification is covered by complete unit tests. If not, please add more unit test to ensure the correctness. 3. The documentation has been modified accordingly, like docstring or example tutorials.
Thanks for your contribution and we appreciate it a lot. The following instructions would make your pull request more healthy and more easily get feedback. If you do not understand some items, don't worry, just make the pull request and seek help from maintainers.
Motivation
Use the pytorch-grad-cam tool to visualize Class Activation Maps (CAM).
Modification
Use the pytorch-grad-cam tool to visualize Class Activation Maps (CAM).
requirement: pip install grad-cam
run commad: python tools/analysis_tools/visualization_cam.py
BC-breaking (Optional)
Does the modification introduce changes that break the backward-compatibility of the downstream repos?
If so, please describe how it breaks the compatibility and how the downstream projects should modify their code to keep compatibility with this PR.
Use cases (Optional)
If this PR introduces a new feature, it is better to list some use cases here, and update the documentation.
Checklist