在 tensorflow 服务中服务 USE4 模型时如何解决 "Table not initialized" 问题?

How to resolve "Table not initialized" issue while serving USE4 model in tensorflow serving?

所以我使用以下 link 准备了一个包含 USE4 的 keras 模型: https://tfhub.dev/google/universal-sentence-encoder-large/4

使用tensorflow 2.3,我将它添加到我的模型中并保存了模型

tf.saved_model.save(
model,
export_dir= '/directory/model/1',
)

在 docker 中投放后,我能够获取元数据,但预测请求不起作用。这是请求:

{"inputs":{"text":["Hello"]}}

回复:

{ "error": "[_Derived_]{{function_node __inference_pruned_16231}} {{function_node __inference_pruned_16231}} Table not initialized.\n\t [[{{node text_preprocessor_1/hash_table_Lookup/hash_table_Lookup/LookupTableFindV2}}]]\n\t [[StatefulPartitionedCall/StatefulPartitionedCall/sequential/keras_layer/StatefulPartitionedCall/StatefulPartitionedCall/StatefulPartitionedCall]]" }

我在这里 https://github.com/awslabs/amazon-sagemaker-examples/issues/773#issuecomment-509433290 了解到,这个问题可以在旧的 tf 版本中通过在旧的 tf.saved_model.simple_save() 函数中使用命令 tf.tables_initializer() 来解决。但是在 2.3.0 中还有什么选择呢?

您可以使用该模型的版本 5 重试吗(link)? Your issue looks related to this GitHub 问题,该问题已通过使用模型的 v5 解决。如果没有解决,请重现一个最小片段这个问题会有所帮助。

函数 'tf.saved_model.simple_save()' 有一个参数 'legacy_init_op',默认值为 'None'。为了避免模型部署 docker 后的问题 'Table not initialized',也许你可以试试这个:

legacy_init_op = tf.group(tf.compat.v1.tables_initializer(), name='init_all_tables2')
tf.saved_model.simple_save(sess,
                       your_model_path, 
                       inputs={"xxx": xxx}, 
                       outputs={"xxx": xxx},
                       legacy_init_op=legacy_init_op
                      )