I create a simple model using TF Estimator and save the model using export_savedmodel
function. I use a simple Iris dataset which has 4 features.
num_epoch = 50
num_train = 120
num_test = 30
# 1 Define input function
def input_function(x, y, is_train):
dict_x = {
"thisisinput" : x,
}
dataset = tf.data.Dataset.from_tensor_slices((
dict_x, y
))
if is_train:
dataset = dataset.shuffle(num_train).repeat(num_epoch).batch(num_train)
else:
dataset = dataset.batch(num_test)
return dataset
def my_serving_input_fn():
input_data = {
"thisisinput" : tf.placeholder(tf.float32, [None, 4], name='inputtensors')
}
return tf.estimator.export.ServingInputReceiver(input_data, input_data)
def main(argv):
tf.set_random_seed(1103) # avoiding different result of random
# 2 Define feature columns
feature_columns = [
tf.feature_column.numeric_column(key="thisisinput",shape=4),
]
# 3 Define an estimator
classifier = tf.estimator.DNNClassifier(
feature_columns=feature_columns,
hidden_units=[10],
n_classes=3,
optimizer=tf.train.GradientDescentOptimizer(0.001),
activation_fn=tf.nn.relu,
model_dir = 'modeliris2/'
)
# Train the model
classifier.train(
input_fn=lambda:input_function(xtrain, ytrain, True)
)
# Evaluate the model
eval_result = classifier.evaluate(
input_fn=lambda:input_function(xtest, ytest, False)
)
print('\nTest set accuracy: {accuracy:0.3f}\n'.format(**eval_result))
print('\nSaving models...')
classifier.export_savedmodel("modeliris2pb", my_serving_input_fn)
if __name__ == "__main__":
os.environ['TF_CPP_MIN_LOG_LEVEL'] = '3'
tf.app.run(main)
After running the program, it produces a folder which contains saved_model.pb
. I see many tutorials suggest to use contrib.predictor
to load saved_model.pb
but I can't. I've use contrib.predictor
function to load the model:
def main(a):
with tf.Session() as sess:
PB_PATH= "modeliris2pb/1536219836/"
predict_fn = predictor.from_saved_model(PB_PATH)
if __name__=="__main__":
main()
But it yields an error:
ValueError: Got signature_def_key "serving_default". Available signatures are ['predict']. Original error: No SignatureDef with key 'serving_default' found in MetaGraphDef.
Is there another way that is better to load *.pb files? Why does this error happen? I'm suspicious it is because the my_serving_input_fn()
function, but I don't know why
I was facing same issue , I tried to search on web but there are no explanation about this so i tried different approach:
SAVING :
First you need to define features length in dict format like this:
Then you have to build a function which have placeholder with same shape of features and return using tf.estimator.export.ServingInputReceiver
Then just save with export_savedmodel :
full example code:
Restoring
Now let's restore the model :
Here is Ipython notebook demo example with data and explanation :