• 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    迪恩网络公众号

Python signature_def_utils.build_signature_def函数代码示例

原作者: [db:作者] 来自: [db:来源] 收藏 邀请

本文整理汇总了Python中tensorflow.python.saved_model.signature_def_utils.build_signature_def函数的典型用法代码示例。如果您正苦于以下问题:Python build_signature_def函数的具体用法?Python build_signature_def怎么用?Python build_signature_def使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。



在下文中一共展示了build_signature_def函数的20个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于我们的系统推荐出更棒的Python代码示例。

示例1: setUp

  def setUp(self):
    """Write test SavedModels to a temp directory."""
    with session.Session(graph=ops.Graph()) as sess:
      x = variables.VariableV1(5, name="x")
      y = variables.VariableV1(11, name="y")
      z = x + y
      self.evaluate(variables.global_variables_initializer())

      foo_sig_def = signature_def_utils.build_signature_def(
          {"foo_input": utils.build_tensor_info(x)},
          {"foo_output": utils.build_tensor_info(z)})
      bar_sig_def = signature_def_utils.build_signature_def(
          {"bar_x": utils.build_tensor_info(x),
           "bar_y": utils.build_tensor_info(y)},
          {"bar_z": utils.build_tensor_info(z)})

      builder = saved_model_builder.SavedModelBuilder(SIMPLE_ADD_SAVED_MODEL)
      builder.add_meta_graph_and_variables(
          sess, ["foo_graph"], {"foo": foo_sig_def, "bar": bar_sig_def})
      builder.save()

      # Write SavedModel with a main_op
      assign_op = control_flow_ops.group(state_ops.assign(y, 7))

      builder = saved_model_builder.SavedModelBuilder(SAVED_MODEL_WITH_MAIN_OP)
      builder.add_meta_graph_and_variables(
          sess, ["foo_graph"], {"foo": foo_sig_def, "bar": bar_sig_def},
          main_op=assign_op)
      builder.save()
开发者ID:JonathanRaiman,项目名称:tensorflow,代码行数:29,代码来源:loader_test.py


示例2: _v1_multi_metagraph_saved_model

 def _v1_multi_metagraph_saved_model(self):
   export_graph = ops.Graph()
   with export_graph.as_default():
     start = array_ops.placeholder(
         shape=[None], dtype=dtypes.float32, name="start")
     v = resource_variable_ops.ResourceVariable(21.)
     first_output = array_ops.identity(start * v, name="first_output")
     second_output = array_ops.identity(v, name="second_output")
     with session_lib.Session() as session:
       session.run(v.initializer)
       path = os.path.join(self.get_temp_dir(), "saved_model", str(ops.uid()))
       builder = builder_impl.SavedModelBuilder(path)
       builder.add_meta_graph_and_variables(
           session, tags=["first"],
           signature_def_map={
               "first_key": signature_def_utils.build_signature_def(
                   {"first_start": utils_impl.build_tensor_info(start)},
                   {"first_output": utils_impl.build_tensor_info(
                       first_output)})})
       builder.add_meta_graph(
           tags=["second"],
           signature_def_map={
               "second_key": signature_def_utils.build_signature_def(
                   {"second_start": utils_impl.build_tensor_info(start)},
                   {"second_output": utils_impl.build_tensor_info(
                       second_output)})})
       builder.save()
   return path
开发者ID:adit-chandra,项目名称:tensorflow,代码行数:28,代码来源:load_v1_in_v2_test.py


