tensorboard icon indicating copy to clipboard operation
tensorboard copied to clipboard

What-If tool could not infer when exporting a model built by estimator.DNNClassifier

Open guygeva opened this issue 8 years ago • 12 comments

  • TensorBoard version - built it locally. I cloned the git repository - the latest commit in my local repository is c1e8878b42c16e7018f54613f847b59d357e971e
  • TensorFlow version - 1.4
  • OS Platform and version - Linux Ubuntu 16.04
  • Python version - 3.6

I am trying to use the What-If tool for a model created by using the tf.estimator.DNNClassifier canned estimator. The model was exported and saved. I ran a tensorflow-serving docker and tested is and got back valid results using the following curl command: curl -d '{"signature_name":"predict","instances":[{.......}]}' -X POST http://192.168.30.15:8501/v1/models/some_model_name:predict

I provided the What-If tool the following definitions: image

No infer was done, and I see this message on the TensorBoard output:

/home/guyg/projects/external/tensorboard/bazel-bin/tensorboard/tensorboard.runfiles/org_tensorflow_tensorboard/tensorboard/plugins/interactive_inference/utils/serving/prediction_service_pb2.py:131: DeprecationWarning: beta_create_PredictionService_stub() method is deprecated. This method will be removed in near future versions of TF Serving. Please switch to GA gRPC API in prediction_service_pb2_grpc. 'prediction_service_pb2_grpc.', DeprecationWarning) E0913 15:51:04.739914 Thread-2 _internal.py:87] Error on request: Traceback (most recent call last): File "/home/guyg/.cache/bazel/_bazel_guyg/10aad5f1b9b4fd4ada8c4066e616f3cc/external/org_pocoo_werkzeug/werkzeug/serving.py", line 193, in run_wsgi execute(self.server.app) File "/home/guyg/.cache/bazel/_bazel_guyg/10aad5f1b9b4fd4ada8c4066e616f3cc/external/org_pocoo_werkzeug/werkzeug/serving.py", line 181, in execute application_iter = app(environ, start_response) File "/home/guyg/projects/external/tensorboard/tensorboard/backend/application.py", line 298, in __call__ return self.data_applications[clean_path](environ, start_response) File "/home/guyg/.cache/bazel/_bazel_guyg/10aad5f1b9b4fd4ada8c4066e616f3cc/external/org_pocoo_werkzeug/werkzeug/wrappers.py", line 291, in application return f(*args[:-2] + (request,))(*args[-2:]) File "/home/guyg/projects/external/tensorboard/tensorboard/plugins/interactive_inference/interactive_inference_plugin.py", line 257, in _infer examples_to_infer, serving_bundle) File "/home/guyg/projects/external/tensorboard/tensorboard/plugins/interactive_inference/utils/oss_utils.py", line 136, in call_servo return stub.Classify(request, 30.0) # 30 secs timeout File "/home/guyg/anaconda3/envs/NERD/lib/python3.6/site-packages/grpc/_channel.py", line 487, in __call__ return _end_unary_response_blocking(state, call, False, deadline) File "/home/guyg/anaconda3/envs/NERD/lib/python3.6/site-packages/grpc/_channel.py", line 437, in _end_unary_response_blocking raise _Rendezvous(state, None, None, deadline) grpc._channel._Rendezvous: <_Rendezvous of RPC that terminated with (StatusCode.UNAVAILABLE, Trying to connect an http1.x server)>

guygeva avatar Sep 13 '18 13:09 guygeva

cc @jameswex

nfelt avatar Sep 13 '18 17:09 nfelt

out of curiosity, does it work if you run the model in a tensorflow serving model server locally on the machine your tensorboard server is running on and use localhost: instead?

jameswex avatar Sep 13 '18 19:09 jameswex

192.168.30.15 is actually the IP address of the localhost, but I also tried replacing it with 'localhost' and got the same result.

guygeva avatar Sep 16 '18 07:09 guygeva

@guygeva are your model and test data senstive, or could you share them with me? I can debug your specific problem if I can have access to them.

jameswex avatar Sep 17 '18 14:09 jameswex

I just tested a simple iris classifier with DNNClassifer that was exported to saved model and served in a TF serving docker and was able to use the What-If Tool with it. One difference is that I had to set the port in the What-If Tool to 8500 (not 8501). In general, I followed directions at https://www.tensorflow.org/serving/docker, but specified "-p 8500:8500" in my docker run command. Port 8500 is what TF serving docker uses for gRPC calls and it uses 8501 for REST API calls. TF serving docker allows serving through gRPC, REST API, or both. The What-If Tool uses gRPC calls to query a model server, so perhaps it will work for you if you make sure to start up your docker with port 8500 specified to enable gRPC and then provide that port in the What-If Tool setup.

