Reputation: 1
I want to use tensorflow2 to achieve cnn extraction of image features and then output to SVM for classification. What are the good ways? I have checked the documentation of tensorflow2 and there is no good explanation. Who can guide me?
Upvotes: 0
Views: 2658
Reputation: 3876
Thank you for your clarifying answers above. I have written answers to similar questions before. But you can extract intermediate-layer outputs from a tf.keras model by constructing an auxiliary model using the so called functional model API. "Function model API" uses tf.keras.Model(). When you call the function, you specify the arguments inputs
and outputs
. You can include the output of the intermediate layer in the outputs
argument. See the simple code example below:
import tensorflow as tf
# This is the original model.
model = tf.keras.Sequential([
tf.keras.layers.Flatten(input_shape=[28, 28, 1]),
tf.keras.layers.Dense(100, activation="relu"),
tf.keras.layers.Dense(10, activation="softmax")])
# Make an auxiliary model that exposes the output from the intermediate layer
# of interest, which is the first Dense layer in this case.
aux_model = tf.keras.Model(inputs=model.inputs,
outputs=model.outputs + [model.layers[1].output])
# Access both the final and intermediate output of the original model
# by calling `aux_model.predict()`.
final_output, intermediate_layer_output = aux_model.predict(some_input)
Upvotes: 5