示例3: testSignatureDefs

  def testSignatureDefs(self):
    export_dir = self._get_export_dir("test_signature_defs")
    builder = saved_model_builder.SavedModelBuilder(export_dir)

    # Graph with a single variable and a single entry in the signature def map.
    # SavedModel is invoked to add with weights.
    with self.test_session(graph=ops.Graph()) as sess:
      self._init_and_validate_variable(sess, "v", 42)
      # Build and populate an empty SignatureDef for testing.
      foo_signature = signature_def_utils.build_signature_def(dict(),
                                                              dict(), "foo")
      builder.add_meta_graph_and_variables(
          sess, ["foo"], signature_def_map={"foo_key": foo_signature})

    # Graph with the same single variable and multiple entries in the signature
    # def map. No weights are saved by SavedModel.
    with self.test_session(graph=ops.Graph()) as sess:
      self._init_and_validate_variable(sess, "v", 43)
      # Build and populate a different SignatureDef for testing.
      bar_signature = signature_def_utils.build_signature_def(dict(),
                                                              dict(), "bar")
      # Also, build a different SignatureDef corresponding to "foo_key" defined
      # in the previous graph.
      foo_new_signature = signature_def_utils.build_signature_def(dict(),
                                                                  dict(),
                                                                  "foo_new")
      builder.add_meta_graph(
          ["bar"],
          signature_def_map={
              "bar_key": bar_signature,
              "foo_key": foo_new_signature
          })

    # Save the SavedModel to disk.
    builder.save()

    # Restore the graph with tag "foo". The single entry in the SignatureDef map
    # corresponding to "foo_key" should exist.
    with self.test_session(graph=ops.Graph()) as sess:
      foo_graph = loader.load(sess, ["foo"], export_dir)
      self.assertEqual(
          42, ops.get_collection(ops.GraphKeys.GLOBAL_VARIABLES)[0].eval())

      foo_signature = foo_graph.signature_def
      self.assertEqual(len(foo_signature), 1)
      self.assertEqual("foo", foo_signature["foo_key"].method_name)

    # Restore the graph with tag "bar". The SignatureDef map should have two
    # entries. One corresponding to "bar_key" and another corresponding to the
    # new value of "foo_key".
    with self.test_session(graph=ops.Graph()) as sess:
      bar_graph = loader.load(sess, ["bar"], export_dir)
      self.assertEqual(
          42, ops.get_collection(ops.GraphKeys.GLOBAL_VARIABLES)[0].eval())

      bar_signature = bar_graph.signature_def
      self.assertEqual(len(bar_signature), 2)
      self.assertEqual("bar", bar_signature["bar_key"].method_name)
      self.assertEqual("foo_new", bar_signature["foo_key"].method_name)
开发者ID:KiaraStarlab,项目名称:tensorflow,代码行数:59,代码来源:saved_model_test.py


示例4: testGetSignatureDefByKey

  def testGetSignatureDefByKey(self):
    x = array_ops.placeholder(dtypes.float32, 1, name="x")
    x_tensor_info = utils.build_tensor_info(x)

    y = array_ops.placeholder(dtypes.float32, name="y")
    y_tensor_info = utils.build_tensor_info(y)

    foo_signature_def = signature_def_utils.build_signature_def({
        "foo-input": x_tensor_info
    }, {"foo-output": y_tensor_info}, "foo-method-name")
    bar_signature_def = signature_def_utils.build_signature_def({
        "bar-input": x_tensor_info
    }, {"bar-output": y_tensor_info}, "bar-method-name")
    meta_graph_def = meta_graph_pb2.MetaGraphDef()
    self._add_to_signature_def_map(
        meta_graph_def, {"foo": foo_signature_def,
                         "bar": bar_signature_def})

    # Look up a key that does not exist in the SignatureDefMap.
    missing_key = "missing-key"
    with self.assertRaisesRegexp(
        ValueError,
        "No SignatureDef with key '%s' found in MetaGraphDef" % missing_key):
      signature_def_contrib_utils.get_signature_def_by_key(
          meta_graph_def, missing_key)

    # Look up the key, `foo` which exists in the SignatureDefMap.
    foo_signature_def = signature_def_contrib_utils.get_signature_def_by_key(
        meta_graph_def, "foo")
    self.assertTrue("foo-method-name", foo_signature_def.method_name)

    # Check inputs in signature def.
    self.assertEqual(1, len(foo_signature_def.inputs))
    self._check_tensor_info(foo_signature_def.inputs, "foo-input", "x:0")

    # Check outputs in signature def.
    self.assertEqual(1, len(foo_signature_def.outputs))
    self._check_tensor_info(foo_signature_def.outputs, "foo-output", "y:0")

    # Look up the key, `bar` which exists in the SignatureDefMap.
    bar_signature_def = signature_def_contrib_utils.get_signature_def_by_key(
        meta_graph_def, "bar")
    self.assertTrue("bar-method-name", bar_signature_def.method_name)

    # Check inputs in signature def.
    self.assertEqual(1, len(bar_signature_def.inputs))
    self._check_tensor_info(bar_signature_def.inputs, "bar-input", "x:0")

    # Check outputs in signature def.
    self.assertEqual(1, len(bar_signature_def.outputs))
    self._check_tensor_info(bar_signature_def.outputs, "bar-output", "y:0")
