在https://github.com/MtDersvan/tf_playground/blob/master/wide_and_deep_tutorial/wide_and_deep_basic_serving.md上完成SavedModel的“深入学习”教程之后,我似乎无法使用Tensorflow随附的saved_model_cli查询SavedModel。
这是我已经尝试过的示例:
saved_model_cli run --dir /tmp/serving_savemodel/1504908631 --tag_set serve --signature_def serving_default --input_exprs 'age=25;capital_gain=0;capital_loss=0;education=11th;education_num=7;gender=Male;hours_per_week=40;native_country=United-States;occupation=Machine-op-inspct;relationship=Own-child;workclass=Private'
最佳答案
MtDersvan answered他的Github repo中的问题对我来说很成功(由于格式错误,请避免引用):
因为输入是序列化的tf.Example,所以您需要将feature_dict转换为序列化的原始张量,如下所示:
import tensorflow as tf
def _float_feature(value):
return tf.train.Feature(float_list=tf.train.FloatList(value=[value]))
def _bytes_feature(value):
return tf.train.Feature(bytes_list=tf.train.BytesList(value=[value]))
feature_dict = {'age': _float_feature(value=25),
'capital_gain': _float_feature(value=0),
'capital_loss': _float_feature(value=0),
'education': _bytes_feature(value='11th'.encode()),
'education_num': _float_feature(value=7),
'gender': _bytes_feature(value='Male'.encode()),
'hours_per_week': _float_feature(value=40),
'native_country': _bytes_feature(value='United-States'.encode()),
'occupation': _bytes_feature(value='Machine-op-inspct'.encode()),
'relationship': _bytes_feature(value='Own-child'.encode()),
'workclass': _bytes_feature(value='Private'.encode())}
example = tf.train.Example(features=tf.train.Features(feature=feature_dict))
serialized = example.SerializeToString()
serialized_proto = tf.contrib.util.make_tensor_proto(serialized, shape=[1])
serialized_proto_handle = serialized_proto.string_val
这个serialized_proto_handle应该看起来像这样:
[b'\n\xaa\x02\n\x1a\n\x0ehours_per_week\x12\x08\x12\x06\n\x04\x00\x00 B\n\x1d\n\x0crelationship\x12\r\n\x0b\n\tOwn-child\n\x19\n\reducation_num\x12\x08\x12\x06\n\x04\x00\x00\xe0@\n#\n\noccupation\x12\x15\n\x13\n\x11Machine-op-inspct\n\x18\n\x0ccapital_gain\x12\x08\x12\x06\n\x04\x00\x00\x00\x00\n\x18\n\x0ccapital_loss\x12\x08\x12\x06\n\x04\x00\x00\x00\x00\n\x15\n\teducation\x12\x08\n\x06\n\x0411th\n\x0f\n\x03age\x12\x08\x12\x06\n\x04\x00\x00\xc8A\n#\n\x0enative_country\x12\x11\n\x0f\n\rUnited-States\n\x18\n\tworkclass\x12\x0b\n\t\n\x07Private\n\x12\n\x06gender\x12\x08\n\x06\n\x04Male']
因此,这可以单独完成,例如在ipython或python脚本中。接下来,您可以将其转储到文件(pickle,.npy,.npz)中,也可以直接从控制台进行射击:
saved_model_cli run --dir ./wide_and_deep_tutorial/serving_savemodel/${MODEL_DIR} \
--tag_set serve --signature_def serving_default \
--input_exprs "inputs=[b'\n\xaa\x02\n\x1a\n\x0ehours_per_week\x12\x08\x12\x06\n\x04\x00\x00 B\n\x1d\n\x0crelationship\x12\r\n\x0b\n\tOwn-child\n\x19\n\reducation_num\x12\x08\x12\x06\n\x04\x00\x00\xe0@\n#\n\noccupation\x12\x15\n\x13\n\x11Machine-op-inspct\n\x18\n\x0ccapital_gain\x12\x08\x12\x06\n\x04\x00\x00\x00\x00\n\x18\n\x0ccapital_loss\x12\x08\x12\x06\n\x04\x00\x00\x00\x00\n\x15\n\teducation\x12\x08\n\x06\n\x0411th\n\x0f\n\x03age\x12\x08\x12\x06\n\x04\x00\x00\xc8A\n#\n\x0enative_country\x12\x11\n\x0f\n\rUnited-States\n\x18\n\tworkclass\x12\x0b\n\t\n\x07Private\n\x12\n\x06gender\x12\x08\n\x06\n\x04Male']"
Result for output key classes:
[[b'0' b'1']]
Result for output key scores:
[[ 0.85787761 0.14212234]]
关于python - 如何使用saved_model_cli查询tensorflow的``宽和深''模型?,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/46186746/