如何使用java客户端为宽和深模型请求张量流服务或如何使用java加载宽和深模型并预测?

时间:2017-11-09 08:05:40

标签: java tensorflow client tensorflow-serving

我已经编写了python客户端,通过张量流请求广泛和深度模型服务成功,但我怀疑如何使用java来解决它,因为示例和文档太缺乏。 使用python我已成功运行它,因为它可以通过Features Dict告诉Tensor流服务如何处理features.like流程:

 example = tf.train.Example(features=tf.train.Features(feature=feature_dict))
  serialized = example.SerializeToString()

  request.inputs['inputs'].CopyFrom(
        tf.contrib.util.make_tensor_proto(serialized, shape=[1]))
  result_future = stub.Predict.future(request, 1.0)

但是使用java我不知道如何通过功能dict来告诉tensor flow_serving如何处理features.i有写java客户端但得到流错误我没有通过功能映射

Nov 09, 2017 7:18:09 AM com.bj58.gul.model.entity.TestWideAndDeepModelClient predict
WARNING: RPC failed: Status{code=INVALID_ARGUMENT, description=Name: <unknown>, Feature: getGBDTDiffTimeBetweenItemShowTimeAndCreatedTime (data type: float) is required but could not be found.
     [[Node: ParseExample/ParseExample = ParseExample[Ndense=15, Nsparse=66, Tdense=[DT_FLOAT, DT_FLOAT, DT_FLOAT, DT_FLOAT, DT_FLOAT, DT_FLOAT, DT_FLOAT, DT_FLOAT, DT_FLOAT, DT_FLOAT, DT_FLOAT, DT_FLOAT, DT_FLOAT, DT_FLOAT, DT_FLOAT], _output_shapes=[[?,2], [?,2], [?,2], [?,2], [?,2], [?,2], [?,2], [?,2], [?,2], [?,2], [?,2], [?,2], [?,2], [?,2], [?,2], [?,2], [?,2], [?,2], [?,2], [?,2], [?,2], [?,2], [?,2], [?,2], [?,2], [?,2], [?,2], [?,2], [?,2], [?,2], [?,2], [?,2], [?,2], [?,2], [?,2], [?,2], [?,2], [?,2], [?,2], [?,2], [?,2], [?,2], [?,2], [?,2], [?,2], [?,2], [?,2], [?,2], [?,2], [?,2], [?,2], [?,2], [?,2], [?,2], [?,2], [?,2], [?,2], [?,2], [?,2], [?,2], [?,2], [?,2], [?,2], [?,2], [?,2], [?,2], [?], [?], [?], [?], [?], [?], [?], [?], [?], [?], [?], [?], [?], [?], [?], [?], [?], [?], [?], [?], [?], [?], [?], [?], [?], [?], [?], [?], [?], [?], [?], [?], [?], [?], [?], [?], [?]...TRUNCATED, cause=null}
Nov 09, 2017 7:18:09 AM io.grpc.internal.ManagedChannelImpl maybeTerminateChannel
INFO: [ManagedChannelImpl@3cb5cdba] Terminated
End of predict client

1 个答案:

答案 0 :(得分:1)

这是一段伪代码片段,我设法使广泛和深入的模型客户端(java)工作。

前提条件:您已将导出的模型置于服务(wide_and_deep_tutorial)。

import com.google.common.collect.ImmutableMap;
import com.google.protobuf.ByteString;
import com.google.protobuf.Int64Value;
import org.tensorflow.example.*;
import org.tensorflow.framework.DataType;
import org.tensorflow.framework.TensorProto;
import org.tensorflow.framework.TensorShapeProto;
import org.tensorflow.framework.TensorShapeProto.Dim;
import tensorflow.serving.Model.ModelSpec;
import tensorflow.serving.Predict.PredictRequest;
import tensorflow.serving.Predict.PredictResponse;
import tensorflow.serving.PredictionServiceGrpc.PredictionServiceBlockingStub;

......(here the declare of class and function is neglected, only showing the core part below)

private static final PredictionServiceBlockingStub stub = PredictionServiceGrpc.newBlockingStub(new ForceDeadlineChannel(TF_SERVICE_HOST, TF_SERVICE_PORT, 5000));
private HashMap<String, Feature> inputFeatureMap = new HashMap();
private ByteString inputStr;
Integer modelVer = 123;

......

for (each feature in feature list) {
    Feature feature = null;
        if (type is string) {
            feature = Feature.newBuilder().setBytesList(BytesList.newBuilder().addValue(ByteString.copyFromUtf8("dummy string"))).build();
        } else if (type if float) {
            feature = Feature.newBuilder().setFloatList(FloatList.newBuilder().addValue(3.1415f)).build();
        } else if (type is int) {
            feature = Feature.newBuilder().setInt64List(Int64List.newBuilder().addValue(1l)).build();
        }
        if (feature != null) {
            inputFeatureMap.put(name, feature);
        }
        Features features = Features.newBuilder().putAllFeature(inputFeatureMap).build();
        inputStr = Example.newBuilder().setFeatures(features).build().toByteString();
}

TensorProto proto = TensorProto.newBuilder()
            .addStringVal(inputStr)
            .setTensorShape(TensorShapeProto.newBuilder().addDim(TensorShapeProto.Dim.newBuilder().setSize(1).build()).build())
            .setDtype(DataType.DT_STRING)
            .build();

PredictRequest req = PredictRequest.newBuilder()
            .setModelSpec(ModelSpec.newBuilder()
                    .setName("your serving model name")
                    .setSignatureName("serving_default")
                    .setVersion(Int64Value.newBuilder().setValue(modelVer)))
            .putAllInputs(ImmutableMap.of("inputs", proto))
            .build();

PredictResponse response = stub.predict(req);

System.out.println(response.getOutputsMap());
......