开发者ID:AbhinavJain13,项目名称:tensorflow,代码行数:51,代码来源:signature_def_utils_test.py


示例5: build_graph_helper

def build_graph_helper():
  g = ops.Graph()
  with g.as_default():
    x = variables.VariableV1(5, name="x")
    y = variables.VariableV1(11, name="y")
    z = x + y

    foo_sig_def = signature_def_utils.build_signature_def({
        "foo_input": utils.build_tensor_info(x)
    }, {"foo_output": utils.build_tensor_info(z)})
    bar_sig_def = signature_def_utils.build_signature_def({
        "bar_x": utils.build_tensor_info(x),
        "bar_y": utils.build_tensor_info(y)
    }, {"bar_z": utils.build_tensor_info(z)})
  return g, {"foo": foo_sig_def, "bar": bar_sig_def}, y
开发者ID:Wajih-O,项目名称:tensorflow,代码行数:15,代码来源:loader_test.py


示例6: export

  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()
开发者ID:cottrell,项目名称:notebooks,代码行数:34,代码来源:model.py


示例7: testBuildSignatureDef

  def testBuildSignatureDef(self):
    x = array_ops.placeholder(dtypes.float32, 1, name="x")
    x_tensor_info = utils.build_tensor_info(x)
    inputs = dict()
    inputs["foo-input"] = x_tensor_info

    y = array_ops.placeholder(dtypes.float32, name="y")
    y_tensor_info = utils.build_tensor_info(y)
    outputs = dict()
    outputs["foo-output"] = y_tensor_info

    signature_def = signature_def_utils.build_signature_def(inputs, outputs,
                                                            "foo-method-name")
    self.assertEqual("foo-method-name", signature_def.method_name)

    # Check inputs in signature def.
    self.assertEqual(1, len(signature_def.inputs))
    x_tensor_info_actual = signature_def.inputs["foo-input"]
    self.assertEqual("x:0", x_tensor_info_actual.name)
    self.assertEqual(types_pb2.DT_FLOAT, x_tensor_info_actual.dtype)
    self.assertEqual(1, len(x_tensor_info_actual.tensor_shape.dim))
    self.assertEqual(1, x_tensor_info_actual.tensor_shape.dim[0].size)

    # Check outputs in signature def.
    self.assertEqual(1, len(signature_def.outputs))
    y_tensor_info_actual = signature_def.outputs["foo-output"]
    self.assertEqual("y:0", y_tensor_info_actual.name)
    self.assertEqual(types_pb2.DT_FLOAT, y_tensor_info_actual.dtype)
    self.assertEqual(0, len(y_tensor_info_actual.tensor_shape.dim))
开发者ID:AlbertXiebnu,项目名称:tensorflow,代码行数:29,代码来源:signature_def_utils_test.py


示例8: _validate_outputs_tensor_info_accept

  def _validate_outputs_tensor_info_accept(self, builder, tensor_info):
    with self.test_session(graph=ops.Graph()) as sess:
      self._init_and_validate_variable(sess, "v", 42)

      foo_signature = signature_def_utils.build_signature_def(
          dict(), {"foo_outputs": tensor_info}, "foo")
      builder.add_meta_graph_and_variables(
          sess, ["foo"],
          signature_def_map={"foo_key": foo_signature})
