raghakot / keras-vis

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

TypeError: Cannot interpret feed_dict key as Tensor: Can not convert a bool into a Tensor. #97

Closed lucasrodes closed 5 years ago

lucasrodes commented 6 years ago

I run the following code:

import keras.backend as K
K.set_learning_phase(0)

from keras.models import load_model
model = load_model('../models/keras/model.h5')
layer_dict = dict([(layer.name, layer) for layer in model.layers[1:]])

from vis.losses import ActivationMaximization
from vis.regularizers import TotalVariation, LPNorm
filter_indices = [1, 2, 3]

# Tuple consists of (loss_function, weight)
# Add regularizers as needed.
losses = [
    (ActivationMaximization(layer_dict['activation_45'], filter_indices), 1),
    (LPNorm(model.input), 10),
    (TotalVariation(model.input), 10)
]

Everything works. Then I attempt to actually run the optimizer:

from vis.optimizer import Optimizer

optimizer = Optimizer(model.input, losses)
opt_img, grads, _ = optimizer.minimize()

And it crashes, TypeError: Cannot interpret feed_dict key as Tensor: Can not convert a int into a Tensor. , whole error here. My model is the following:

_________________________________________________________________
Layer (type)                 Output Shape              Param #   
=================================================================
conv2d_22 (Conv2D)           (None, 254, 254, 8)       72        
_________________________________________________________________
activation_44 (Activation)   (None, 254, 254, 8)       0         
_________________________________________________________________
batch_normalization_36 (Batc (None, 254, 254, 8)       32        
_________________________________________________________________
max_pooling2d_22 (MaxPooling (None, 127, 127, 8)       0         
_________________________________________________________________
conv2d_23 (Conv2D)           (None, 125, 125, 16)      1152      
_________________________________________________________________
activation_45 (Activation)   (None, 125, 125, 16)      0         
_________________________________________________________________
batch_normalization_37 (Batc (None, 125, 125, 16)      64        
_________________________________________________________________
max_pooling2d_23 (MaxPooling (None, 62, 62, 16)        0         
_________________________________________________________________
conv2d_24 (Conv2D)           (None, 60, 60, 32)        4608      
_________________________________________________________________
activation_46 (Activation)   (None, 60, 60, 32)        0         
_________________________________________________________________
batch_normalization_38 (Batc (None, 60, 60, 32)        128       
_________________________________________________________________
max_pooling2d_24 (MaxPooling (None, 30, 30, 32)        0         
_________________________________________________________________
flatten_8 (Flatten)          (None, 28800)             0         
_________________________________________________________________
dense_22 (Dense)             (None, 100)               2880000   
_________________________________________________________________
activation_47 (Activation)   (None, 100)               0         
_________________________________________________________________
batch_normalization_39 (Batc (None, 100)               400       
_________________________________________________________________
dropout_8 (Dropout)          (None, 100)               0         
_________________________________________________________________
dense_23 (Dense)             (None, 50)                5000      
_________________________________________________________________
activation_48 (Activation)   (None, 50)                0         
_________________________________________________________________
batch_normalization_40 (Batc (None, 50)                200       
_________________________________________________________________
dense_24 (Dense)             (None, 1)                 51        
=================================================================
Total params: 2,891,707
Trainable params: 2,891,295
Non-trainable params: 412
_________________________________________________________________

I tried installing using pip, then upgraded it using git repo. Then tried installing from source. Same error keeps showing up.

Thanks for your project.

loodvn commented 5 years ago

Hey - Check out https://github.com/tensorflow/tensorflow/issues/13283 or https://stackoverflow.com/questions/37267584/tensorflow-feed-dict-using-same-symbol-for-key-value-pair-got-typeerror-can

I just got this error a few minutes ago while doing an assignment in tf, and it was due to me declaring a variable (say, is_training = True) in my own code, overwriting an existing tf.placeholder declared earlier with the same name.

So perhaps one of your variable names is overwriting a placeholder defined earlier within the module/project? (I think StackOverflow would be a better place to ask though)

keisen commented 5 years ago

Hi, @lucasrodes

You can avoid this problem if it remove the following code.

# K.set_learning_phase(0)

I believe this issue is resolved but if I have mistaken, please reopen this issue.