raghakot / keras-vis

Neural network visualization toolkit for keras
https://raghakot.github.io/keras-vis
MIT License
2.97k stars 664 forks source link

Problems for visualize_saliency with my 'channels_first' model #123

Closed Guoyinzh closed 6 years ago

Guoyinzh commented 6 years ago

Thanks for your excellent job! I have run your examples very well. But I got some problems when running it for my model.

I trained a very small model using keras with tensorflow as backend. The input image is 'channels_first': img.shape = (1,15,20) # one channel

When I run visualize_saliency as follows, I should get: grads.shape = (15, 20) #right? But I got: grads.shape = (1,15) Keras-vis still treat the input as 'channels_last' data format. The same for visualize_cam.

Thanks for your time.

from vis.utils import utils from keras import activations from vis.visualization import visualize_saliency

model.layers[-1].activation = activations.linear model = utils.apply_modifications(model)

grads = visualize_saliency(model,-1, filter_indices=1, seed_input=img, backprop_modifier='guided')

keisen commented 6 years ago

Hi, @Guoyinzh

You should also write the results of below.

import keras.backend as K

print(K.image_data_format()) # 'channels_first' ?

Thanks

Guoyinzh commented 6 years ago

K.image_data_format() = 'channels_last' Maybe it is because I am usinig tensorflow as backend, and it is using 'channels_last' for the default? Also, I defined the data_format='channels_first' in the Conv2D layer in my model, as follows

model = Sequential() model.add(Conv2D(input_shape=X_train[0].shape, filters=filters, kernel_size=kernel_size, strides=strides, padding='same', data_format='channels_first')

Guoyinzh commented 6 years ago

Problem solved! Just changing the data_format in Conv2D layer is not enough. I change the backend image_data_format to 'channels_first' and It works now! Thanks!

keisen commented 6 years ago

@Guoyinzh , Could you close this issue ?