看看this post,它包含您需要的信息。如果没有,请回复,我会帮助您准备一些代码,不过您可能需要 url 安全 b64 变体。
编辑
您的代码有点混乱,我不认为输入已经连接到您的图表,您是否使用 tf.summary.FileWriter('output folder', sess.graph) 看过图表?
我将逐步解释如何在模型前面构建一些层,并通过一些示例,这段代码不应该在 retrain.py 中,可以在训练模型后运行。
1) 加载您的 tensorflow 模型,如果它是使用 savedModelBuilder 构建的,或者您可以像 this 一样进行简单的保存操作:
def loader(path):
with tf.Session(graph=tf.Graph()) as sess:
tf.saved_model.loader.load(sess, [tag_constants.TRAINING], path)
return tf.get_default_graph().as_graph_def()
tagconstants 可以用saved_model_cli tool 检查,在你的情况下这可能必须是空的[]。
2) 添加您需要的层/张量,您需要接受一个字节字符串,或者在这种情况下为 base 64 的东西,将其解码并将其转换为 3D 图像:
image_str_tensor = tf.placeholder(dtype=tf.string, shape=(None,), name='input_image_bytes')
input_image = tf.decode_base64(image_str_tensor)
decoder = tf.image.decode_jpeg(input_image[0], channels=3)
如果您从 retrain.py 中获得其他张量,例如转换为浮点数、dim_expanding 和整形,应该已经在图中。
3) 通过将它们输入到图表中来将它们实现到您的图表中。
graph_def_inception = loader('path to your saved model')
output_prediction, = tf.import_graph_def(graph_def_inception, input_map={"DecodeJpeg:0": decoder}, return_elements=['final_result:0'], name="")
4) 创建一个已保存的模型并检查一切是否如您所愿!
builder = tf.saved_model.builder.SavedModelBuilder('output/model/path')
with tf.Session() as sess:
tf.summary.FileWriter('output/graph_log/files', sess.graph)
input_tensor_info = tf.saved_model.utils.build_tensor_info(input_image)
output_tensor_info = tf.saved_model.utils.build_tensor_info(output_prediction)
signature = tf.saved_model.signature_def_utils.build_signature_def(
inputs={'input_image': input_tensor_info},
outputs={'output_prediction': output_tensor_info},
method_name=tf.saved_model.signature_constants.PREDICT_METHOD_NAME)
# save as SavedModel
builder.add_meta_graph_and_variables(sess,
[tf.saved_model.tag_constants.SERVING],
signature_def_map={'serving_default': signature})
builder.save()
5) 如果遇到错误,请尝试使用 tensorboard 进行调试
tensorboard --logdir=output/graph_log/files
希望对我有所帮助,此代码在第一次尝试时无法正常工作,您需要对某些部分感到困惑。如果你真的不能成功,那么你应该分享模型,也许我可以做到,如果我有时间的话,把代码分享给你。