Obtaining the output of the intermediate layer in TensorFlow / Keras

I am trying to get middleware output in Keras, the following is my code:

XX = model.input # Keras Sequential() model object
YY = model.layers[0].output
F = K.function([XX], [YY]) # K refers to keras.backend


Xaug = X_train[:9]
Xresult = F([Xaug.astype('float32')])

By running this, I got an error message:

InvalidArgumentError (see above for traceback): You must feed a value for placeholder tensor 'dropout_1/keras_learning_phase' with dtype bool

I found out that since I use a screening layer in my model, I have to specify a flag learning_phase()for my function according to the keras documentation . I changed my code to the following:

XX = model.input
YY = model.layers[0].output
F = K.function([XX, K.learning_phase()], [YY])


Xaug = X_train[:9]
Xresult = F([Xaug.astype('float32'), 0])

Now I get a new error that I cannot understand:

TypeError: Cannot interpret feed_dict key as Tensor: Can not convert a int into a Tensor.

Any help would be appreciated. PS: I am new to TensorFlow and Keras.

Edit 1: The following is the complete code that I am using. I use the Spatial Transformer Network, as discussed in this NIPS paper , and this Kera implementation here

input_shape =  X_train.shape[1:]

# initial weights
b = np.zeros((2, 3), dtype='float32')
b[0, 0] = 1
b[1, 1] = 1
W = np.zeros((100, 6), dtype='float32')
weights = [W, b.flatten()]

locnet = Sequential()
locnet.add(Convolution2D(64, (3, 3), input_shape=input_shape, padding='same'))
locnet.add(Activation('relu'))
locnet.add(Convolution2D(64, (3, 3), padding='same'))
locnet.add(Activation('relu'))
locnet.add(MaxPooling2D(pool_size=(2, 2)))
locnet.add(Convolution2D(128, (3, 3), padding='same'))
locnet.add(Activation('relu'))
locnet.add(Convolution2D(128, (3, 3), padding='same'))
locnet.add(Activation('relu'))
locnet.add(MaxPooling2D(pool_size=(2, 2)))
locnet.add(Convolution2D(256, (3, 3), padding='same'))
locnet.add(Activation('relu'))
locnet.add(Convolution2D(256, (3, 3), padding='same'))
locnet.add(Activation('relu'))
locnet.add(MaxPooling2D(pool_size=(2, 2)))
locnet.add(Dropout(0.5))
locnet.add(Flatten())
locnet.add(Dense(100))
locnet.add(Activation('relu'))
locnet.add(Dense(6, weights=weights))


model = Sequential()

model.add(SpatialTransformer(localization_net=locnet,
                             output_size=(128, 128), input_shape=input_shape))

model.add(Convolution2D(64, (3, 3), padding='same'))
model.add(Activation('relu'))
model.add(Convolution2D(64, (3, 3), padding='same'))
model.add(Activation('relu'))
model.add(MaxPooling2D(pool_size=(2, 2)))
model.add(Convolution2D(128, (3, 3), padding='same'))
model.add(Activation('relu'))
model.add(Convolution2D(128, (3, 3), padding='same'))
model.add(Activation('relu'))
model.add(MaxPooling2D(pool_size=(2, 2)))
model.add(Convolution2D(256, (3, 3), padding='same'))
model.add(Activation('relu'))
model.add(Convolution2D(256, (3, 3), padding='same'))
model.add(Activation('relu'))
model.add(MaxPooling2D(pool_size=(2, 2)))
model.add(Convolution2D(256, (3, 3), padding='same'))
model.add(Activation('relu'))
model.add(Convolution2D(256, (3, 3), padding='same'))
model.add(Activation('relu'))
model.add(MaxPooling2D(pool_size=(2, 2)))
model.add(Dropout(0.5))
model.add(Flatten())
model.add(Dense(256))
model.add(Activation('relu'))

model.add(Dense(num_classes))
model.add(Activation('softmax'))

model.compile(loss='categorical_crossentropy', optimizer='adam', metrics=['accuracy'])

#==============================================================================
# Start Training
#==============================================================================
#define training results logger callback
csv_logger = keras.callbacks.CSVLogger(training_logs_path+'.csv')
model.fit(X_train, y_train,
          batch_size=batch_size,
          epochs=20,
          validation_data=(X_valid, y_valid),
          shuffle=True,
          callbacks=[SaveModelCallback(), csv_logger])




#==============================================================================
# Visualize what Transformer layer has learned
#==============================================================================

XX = model.input
YY = model.layers[0].output
F = K.function([XX, K.learning_phase()], [YY])


Xaug = X_train[:9]
Xresult = F([Xaug.astype('float32'), 0])

# input
for i in range(9):
    plt.subplot(3, 3, i+1)
    plt.imshow(np.squeeze(Xaug[i]))
    plt.axis('off')

for i in range(9):
    plt.subplot(3, 3, i + 1)
    plt.imshow(np.squeeze(Xresult[0][i]))
    plt.axis('off')
+6
1

- Keras . API :

from keras.models import Model

XX = model.input 
YY = model.layers[0].output
new_model = Model(XX, YY)

Xaug = X_train[:9]
Xresult = new_model.predict(Xaug)
+1

Source: https://habr.com/ru/post/1016585/


All Articles