Comments (3)
It's a BGR<>RGB issue.
show_cam_on_image Creates the colorful image in BGR format, unless use_rgb is passed.
But then you save the image with PIL, which is RGB, so the colors are inverted.
You can pass use_rgb=True to show_cam_on_image.
from pytorch-grad-cam.
It could be, but need to make sure it's not and rgb <> bgr issue.
How did you create it?
And details will help.
from pytorch-grad-cam.
It could be, but need to make sure it's not and rgb <> bgr issue.
How did you create it? And details will help.
actually, my inputs_image is a one-channel image. I repeat it to 3-channels. here is my code
`
dirs = "data/AFD/AFDB_train"
for d in os.listdir(dirs):
for file in os.listdir(f"{dirs}/{d}"):
image_path = f"{dirs}/{d}/{file}"
FACE_SHAPE = (128, 128)
input_tensor = preprocess_image(image_path, FACE_SHAPE)
# Apply the heatmap on the original image
image = Image.open(image_path)
image = image.resize(FACE_SHAPE)
image_array = np.array(image)
# image_array = np.moveaxis(image_array, -1, 0)
# print(image_array/255)
for name, layer in model.named_modules():
if "conv" not in name or "." in name:
continue
target_layers = [layer, ]
# Construct the GradCAM object
cam = ScoreCAM(model=model, target_layers=target_layers)
# Perform CAM and generate heatmap
targets = [ClassifierOutputTarget(0)]
grayscale_cam = cam(input_tensor=input_tensor, targets=targets, aug_smooth=False)
grayscale_cam = np.repeat(grayscale_cam, 3, axis=0)
grayscale_cam = grayscale_cam[0, :]
# print(grayscale_cam)
# print(image_array.shape,grayscale_cam.shape)
try:
visualization = show_cam_on_image(image_array / 255, grayscale_cam)
except ValueError:
continue
pil_image = Image.fromarray(visualization)
# Save the PIL Image object to a file
if not os.path.exists(f"pic/{d}"):
os.mkdir(f"pic/{d}")
pil_image.save(f"pic/{d}/{file}{name}.jpg")
`
from pytorch-grad-cam.
Related Issues (20)
- GradCAM for Dual Attention ViT HOT 1
- how to apply grad-cam to image fusion model ?
- gradcam for binary segmentaion network
- Installation showing missing requirements.txt
- Model improvement HOT 1
- GradCAM throws error for models that give ClassifierOutput class as model output instead of tensors
- How to Generate Attention Graphs on Custom Models ?
- Memory leakage during multiple loss calculations HOT 1
- AttributeError: 'AblationLayer' object has no attribute 'f' HOT 1
- Problem generating CAMs using the saved model HOT 2
- [Q] Are CAMs generated using the output of the specified layer or its input? HOT 2
- How to extract cam during training without creating issues to the bakpropagation HOT 1
- help me!!!thank you AttributeError: 'function' object has no attribute 'register_forward_hook'
- the example in README need to update HOT 1
- How should I assign a value to the "targets" when I draw the heatmap of yolov8-pose?
- float16 is not supported (src data type = 23 is not supported)
- YOLOv5 2021-10-21 torch 2.0.1+cpu CPU Fusing layers... Model Summary: 213 layers, 7225885 parameters, 0 gradients Adding AutoShape... Traceback (most recent call last): File "E:\yolov5-v6.1\yolov5热力图.py", line 70, in <module> print(model.model.model.model[-2]) File "D:\anaconda\envs\fastapi\lib\site-packages\torch\nn\modules\module.py", line 1614, in __getattr__ raise AttributeError("'{}' object has no attribute '{}'".format( AttributeError: 'Sequential' object has no attribute 'model'
- Mismatch in the number of attribution slices for 3D volume Torch tensor
- How can I use gradcam for Contrastive Unsupervised Representations (CURL), one of the methods for reinforcement learning?
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from pytorch-grad-cam.