getting the weights of intermediate layer in keras

I have an image dataset 376 classes each class has 15 pictures corresponds to a person. I would like to get the feature vector that corresponds to each person.

What I have done is, after I compiled the model I then used this link as a reference to get the weights of the last convolutional layer. However, when I do this, I get the error:

InvalidArgumentError: You must feed a value for placeholder tensor 'conv_layer' with dtype float and shape [?,19,19,360]


How can I resolve this issue?

Here is the code that I have done so far:

train_data = np.array(train_data, dtype=np.float32)
test_data = np.array(test_data, dtype=np.float32)
train_data = train_data / 180 # to make the array values between 0-1
test_data = test_data / 180
train_label = keras.utils.to_categorical(train_label, 376)
test_label = keras.utils.to_categorical(test_label, 376)
# CNN MODEL
model = Sequential()
activation="relu")) #180 is the number of filters
#compiling the model
model.compile(
loss='categorical_crossentropy',
metrics=['accuracy']
)
model.fit(
train_data,
train_label,
batch_size=32,
epochs=40,
verbose = 2 ,
validation_split=0.1,
shuffle=True)
# getting intermediate  layer weights
get_layer_output = K.function([model.layers[0].input],
[model.layers[11].output])
layer_output = get_layer_output([conv_layer])[0]

• Which layer's output are expecting to keep as face feature vectors? – Kiritee Gak Mar 24 '19 at 14:09
• @KiriteeGak last convolutional layer in this example 7th – Alfaisal Albakri Mar 24 '19 at 14:39

The easiest way to create a truncated output from a network is create a sub-network of it and apply weights of your trained network. The following example is a modification of what you have shown up there, but it will guide you out

Network you want to train originally

model = Sequential()
activation="relu"))
model.compile(
loss='categorical_crossentropy',
metrics=['accuracy'])

model.fit(
train_data,
train_label)


Now create a subnetwork from which you want the outputs, like from above example

model_new = Sequential()
activation="relu"))

model_new.compile(
loss='categorical_crossentropy',
metrics=['mse'])

# You need to apply fit on random array's created, just so as to initialise
# weights. Anyways you will replacing them with original ones from above.
model_new.fit(train_data, y=np.random.rand(40, 3610))


Now take weights from the first trained network and replace the weights of the second network like

model_new.set_weights(weights=model.get_weights())


You can check whether the weights are changed in the above step by actually adding these check statements like

print("Are arrays equal before fit - ",
any([np.array_equal(a1, a2) for a1, a2 in zip(model_new.get_weights(), model.get_weights()[:4])]))

model_new.set_weights(weights=model.get_weights())
print("Are arrays equal after applying weights - ",
all([np.array_equal(a1, a2) for a1, a2 in zip(model_new.get_weights(), model.get_weights()[:4])]))


This should yeild

Are arrays equal before fit -  False
Are arrays equal after applying weights -  True


Hope this helps.

• works perfectly thanks . one more question , how do i know which array corresponds to image class? – Alfaisal Albakri Mar 24 '19 at 18:13
• What do you mean by array? Are you saying output of a filter? You accurately cannot find it. Remember after flattening you have a huge vector and you mapped all of them with some weight onto low dim. using dense layers. So any of the values from the filters would have contributed to the class weight. – Kiritee Gak Mar 24 '19 at 18:25