开发者ID:KiaraStarlab,项目名称:tensorflow,代码行数:9,代码来源:saved_model_test.py


示例9: export

def export(model_version, model_dir, sess, x, y_op):
    """导出tensorflow_serving可用的模型
    SavedModel(tensorflow.python.saved_model)提供了一种跨语言格式来保存和恢复训练后的TensorFlow模型。它使用方法签名来定义Graph的输入和输出,使上层系统能够更方便地生成、调用或转换TensorFlow模型。
    SavedModelBuilder类提供保存Graphs、Variables及Assets的方法。所保存的Graphs必须标注用途标签。在这个实例中我们打算将模型用于服务而非训练,因此我们用SavedModel预定义好的tag_constant.Serving标签。
    为了方便地构建签名,SavedModel提供了signature_def_utils API。我们通过signature_def_utils.build_signature_def()来构建predict_signature。一个predict_signature至少包含以下参数:
    * inputs  = {'x': tensor_info_x} 指定输入的tensor信息
    * outputs = {'y': tensor_info_y} 指定输出的tensor信息
    * method_name = signature_constants.PREDICT_METHOD_NAME
    method_name定义方法名,它的值应该是tensorflow/serving/predict、tensorflow/serving/classify和tensorflow/serving/regress三者之一。Builder标签用来明确Meta Graph被加载的方式,只接受serve和train两种类型。
    """
    if model_version <= 0:
        logging.warning('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(x)
    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},
        # signature_constants.CLASSIFY_METHOD_NAME = "tensorflow/serving/classify"
        # signature_constants.PREDICT_METHOD_NAME  = "tensorflow/serving/predict"
        # signature_constants.REGRESS_METHOD_NAME  = "tensorflow/serving/regress"
        # 如果缺失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: ")
        method_name=signature_constants.PREDICT_METHOD_NAME)

    builder.add_meta_graph_and_variables(
        sess,
        # tag_constants.SERVING  = "serve"
        # tag_constants.TRAINING = "train"
        # 如果只有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.
        [tag_constants.SERVING],
        signature_def_map={
            'predict_text': prediction_signature,
            # 如果缺失会报错:
            # grpc.framework.interfaces.face.face.AbortionError: AbortionError(code=StatusCode.FAILED_PRECONDITION, details="Default serving signature key not found.")
            signature_constants.DEFAULT_SERVING_SIGNATURE_DEF_KEY: prediction_signature
        })

    builder.save()
开发者ID:lacatc,项目名称:text-antispam,代码行数:56,代码来源:rnn_classifier.py


示例10: _generate_signatures

def _generate_signatures(signature_functions, resource_map):
  """Validates and calls `signature_functions` in the default graph.

  Args:
    signature_functions: A dictionary mapping string keys to concrete TensorFlow
      functions (e.g. from `_canonicalize_signatures`) which will be used to
      generate SignatureDefs.
    resource_map: A dictionary mapping from resource tensors in the eager
      context to resource tensors in the Graph being exported. This dictionary
      is used to re-bind resources captured by functions to tensors which will
      exist in the SavedModel.

  Returns:
    Each function in the `signature_functions` dictionary is called with
    placeholder Tensors, generating a function call operation and output
    Tensors. The placeholder Tensors, the function call operation, and the
    output Tensors from the function call are part of the default Graph.

    This function then returns a dictionary with the same structure as
    `signature_functions`, with the concrete functions replaced by SignatureDefs
    implicitly containing information about how to call each function from a
    TensorFlow 1.x Session / the C++ Loader API. These SignatureDefs reference
    the generated placeholders and Tensor outputs by name.

    The caller is expected to include the default Graph set while calling this
    function as a MetaGraph in a SavedModel, including the returned
    SignatureDefs as part of that MetaGraph.
  """
  signatures = {}
  for signature_key, func in sorted(signature_functions.items()):
    # Register the inference function for this signature in the exported
    # graph. There is no direct use for the gradient of this function, so we
    # don't generate/register a gradient function here (but may end up with one
    # if another function relies on it). Users can still take symbolic gradients
    # of the function on import, the gradient just won't be in the saved
    # graph. When exporting a signature which already computes gradients, this
    # stops us from taking needless second-order gradients.
    func.add_to_graph(register_gradient_functions=False)
    export_captures = _map_captured_resources_to_created_resources(
        func.graph.captures, resource_map)
    mapped_inputs, exterior_argument_placeholders = (
        _map_function_inputs_to_created_inputs(
            func.inputs, export_captures, signature_key, func.name))
    # Calls the function quite directly, since we have new captured resource
    # tensors we need to feed in which weren't part of the original function
    # definition.
    # pylint: disable=protected-access
    outputs = _normalize_outputs(
        func._build_call_outputs(
            func._inference_function.call(context.context(), mapped_inputs)),
        func.name, signature_key)
    # pylint: enable=protected-access
    signatures[signature_key] = signature_def_utils.build_signature_def(
        _tensor_dict_to_tensorinfo(exterior_argument_placeholders),
        _tensor_dict_to_tensorinfo(outputs))
  return signatures
