tensorflow_serving/experimental/example/half_plus_two_with_rpop.py
def _generate_saved_model_for_half_plus_two(export_dir, target_address,
                                            remote_model_name):
  """Generates SavedModel for half plus two with RemotePredictOp.

  Args:
    export_dir: The directory to which the SavedModel should be written.
    target_address: The target_address for RemotePredictOp.
    remote_model_name: The model name for RemotePredictOp.
  """
  builder = tf.saved_model.builder.SavedModelBuilder(export_dir)

  device_name = "/cpu:0"

  with tf.Session(
      graph=tf.Graph(),
      config=tf.ConfigProto(log_device_placement=True)) as sess:
    with tf.device(device_name):
      a = tf.Variable(0.5, name="a")

      model_name = remote_model_name
      input_tensor_aliases = tf.constant(["x"])
      input_tensors = [tf.constant(2.0, tf.float32)]

      output_tensor_aliases = tf.constant(["y"])
      output_types = [tf.float32]

      results = remote_predict_ops.run(
          input_tensor_aliases,
          input_tensors,
          output_tensor_aliases,
          target_address=target_address,
          model_name=model_name,
          model_version=FLAGS.remote_model_version,
          output_types=output_types)
      b = results.output_tensors[0]

      # Create a placeholder for serialized tensorflow.Example messages to be
      # fed.
      serialized_tf_example = tf.placeholder(tf.string, name="tf_example")

      # Parse the tensorflow.Example looking for a feature named "x" with a
      # single floating point value.
      feature_configs = {"x": tf.FixedLenFeature([1], dtype=tf.float32)}
      # parse_example only works on CPU
      with tf.device("/cpu:0"):
        tf_example = tf.parse_example(serialized_tf_example, feature_configs)
      # Use tf.identity() to assign name
      x = tf.identity(tf_example["x"], name="x")
      y = tf.add(tf.multiply(a, x), b, name="y")

    # Set up the signature for Predict with input and output tensor
    # specification.
    predict_input_tensor = tf.saved_model.utils.build_tensor_info(x)
    predict_signature_inputs = {"x": predict_input_tensor}

    predict_output_tensor = tf.saved_model.utils.build_tensor_info(y)
    predict_signature_outputs = {"y": predict_output_tensor}
    predict_signature_def = (
        tf.saved_model.signature_def_utils.build_signature_def(
            inputs=predict_signature_inputs,
            outputs=predict_signature_outputs,
            method_name=tf.saved_model.signature_constants.PREDICT_METHOD_NAME))

    signature_def_map = {
        tf.saved_model.signature_constants.DEFAULT_SERVING_SIGNATURE_DEF_KEY:
            predict_signature_def
    }
    # Initialize all variables and then save the SavedModel.
    sess.run(tf.global_variables_initializer())

    builder.add_meta_graph_and_variables(
        sess, [tf.saved_model.tag_constants.SERVING],
        signature_def_map=signature_def_map,
        assets_collection=tf.get_collection(tf.GraphKeys.ASSET_FILEPATHS),
        main_op=tf.compat.v1.tables_initializer(),
        strip_default_attrs=True)
  builder.save(False)
Generates SavedModel for half plus two with RemotePredictOp.

  Args:
    export_dir: The directory to which the SavedModel should be written.
    target_address: The target_address for RemotePredictOp.
    remote_model_name: The model name for RemotePredictOp.
  
Generates SavedModel for half plus two with RemotePredictOp.
Generates SavedModel for half plus two with RemotePredictOp .
def _generate_saved_model_for_half_plus_two(export_dir, target_address,
                                            remote_model_name):
  builder = tf.saved_model.builder.SavedModelBuilder(export_dir)
  device_name = "/cpu:0"
  with tf.Session(
      graph=tf.Graph(),
      config=tf.ConfigProto(log_device_placement=True)) as sess:
    with tf.device(device_name):
      a = tf.Variable(0.5, name="a")
      model_name = remote_model_name
      input_tensor_aliases = tf.constant(["x"])
      input_tensors = [tf.constant(2.0, tf.float32)]
      output_tensor_aliases = tf.constant(["y"])
      output_types = [tf.float32]
      results = remote_predict_ops.run(
          input_tensor_aliases,
          input_tensors,
          output_tensor_aliases,
          target_address=target_address,
          model_name=model_name,
          model_version=FLAGS.remote_model_version,
          output_types=output_types)
      b = results.output_tensors[0]
      serialized_tf_example = tf.placeholder(tf.string, name="tf_example")
      feature_configs = {"x": tf.FixedLenFeature([1], dtype=tf.float32)}
      with tf.device("/cpu:0"):
        tf_example = tf.parse_example(serialized_tf_example, feature_configs)
      x = tf.identity(tf_example["x"], name="x")
      y = tf.add(tf.multiply(a, x), b, name="y")
    predict_input_tensor = tf.saved_model.utils.build_tensor_info(x)
    predict_signature_inputs = {"x": predict_input_tensor}
    predict_output_tensor = tf.saved_model.utils.build_tensor_info(y)
    predict_signature_outputs = {"y": predict_output_tensor}
    predict_signature_def = (
        tf.saved_model.signature_def_utils.build_signature_def(
            inputs=predict_signature_inputs,
            outputs=predict_signature_outputs,
            method_name=tf.saved_model.signature_constants.PREDICT_METHOD_NAME))
    signature_def_map = {
        tf.saved_model.signature_constants.DEFAULT_SERVING_SIGNATURE_DEF_KEY:
            predict_signature_def
    }
    sess.run(tf.global_variables_initializer())
    builder.add_meta_graph_and_variables(
        sess, [tf.saved_model.tag_constants.SERVING],
        signature_def_map=signature_def_map,
        assets_collection=tf.get_collection(tf.GraphKeys.ASSET_FILEPATHS),
        main_op=tf.compat.v1.tables_initializer(),
        strip_default_attrs=True)
  builder.save(False)