jameswex avatar Sep 17 '18 15:09 jameswex

OK. This solved the main issue because I am getting a different traceback now: E0920 10:01:50.152777 Thread-2 _internal.py:87] Error on request: Traceback (most recent call last): File "/home/guyg/.cache/bazel/_bazel_guyg/10aad5f1b9b4fd4ada8c4066e616f3cc/external/org_pocoo_werkzeug/werkzeug/serving.py", line 193, in run_wsgi execute(self.server.app) File "/home/guyg/.cache/bazel/_bazel_guyg/10aad5f1b9b4fd4ada8c4066e616f3cc/external/org_pocoo_werkzeug/werkzeug/serving.py", line 181, in execute application_iter = app(environ, start_response) File "/home/guyg/projects/external/tensorboard/tensorboard/backend/application.py", line 298, in __call__ return self.data_applications[clean_path](environ, start_response) File "/home/guyg/.cache/bazel/_bazel_guyg/10aad5f1b9b4fd4ada8c4066e616f3cc/external/org_pocoo_werkzeug/werkzeug/wrappers.py", line 291, in application return f(*args[:-2] + (request,))(*args[-2:]) File "/home/guyg/projects/external/tensorboard/tensorboard/plugins/interactive_inference/interactive_inference_plugin.py", line 257, in _infer examples_to_infer, serving_bundle) File "/home/guyg/projects/external/tensorboard/tensorboard/plugins/interactive_inference/utils/oss_utils.py", line 136, in call_servo return stub.Classify(request, 30.0) # 30 secs timeout File "/home/guyg/anaconda3/envs/NERD/lib/python3.6/site-packages/grpc/_channel.py", line 487, in __call__ return _end_unary_response_blocking(state, call, False, deadline) File "/home/guyg/anaconda3/envs/NERD/lib/python3.6/site-packages/grpc/_channel.py", line 437, in _end_unary_response_blocking raise _Rendezvous(state, None, None, deadline) grpc._channel._Rendezvous: <_Rendezvous of RPC that terminated with (StatusCode.INVALID_ARGUMENT, Expected classification signature method_name to be tensorflow/serving/classify. Was: tensorflow/serving/predict)>

The DNNClassifier does not export a classify method. @jameswex Where can I the code for the simple iris classifier that you ran? I would like to the code and find out what I missed, and also test it on the tensorflow version I am using.

guygeva avatar Sep 20 '18 07:09 guygeva

I'm wondering if your issue is that when you save the model you are not providing the apporiate build_parsing_serving_input_receiver_fn for use of the classification API. If you inspect your saved model pbtxt (by converting it to pbtext from binary) or use saved_model_cli, you should be able to see the signatures that the saved model are providing. In my case it seems to be providing the tensorflow/serving/classify and tensorflow/serving/predict methods, with the default being tensorflow/serving/classify, which is why the what-if tool can use it. Perhaps with how you save your model the default is predict instead of classify (in which case you could provide the signature_name for the classify endpoint in the what-if tool setup screen), or your model doesn't have the predict endpoint at all (in which case you may need to adjust the code that saves the model).

Here is the gist of the code:

` def load_data(y_name="Species"): """Returns the iris dataset as (train_x, train_y).""" train = pd.read_csv(IRIS_TRAINING, names=CSV_COLUMN_NAMES, header=0) train_x, train_y = train, train.pop(y_name)

return (train_x, train_y)

def train_input_fn(features, labels, batch_size): """An input function for training."""

Convert the inputs to a Dataset.

dataset = tf.data.Dataset.from_tensor_slices((dict(features), labels))

Shuffle, repeat, and batch the examples.

dataset = dataset.shuffle(1000).repeat().batch(batch_size)

Return the dataset.

return dataset

def main(_): train_x, train_y = load_data()

Feature columns describe how to use the input.

my_feature_columns = [] for key in train_x.keys(): my_feature_columns.append(tf.feature_column.numeric_column(key=key))

Serving input receiver function prepares input for the model

features = tf.feature_column.make_parse_example_spec(my_feature_columns) recvfn = tf.estimator.export.build_parsing_serving_input_receiver_fn(features)

Build and train a DNN classifier model.

classifier = tf.estimator.DNNClassifier( feature_columns=my_feature_columns, hidden_units=[10, 20, 10], n_classes=3, model_dir="/tmp/iris_classification") classifier.train( input_fn=lambda: train_input_fn(train_x, train_y, 100), steps=2000) classifier.export_savedmodel(FLAGS.cns_classification_export_path, recvfn)

if name == "main": tf.app.run() `

