Skip to content

Instantly share code, notes, and snippets.

@himanshurawlani
Created October 19, 2018 21:54
Show Gist options
  • Star 6 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save himanshurawlani/8a017b2a893060d1282d13ea4a8ddefc to your computer and use it in GitHub Desktop.
Save himanshurawlani/8a017b2a893060d1282d13ea4a8ddefc to your computer and use it in GitHub Desktop.
Script to convert Keras models to TensorFlow Serving SavedModel format
import tensorflow as tf
# The export path contains the name and the version of the model
tf.keras.backend.set_learning_phase(0) # Ignore dropout at inference
model = tf.keras.models.load_model('./inception.h5')
export_path = '../my_image_classifier/1'
# Fetch the Keras session and save the model
# The signature definition is defined by the input and output tensors
# And stored with the default serving key
with tf.keras.backend.get_session() as sess:
tf.saved_model.simple_save(
sess,
export_path,
inputs={'input_image': model.input},
outputs={t.name: t for t in model.outputs})
@Jonarod
Copy link

Jonarod commented Jul 12, 2019

I get the following error:

RuntimeError: Attempted to use a closed Session.

@LunaonMSK
Copy link

I get the same error
RuntimeError: Attempted to use a closed Session.

@LunaonMSK
Copy link

solved..

with keras.backend.get_session() as sess:
         tf.saved_model.simple_save(
              sess,
              export_path,
              inputs={'input_image': model.input},
              outputs={t.name: t for t in model.outputs}
        )

the last line break is needed

@i-amgeek
Copy link

i-amgeek commented Feb 3, 2020

I got the error - AttributeError: 'list' object has no attribute 'dtype'

@svetlanadataper
Copy link

I have the same error as @i-amgeek , how do I solve this?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment