TensorFlow 服务错误 - 找不到与提供的标签匹配的元图 def:{ serve }

TensorFlow Serving Error - Could not find meta graph def matching supplied tags: { serve }

我正在尝试恢复 TensorFlow 的 Saver 对象 (.ckpt.*) 并将其转换为 SavedModel 对象 (.pb),以便我可以使用 TensorFlow Serving 对其进行部署。

我是这样转换的:

    with tf.Session() as sess:

        # Restore the graph from (.meta .data .index)
        saver = tf.train.import_meta_graph(f"{checkpoint_path}/{meta_file_string}")
        saver.restore(sess, tf.train.latest_checkpoint(str(checkpoint_path)))


        # Convert into ".pb" using SavedModel API.
        model_path = f'{savedmodel_path}/1'
        builder = tf.saved_model.builder.SavedModelBuilder(model_path)

        builder.add_meta_graph_and_variables(
            sess, [tf.saved_model.SERVING],
            main_op=tf.tables_initializer(),
            strip_default_attrs=True)

        builder.save()
        print("Saved")

树时保存似乎工作正常:

$ tree 1
1
├── saved_model.pb
└── variables
    ├── variables.data-00000-of-00001
    └── variables.index

1 directory, 3 files

当我使用 saved_model_cli:

$ saved_model_cli show --dir path/to/model/1
The given SavedModel contains the following tag-sets:
serve

但是,当我 运行 TensorFlow 服务 docker 容器时,

$ docker run \
-p 8500:8500 \
-v path/to/model:/models/aaa \
--env MODEL_NAME=aaa \
--name aaa \
tensorflow/serving 

它抱怨找不到我添加的标签 "serve":

2019-11-19 02:35:30.844163: I external/org_tensorflow/tensorflow/cc/saved_model/reader.cc:31] Reading SavedModel from: /models/aaa/1
2019-11-19 02:35:30.916952: I external/org_tensorflow/tensorflow/cc/saved_model/reader.cc:54] Reading meta graph with tags { serve }
2019-11-19 02:35:30.927640: I external/org_tensorflow/tensorflow/cc/saved_model/loader.cc:311] SavedModel load for tags { serve }; Status: fail. Took 83527 microseconds.
2019-11-19 02:35:30.927781: E tensorflow_serving/util/retrier.cc:37] Loading servable: {name: aaa version: 1} failed: Not found: Could not find meta graph def matching supplied tags: { serve }. To inspect available tag-sets in the SavedModel, please use the SavedModel CLI: `saved_model_cli`

我做错了什么,我该如何解决? 否则,我该如何更深入地研究这个问题?

我正在使用 tensorflow 1.14.0。 并使用 docker 图片 tensorFlow-serving:1.14.0-devel.

您需要为您的构建器添加预测签名-

prediction_signature = tf.saved_model.signature_def_utils.predict_signature_def({"input": inputs}, {"output":output})

builder = saved_model_builder.SavedModelBuilder('exported_moddel/')

builder.add_meta_graph_and_variables(session,
                                     [tag_constants.SERVING],
                                    signature_def_map={"classification":prediction_signature})
builder.save()

您可以参考此笔记本了解更多详情 - https://github.com/CS-savvy/tf-graph-preprocessing-addition/blob/master/keras%20inject.ipynb

正在用 :latest 版本替换 tensorflow/serving 图像,目前是 :2.0.0。并且工作正常。

我当地的火车环境仍然使用 TensorFlow 1.14 不知道为什么会这样。