def _generate_saved_model_for_half_plus_two ( export_dir , target_address , remote_model_name ) : builder = tf . saved_model . builder . SavedModelBuilder ( export_dir ) device_name = "/cpu:0" with tf . Session ( graph = tf . Graph ( ) , config = tf . ConfigProto ( log_device_placement = True ) ) as sess : with tf . device ( device_name ) : a = tf . Variable ( 0.5 , name = "a" ) model_name = remote_model_name input_tensor_aliases = tf . constant ( [ "x" ] ) input_tensors = [ tf . constant ( 2.0 , tf . float32 ) ] output_tensor_aliases = tf . constant ( [ "y" ] ) output_types = [ tf . float32 ] results = remote_predict_ops . run ( input_tensor_aliases , input_tensors , output_tensor_aliases , target_address = target_address , model_name = model_name , model_version = FLAGS . remote_model_version , output_types = output_types ) b = results . output_tensors [ 0 ] serialized_tf_example = tf . placeholder ( tf . string , name = "tf_example" ) feature_configs = { "x" : tf . FixedLenFeature ( [ 1 ] , dtype = tf . float32 ) } with tf . device ( "/cpu:0" ) : tf_example = tf . parse_example ( serialized_tf_example , feature_configs ) x = tf . identity ( tf_example [ "x" ] , name = "x" ) y = tf . add ( tf . multiply ( a , x ) , b , name = "y" ) predict_input_tensor = tf . saved_model . utils . build_tensor_info ( x ) predict_signature_inputs = { "x" : predict_input_tensor } predict_output_tensor = tf . saved_model . utils . build_tensor_info ( y ) predict_signature_outputs = { "y" : predict_output_tensor } predict_signature_def = ( tf . saved_model . signature_def_utils . build_signature_def ( inputs = predict_signature_inputs , outputs = predict_signature_outputs , method_name = tf . saved_model . signature_constants . PREDICT_METHOD_NAME ) ) signature_def_map = { tf . saved_model . signature_constants . DEFAULT_SERVING_SIGNATURE_DEF_KEY : predict_signature_def } sess . run ( tf . global_variables_initializer ( ) ) builder . add_meta_graph_and_variables ( sess , [ tf . saved_model . tag_constants . SERVING ] , signature_def_map = signature_def_map , assets_collection = tf . get_collection ( tf . GraphKeys . ASSET_FILEPATHS ) , main_op = tf . compat . v1 . tables_initializer ( ) , strip_default_attrs = True ) builder . save ( False )
Generates SavedModel for half plus two with RemotePredictOp.
Generates SavedModel for half plus two with RemotePredictOp .
"""Generates SavedModel for half plus two with RemotePredictOp.

  Args:
    export_dir: The directory to which the SavedModel should be written.
    target_address: The target_address for RemotePredictOp.
    remote_model_name: The model name for RemotePredictOp.
  """ # Create a placeholder for serialized tensorflow.Example messages to be # fed. # Parse the tensorflow.Example looking for a feature named "x" with a # single floating point value. # parse_example only works on CPU # Use tf.identity() to assign name # Set up the signature for Predict with input and output tensor # specification. # Initialize all variables and then save the SavedModel.
export_dir
The directory to which the SavedModel should be written.
The directory to which the SavedModel should be written .
target_address
The target_address for RemotePredictOp.
The target_address for RemotePredictOp .
remote_model_name
The model name for RemotePredictOp.
The model name for RemotePredictOp .