【问题标题】:Exporting a Keras model as a TF Estimator: trained model cannot be found将 Keras 模型导出为 TF Estimator:找不到经过训练的模型
【发布时间】:2019-02-10 11:00:21
【问题描述】:

在尝试将 Keras 模型导出为 TensorFlow Estimator 以提供模型服务时,我遇到了以下问题。由于同样的问题也出现了in an answer to this question,我将说明在一个玩具示例上发生了什么,并提供我的解决方案以用于文档目的。 Tensorflow 1.12.0 和 Keras 2.2.4 会出现此行为。实际的 Keras 以及 tf.keras 都会发生这种情况。

当尝试使用tf.keras.estimator.model_to_estimator 导出从 Keras 模型创建的 Estimator 时会出现问题。调用 estimator.export_savedmodel 时,会抛出 NotFoundErrorValueError

下面的代码为玩具示例重现了这一点。

创建一个 Keras 模型并保存:

import keras
model = keras.Sequential()
model.add(keras.layers.Dense(units=1,
                                activation='sigmoid',
                                input_shape=(10, )))
model.compile(loss='binary_crossentropy', optimizer='sgd')
model.save('./model.h5')

接下来,使用tf.keras.estimator.model_to_estimator 将模型转换为估计器,添加输入接收器函数并使用Savedmodel 将其导出为estimator.export_savedmodel 格式:

# Convert keras model to TF estimator
tf_files_path = './tf'
estimator =\
    tf.keras.estimator.model_to_estimator(keras_model=model,
                                          model_dir=tf_files_path)
def serving_input_receiver_fn():
    return tf.estimator.export.build_raw_serving_input_receiver_fn(
        {model.input_names[0]: tf.placeholder(tf.float32, shape=[None, 10])})

# Export the estimator
export_path = './export'
estimator.export_savedmodel(
    export_path,
    serving_input_receiver_fn=serving_input_receiver_fn())

这会抛出:

ValueError: Couldn't find trained model at ./tf.

【问题讨论】:

    标签: python tensorflow keras


    【解决方案1】:

    我的解决方法如下。检查./tf 文件夹可以清楚地看到对model_to_estimator 的调用将必要的文件存储在keras 子文件夹中,而export_model 期望这些文件直接位于./tf 文件夹中,因为这是我们指定的路径对于model_dir 参数:

    $ tree ./tf
    ./tf
    └── keras
        ├── checkpoint
        ├── keras_model.ckpt.data-00000-of-00001
        ├── keras_model.ckpt.index
        └── keras_model.ckpt.meta
    
    1 directory, 4 files
    

    简单的解决方法是将这些文件移到一个文件夹中。这可以用 Python 完成:

    import os
    import shutil
    from pathlib import Path
    
    def up_one_dir(path):
        """Move all files in path up one folder, and delete the empty folder
        """
        parent_dir = str(Path(path).parents[0])
        for f in os.listdir(path):
            shutil.move(os.path.join(path, f), parent_dir)
        shutil.rmtree(path)
    
    up_one_dir('./tf/keras')
    

    这将使model_dir 目录看起来像这样:

    $ tree ./tf
    ./tf
    ├── checkpoint
    ├── keras_model.ckpt.data-00000-of-00001
    ├── keras_model.ckpt.index
    └── keras_model.ckpt.meta
    
    0 directories, 4 files
    

    model_to_estimatorexport_savedmodel 调用之间进行此操作允许根据需要导出模型:

    export_path = './export'
    estimator.export_savedmodel(
        export_path,
        serving_input_receiver_fn=serving_input_receiver_fn())
    

    INFO:tensorflow:SavedModel 写入: ./export/temp-b'1549796240'/saved_model.pb

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2019-01-20
      • 2017-07-28
      • 1970-01-01
      • 2020-01-16
      • 2019-05-28
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多