I want to use the pre-trained VGG16 model of Keras, along with another TensorFlow model. I want to take the output from one of the layers of VGG16 in Keras, put it into the TensorFlow model and train only the latter. Is this possible if I use a TensorFlow backend for Keras?
2
-
1$\begingroup$ Yes, it is possible. Perhaps this may help you: youtube.com/watch?v=UeheTiBJ0Io $\endgroup$ – Rohan Saxena Apr 11 '18 at 15:36
-
1$\begingroup$ Keras is now officially part of TF, so it seems possible $\endgroup$ – Aditya Apr 11 '18 at 15:45
Add a comment
|
$\begingroup$
$\endgroup$
1
A naive approach to integrate Keras and Tensorflow:
input_img = tf.placeholder(tf.float32, (1,200,300,3), name='input_img')
vgg19 = tf.keras.applications.VGG19(weights='imagenet', include_top=False)
output = vgg19(input_img)
with tf.Session() as sess:
sess.run(tf.global_variables_initializer())
output_val = sess.run(output, {input_img:
np.expand_dims(img,0)})
output_val.shape, output_val.mean()
$\begingroup$
$\endgroup$
I believe you must be able to employ K.function
to get an intermediate tensor from the Keras model. Something like that:
input_tensor = Input(shape=(224, 224, 3))
base_model = VGG16(input_tensor=input_tensor, weights='imagenet')
for layer in base_model.layers:
layer.trainable = False
model = Model(inputs=base_model.input, outputs=base_model.get_layer('block4_pool').output)
keras_model_output = K.function([model.input, model.output])
Then, build a Tensorflow model on the top of the keras_model_output
and train as usual.