开发者ID:bunbutter,项目名称:tensorflow,代码行数:56,代码来源:save.py


示例11: _validate_inputs_tensor_info_fail

  def _validate_inputs_tensor_info_fail(self, builder, tensor_info):
    with self.test_session(graph=ops.Graph()) as sess:
      self._init_and_validate_variable(sess, "v", 42)

      foo_signature = signature_def_utils.build_signature_def({
          "foo_inputs": tensor_info
      }, dict(), "foo")
      self.assertRaises(
          AssertionError,
          builder.add_meta_graph_and_variables,
          sess, ["foo"],
          signature_def_map={"foo_key": foo_signature})
开发者ID:KiaraStarlab,项目名称:tensorflow,代码行数:12,代码来源:saved_model_test.py


示例12: _WriteInputSavedModel

 def _WriteInputSavedModel(self, input_saved_model_dir):
   """Write the saved model as an input for testing."""
   g, var, inp, out = self._GetGraph()
   signature_def = signature_def_utils.build_signature_def(
       inputs={"myinput": utils.build_tensor_info(inp)},
       outputs={"myoutput": utils.build_tensor_info(out)},
       method_name=signature_constants.PREDICT_METHOD_NAME)
   saved_model_builder = builder.SavedModelBuilder(input_saved_model_dir)
   with self.session(graph=g, config=self._GetConfigProto()) as sess:
     sess.run(var.initializer)
     saved_model_builder.add_meta_graph_and_variables(
         sess, [tag_constants.SERVING],
         signature_def_map={"mypredict": signature_def})
   saved_model_builder.save()
开发者ID:ThunderQi,项目名称:tensorflow,代码行数:14,代码来源:trt_convert_test.py


示例13: _signature_with_no_inputs

 def _signature_with_no_inputs(self):
   export_graph = ops.Graph()
   with export_graph.as_default():
     array_ops.placeholder(name="x", shape=[], dtype=dtypes.float32)
     output = random_ops.random_normal([2])
     with session_lib.Session() as session:
       path = os.path.join(self.get_temp_dir(), "saved_model", str(ops.uid()))
       b = builder_impl.SavedModelBuilder(path)
       b.add_meta_graph_and_variables(
           session,
           tags=[tag_constants.SERVING],
           signature_def_map={
               "key": signature_def_utils.build_signature_def(
                   {}, dict(value=utils_impl.build_tensor_info(output)))})
       b.save()
   return path
开发者ID:adit-chandra,项目名称:tensorflow,代码行数:16,代码来源:load_v1_in_v2_test.py


示例14: _generate_signatures

