Using Keras 2.2.4, I'm working my way though this notebook 5.4-visualizing-what-convnets-learn , except I switched the model with a unet one provided by Kaggle-Carvana-Image-Masking-Challenge . The first layer of the Kaggle model looks like this, followed by the rest of the example code.
def get_unet_512(input_shape=(512, 512, 3),
num_classes=1):
inputs = Input(shape=input_shape)
...
Layer (type) Output Shape Param # Connected to
==================================================================================================
input_13 (InputLayer) (None, 512, 512, 3) 0
...
from keras import models
layer_outputs = [layer.output for layer in model.layers[:8]]
activation_model = models.Model(inputs=model.input, outputs=layer_outputs)
activations = activation_model.predict(img_tensor)
Now the error I am getting is
InvalidArgumentError: input_13:0 is both fed and fetched.
Does anyone have any suggestions on how to work around this?
This error is caused by:
layer_outputs = [layer.output for layer in model.layers[:8]]
, and it says that the first layer(input layer) is both fed and fetched.
Here's a workaround:
import keras.backend as K
layer_outputs = [K.identity(layer.output) for layer in model.layers[:8]]
EDIT: Full example, code adapted from: Mask_RCNN - run_graph
import numpy as np
import keras.backend as K
from keras.models import Sequential, Model
from keras.layers import Input, Dense, Flatten
model = Sequential()
ip = Input(shape=(512,512,3,))
fl = Flatten()(ip)
d1 = Dense(20, activation='relu')(fl)
d2 = Dense(3, activation='softmax')(d1)
model = Model(ip, d2)
model.compile('adam', 'categorical_crossentropy')
model.summary()
layer_outputs = [K.identity(layer.output) for layer in model.layers]
#layer_outputs = [layer.output for layer in model.layers] #fails
kf = K.function([ip], layer_outputs)
activations = kf([np.random.random((1,512,512,3))])
print(activations)