将Keras模型导出为TF估计器:找不到经过训练的模型 [英] Exporting a Keras model as a TF Estimator: trained model cannot be found

查看:244
本文介绍了将Keras模型导出为TF估计器:找不到经过训练的模型的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当试图将Keras模型导出为TensorFlow Estimator以便服务模型时遇到了以下问题.由于同样的问题也弹出了

I encountered the following issue when trying to export a Keras model as a TensorFlow Estimator with the purpose of serving the model. Since the same problem also popped up in an answer to this question, I will illustrate what happens on a toy example and provide my workaround solution for documentation purposes. This behaviour occurs with Tensorflow 1.12.0 and Keras 2.2.4. This happens with actual Keras as well as with tf.keras.

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

The problem occurs when trying to export an Estimator that was created from a Keras model with tf.keras.estimator.model_to_estimator. Upon calling estimator.export_savedmodel, either a NotFoundError or a ValueError is thrown.

以下代码将其复制为一个玩具示例.

The below code reproduces this for a toy example.

创建Keras模型并保存:

Create a Keras model and save it:

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将模型转换为估算器,添加输入接收器函数,并使用estimator.export_savedmodel将其导出为Savedmodel格式:

Next, convert the model to an estimator with tf.keras.estimator.model_to_estimator, add an input receiver function and export it in the Savedmodel format with 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.

推荐答案

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

My workaround solution is as follows. Inspecting the ./tf folder makes clear that the call to model_to_estimator stored the necessary files in a keras subfolder, while export_model expects those files to be in the ./tf folder directly, as this is the path we specified for the model_dir argument:

$ 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完成:

The simple workaround is to move these files up one folder. This can be done with 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目录看起来像这样:

Which will make the model_dir directory look like this:

$ 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调用之间进行此操作可以根据需要导出模型:

Doing this manipulation in between the model_to_estimator and the export_savedmodel calls allows to export the model as desired:

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

INFO:tensorflow:SavedModel written to: ./export/temp-b'1549796240'/saved_model.pb

这篇关于将Keras模型导出为TF估计器:找不到经过训练的模型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