我正在尝试加载在Python3中使用export_savedmodel
保存的TensorFlow模型,但我无法弄清错误的出处。
按照here
的说明创建和训练模型然后将其保存如下:
def serving_input_receiver_fn():
serialized_tf_example = tf.placeholder(tf.string, name='tf_example')
feature_configs = {'x': tf.FixedLenFeature(shape=[784], dtype=tf.float32), }
tf_example = tf.parse_example(serialized_tf_example, feature_configs)
receiver_tensors = {'examples': serialized_tf_example}
return tf.estimator.export.ServingInputReceiver(tf_example, receiver_tensors)
mnist_classifier.export_savedmodel(export_path, serving_input_receiver_fn)
在另一个脚本中,我尝试加载模型:
import os
import tensorflow as tf
import numpy as np
from tensorflow.contrib import predictor
import_dir = os.path.join("save", "1")
# Load some data
mnist = tf.contrib.learn.datasets.load_dataset("mnist")
eval_data = mnist.test.images # Returns np.array
eval_labels = np.asarray(mnist.test.labels, dtype=np.int32)
eval_input_fn = tf.estimator.inputs.numpy_input_fn(
x={"examples": eval_data},
y=eval_labels,
batch_size=100,
num_epochs=None,
shuffle=True)
#try to load it
predict_fn = predictor.from_saved_model(import_dir)
predictions = predict_fn(
{"examples": eval_input_fn})
print(predictions['probabilities'])
#try to load it in a session
with tf.Session() as sess:
tf.saved_model.loader.load(sess, [tf.saved_model.tag_constants.SERVING], import_dir)
predictor2 = predictor.from_saved_model(import_dir)
output = predictor2(
{"examples": eval_input_fn}
)
print(output['probabilities'])
到达output = predictor2({"examples": eval_input_fn})
时,出现以下错误:
tensorflow.python.framework.errors_impl.InternalError:无法将元素获取为字节。
我认为将输入数据提供给预测变量可能会有问题,这就是为什么我也尝试仅使用eval_data和eval_input_fn的原因。
模型文件夹包含[timestamp]> saved_model.pb和变量文件夹。变量文件夹包含variables.data和variables.index。
您是否知道导致此错误的原因?
堆栈跟踪:
Traceback (most recent call last):
File "C:\Users\owner\AppData\Local\conda\conda\envs\tensorflowvenv\lib\site-packages\tensorflow\python\client\session.py", line 1322, in _do_call
return fn(*args)
File "C:\Users\owner\AppData\Local\conda\conda\envs\tensorflowvenv\lib\site-packages\tensorflow\python\client\session.py", line 1307, in _run_fn
options, feed_dict, fetch_list, target_list, run_metadata)
File "C:\Users\owner\AppData\Local\conda\conda\envs\tensorflowvenv\lib\site-packages\tensorflow\python\client\session.py", line 1409, in _call_tf_sessionrun
run_metadata)
tensorflow.python.framework.errors_impl.InternalError: Unable to get element as bytes.
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "C:/Users/owner/PycharmProjects/DiU/loadsavedmodel.py", line 26, in <module>
{"examples": eval_input_fn})
File "C:\Users\owner\AppData\Local\conda\conda\envs\tensorflowvenv\lib\site-packages\tensorflow\contrib\predictor\predictor.py", line 77, in __call__
return self._session.run(fetches=self.fetch_tensors, feed_dict=feed_dict)
File "C:\Users\owner\AppData\Local\conda\conda\envs\tensorflowvenv\lib\site-packages\tensorflow\python\client\session.py", line 900, in run
run_metadata_ptr)
File "C:\Users\owner\AppData\Local\conda\conda\envs\tensorflowvenv\lib\site-packages\tensorflow\python\client\session.py", line 1135, in _run
feed_dict_tensor, options, run_metadata)
File "C:\Users\owner\AppData\Local\conda\conda\envs\tensorflowvenv\lib\site-packages\tensorflow\python\client\session.py", line 1316, in _do_run
run_metadata)
File "C:\Users\owner\AppData\Local\conda\conda\envs\tensorflowvenv\lib\site-packages\tensorflow\python\client\session.py", line 1335, in _do_call
raise type(e)(node_def, op, message)
tensorflow.python.framework.errors_impl.InternalError: Unable to get element as bytes.