Skip to content

Instantly share code, notes, and snippets.

@adityaiitb
Last active August 27, 2020 14:41
Show Gist options
  • Save adityaiitb/17cb98d26248650e5d8c583b68f42523 to your computer and use it in GitHub Desktop.
Save adityaiitb/17cb98d26248650e5d8c583b68f42523 to your computer and use it in GitHub Desktop.
Save & Load a TF saved model. Get TF graph (GraphDef) from a saved model.

TF Saved Model

Save a saved_model

#!/usr/bin/env python3

import tensorflow as tf
import numpy as np

# Get model
model = tf.keras.applications.ResNet50()

# Random input
x = np.random.rand(1,224,224,3).astype(np.float32)

# Run once
result = model(x)

# Save
tf.saved_model.save(model, "./resnet/1/")

Load a saved_model. Get Graph Def.

import tensorflow as tf
from tensorflow.core.protobuf import saved_model_pb2

with tf.io.gfile.GFile("saved_model.pb", mode='rb') as f:
    model = f.read()
    
saved_model = saved_model_pb2.SavedModel()
saved_model.ParseFromString(model)

graph = saved_model.meta_graphs[0].graph_def
print(graph)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment