您好, 欢迎来到 !    登录 | 注册 | | 设为首页 | 收藏本站

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

将Keras模型导出为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

其他 2022/1/1 18:39:49 有465人围观

撰写回答


你尚未登录,登录后可以

和开发者交流问题的细节

关注并接收问题和回答的更新提醒

参与内容的编辑和改进,让解决方法与时俱进

请先登录

推荐问题


联系我
置顶