Skip to content

Instantly share code, notes, and snippets.

@CleanPegasus
Last active September 28, 2020 07:22
Show Gist options
  • Save CleanPegasus/420a684faed737eb4395778a4de15551 to your computer and use it in GitHub Desktop.
Save CleanPegasus/420a684faed737eb4395778a4de15551 to your computer and use it in GitHub Desktop.
Saving model for serving
import tempfile
model_dir = tempfile.gettempdir() # gets the path of the temporary folder
version = 1 # version of the specific model
export_path = os.path.join(model_dir, str(version))
if os.path.isdir(export_path):
print("Found an existing version of the model. Deleting the previous version\n")
!rm -r {export_path} # terminal command to remove a directory
# Saving the model
tf.keras.models.save_model(
model, # the trained model
export_path, # export path for the model
overwrite = True, # overwrite if existing version found
include_optimizer = True, # include the model optimizer
save_format = None,
signature = None,
options = None
)
# If tf.keras.models.save_model() doesn't work, use the model.save function
model.save(export_path, save_format="tf")
print("Model Saved\n")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment