Skip to content
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

Support session config in tf.contrib.predictor #19542

Merged
merged 2 commits into from
Jun 4, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
5 changes: 4 additions & 1 deletion tensorflow/contrib/predictor/contrib_estimator_predictor.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,8 @@ def __init__(self,
prediction_input_fn,
input_alternative_key=None,
output_alternative_key=None,
graph=None):
graph=None,
config=None):
"""Initialize a `ContribEstimatorPredictor`.

Args:
Expand All @@ -48,6 +49,7 @@ def __init__(self,
multi-headed models.
graph: Optional. The Tensorflow `graph` in which prediction should be
done.
config: `ConfigProto` proto used to configure the session.
"""
self._graph = graph or ops.Graph()
with self._graph.as_default():
Expand All @@ -58,6 +60,7 @@ def __init__(self,
checkpoint_path = saver.latest_checkpoint(estimator.model_dir)
self._session = monitored_session.MonitoredSession(
session_creator=monitored_session.ChiefSessionCreator(
config=config,
checkpoint_filename_with_path=checkpoint_path))

input_alternative_key = (
Expand Down
5 changes: 4 additions & 1 deletion tensorflow/contrib/predictor/core_estimator_predictor.py
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,8 @@ def __init__(self,
estimator,
serving_input_receiver_fn,
output_key=None,
graph=None):
graph=None,
config=None):
"""Initialize a `CoreEstimatorPredictor`.

Args:
Expand All @@ -62,6 +63,7 @@ def __init__(self,
`None`, then `DEFAULT_SERVING_SIGNATURE_DEF_KEY` is used.
graph: Optional. The Tensorflow `graph` in which prediction should be
done.
config: `ConfigProto` proto used to configure the session.
"""
self._graph = graph or ops.Graph()
with self._graph.as_default():
Expand All @@ -71,6 +73,7 @@ def __init__(self,
checkpoint_dir = estimator.model_dir
self._session = monitored_session.MonitoredSession(
session_creator=monitored_session.ChiefSessionCreator(
config=config,
checkpoint_dir=checkpoint_dir))

feed_tensor_info = signature_def.inputs
Expand Down
24 changes: 18 additions & 6 deletions tensorflow/contrib/predictor/predictor_factories.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,8 @@ def from_contrib_estimator(estimator,
prediction_input_fn,
input_alternative_key=None,
output_alternative_key=None,
graph=None):
graph=None,
config=None):
"""Constructs a `Predictor` from a `tf.contrib.learn.Estimator`.

Args:
Expand All @@ -44,6 +45,7 @@ def from_contrib_estimator(estimator,
multi-headed models.
graph: Optional. The Tensorflow `graph` in which prediction should be
done.
config: `ConfigProto` proto used to configure the session.

Returns:
An initialized `Predictor`.
Expand All @@ -62,13 +64,15 @@ def from_contrib_estimator(estimator,
prediction_input_fn,
input_alternative_key=input_alternative_key,
output_alternative_key=output_alternative_key,
graph=graph)
graph=graph,
config=config)


def from_estimator(estimator,
serving_input_receiver_fn,
output_key=None,
graph=None):
graph=None,
config=None):
"""Constructs a `Predictor` from a `tf.python.estimator.Estimator`.

Args:
Expand All @@ -79,6 +83,7 @@ def from_estimator(estimator,
`None`, then `DEFAULT_SERVING_SIGNATURE_DEF_KEY` is used.
graph: Optional. The Tensorflow `graph` in which prediction should be
done.
config: `ConfigProto` proto used to configure the session.

Returns:
An initialized `Predictor`.
Expand All @@ -93,14 +98,19 @@ def from_estimator(estimator,
'tf.contrib.learn.Estimator. You likely want to call '
'from_contrib_estimator.')
return core_estimator_predictor.CoreEstimatorPredictor(
estimator, serving_input_receiver_fn, output_key=output_key, graph=graph)
estimator,
serving_input_receiver_fn,
output_key=output_key,
graph=graph,
config=config)


def from_saved_model(export_dir,
signature_def_key=None,
signature_def=None,
tags=None,
graph=None):
graph=None,
config=None):
"""Constructs a `Predictor` from a `SavedModel` on disk.

Args:
Expand All @@ -115,6 +125,7 @@ def from_saved_model(export_dir,
`SignatureDef`. Defaults to `DEFAULT_TAGS`.
graph: Optional. The Tensorflow `graph` in which prediction should be
done.
config: `ConfigProto` proto used to configure the session.

Returns:
An initialized `Predictor`.
Expand All @@ -128,4 +139,5 @@ def from_saved_model(export_dir,
signature_def_key=signature_def_key,
signature_def=signature_def,
tags=tags,
graph=graph)
graph=graph,
config=config)
19 changes: 19 additions & 0 deletions tensorflow/contrib/predictor/predictor_factories_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@

from tensorflow.contrib.predictor import predictor_factories
from tensorflow.contrib.predictor import testing_common
from tensorflow.core.protobuf import config_pb2
from tensorflow.python.platform import test

MODEL_DIR_NAME = 'contrib/predictor/test_export_dir'
Expand All @@ -41,6 +42,11 @@ def testFromSavedModelWithTags(self):
"""Test loading from_saved_model with tags."""
predictor_factories.from_saved_model(self._export_dir, tags='serve')

def testFromSavedModelWithSessionConfig(self):
"""Test loading from_saved_model with session config."""
predictor_factories.from_saved_model(
self._export_dir, config=config_pb2.ConfigProto())

def testFromSavedModelWithBadTags(self):
"""Test that loading fails for bad tags."""
bad_tags_regex = ('.*? could not be found in SavedModel')
Expand All @@ -53,6 +59,13 @@ def testFromContribEstimator(self):
predictor_factories.from_contrib_estimator(
estimator, input_fn, output_alternative_key='sum')

def testFromContribEstimatorWithSessionConfig(self):
estimator = testing_common.get_arithmetic_estimator(core=False)
input_fn = testing_common.get_arithmetic_input_fn(core=False)
predictor_factories.from_contrib_estimator(
estimator, input_fn, output_alternative_key='sum',
config=config_pb2.ConfigProto())

def testFromContribEstimatorWithCoreEstimatorRaises(self):
estimator = testing_common.get_arithmetic_estimator(core=True)
input_fn = testing_common.get_arithmetic_input_fn(core=True)
Expand All @@ -64,6 +77,12 @@ def testFromCoreEstimator(self):
input_fn = testing_common.get_arithmetic_input_fn(core=True)
predictor_factories.from_estimator(estimator, input_fn)

def testFromCoreEstimatorWithSessionConfig(self):
estimator = testing_common.get_arithmetic_estimator(core=True)
input_fn = testing_common.get_arithmetic_input_fn(core=True)
predictor_factories.from_estimator(
estimator, input_fn, config=config_pb2.ConfigProto())

def testFromCoreEstimatorWithContribEstimatorRaises(self):
estimator = testing_common.get_arithmetic_estimator(core=False)
input_fn = testing_common.get_arithmetic_input_fn(core=False)
Expand Down
6 changes: 4 additions & 2 deletions tensorflow/contrib/predictor/saved_model_predictor.py
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,8 @@ def __init__(self,
input_names=None,
output_names=None,
tags=None,
graph=None):
graph=None,
config=None):
"""Initialize a `CoreEstimatorPredictor`.

Args:
Expand All @@ -142,6 +143,7 @@ def __init__(self,
the correct `SignatureDef`. Defaults to `DEFAULT_TAGS`.
graph: Optional. The Tensorflow `graph` in which prediction should be
done.
config: `ConfigProto` proto used to configure the session.
Raises:
ValueError: If more than one of signature_def_key OR signature_def OR
(input_names AND output_names) is specified.
Expand All @@ -152,7 +154,7 @@ def __init__(self,
self._graph = graph or ops.Graph()

with self._graph.as_default():
self._session = session.Session()
self._session = session.Session(config=config)
loader.load(self._session, tags.split(','), export_dir)

if input_names is None:
Expand Down