本文整理汇总了Python中tensorflow.python.saved_model.signature_constants.DEFAULT_SERVING_SIGNATURE_DEF_KEY属性的典型用法代码示例。如果您正苦于以下问题:Python signature_constants.DEFAULT_SERVING_SIGNATURE_DEF_KEY属性的具体用法?Python signature_constants.DEFAULT_SERVING_SIGNATURE_DEF_KEY怎么用?Python signature_constants.DEFAULT_SERVING_SIGNATURE_DEF_KEY使用的例子?那么恭喜您, 这里精选的属性代码示例或许可以为您提供帮助。您也可以进一步了解该属性所在类tensorflow.python.saved_model.signature_constants
的用法示例。
在下文中一共展示了signature_constants.DEFAULT_SERVING_SIGNATURE_DEF_KEY属性的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: __init__
# 需要导入模块: from tensorflow.python.saved_model import signature_constants [as 别名]
# 或者: from tensorflow.python.saved_model.signature_constants import DEFAULT_SERVING_SIGNATURE_DEF_KEY [as 别名]
def __init__(self):
model_path = os.environ.get('MODEL_PATH', '/model')
self.sess = tf.Session(graph=tf.Graph())
saved_metagraphdef = tf.saved_model.loader.load(self.sess,
[tag_constants.SERVING], model_path)
self.inputs_tensor_info = signature_def_utils.get_signature_def_by_key(
saved_metagraphdef,
signature_constants.DEFAULT_SERVING_SIGNATURE_DEF_KEY).inputs
outputs_tensor_info = signature_def_utils.get_signature_def_by_key(
saved_metagraphdef,
signature_constants.DEFAULT_SERVING_SIGNATURE_DEF_KEY).outputs
self.output_tensor_keys_sorted = sorted(outputs_tensor_info.keys())
self.output_tensor_names_sorted = [
outputs_tensor_info[tensor_key].name
for tensor_key in self.output_tensor_keys_sorted
]
示例2: export
# 需要导入模块: from tensorflow.python.saved_model import signature_constants [as 别名]
# 或者: from tensorflow.python.saved_model.signature_constants import DEFAULT_SERVING_SIGNATURE_DEF_KEY [as 别名]
def export(self, last_checkpoint, output_dir):
"""Builds a prediction graph and xports the model.
Args:
last_checkpoint: Path to the latest checkpoint file from training.
output_dir: Path to the folder to be used to output the model.
"""
logging.info('Exporting prediction graph to %s', output_dir)
with tf.Session(graph=tf.Graph()) as sess:
# Build and save prediction meta graph and trained variable values.
inputs, outputs = self.build_prediction_graph()
init_op = tf.global_variables_initializer()
sess.run(init_op)
self.restore_from_checkpoint(sess, self.inception_checkpoint_file,
last_checkpoint)
signature_def = build_signature(inputs=inputs, outputs=outputs)
signature_def_map = {
signature_constants.DEFAULT_SERVING_SIGNATURE_DEF_KEY: signature_def
}
builder = saved_model_builder.SavedModelBuilder(output_dir)
builder.add_meta_graph_and_variables(
sess,
tags=[tag_constants.SERVING],
signature_def_map=signature_def_map)
builder.save()
示例3: export_model
# 需要导入模块: from tensorflow.python.saved_model import signature_constants [as 别名]
# 或者: from tensorflow.python.saved_model.signature_constants import DEFAULT_SERVING_SIGNATURE_DEF_KEY [as 别名]
def export_model(self, model_dir, global_step_val, last_checkpoint):
"""Exports the model so that it can used for batch predictions."""
with self.graph.as_default():
with tf.Session() as session:
session.run(tf.global_variables_initializer())
self.saver.restore(session, last_checkpoint)
signature = signature_def_utils.build_signature_def(
inputs=self.inputs,
outputs=self.outputs,
method_name=signature_constants.PREDICT_METHOD_NAME)
signature_map = {signature_constants.DEFAULT_SERVING_SIGNATURE_DEF_KEY:
signature}
model_builder = saved_model_builder.SavedModelBuilder(model_dir)
model_builder.add_meta_graph_and_variables(session,
tags=[tag_constants.SERVING],
signature_def_map=signature_map,
clear_devices=True)
model_builder.save()
示例4: to_savedmodel
# 需要导入模块: from tensorflow.python.saved_model import signature_constants [as 别名]
# 或者: from tensorflow.python.saved_model.signature_constants import DEFAULT_SERVING_SIGNATURE_DEF_KEY [as 别名]
def to_savedmodel(model, export_path):
"""Convert the Keras HDF5 model into TensorFlow SavedModel."""
builder = saved_model_builder.SavedModelBuilder(export_path)
signature = predict_signature_def(
inputs={'input': model.inputs[0]}, outputs={'income': model.outputs[0]})
with K.get_session() as sess:
builder.add_meta_graph_and_variables(
sess=sess,
tags=[tag_constants.SERVING],
signature_def_map={
signature_constants.DEFAULT_SERVING_SIGNATURE_DEF_KEY: signature
})
builder.save()
示例5: build_all_signature_defs
# 需要导入模块: from tensorflow.python.saved_model import signature_constants [as 别名]
# 或者: from tensorflow.python.saved_model.signature_constants import DEFAULT_SERVING_SIGNATURE_DEF_KEY [as 别名]
def build_all_signature_defs(input_alternatives, output_alternatives,
actual_default_output_alternative_key):
"""Build `SignatureDef`s from all pairs of input and output alternatives."""
signature_def_map = {
('%s:%s' % (input_key, output_key or 'None')):
build_standardized_signature_def(
inputs, outputs, problem_type)
for input_key, inputs in input_alternatives.items()
for output_key, (problem_type, outputs)
in output_alternatives.items()}
# Add the default SignatureDef
default_inputs = input_alternatives.get(DEFAULT_INPUT_ALTERNATIVE_KEY)
if not default_inputs:
raise ValueError('A default input_alternative must be provided.')
# default_inputs = input_alternatives[FEATURES_INPUT_ALTERNATIVE_KEY]
# default outputs are guaranteed to exist above
(default_problem_type, default_outputs) = (
output_alternatives[actual_default_output_alternative_key])
signature_def_map[signature_constants.DEFAULT_SERVING_SIGNATURE_DEF_KEY] = (
build_standardized_signature_def(
default_inputs, default_outputs, default_problem_type))
return signature_def_map
示例6: export_model
# 需要导入模块: from tensorflow.python.saved_model import signature_constants [as 别名]
# 或者: from tensorflow.python.saved_model.signature_constants import DEFAULT_SERVING_SIGNATURE_DEF_KEY [as 别名]
def export_model(self, model_dir, global_step_val, last_checkpoint):
"""Exports the model so that it can used for batch predictions."""
with self.graph.as_default():
with tf.Session() as session:
session.run(tf.global_variables_initializer())
self.saver.restore(session, last_checkpoint)
signature = signature_def_utils.build_signature_def(
inputs=self.inputs,
outputs=self.outputs,
method_name=signature_constants.PREDICT_METHOD_NAME)
signature_map = {
signature_constants.DEFAULT_SERVING_SIGNATURE_DEF_KEY: signature
}
model_builder = saved_model_builder.SavedModelBuilder(model_dir)
model_builder.add_meta_graph_and_variables(
session,
tags=[tag_constants.SERVING],
signature_def_map=signature_map,
clear_devices=True)
model_builder.save()
示例7: export_model
# 需要导入模块: from tensorflow.python.saved_model import signature_constants [as 别名]
# 或者: from tensorflow.python.saved_model.signature_constants import DEFAULT_SERVING_SIGNATURE_DEF_KEY [as 别名]
def export_model(self, model_dir, global_step_val, last_checkpoint):
"""Exports the model so that it can used for batch predictions."""
with self.graph.as_default():
with tf.Session() as session:
session.run(tf.global_variables_initializer())
self.saver.restore(session, last_checkpoint)
signature = signature_def_utils.build_signature_def(
inputs=self.inputs,
outputs=self.outputs,
method_name=signature_constants.PREDICT_METHOD_NAME)
signature_map = {signature_constants.DEFAULT_SERVING_SIGNATURE_DEF_KEY:
signature}
model_builder = saved_model_builder.SavedModelBuilder(model_dir)
model_builder.add_meta_graph_and_variables(session,
tags=[tag_constants.SERVING],
signature_def_map=signature_map,
clear_devices=True)
model_builder.save()
示例8: save_signature
# 需要导入模块: from tensorflow.python.saved_model import signature_constants [as 别名]
# 或者: from tensorflow.python.saved_model.signature_constants import DEFAULT_SERVING_SIGNATURE_DEF_KEY [as 别名]
def save_signature(self, directory):
signature = signature_def_utils.build_signature_def(
inputs={
'input':
saved_model_utils.build_tensor_info(self.input),
'dropout_rate':
saved_model_utils.build_tensor_info(self.dropout_rate)
},
outputs={
'output': saved_model_utils.build_tensor_info(self.output)
},
method_name=signature_constants.PREDICT_METHOD_NAME)
signature_map = {
signature_constants.DEFAULT_SERVING_SIGNATURE_DEF_KEY: signature
}
model_builder = saved_model_builder.SavedModelBuilder(directory)
model_builder.add_meta_graph_and_variables(
self.sess,
tags=[tag_constants.SERVING],
signature_def_map=signature_map,
clear_devices=True)
model_builder.save(as_text=False)
示例9: export_model
# 需要导入模块: from tensorflow.python.saved_model import signature_constants [as 别名]
# 或者: from tensorflow.python.saved_model.signature_constants import DEFAULT_SERVING_SIGNATURE_DEF_KEY [as 别名]
def export_model(self, model_dir, global_step_val, last_checkpoint):
"""Exports the model so that it can used for batch predictions."""
with self.graph.as_default():
with tf.Session() as session:
session.run(tf.global_variables_initializer())
self.saver.restore(session, last_checkpoint)
signature = signature_def_utils.build_signature_def(
inputs=self.inputs,
outputs=self.outputs,
method_name=signature_constants.PREDICT_METHOD_NAME)
signature_map = {signature_constants.DEFAULT_SERVING_SIGNATURE_DEF_KEY:
signature}
model_builder = saved_model_builder.SavedModelBuilder(model_dir)
model_builder.add_meta_graph_and_variables(session,
tags=[tag_constants.SERVING],
signature_def_map=signature_map,
clear_devices=True)
model_builder.save()
示例10: export_model
# 需要导入模块: from tensorflow.python.saved_model import signature_constants [as 别名]
# 或者: from tensorflow.python.saved_model.signature_constants import DEFAULT_SERVING_SIGNATURE_DEF_KEY [as 别名]
def export_model(self, model_dir, global_step_val, last_checkpoint):
"""Exports the model so that it can used for batch predictions."""
with self.graph.as_default():
with tf.Session(config=self.config) as session:
session.run(tf.global_variables_initializer())
self.saver.restore(session, last_checkpoint)
signature = signature_def_utils.build_signature_def(
inputs=self.inputs,
outputs=self.outputs,
method_name=signature_constants.PREDICT_METHOD_NAME)
signature_map = {signature_constants.DEFAULT_SERVING_SIGNATURE_DEF_KEY:
signature}
model_builder = saved_model_builder.SavedModelBuilder(model_dir)
model_builder.add_meta_graph_and_variables(session,
tags=[tag_constants.SERVING],
signature_def_map=signature_map,
clear_devices=True)
model_builder.save()
示例11: export
# 需要导入模块: from tensorflow.python.saved_model import signature_constants [as 别名]
# 或者: from tensorflow.python.saved_model.signature_constants import DEFAULT_SERVING_SIGNATURE_DEF_KEY [as 别名]
def export(self, last_checkpoint, output_dir):
"""Builds a prediction graph and xports the model.
Args:
last_checkpoint: The latest checkpoint from training.
output_dir: Path to the folder to be used to output the model.
"""
logging.info('Exporting prediction graph to %s', output_dir)
with tf.Session(graph=tf.Graph()) as sess:
# Build and save prediction meta graph and trained variable values.
input_signatures, output_signatures = self.build_prediction_graph()
# Remove this if once Tensorflow 0.12 is standard.
try:
init_op = tf.global_variables_initializer()
except AttributeError:
init_op = tf.initialize_all_variables()
sess.run(init_op)
trained_saver = tf.train.Saver()
trained_saver.restore(sess, last_checkpoint)
predict_signature_def = signature_def_utils.build_signature_def(
input_signatures, output_signatures,
signature_constants.PREDICT_METHOD_NAME)
# Create a saver for writing SavedModel training checkpoints.
build = builder.SavedModelBuilder(
os.path.join(output_dir, 'saved_model'))
build.add_meta_graph_and_variables(
sess, [tag_constants.SERVING],
signature_def_map={
signature_constants.DEFAULT_SERVING_SIGNATURE_DEF_KEY:
predict_signature_def
},
assets_collection=tf.get_collection(tf.GraphKeys.ASSET_FILEPATHS))
build.save()
示例12: export
# 需要导入模块: from tensorflow.python.saved_model import signature_constants [as 别名]
# 或者: from tensorflow.python.saved_model.signature_constants import DEFAULT_SERVING_SIGNATURE_DEF_KEY [as 别名]
def export(model_version, model_dir, sess, inputs, y_op):
"""导出tensorflow_serving可用的模型(Saved Model方式)(推荐)
prediction_signature必备的三个参数分别是输入inputs、输出outputs和方法名method_name,如果缺失方法名将会报错:“grpc.framework.interfaces.face.face.AbortionError: AbortionError(code=StatusCode.INTERNAL, details="Expected prediction signature method_name to be one of {tensorflow/serving/predict, tensorflow/serving/classify, tensorflow/serving/regress}. Was: ")”。每一个SavedModel关联着一个独立的checkpoint。每一个图元都绑定一个或多个标签,这些标签用来明确图元被加载的方式。标签只接受两种类型:serve或者train,保存时可以同时包含两个标签。其中tag_constants.SERVING = "serve",tag_constants.TRAINING = "train"。模型用于TensorFlow Serving时,标签必须包含serve类型。如果标签只包含train类型,TensorFlow Serving加载模型时会报错:“E tensorflow_serving/core/aspired_versions_manager.cc:351] Servable {name: default version: 2} cannot be loaded: Not found: Could not find meta graph def matching supplied tags.”。定义signature_def_map时注意定义默认服务签名键,如果缺少则会报错:“grpc.framework.interfaces.face.face.AbortionError: AbortionError(code=StatusCode.FAILED_PRECONDITION, details="Default serving signature key not found.")”。
"""
if model_version <= 0:
print('Please specify a positive value for version number.')
sys.exit()
path = os.path.dirname(os.path.abspath(model_dir))
if os.path.isdir(path) == False:
logging.warning('Path (%s) not exists, making directories...', path)
os.makedirs(path)
export_path = os.path.join(
compat.as_bytes(model_dir),
compat.as_bytes(str(model_version)))
if os.path.isdir(export_path) == True:
logging.warning('Path (%s) exists, removing directories...', export_path)
shutil.rmtree(export_path)
builder = saved_model_builder.SavedModelBuilder(export_path)
tensor_info_x = utils.build_tensor_info(inputs)
tensor_info_y = utils.build_tensor_info(y_op)
prediction_signature = signature_def_utils.build_signature_def(
inputs={'x': tensor_info_x},
outputs={'y': tensor_info_y},
method_name=signature_constants.PREDICT_METHOD_NAME)
builder.add_meta_graph_and_variables(
sess,
[tag_constants.SERVING],
signature_def_map={
'predict_text': prediction_signature,
signature_constants.DEFAULT_SERVING_SIGNATURE_DEF_KEY: prediction_signature
})
builder.save()
示例13: log_saved_model
# 需要导入模块: from tensorflow.python.saved_model import signature_constants [as 别名]
# 或者: from tensorflow.python.saved_model.signature_constants import DEFAULT_SERVING_SIGNATURE_DEF_KEY [as 别名]
def log_saved_model(saved_model_path: Union[bytes, str],
global_step: int,
saved_model_load_fn: Callable):
"""
Log all the saved models to mlflow
Parameters
----------
saved_model_path
path to saved model
global_step
global step for saved model
"""
# pylint: disable=unused-argument
# saved_model_load_fn is coming from patch
if mlflow.active_run() is None:
_warn_about_no_run()
return
if isinstance(saved_model_path, bytes):
saved_model_path = saved_model_path.decode()
saved_model_tag = os.path.split(saved_model_path)[-1]
artifact_path = os.path.join("models", saved_model_tag)
mlflow_tf.log_model(
tf_saved_model_dir=saved_model_path,
tf_meta_graph_tags=[tag_constants.SERVING],
tf_signature_def_key=
signature_constants.DEFAULT_SERVING_SIGNATURE_DEF_KEY,
artifact_path=artifact_path)
mlflow_artifacts_path = mlflow.get_artifact_uri()
saved_model_artifact_path = os.path.join(
mlflow_artifacts_path, artifact_path)
project_utils.log_exported_model_info(
saved_model_artifact_path, global_step)
示例14: _tf_load_model
# 需要导入模块: from tensorflow.python.saved_model import signature_constants [as 别名]
# 或者: from tensorflow.python.saved_model.signature_constants import DEFAULT_SERVING_SIGNATURE_DEF_KEY [as 别名]
def _tf_load_model(sess, model_dir):
"""Load a tf model from model_dir, and return input/output alias maps."""
meta_graph_pb = tf.saved_model.loader.load(
sess=sess,
tags=[tf.saved_model.tag_constants.SERVING],
export_dir=model_dir)
signature = meta_graph_pb.signature_def[signature_constants.DEFAULT_SERVING_SIGNATURE_DEF_KEY]
input_alias_map = {friendly_name: tensor_info_proto.name
for (friendly_name, tensor_info_proto) in signature.inputs.items()}
output_alias_map = {friendly_name: tensor_info_proto.name
for (friendly_name, tensor_info_proto) in signature.outputs.items()}
return input_alias_map, output_alias_map
示例15: _load_tf_saved_model
# 需要导入模块: from tensorflow.python.saved_model import signature_constants [as 别名]
# 或者: from tensorflow.python.saved_model.signature_constants import DEFAULT_SERVING_SIGNATURE_DEF_KEY [as 别名]
def _load_tf_saved_model(path):
try:
import tensorflow as tf
from tensorflow.python.training.tracking.tracking import AutoTrackable
TF2 = tf.__version__.startswith('2')
except ImportError:
raise MissingDependencyException(
"Tensorflow package is required to use TfSavedModelArtifact"
)
if TF2:
return tf.saved_model.load(path)
else:
loaded = tf.compat.v2.saved_model.load(path)
if isinstance(loaded, AutoTrackable) and not hasattr(loaded, "__call__"):
logger.warning(
'''Importing SavedModels from TensorFlow 1.x.
`outputs = imported(inputs)` is not supported in bento service due to
tensorflow API.
Recommended usage:
```python
from tensorflow.python.saved_model import signature_constants
imported = tf.saved_model.load(path_to_v1_saved_model)
wrapped_function = imported.signatures[
signature_constants.DEFAULT_SERVING_SIGNATURE_DEF_KEY]
wrapped_function(tf.ones([]))
```
See https://www.tensorflow.org/api_docs/python/tf/saved_model/load for
details.
'''
)
return loaded