首页 > 解决方案 > tf.keras.estimator.model_to_estimator 与 Tf.Hub 故障

问题描述

我需要将模型转换为处理 B64 图像而不是我正在使用的 JSONtf.keras.estimator.model_to_estimator

tf-nightly version:

Version:  1.15.0-dev20190714
Eager mode:  True
Hub version:  0.5.0
GPU is available

基于教程

我使用 tf.Hub 层创建了一个 Keras 模型:

https://tfhub.dev/google/tf2-preview/mobilenet_v2/feature_vector/4

转换为估计器:

estimator = tf.keras.estimator.model_to_estimator(
    keras_model_path=h5_model_path,
    custom_objects={'KerasLayer':hub.KerasLayer},
    model_dir=tf_model_path)

我得到:

ValueError: Unknown layer: KerasLayer

型号总结:

model.summary()

Model: "sequential"
_________________________________________________________________
Layer (type)                 Output Shape              Param #   
=================================================================
keras_layer (KerasLayer)     (None, 1280)              2257984   
_________________________________________________________________
dense (Dense)                (None, 5)                 6405      
=================================================================
Total params: 2,264,389
Trainable params: 6,405
Non-trainable params: 2,257,984

如果我通过 Keras 模型对象:

estimator = tf.keras.estimator.model_to_estimator(
    keras_model=model,
    custom_objects={'KerasLayer':hub.KerasLayer},
    model_dir=tf_model_path)

我得到:

FailedPreconditionError: 2 root error(s) found.
  (0) Failed precondition: Error while reading resource variable save_counter from Container: localhost. This could mean that the variable was uninitialized. Not found: Resource localhost/save_counter/N10tensorflow3VarE does not exist.
     [[{{node save_counter/Read/ReadVariableOp}}]]
  (1) Failed precondition: Error while reading resource variable save_counter from Container: localhost. This could mean that the variable was uninitialized. Not found: Resource localhost/save_counter/N10tensorflow3VarE does not exist.
     [[{{node save_counter/Read/ReadVariableOp}}]]
     [[MobilenetV2/expanded_conv_15/project/weights/Read/ReadVariableOp/_2363]]
0 successful operations.
0 derived errors ignored.

标签: pythontensorflowkeras

解决方案


推荐阅读