-
Notifications
You must be signed in to change notification settings - Fork 74.2k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
DNNClassifier estimator cannot be exported #12508
Comments
Hi, @mvsusp . Could you give a minimal reproducible test case? Something seems wrong with your |
Hi, @facaiy. Here is a minimal reproducible test case: from __future__ import absolute_import
from __future__ import division
from __future__ import print_function
import os
import urllib
import numpy as np
import tensorflow as tf
# Data sets
from tensorflow.contrib.learn import DNNClassifier
IRIS_TRAINING = "iris_training.csv"
IRIS_TRAINING_URL = "http://download.tensorflow.org/data/iris_training.csv"
IRIS_TEST = "iris_test.csv"
IRIS_TEST_URL = "http://download.tensorflow.org/data/iris_test.csv"
def main():
# If the training and test sets aren't stored locally, download them.
if not os.path.exists(IRIS_TRAINING):
raw = urllib.urlopen(IRIS_TRAINING_URL).read()
with open(IRIS_TRAINING, "w") as f:
f.write(raw)
if not os.path.exists(IRIS_TEST):
raw = urllib.urlopen(IRIS_TEST_URL).read()
with open(IRIS_TEST, "w") as f:
f.write(raw)
# Load datasets.
training_set = tf.contrib.learn.datasets.base.load_csv_with_header(
filename=IRIS_TRAINING,
target_dtype=np.int,
features_dtype=np.float32)
# Specify that all features have real-value data
feature_columns = [tf.feature_column.numeric_column("x", shape=[4])]
# Build 3 layer DNN with 10, 20, 10 units respectively.
classifier = DNNClassifier(feature_columns=feature_columns,
hidden_units=[10, 20, 10],
n_classes=3,
model_dir="/tmp/iris_model")
# Define the training inputs
train_input_fn = tf.estimator.inputs.numpy_input_fn(
x={"x": np.array(training_set.data)},
y=np.array(training_set.target),
num_epochs=None,
shuffle=True)
# Train model.
classifier.fit(input_fn=train_input_fn, steps=2000)
def serving_input_fn():
inputs = {'x': tf.placeholder(tf.float32, [4])}
return tf.estimator.export.ServingInputReceiver(inputs, inputs)
classifier.export_savedmodel(export_dir_base="/tmp/iris_model", serving_input_fn=serving_input_fn)
if __name__ == "__main__":
main() The error is: Traceback (most recent call last):
File "/reproducible_example.py", line 64, in <module>
main()
File "/reproducible_example.py", line 61, in main
classifier.export_savedmodel(export_dir_base="/tmp/iris_model", serving_input_fn=serving_input_fn)
File "/Users/mvs/python2.7/lib/python2.7/site-packages/tensorflow/contrib/learn/python/learn/estimators/estimator.py", line 1280, in export_savedmodel
actual_default_output_alternative_key)
File "/Users/mvs/python2.7/lib/python2.7/site-packages/tensorflow/contrib/learn/python/learn/utils/saved_model_export_utils.py", line 259, in build_all_signature_defs
raise ValueError('A default input_alternative must be provided.')
ValueError: A default input_alternative must be provided. |
Thanks for your test case, @mvsusp . It's really concise. If I understand correctly, learn.DNNClassifier expects an Correct me if I'm wrong, I believe that By the way, |
Unfortunately, I don't know yet. Perhaps API is the most reliable source, except source code itself. Thanks, happy weekend, @mvsusp . |
@facaiy following your suggestion I got stuck if another issue: I got a It seems that I cannot feed a float tensor for classification using tf serving? My code: from __future__ import absolute_import
from __future__ import division
from __future__ import print_function
import os
import urllib
import numpy as np
import tensorflow as tf
# Data sets
IRIS_TRAINING = "iris_training.csv"
IRIS_TRAINING_URL = "http://download.tensorflow.org/data/iris_training.csv"
IRIS_TEST = "iris_test.csv"
IRIS_TEST_URL = "http://download.tensorflow.org/data/iris_test.csv"
def main():
# If the training and test sets aren't stored locally, download them.
if not os.path.exists(IRIS_TRAINING):
raw = urllib.urlopen(IRIS_TRAINING_URL).read()
with open(IRIS_TRAINING, "w") as f:
f.write(raw)
if not os.path.exists(IRIS_TEST):
raw = urllib.urlopen(IRIS_TEST_URL).read()
with open(IRIS_TEST, "w") as f:
f.write(raw)
# Load datasets.
training_set = tf.contrib.learn.datasets.base.load_csv_with_header(
filename=IRIS_TRAINING,
target_dtype=np.int,
features_dtype=np.float32)
# Specify that all features have real-value data
feature_columns = [tf.feature_column.numeric_column("x", shape=[4])]
# Build 3 layer DNN with 10, 20, 10 units respectively.
classifier = tf.estimator.DNNClassifier(feature_columns=feature_columns,
hidden_units=[10, 20, 10],
n_classes=3,
model_dir="/tmp/iris_model")
# Define the training inputs
train_input_fn = tf.estimator.inputs.numpy_input_fn(
x={"x": np.array(training_set.data)},
y=np.array(training_set.target),
num_epochs=None,
shuffle=True)
# Train model.
classifier.train(input_fn=train_input_fn, steps=2000)
def serving_input_fn():
inputs = {'x': tf.placeholder(tf.float32, [4])}
return tf.estimator.export.ServingInputReceiver(inputs, inputs)
classifier.export_savedmodel(export_dir_base="/tmp/iris_model", serving_input_receiver_fn=serving_input_fn)
if __name__ == "__main__":
main() How can I fix that? Thank you for all the support. |
How about using build_raw_serving_input_receiver_fn ? Perhaps tf.estimator.export will be useful for you. Good luck. |
It does not work as well. I will study tf.estimator.export better. Thanks
for your help.
…On Sat, Aug 26, 2017 at 17:07 Yan Facai (颜发才) ***@***.***> wrote:
How about using build_raw_serving_input_receiver_fn
<https://www.tensorflow.org/api_docs/python/tf/estimator/export/build_raw_serving_input_receiver_fn>
?
—
You are receiving this because you were mentioned.
Reply to this email directly, view it on GitHub
<#12508 (comment)>,
or mute the thread
<https://github.com/notifications/unsubscribe-auth/AAIq-sSPdkib45rNZA87QfOEcCyxKXnKks5scLNVgaJpZM4O_XNK>
.
|
Hi @facaiy The issue was solved using tf.estimator.export.build_parsing_serving_input_receiver_fn. Thank you! |
@mvsusp Can you please post the fixed code for your example ,I'm trying tho export and serve a DNNLinearCombinedRegressor model , and i cant find any working example |
Hello @samithaj Canned estimator don't have a lot of documentation yet. Here it go my code: INPUT_TENSOR_NAME = 'inputs'
def estimator(model_path):
feature_columns = [tf.feature_column.numeric_column(INPUT_TENSOR_NAME, shape=[4])]
return tf.estimator.DNNClassifier(feature_columns=feature_columns,
hidden_units=[10, 20, 10],
n_classes=3,
model_dir=model_path)
def serving_input_receiver_fn():
feature_spec = {INPUT_TENSOR_NAME: tf.FixedLenFeature(dtype=tf.float32, shape=[4])}
return tf.estimator.export.build_parsing_serving_input_receiver_fn(feature_spec)()
def train_input_fn(training_dir):
training_set = tf.contrib.learn.datasets.base.load_csv_with_header(
filename=os.path.join(training_dir, 'iris_training.csv'),
target_dtype=np.int,
features_dtype=np.float32)
return tf.estimator.inputs.numpy_input_fn(
x={INPUT_TENSOR_NAME: np.array(training_set.data)},
y=np.array(training_set.target),
num_epochs=None,
shuffle=True)() Hope it helps you! |
Cool, @mvsusp . By the way, if feature_spec = tf.feature_column.make_parse_example_spec(feature_columns) |
thanks @mvsusp |
@mvsusp @MtDersvan this doesnt work in version 1.4.0 |
@AKhilGarg91 Do you have any tracebacks? |
@MtDersvan No when I run this file I got error during export_savedmodel that too many values to unpack. |
@MtDersvan have u tried to run below command in window? |
I got the too many values to unpack on 1.4, too. a small dig into that I changed In
In
Seems the new class causes the error.
should change to something like
My work around is to use |
@twksos Thanks. . Yes that worked for me as well and I also tried that one only before. |
hey everyone, i'm trying to export a CNN model which accepts 200x200 rgb images as inputs, however while exporting the model i'm getting the following error
Here is my feature spec and serving input fn definition :
and call to export_savedmodel : I'm not sure whats causing export_outputs to be not a dict object. Can anyone help me figure out what i'm doing wrong? |
I have also tried using |
@AKhilGarg91 @twksos Thanks for the information, can you open an issue or a PR in the tutorial repo, so we can deal with it there? Also, @twksos it looks like a
is already in the core library, so technically it should work. Maybe a tf core team can verify this? Or else, a new issue might be needed to be created. |
@Anmol-Sharma I guess you need to return export_outputs in your model_fn, like
|
@felicitywang did it work for your use case? |
is out on TensorFlow 1.7.0-rc0 |
Thanks! You are right, if I trained a model from Working example: def serving_input_receiver_fn():
feature_spec = tf.feature_column.make_parse_example_spec(feature_columns)
return tf.contrib.learn.build_parsing_serving_input_fn(feature_spec)()
servable_model_dir = "./DNNRegressors/Servable/"
regressor.export_savedmodel(servable_model_dir, serving_input_receiver_fn) |
Please go to Stack Overflow for help and support:
https://stackoverflow.com/questions/tagged/tensorflow
If you open a GitHub issue, here is our policy:
Here's why we have that policy: TensorFlow developers respond to issues. We want to focus on work that benefits the whole community, e.g., fixing bugs and adding features. Support only helps individuals. GitHub also notifies thousands of people when issues are filed. We want them to see you communicating an interesting problem, rather than being redirected to Stack Overflow.
System information
Exact command to reproduce
Describe the problem
Trying to export the model DNNClassifier throws the exception:
The problem happens because DNNClassifier constructor creates a head with name
None
: https://github.com/tensorflow/tensorflow/blob/master/tensorflow/contrib/learn/python/learn/estimators/dnn.py#L365The text was updated successfully, but these errors were encountered: