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