python - Can't run a command line prediction against a saved Tensorflow model -
i'm having trouble exporting , running simple example command line. example trains on random numbers 0-99 , labels values 0 if less 50, otherwise label 1. want able save model use later generate prediction against value entered via command line.
here rough guide using model saving , cli.
the environment tensorflow docker image.
here program - takes around 10 seconds run in environment.
"""create simple model can run cli.""" import math import os import numpy np os.environ['tf_cpp_min_log_level'] = '2' # prevents compiler warnings import tensorflow tf # pylint: disable=import-error def generate_value_list(size=100): """return array of random values 0-99.""" value_list = np.random.rand(size) value_list = [math.trunc(x * 100) x in value_list] return value_list def generate_label_list(value_list): """return array of labels passed values.""" label_list = [0 if x < 50 else 1 x in value_list] return label_list def input_fn(): """generate input training or evaluating.""" value_list = generate_value_list() label_list = generate_label_list(value_list) features = {"value_list": value_list} return features, label_list def main(): """execute main program.""" content = tf.feature_column.numeric_column(key='value_list') columns = [content] estimator = tf.estimator.linearclassifier( feature_columns=columns, optimizer=tf.train.ftrloptimizer( learning_rate=0.1, l1_regularization_strength=0.001)) estimator.train(input_fn=input_fn, steps=500) result = estimator.evaluate(input_fn=input_fn, steps=5) print result feature_spec = tf.feature_column.make_parse_example_spec(columns) export_input_fn = tf.estimator.export.build_parsing_serving_input_receiver_fn(feature_spec) estimator.export_savedmodel('/notebooks/model', export_input_fn) if __name__ == "__main__": main() after running program have model saved in model directory. see model interface, run following command:
saved_model_cli show --dir /notebooks/model/1505225506 --all and this:
metagraphdef tag-set: 'serve' contains following signaturedefs: signature_def['none']: given savedmodel signaturedef contains following input(s): inputs['inputs'] tensor_info: dtype: dt_string shape: (-1) name: input_example_tensor:0 given savedmodel signaturedef contains following output(s): outputs['classes'] tensor_info: dtype: dt_string shape: (-1, 2) name: linear/head/tile:0 outputs['scores'] tensor_info: dtype: dt_float shape: (-1, 2) name: linear/head/predictions/probabilities:0 method name is: tensorflow/serving/classify signature_def['classification']: given savedmodel signaturedef contains following input(s): inputs['inputs'] tensor_info: dtype: dt_string shape: (-1) name: input_example_tensor:0 given savedmodel signaturedef contains following output(s): outputs['classes'] tensor_info: dtype: dt_string shape: (-1, 2) name: linear/head/tile:0 outputs['scores'] tensor_info: dtype: dt_float shape: (-1, 2) name: linear/head/predictions/probabilities:0 method name is: tensorflow/serving/classify signature_def['regression']: given savedmodel signaturedef contains following input(s): inputs['inputs'] tensor_info: dtype: dt_string shape: (-1) name: input_example_tensor:0 given savedmodel signaturedef contains following output(s): outputs['outputs'] tensor_info: dtype: dt_float shape: (-1, 1) name: linear/head/predictions/logistic:0 method name is: tensorflow/serving/regress signature_def['serving_default']: given savedmodel signaturedef contains following input(s): inputs['inputs'] tensor_info: dtype: dt_string shape: (-1) name: input_example_tensor:0 given savedmodel signaturedef contains following output(s): outputs['classes'] tensor_info: dtype: dt_string shape: (-1, 2) name: linear/head/tile:0 outputs['scores'] tensor_info: dtype: dt_float shape: (-1, 2) name: linear/head/predictions/probabilities:0 method name is: tensorflow/serving/classify next run prediction on number input:
saved_model_cli run --dir /notebooks/model/1505225506 --tag_set serve --signature_def serving_default --input_exprs 'inputs=[32]' and this:
traceback (most recent call last): file "/usr/local/bin/saved_model_cli", line 11, in <module> sys.exit(main()) file "/usr/local/lib/python2.7/dist-packages/tensorflow/python/tools/saved_model_cli.py", line 649, in main args.func(args) file "/usr/local/lib/python2.7/dist-packages/tensorflow/python/tools/saved_model_cli.py", line 529, in run args.overwrite, tf_debug=args.tf_debug) file "/usr/local/lib/python2.7/dist-packages/tensorflow/python/tools/saved_model_cli.py", line 298, in run_saved_model_with_feed_dict outputs = sess.run(output_tensor_names_sorted, feed_dict=inputs_feed_dict) file "/usr/local/lib/python2.7/dist-packages/tensorflow/python/client/session.py", line 895, in run run_metadata_ptr) file "/usr/local/lib/python2.7/dist-packages/tensorflow/python/client/session.py", line 1124, in _run feed_dict_tensor, options, run_metadata) file "/usr/local/lib/python2.7/dist-packages/tensorflow/python/client/session.py", line 1321, in _do_run options, run_metadata) file "/usr/local/lib/python2.7/dist-packages/tensorflow/python/client/session.py", line 1340, in _do_call raise type(e)(node_def, op, message) tensorflow.python.framework.errors_impl.internalerror: unable element feed bytes. i've tried many variations input model , 1 seems furthest in code before erroring out.
any ideas i'm doing wrong? there better way accomplish similar?
Comments
Post a Comment