Tensorflow版本1.10
使用:DNNClassifier
和tf.estimator.FinalExporter
我正在使用TF blog中的虹膜示例。 我定义了以下代码:
# The CSV features in our training & test data.
COLUMN_NAMES = ['SepalLength',
'SepalWidth',
'PetalLength',
'PetalWidth',
'Species']
FEATURE_COLUMNS = COLUMN_NAMES[:4]
INPUT_COLUMNS = [
tf.feature_column.numeric_column(column) for column in COLUMN_NAMES
]
def serving_input_receiver_fn():
"""Build the serving inputs."""
inputs = {}
for feat in INPUT_COLUMNS:
inputs[feat.name] = tf.placeholder(shape=[None], dtype=feat.dtype)
return tf.estimator.export.ServingInputReceiver(inputs, inputs)
这就是我调用函数的方式:
train_spec = tf.estimator.TrainSpec(
train_input, max_steps=hparams.train_steps)
exporter = tf.estimator.FinalExporter(
'iris', serving_input_receiver_fn)
eval_spec = tf.estimator.EvalSpec(
eval_input,
steps=hparams.eval_steps,
exporters=[exporter],
name='iris-eval')
run_config = tf.estimator.RunConfig(
session_config=_get_session_config_from_env_var())
run_config = run_config.replace(model_dir=hparams.job_dir)
print('Model dir: %s', run_config.model_dir)
estimator = model.build_estimator(
# Construct layers sizes.
config=run_config,
hidden_units=[10, 20, 10],
n_classes=3)
tf.estimator.train_and_evaluate(estimator, train_spec, eval_spec)
我收到以下消息:
INFO:tensorflow:Done calling model_fn.
INFO:tensorflow:Signatures INCLUDED in export for Eval: None
INFO:tensorflow:Signatures INCLUDED in export for Classify: None
INFO:tensorflow:Signatures INCLUDED in export for Regress: None
INFO:tensorflow:Signatures INCLUDED in export for Predict: ['predict']
INFO:tensorflow:Signatures INCLUDED in export for Train: None
INFO:tensorflow:Signatures EXCLUDED from export because they cannot be be served via TensorFlow Serving APIs:
INFO:tensorflow:'serving_default' : Classification input must be a single string Tensor; got {'SepalLength': <tf.Tensor 'Placeholder:0' shape=(?,) dtype=float32>, 'PetalLength': <tf.Tensor 'Placeholder_2:0' shape=(?,) dtype=float32>, 'PetalWidth': <tf.Tensor 'Placeholder_3:0' shape=(?,) dtype=float32>, 'SepalWidth': <tf.Tensor 'Placeholder_1:0' shape=(?,) dtype=float32>, 'Species': <tf.Tensor 'Placeholder_4:0' shape=(?,) dtype=float32>}
INFO:tensorflow:'classification' : Classification input must be a single string Tensor; got {'SepalLength': <tf.Tensor 'Placeholder:0' shape=(?,) dtype=float32>, 'PetalLength': <tf.Tensor 'Placeholder_2:0' shape=(?,) dtype=float32>, 'PetalWidth': <tf.Tensor 'Placeholder_3:0' shape=(?,) dtype=float32>, 'SepalWidth': <tf.Tensor 'Placeholder_1:0' shape=(?,) dtype=float32>, 'Species': <tf.Tensor 'Placeholder_4:0' shape=(?,) dtype=float32>}
WARNING:tensorflow:Export includes no default signature!
当我打印serving_input_receiver_fn
时,我得到:
ServingInputReceiver(features={'sepal_width': <tf.Tensor 'Placeholder_1:0' shape=(?, 1) dtype=float32>, 'petal_width': <tf.Tensor 'Placeholder_3:0' shape=(?, 1) dtype=float32>, 'sepal_length': <tf.Tensor 'Placeholder:0' shape=(?, 1) dtype=float32>, 'petal_length': <tf.Tensor 'Placeholder_2:0' shape=(?, 1) dtype=float32>}, receiver_tensors={'sepal_width': <tf.Tensor 'Placeholder_1:0' shape=(?, 1) dtype=float32>, 'petal_width': <tf.Tensor 'Placeholder_3:0' shape=(?, 1) dtype=float32>, 'sepal_length': <tf.Tensor 'Placeholder:0' shape=(?, 1) dtype=float32>, 'petal_length': <tf.Tensor 'Placeholder_2:0' shape=(?, 1) dtype=float32>}, receiver_tensors_alternatives=None)
在导出文件夹中没有任何内容(CSV,JSON等):
gs://<my-bucket>/iris/iris_20181014_214916/export/:
gs://<my-bucket>/iris/iris_20181014_214916/export/
答案 0 :(得分:0)
我找到了解决方法here。
def _make_input_parser(with_target=True):
"""Returns a parser func according to file_type, task_type and target.
Need to set record_default for last column to integer instead of float in
case of classification tasks.
Args:
with_target (boolean): Pass label or not.
Returns:
It returns a parser.
"""
def _decode_csv(line):
"""Takes the string input tensor and parses it to feature dict and target.
All the columns except the first one are treated as feature column. The
first column is expected to be the target.
Only returns target for if with_target is True.
Args:
line: csv rows in tensor format.
Returns:
features: A dictionary of features with key as "column_names" from
self._column_header.
target: tensor of target values which is the first column of the file.
This will only be returned if with_target==True.
"""
column_header = column_names if with_target else column_names[:4]
record_defaults = [[0.] for _ in xrange(len(column_names) - 1)]
# Pass label as integer.
if with_target:
record_defaults.append([0])
columns = tf.decode_csv(line, record_defaults=record_defaults)
features = dict(zip(column_header, columns))
target = features.pop(column_names[4]) if with_target else None
return features, target
return _decode_csv
def serving_input_receiver_fn():
"""This is used to define inputs to serve the model.
Returns:
A ServingInputReciever object.
"""
csv_row = tf.placeholder(shape=[None], dtype=tf.string)
features, _ = _make_input_parser(with_target=False)(csv_row)
return tf.estimator.export.ServingInputReceiver(features,
{'csv_row': csv_row})