Skip to content
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

Merged
merged 4 commits into from
Sep 20, 2023

Conversation

zhen6618
Copy link
Contributor

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.

Comment on lines 88 to 90
# select visualization layer, e.g. model.backbone.layer4[2] in
# deeplabv3_r50 it can be multiple layers
target_layers = [model.backbone.layer4[2]]
Copy link
Collaborator

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.

Copy link
Contributor Author

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.

Comment on lines 100 to 101
input_tensor = preprocess_image(
rgb_img, mean=[0.485, 0.456, 0.406], std=[0.229, 0.224, 0.225])
Copy link
Collaborator

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.

Copy link
Contributor Author

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]
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
Height, Width = image.shape[0], image.shape[1]
height, width = image.shape[0], image.shape[1]

Comment on lines 50 to 56
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')
Copy link
Collaborator

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.

Copy link
Contributor Author

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')
Copy link
Collaborator

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.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Of course.

@xiexinch xiexinch merged commit 56a40d7 into open-mmlab:dev-1.x Sep 20, 2023
3 checks passed
emily-lin pushed a commit to emily-lin/mmsegmentation that referenced this pull request Nov 18, 2023
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.
nahidnazifi87 pushed a commit to nahidnazifi87/mmsegmentation_playground that referenced this pull request Apr 5, 2024
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.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants