pytorch-cnn-visualizations icon indicating copy to clipboard operation
pytorch-cnn-visualizations copied to clipboard

image size problem

Open zhaoxin111 opened this issue 6 years ago • 6 comments

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

zhaoxin111 avatar Mar 29 '19 02:03 zhaoxin111

Hey,

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

utkuozbulak avatar Mar 29 '19 05:03 utkuozbulak

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.

utkuozbulak avatar May 04 '19 05:05 utkuozbulak

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

tom-a-horrocks avatar Jun 25 '19 07:06 tom-a-horrocks

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

utkuozbulak avatar Jun 27 '19 18:06 utkuozbulak

hi

Radha2020-py avatar Apr 23 '20 11:04 Radha2020-py

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)

BartvanMarrewijk avatar Feb 19 '21 14:02 BartvanMarrewijk