Saliency map for a chosen sample with its features.

111 views
Skip to first unread message

Vinayakumar R

unread,
Mar 29, 2017, 1:20:30 AM3/29/17
to Keras-users
K. Simonyan, A. Vedaldi, and A. Zisserman, “Deep Inside Convolutional Networks: Visualising Image Classification Models and Saliency Maps,” arXiv preprint arXiv:1312.6034, 2013

Hi, 
      I have trained a CNN model and I want to create a Saliency map for a chosen sample with its features. This is plotted by using Taylor expansion and compute first partial derivative of the classification results before putting them through the softmax function. Does anybody have a sample code to do this?

Daπid

unread,
Mar 29, 2017, 1:38:31 AM3/29/17
to Vinayakumar R, Keras-users
First of all, you need to build your model with the softmax activation as its own layer, so you can take the output before it:

x = Dense(1, activation='linear')(x)
x = Activation('softmax')(x)

model = Model(inputs=..., outputs=x)


Now, for the actual extraction you'll have to do something like this (untested):

model = keras.models.load_model('trained_model.h5')
output_layer = model.layers_by_depth[1][0] # Adjust appropriately
features_out = K.function(model.inputs + [K.learning_phase()], (output_layer.output,))
derivatives = K.gradients(features_out, model.inputs + [False])

--
You received this message because you are subscribed to the Google Groups "Keras-users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to keras-users+unsubscribe@googlegroups.com.
To view this discussion on the web, visit https://groups.google.com/d/msgid/keras-users/d889881c-459c-40ae-8d49-37b00c87dd34%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Vinayakumar R

unread,
Mar 29, 2017, 7:52:02 AM3/29/17
to Keras-users
my architecture is 

model = Sequential()
model.add(Embedding(max_features, 128, input_length=500, dropout=0.1))
model.add(LSTM(128, dropout_W=0.1, dropout_U=0.1))
model.add(Dense(5))
model.add(Activation('softmax'))

i used your code like given below

output_layer = model.layers_by_depth[1][0] # Adjust appropriately
features_out = K.function(model.inputs + [K.learning_phase()], (output_layer.output,))
derivatives = K.gradients(features_out, model.inputs + [False])

but it is showing the below error

Traceback (most recent call last):
  File "try.py", line 60, in <module>
    output_layer = model.layers_by_depth[1][0] # Adjust appropriately
AttributeError: 'Sequential' object has no attribute 'layers_by_depth'



Vinayakumar R

unread,
Mar 29, 2017, 12:55:54 PM3/29/17
to Keras-users
my network is given below

model = Sequential()
model.add(Embedding(max_features, 128, input_length=500, dropout=0.1))
model.add(LSTM(128, dropout_W=0.1, dropout_U=0.1))
model.add(Dense(5))
model.add(Activation('softmax'))

to get weights of layer before 

i can get lstm layer weights by using 

embeddings = model.layers[1].get_weights()[0]

and dense layer weights using 

embeddings = model.layers[2].get_weights()[0]

after that what to do
Reply all
Reply to author
Forward
0 new messages