jameswex avatar Sep 20 '18 15:09 jameswex

Thank you @jameswex. The saved_model_cli shows that there is only the 'predict' method in the saved model. My code is very similar to the snippets you posted.

The only difference I can see is that I am using build_raw_serving_input_receiver_fn. Could it be that the defaults for the export for estimator have been changed since Tensoflow v1.4?

guygeva avatar Sep 25 '18 09:09 guygeva

After exporting a model twice with this code: ` features = tf.feature_column.make_parse_example_spec(feature_columns) recvfn = tf.estimator.export.build_parsing_serving_input_receiver_fn(features) classifier.export_savedmodel('./savedmodel', recvfn)

recvfn = tf.estimator.export.build_raw_serving_input_receiver_fn({"x" :tf.placeholder(feature_columns[0].dtype, feature_columns[0].shape, feature_columns[0].name)}) classifier.export_savedmodel('./savedmodel2', recvfn) `

I saw that this is indeed the cause for the lack of the classify method.

Some debugging led me to the cause of the problem. You can see it in this snippet (from tensorflow/python/estimator/export/export.py): def build_all_signature_defs(receiver_tensors, export_outputs, receiver_tensors_alternatives=None): """Build SignatureDef`s for all export outputs.""" if not isinstance(receiver_tensors, dict): receiver_tensors = {_SINGLE_RECEIVER_DEFAULT_NAME: receiver_tensors} if export_outputs is None or not isinstance(export_outputs, dict): raise ValueError('export_outputs must be a dict.')

signature_def_map = {} for output_key, export_output in export_outputs.items(): signature_name = '{}'.format(output_key or 'None') try: signature = export_output.as_signature_def(receiver_tensors) signature_def_map[signature_name] = signature except ValueError: pass`

When iterating over the classification export output we get this traceback (I removed the value of the dictionary mapping feature name to a tensor): ` Traceback (most recent call last): File "/home/guyg/anaconda3/envs/NERD/lib/python3.6/site-packages/tensorflow/python/estimator/export/export.py", line 199, in build_all_signature_defs signature = export_output.as_signature_def(receiver_tensors) File "/home/guyg/anaconda3/envs/NERD/lib/python3.6/site-packages/tensorflow/python/estimator/export/export_output.py", line 112, in as_signature_def 'got {}'.format(receiver_tensors)) ValueError: Classification input must be a single string Tensor; got {...}

`

The ClassificationOutput class does not support more than one tensor in receiver_tensors. While build_parsing_serving_input_receiver_fn provide only one tensor(The serialized example string) build_raw_serving_input_receiver_fn provides a dict with all features as tensors - which means - more than one tensor.

I am providing build_raw_serving_input_receiver_fn a dict that is built using this statement: {feature.name: tf.placeholder(feature.dtype, feature.shape, feature.name) for feature in build_features(dict())}

Is this a tensorflow bug, or am I missing something?

guygeva avatar Sep 25 '18 11:09 guygeva

Classification and RegressionOutputs currently only support serialized input. There is potentially some historical reason for this (@nfiedel @christisg), but if not, it seems reasonable to me to relax this check. @christisg - thoughts? Also @ispirmustafa who might have some historical context.

karmel avatar Oct 05 '18 15:10 karmel

The Classification & Regression signatures are programming contracts, between the exporter of a model and the ecosystem of tooling that performs inference with them. This contract expects serialized input. Changing the contract to be un-typed, allowing any form of input whether serialized or not, would make it increasingly difficult to publish, share, deploy, or inspect SavedModels.

A potential solution would be to create dedicated signatures for these types, and to namespace them differently. E.g. instead of /tensorflow/serving/regression, they could be /tensorflow/whatif/regression or /tensorflow/untyped/regression.

Hope this helps. Thanks! Noah

nfiedel avatar Oct 08 '18 19:10 nfiedel

@guygeva , is it possible for you to change your model to take in serialized input as per the code above that does?

features = tf.feature_column.make_parse_example_spec(my_feature_columns) recvfn = tf.estimator.export.build_parsing_serving_input_receiver_fn(features)

It seems that your use of build_raw_serving_input_receiver_fn causes the saved model to not take in serialized tf.examples as input, which is why the saved model does not contain the classify endpoint that the what-if tool would use.

jameswex avatar Oct 09 '18 20:10 jameswex