raghakot / keras-vis

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

Unable to visualize a VGG16-type model #105

Open felix-tracxpoint opened 6 years ago

felix-tracxpoint commented 6 years ago

I created a simple model that adds a global pooling layer and then 2 dense layers to VGG16 and learns to discriminate between 3 classes. It goes like this:

base_model = VGG16(weights='imagenet', include_top=False)
x = base_model.output
x = GlobalAveragePooling2D()(x)
x = Dense(200, activation='relu')(x)
predictions = Dense(3, activation='softmax')(x)
model = Model(inputs=base_model.input, outputs=predictions)

for layer in base_model.layers:
    layer.trainable = False

model.compile(optimizer='rmsprop', loss='categorical_crossentropy',  metrics=['accuracy'])
model.fit_generator( train_generator,
        steps_per_epoch=100 // batch_size,
        epochs=8)

Now I am trying to visualize its saliency and/or GRAD-CAM using keras-vis but keep getting weird errors. Specifically, I am running this code:

layer_idx = utils.find_layer_idx(model, 'dense_2')
grads = visualize_saliency(model, layer_idx, filter_indices=0, seed_input=img)

and getting this error:


TypeError Traceback (most recent call last)

in () 1 layer_idx = utils.find_layer_idx(model, 'dense_2') ----> 2 grads = visualize_saliency(model, layer_idx, filter_indices=0, seed_input=img) ~\Anaconda3\envs\tracx\lib\site-packages\vis\visualization\saliency.py in visualize_saliency(model, layer_idx, filter_indices, seed_input, wrt_tensor, backprop_modifier, grad_modifier) 123 # for increasing activations. Multiply with -1 so that positive gradients indicate increase instead. 124 losses = [ --> 125 (ActivationMaximization(model.layers[layer_idx], filter_indices), -1) 126 ] 127 return visualize_saliency_with_losses(model.input, losses, seed_input, wrt_tensor, grad_modifier) ~\Anaconda3\envs\tracx\lib\site-packages\vis\losses.py in __init__(self, layer, filter_indices) 68 output can be maximized by minimizing scores for other classes. 69 """ ---> 70 super(ActivationMaximization, self).__init__() 71 self.name = "ActivationMax Loss" 72 self.layer = layer TypeError: super(type, obj): obj must be an instance or subtype of type

What am I doing wrong? Any help will be much appreciated.

keisen commented 6 years ago

This exception is raised if the class definition is overwritten after instance creation. (It may occur in other cases, but I don't know.)

I write code for generating the same exception below.

# first "A" class definition
class A:
    def test(self):
        print(super(A, self))

# "A" class instantiation generation
a = A()
a.test() # no problem

# second "A" class definition (overwrite)
class A:
    def test(self):
        print(super(A, self))

a.test() # Error occurred!

TypeError: super(type, obj): obj must be an instance or subtype of type

Please check the environment where keras-vis was installed. Is there keras-vis not only in ~\Anaconda3\envs\tracx\lib\site-packages\vis but also in ./vis etc?