3
$\begingroup$

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?

$\endgroup$
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
2
$\begingroup$

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()
$\endgroup$
1
  • $\begingroup$ vgg16.predict() $\endgroup$ – Dmytro Prylipko Jan 10 '19 at 16:03
2
$\begingroup$

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.

$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.