当前位置: 首页 > 面试题库 >

将Keras模型导出为TF估计器:找不到经过训练的模型

裴俊智
2023-03-14
问题内容

尝试将Keras模型导出为TensorFlow
Estimator以便服务模型时遇到以下问题。由于该问题的答案中也弹出相同的问题,因此,我将说明玩具示例中发生的情况,并提供用于文档目的的解决方法。Tensorflow 1.12.0和Keras
2.2.4会发生此行为。实际的Keras和tf.keras

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

下面的代码将其复制为一个玩具示例。

创建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')

接下来,使用将模型转换为estimator
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.

问题答案:

我的解决方法如下。检查./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



 类似资料:
  • 问题内容: 我想知道是否有可能保存经过部分训练的Keras模型并在再次加载模型后继续进行训练。 这样做的原因是,将来我将拥有更多的训练数据,并且我不想再次对整个模型进行训练。 我正在使用的功能是: 编辑1:添加了完全正常的示例 对于10个纪元后的第一个数据集,最后一个纪元的损失将为0.0748,精度为0.9863。 保存,删除和重新加载模型后,第二个数据集上训练的模型的损失和准确性分别为0.171

  • 我的代码是: 我的数据如下: 我的结果是: 两个时代后它就卡在那里了。我能做些什么来防止它这么快卡住?

  • 我们在转换预应变张量流模型时遇到问题,我们将该模型作为以下文件 snapshot_140.ckpt.index snapshot_140.ckpt.meta snapshot_140.ckpt.data-00000-of-00001 当我们使用转换后的tflie文件进行预测时,所有的预测在加载正常的张量流模型时都给出了正确的回归预测 我们得到了上面的这些文件,从ckpt文件到张量流.pb图的转换是

  • /usr/local/lib/python3.6/dist-packages/keras/backend/tensorflow_backend.py:174:不推荐使用名称tf.get_default_session。请改用tf.compat.v1.get_default_session。 /usr/local/lib/python3.6/dist-packages/keras/backend/t

  • 本文向大家介绍浅谈keras通过model.fit_generator训练模型(节省内存),包括了浅谈keras通过model.fit_generator训练模型(节省内存)的使用技巧和注意事项,需要的朋友参考一下 前言 前段时间在训练模型的时候,发现当训练集的数量过大,并且输入的图片维度过大时,很容易就超内存了,举个简单例子,如果我们有20000个样本,输入图片的维度是224x224x3,用fl

  • 在之前的描述中,我们通常把机器学习模型和训练算法当作黑箱子来处理。如果你实践过前几章的一些示例,你惊奇的发现你可以优化回归系统,改进数字图像的分类器,你甚至可以零基础搭建一个垃圾邮件的分类器,但是你却对它们内部的工作流程一无所知。事实上,许多场合你都不需要知道这些黑箱子的内部有什么,干了什么。 然而,如果你对其内部的工作流程有一定了解的话,当面对一个机器学习任务时候,这些理论可以帮助你快速的找到恰