def _generate_signatures(signature_functions, resource_map):
  """Validates and calls `signature_functions` in the default graph.

  Args:
    signature_functions: A dictionary mapping string keys to concrete TensorFlow
      functions (e.g. from `_canonicalize_signatures`) which will be used to
      generate SignatureDefs.
    resource_map: A dictionary mapping from resource tensors in the eager
      context to resource tensors in the Graph being exported. This dictionary
      is used to re-bind resources captured by functions to tensors which will
      exist in the SavedModel.

  Returns:
    Each function in the `signature_functions` dictionary is called with
    placeholder Tensors, generating a function call operation and output
    Tensors. The placeholder Tensors, the function call operation, and the
    output Tensors from the function call are part of the default Graph.

    This function then returns a dictionary with the same structure as
    `signature_functions`, with the concrete functions replaced by SignatureDefs
    implicitly containing information about how to call each function from a
    TensorFlow 1.x Session / the C++ Loader API. These SignatureDefs reference
    the generated placeholders and Tensor outputs by name.

    The caller is expected to include the default Graph set while calling this
    function as a MetaGraph in a SavedModel, including the returned
    SignatureDefs as part of that MetaGraph.
  """
  signatures = {}
  for signature_key, function in sorted(signature_functions.items()):
    if function.graph.captures:
      argument_inputs = function.graph.inputs[:-len(function.graph.captures)]
    else:
      argument_inputs = function.graph.inputs
    mapped_inputs, exterior_argument_placeholders = (
        _map_function_arguments_to_created_inputs(
            argument_inputs, signature_key, function.name))
    outputs = _normalize_outputs(
        _call_function_with_mapped_captures(
            function, mapped_inputs, resource_map),
        function.name, signature_key)
    signatures[signature_key] = signature_def_utils.build_signature_def(
        _tensor_dict_to_tensorinfo(exterior_argument_placeholders),
        _tensor_dict_to_tensorinfo(outputs),
        method_name=signature_constants.PREDICT_METHOD_NAME)
  return signatures
开发者ID:terrytangyuan,项目名称:tensorflow,代码行数:46,代码来源:save.py


示例15: test_load_saved_model_with_no_variables

  def test_load_saved_model_with_no_variables(self, builder_cls):
    """Test that SavedModel runs saver when there appear to be no variables.

    When no variables are detected, this may mean that the variables were saved
    to different collections, or the collections weren't saved to the
    SavedModel. If the SavedModel MetaGraphDef contains a saver, it should still
    run in either of these cases.

    Args:
      builder_cls: SavedModelBuilder or _SavedModelBuilder class
    """
    path = _get_export_dir("no_variable_saved_model")
    with session.Session(graph=ops.Graph()) as sess:
      x = variables.VariableV1(
          5, name="x", collections=["not_global_variable"])
      y = variables.VariableV1(
          11, name="y", collections=["not_global_variable"])
      self.assertFalse(variables._all_saveable_objects())
      z = x + y
      self.evaluate(variables.variables_initializer([x, y]))

      foo_sig_def = signature_def_utils.build_signature_def(
          {"foo_input": utils.build_tensor_info(x)},
          {"foo_output": utils.build_tensor_info(z)})

      builder = saved_model_builder.SavedModelBuilder(path)
      builder.add_meta_graph_and_variables(
          sess, ["foo_graph"], {"foo": foo_sig_def},
          saver=tf_saver.Saver([x, y]))
      builder.save()

    loader = loader_impl.SavedModelLoader(path)
    with self.session(graph=ops.Graph()) as sess:
      saver, _ = loader.load_graph(sess.graph, ["foo_graph"])
      self.assertFalse(variables._all_saveable_objects())
      self.assertIsNotNone(saver)

    with self.session(graph=ops.Graph()) as sess:
      loader.load(sess, ["foo_graph"])
      self.assertEqual(5, sess.graph.get_tensor_by_name("x:0").eval())
      self.assertEqual(11, sess.graph.get_tensor_by_name("y:0").eval())
开发者ID:Wajih-O,项目名称:tensorflow,代码行数:41,代码来源:loader_test.py


