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

What is the different colors mean? #484

Open
liuxiangchao369 opened this issue Feb 28, 2024 · 3 comments
Open

What is the different colors mean? #484

liuxiangchao369 opened this issue Feb 28, 2024 · 3 comments

Comments

@liuxiangchao369
Copy link

I found that the color of eyes,mouth of the face if blue and other part of the face is red.

Does it mean that the eyes,mouth are not important in my model?

001conv7

@liuxiangchao369 liuxiangchao369 changed the title What is the diffirent colors mean? What is the different colors mean? Feb 29, 2024
@jacobgil
Copy link
Owner

It could be, but need to make sure it's not and rgb <> bgr issue.

How did you create it?
And details will help.

@liuxiangchao369
Copy link
Author

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")

`

@jacobgil
Copy link
Owner

jacobgil commented Mar 6, 2024

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.

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

No branches or pull requests

2 participants