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

image size problem #40

Open
zhaoxin111 opened this issue Mar 29, 2019 · 6 comments
Open

image size problem #40

zhaoxin111 opened this issue Mar 29, 2019 · 6 comments

Comments

@zhaoxin111
Copy link

Hi,I find a bug in gradcam.py
The Image package use w,h mode. However, the precessed image is a tensor with 1,c,h,w. So,the order of w and h need to be switched when using Image.resize.

Line 88,change
cam = np.uint8(Image.fromarray(cam).resize((input_image.shape[2],
input_image.shape[3]), Image.ANTIALIAS))
to
cam = np.uint8(Image.fromarray(cam).resize((input_image.shape[3],
input_image.shape[2]), Image.ANTIALIAS))

@utkuozbulak
Copy link
Owner

Hey,

Thanks for the notification! I will check/fix it by tonight.

@utkuozbulak
Copy link
Owner

Hello again,

Sorry for (very) late reply. I think it is correct as it is. PIL uses HWC and torch uses BCHW so HW channels are always one after another.

You can convince yourself that PIL uses HWC with

from PIL import Image
import numpy as np

im = Image.open('image.png')
im_arr = np.asarray(im)
print(im_arr.shape)

I will check the output with a model that accepts input with different H and W size and see if it is actually correct though. Thanks for the heads up.

@tom-a-horrocks
Copy link

Hi,

I believe OP is correct. My code crashes on that line with rectangular input images. Regardless of what PIL uses behind the scenes, the Image.Resize function takes shape as (W,H):

image

Implementing OP's fix stops the crash and results in the correct CAM (I verified it against CAMs from other packages).

@utkuozbulak
Copy link
Owner

Thanks for the heads up! I will have a look at it again!

@utkuozbulak utkuozbulak reopened this Jun 27, 2019
@Radha2020-py
Copy link

hi

@BartvanMarrewijk
Copy link

Indeed a problem, the solution of @zhaoxin111 helped. In addition there is another bug when using grad cam with larger image sizes. The adaptivepooling before the classification is not loaded for both alexnet and vgg. I solved it by adding following line before flattening line43:
x = self.pool.avgpool(x)

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

5 participants