Home > database >  How to use Java to implement code to serialize protobuf object in memory?
How to use Java to implement code to serialize protobuf object in memory?

Time:03-12

I want to know how to use Java to do the same thing like this :

exam_input = tf.constant([example_proto.SerializeToString()]) 

I try to do something like this:

val result = sparkEnv.spark.read.parquet(inputPath).map(item => {
      val map = new java.util.HashMap[String,Tensor]()
      val tensor = TString.vectorOf(new String(example.toByteArray, StandardCharsets.UTF_8))
      map.put("examples",tensor)
      val score = model.value.call(map).get("score")
      score.toString
    }).rdd

However, it is not correct, so what should I do?

CodePudding user response:

There are multiple ways for doing this but I think in your case you first want to parse your example proto file to extract the features to be passed as tensors to your function.

A starting point for parsing is the set of protos available in TFJava. Once you get hold on a list of values (such as a FloatList), you can create a tensor from it or by passing that list as an array of bytes to this factory.

For example this converts the Int64List input feature to a vector:

    Example example = Example.parseFrom(new FileInputStream("your_example.pb"));
    Feature feature = example.getFeatures().getFeatureOrThrow("input");
    long[] values = feature.getInt64List().getValueList().stream().mapToLong(i -> i.longValue()).toArray();
    try (TInt64 tensor = TInt64.vectorOf(values)) {
      System.out.println("Tensor shape is "   tensor.shape());
    }

CodePudding user response:

@Karl Lessard:

thank u for your answer. but my problem is to due with lots of features, and the model input is seriliazed example, just like this:

MetaGraphDef with tag-set: 'serve' contains the following SignatureDefs:

signature_def['serving_default']:
  The given SavedModel SignatureDef contains the following input(s):
    inputs['examples'] tensor_info:
        dtype: DT_STRING
        shape: (-1)
        name: input_example_tensor:0
  The given SavedModel SignatureDef contains the following output(s):
    outputs['cf_1'] tensor_info:
        dtype: DT_INT64
        shape: (-1, 1)
        name: ParseExample/ParseExampleV2:0
    outputs['cf_2'] tensor_info:
        dtype: DT_INT64
        shape: (-1, 1)
        name: ParseExample/ParseExampleV2:1
    outputs['cf_label'] tensor_info:
        dtype: DT_INT64
        shape: (-1, 1)
        name: ParseExample/ParseExampleV2:2
    outputs['cf_user_id'] tensor_info:
        dtype: DT_INT64
        shape: (-1, 1)
        name: ParseExample/ParseExampleV2:3
    outputs['score'] tensor_info:
        dtype: DT_FLOAT
        shape: (-1, 1)
        name: score:0
  Method name is: tensorflow/serving/predict

i wanna to predict the result using serilized example like python

  • Related