# Is it possible to integrate Keras and TensorFlow code?

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?

• Yes, it is possible. Perhaps this may help you: youtube.com/watch?v=UeheTiBJ0Io – Rohan Saxena Apr 11 '18 at 15:36
• Keras is now officially part of TF, so it seems possible – Aditya Apr 11 '18 at 15:45

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()

• vgg16.predict() – Dmytro Prylipko Jan 10 '19 at 16:03

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.