keras-vis
keras-vis copied to clipboard
[Error]:ValueError: Cannot feed value of shape (1, 3, 224, 224) for Tensor u'input_1_1:0', which has shape '(?, ?, ?, 3)'
I am doing it on my own network:
model = load_model('./model_single_frame_cnn/model_allframe_101_ori.h5')
name = model.layers[-1].name
layer_idx = utils.find_layer_idx(model, name)
model.layers[layer_idx].activation = activations.linear
model = utils.apply_modifications(model)
img1 = utils.load_img('test-a.jpg', target_size=(224, 224))
f, ax = plt.subplots(1, 2)
for i, img in enumerate([img1]):
grads = visualize_saliency(model, layer_idx, filter_indices=0, seed_input=img)
ax[i].imshow(grads, cmap='jet')
But, i get the error:
Traceback (most recent call last):
File
"/Users/qiu/Documents/cwzpaper/video_classification/video_classification/single_frame_cnn/visualization.py", line 43, in
My image format is channel_last. How can I solve it? Thank you!
I found _get_seed_input
in optimizer.py
:
('desired_shape', (1, None, None, 3))
and ('seed_input_shape', (1, 229, 229, 3))
, the code will be in if seed_input.shape != desired_shape: seed_input = np.moveaxis(seed_input, -1, 1)
could you solve the problem?
And, when i was using visualize_cam
,i met the problem:
penultimate_layer.output
is (None, None, None, 192)
and the output_dims
will be None.
I think they are similar problem, could you solve it?
I had similar problems when modifying the example code for Inception V3. My issue was solved by defining the input_shape
to the network, so that the neural network layers were all of defined sizes:
from keras.applications import InceptionV3
model = InceptionV3(weights='imagenet', include_top=True, input_shape=(299, 299, 3))
and then changing the image load size to (299, 299)
:
img1 = utils.load_img('test-a.jpg', target_size=(299, 299))
I had the same problem when using variable sized inputs.
The problem comes from vis.optimizers.Optimizer._get_seed_input(self, seed_input)
, in
if seed_input.shape != desired_shape:
seed_input = np.moveaxis(seed_input, -1, 1)
The problem is that in Keras, variable shaped entries in the model are defined as None
, which compared to an integer results in False
.
For example the model input could have shape (None, None, 224, 224, 3)
when using 224 x 224 RGB videos as input, while the input data has shape (1, 10, 224, 224,3)
for a video of length 10. This will make seed_input.shape != desired_shape
evaluate to True
, which results in swapping the seed input channel axis with np.moveaxis(seed_input, -1, 1)
My quickfix was just to comment out the two lines in vis.optimizer
# if seed_input.shape != desired_shape:
# seed_input = np.moveaxis(seed_input, -1, 1)