cmed123
cmed123

Reputation: 705

Tensorflow saved_model.load issue

I'm trying to just be able to load a tensorflow model from a checkpoint, but for some reason I'm getting the error: "The passed save_path is not a valid checkpoint: /path/variables/variables"

I noticed that it adds an extra "variables" string to the path, for some reason. Is that correct? My directory file structure contains the variables.data-- and variables.index files in the /path/variables folder.

The code I'm using to load the model is:

tf.saved_model.loader.load(current_session, [tf.saved_model.tag_constants.SERVING], path)

For saving it, I'm doing:

self.builder = tf.saved_model.builder.SavedModelBuilder(path)
self.builder.add_meta_graph_and_variables(self.sess, [tf.saved_model.tag_constants.SERVING], signature_def_map='prediction': self.prediction.signature,})
self.builder.save()

Upvotes: 1

Views: 2023

Answers (1)

user11530462
user11530462

Reputation:

I noticed that it adds an extra "variables" string to the path, for some reason. Is that correct?

Yes, it correct. Please refer below code, here model saved at ./savedmodel/, where as it loaded from ./savedmodel/variables/variables.

I am able to execute code to save and restore using tf.saved_model works as expected in both Google Colab and Ananconda (Jupyter Notebook).

For the benefit of community here adding save and load using tf.saved_model in Google Colab.

Save Model:

%tensorflow_version 1.x

import tensorflow as tf

# define the tensorflow network and do some trains
x = tf.placeholder("float", name="x")
w = tf.Variable(2.0, name="w")
b = tf.Variable(0.0, name="bias")

h = tf.multiply(x, w)
y = tf.add(h, b, name="y")
sess = tf.Session()
sess.run(tf.global_variables_initializer())

# save the model
export_path =  './savedmodel'
builder = tf.saved_model.builder.SavedModelBuilder(export_path)

tensor_info_x = tf.saved_model.utils.build_tensor_info(x)
tensor_info_y = tf.saved_model.utils.build_tensor_info(y)

prediction_signature = (
  tf.saved_model.signature_def_utils.build_signature_def(
      inputs={'x_input': tensor_info_x},
      outputs={'y_output': tensor_info_y},
      method_name=tf.saved_model.signature_constants.PREDICT_METHOD_NAME))

builder.add_meta_graph_and_variables(
  sess, [tf.saved_model.tag_constants.SERVING],
  signature_def_map={
      tf.saved_model.signature_constants.DEFAULT_SERVING_SIGNATURE_DEF_KEY:
          prediction_signature 
  },
  )
builder.save()

Output:

TensorFlow 1.x selected.
WARNING:tensorflow:From <ipython-input-1-1c4a4b6eef10>:19: build_tensor_info (from tensorflow.python.saved_model.utils_impl) is deprecated and will be removed in a future version.
Instructions for updating:
This function will only be available through the v1 compatibility library as tf.compat.v1.saved_model.utils.build_tensor_info or tf.compat.v1.saved_model.build_tensor_info.
INFO:tensorflow:No assets to save.
INFO:tensorflow:No assets to write.
INFO:tensorflow:SavedModel written to: ./savedmodel/saved_model.pb
b'./savedmodel/saved_model.pb'

Load Model:

import tensorflow as tf
sess=tf.Session() 
signature_key = tf.saved_model.signature_constants.DEFAULT_SERVING_SIGNATURE_DEF_KEY
input_key = 'x_input'
output_key = 'y_output'

export_path =  './savedmodel'
meta_graph_def = tf.saved_model.loader.load(
           sess,
          [tf.saved_model.tag_constants.SERVING],
          export_path)
signature = meta_graph_def.signature_def

x_tensor_name = signature[signature_key].inputs[input_key].name
y_tensor_name = signature[signature_key].outputs[output_key].name

x = sess.graph.get_tensor_by_name(x_tensor_name)
y = sess.graph.get_tensor_by_name(y_tensor_name)

y_out = sess.run(y, {x: 3.0})

print(y_out)

Output:

TensorFlow 1.x selected.
WARNING:tensorflow:From <ipython-input-1-097ac1a9f3ad>:14: load (from tensorflow.python.saved_model.loader_impl) is deprecated and will be removed in a future version.
Instructions for updating:
This function will only be available through the v1 compatibility library as tf.compat.v1.saved_model.loader.load or tf.compat.v1.saved_model.load. There will be a new function for importing SavedModels in Tensorflow 2.0.
INFO:tensorflow:Restoring parameters from ./savedmodel/variables/variables
6.0

Please refer tf.compat.v1 version for Save and load for more details.

Upvotes: 0

Related Questions