tensorflow - 加载 TF 集线器模型时使用 @tf.function 的表未初始化问题
问题描述
我正在尝试加载 Tf 集线器模型并使用 @tf.function 装饰器预测输出。它正在抛出tensorflow.python.framework.errors_impl.FailedPreconditionError: Table not initialized。错误。
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
将用于 tf2.0 模型的 TensorFlow V1 集线器模型
在 TensorFlow hub 中,您可以找到一个切换按钮来查看特定 TensorFlow 版本的 tf hub 模型。
要使其工作hub.KeralLayer
,请将 URL 更改为以下任一 tf2.0 mobilenet 版本
- https://tfhub.dev/google/tf2-preview/mobilenet_v2/classification/4
- https://tfhub.dev/google/imagenet/mobilenet_v2_050_96/classification/4
或者如果您必须使用示例中的确切 URL。使用hub.Module
代替hub.KeralLayer
推荐阅读
- powerbi - 如何在 PowerBI WebApp 中应用过滤器
- django - 如何管理具有相同后端和前端列表页面的国家/地区部分的 django 管理员
- php - 在 Woocommerce 3 中访问运费对象数据
- python - Add calculated column in lateral join SQLAlchemy
- node.js - Google Auth Exchange 代码、Refresh Token 和 Access Token
- swift - Swift String 省略/转换波兰语重音
- angular - Typescript 动态模块导入抛出错误
- system-verilog - system verilog 二维动态数组随机化
- spring-mvc - 将 Set 中的对象传递给 Controller
- go - 没有参数的函数没有足够的参数