示例16: _v1_output_shape_saved_model

 def _v1_output_shape_saved_model(self):
   export_graph = ops.Graph()
   with export_graph.as_default():
     start = array_ops.placeholder(
         shape=[None], dtype=dtypes.float32, name="start")
     output = array_ops.identity(start, name="output")
     output.set_shape([1])  # Ok to use [1] because shape is only informational
     with session_lib.Session() as session:
       path = os.path.join(self.get_temp_dir(), "saved_model", str(ops.uid()))
       builder = builder_impl.SavedModelBuilder(path)
       builder.add_meta_graph_and_variables(
           session,
           tags=[tag_constants.SERVING],
           signature_def_map={
               "serving_default":
                   signature_def_utils.build_signature_def(
                       {"start": utils_impl.build_tensor_info(start)},
                       {"output": utils_impl.build_tensor_info(output)})
           })
       builder.save()
   return path
开发者ID:aritratony,项目名称:tensorflow,代码行数:21,代码来源:load_v1_in_v2_test.py


示例17: main

def main():
  # test_preprocess()
  # test_postprocess()

  preprocess_function_string = marshal.dumps(preprocess.func_code)
  tf.add_to_collection("preprocess_function", preprocess_function_string)
  postprocess_function_string = marshal.dumps(postprocess.func_code)
  tf.add_to_collection("postprocess_function", postprocess_function_string)

  model_path = "preprocess_model"
  model_version = 1

  keys_placeholder = tf.placeholder(tf.int32, shape=[None], name="keys")
  keys_identity = tf.identity(keys_placeholder, name="inference_keys")

  sess = tf.Session()
  sess.run(tf.global_variables_initializer())

  model_signature = signature_def_utils.build_signature_def(
      inputs={
          "keys": utils.build_tensor_info(keys_placeholder),
      },
      outputs={
          "keys": utils.build_tensor_info(keys_identity),
      },
      method_name=signature_constants.PREDICT_METHOD_NAME)

  export_path = os.path.join(
      compat.as_bytes(model_path), compat.as_bytes(str(model_version)))

  builder = saved_model_builder.SavedModelBuilder(export_path)
  builder.add_meta_graph_and_variables(
      sess, [tag_constants.SERVING],
      clear_devices=True,
      signature_def_map={
          signature_constants.DEFAULT_SERVING_SIGNATURE_DEF_KEY:
          model_signature,
      })

  builder.save()
开发者ID:tobegit3hub,项目名称:tensorflow_examples,代码行数:40,代码来源:export_preprocess_simplest_model.py


示例18: export

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()
开发者ID:lacatc,项目名称:text-antispam,代码行数:39,代码来源:mlp_classifier.py


示例19: export_model

  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()
开发者ID:lvaleriu,项目名称:Youtube-8M-WILLOW,代码行数:22,代码来源:export_model.py


示例20: build_signature

def build_signature(inputs, outputs):
  """Build the signature.

  Not using predic_signature_def in saved_model because it is replacing the
  tensor name, b/35900497.

  Args:
    inputs: a dictionary of tensor name to tensor
    outputs: a dictionary of tensor name to tensor
  Returns:
    The signature, a SignatureDef proto.
  """
  signature_inputs = {key: saved_model_utils.build_tensor_info(tensor)
                      for key, tensor in inputs.items()}
  signature_outputs = {key: saved_model_utils.build_tensor_info(tensor)
                       for key, tensor in outputs.items()}

  signature_def = signature_def_utils.build_signature_def(
      signature_inputs, signature_outputs,
      signature_constants.PREDICT_METHOD_NAME)

  return signature_def
开发者ID:amygdala,项目名称:tensorflow-workshop,代码行数:22,代码来源:model.py



注:本文中的tensorflow.python.saved_model.signature_def_utils.build_signature_def函数示例由纯净天空整理自Github/MSDocs等源码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。


鲜花

握手

雷人

路过

鸡蛋
该文章已有0人参与评论

请发表评论

全部评论

专题导读
上一篇:
Python signature_def_utils.classification_signature_def函数代码示例发布时间:2022-05-27
下一篇:
Python save.save函数代码示例发布时间:2022-05-27
热门推荐
阅读排行榜

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

在线客服(服务时间 9:00~18:00)

在线QQ客服
地址:深圳市南山区西丽大学城创智工业园
电邮:jeky_zhao#qq.com
移动电话:139-2527-9053

Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap