Table 加载 TF 集线器模型时使用 @tf.function 未初始化问题

Table not initialized issue using @tf.function while loading TF hub model

我正在尝试加载 Tf hub 模型并使用 @tf.function 装饰器预测输出。它正在抛出 tensorflow.python.framework.errors_impl.FailedPreconditionError: Table 未初始化。 错误。

TF 版本 - 2.1.0

TF 中心版本 - 0.8.0

注意:它在不使用@tf.function装饰器的情况下工作

import tensorflow as tf
import tensorflow_hub as hub

image_tensor = tf.constant(2.0, shape=[1, 298, 298, 3])


@tf.function
def run_function(method, args):
    return method(args)


detector = hub.KerasLayer("https://tfhub.dev/google/openimages_v4/ssd/mobilenet_v2/1", 
signature_outputs_as_dict=True)
detector_output = run_function(detector, image_tensor)
class_names = detector_output["detection_class_entities"]
print(class_names)

谁能知道它不能与@tf.function一起工作的原因?

您正在 hub.KerasLayer 中使用 TensorFlow V1 hub 模型,该模型将用于 tf2.0 模型

在 TensorFlow hub 中,您可以找到一个切换按钮来查看特定 TensorFlow 版本的 tf hub 模型。

要使用 hub.KeralLayer 使其正常工作,请将 URL 更改为以下任一 tf2.0 mobilenet 版本

  1. https://tfhub.dev/google/tf2-preview/mobilenet_v2/classification/4
  2. https://tfhub.dev/google/imagenet/mobilenet_v2_050_96/classification/4

或者如果您必须像您的示例中那样使用确切的 URL。使用 hub.Module 而不是 hub.KeralLayer