简体   繁体   中英

Get the values from the flatten layer in a VGG16 architecture fine-tuned model

I am trying to get the values of the flatten layer after fine-tuned it with an image dataset using this method "flatten=model.layers[18].output". This returns a symbolic tensor which cannot be accessed with the.eval() method and it returns this error:

[ValueError: Cannot use the given session to evaluate tensor: the tensor's graph is different from the session's graph.]

Could you please tell me if there is a way to extract the values of a specific layer of a network in order to use it as a numpy array to implement different techniques?

I have used Keras to build the model.

Thank you.

You can use keras functions to get the output of an intermediate layer

import tensorflow as tf

vgg16 = tf.keras.applications.VGG16()
flatten_output = tf.keras.backend.function(vgg16.input, vgg16.get_layer('flatten').output)

image = tf.random.uniform([1, 224, 224, 3])
result = flatten_output(image)

print('Flatten layer outputs:', result)
print('Shape:', result.shape)

output:

Flatten layer outputs: [[0.00208812 0.00597608 0.00412651 ... 0.00086833 0.         0.00281419]]
Shape: (1, 25088)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM