// 下载模型 private static final SavedModelBundle modelBundle = SavedModelBundle.load(模型路径,"serve"); // session private static final Session tfSession = modelBundle.session(); // 预测 private static final Operation operationPredict = modelBundle.graph().operation("score/my_predict"); // 输出 private static final Output output = new Output(operationPredict,0); // Tensor private static final Tensor keep_prob = Tensor.create(Float.parseFloat("0.6"));
// 输入转换成向量,a为向量
Tensor input_x = Tensor.create(a); // 预测输出,类目对应编号 Tensor out = tfSession.runner().feed("input_x", input_x).feed("keep_prob",keep_prob).fetch(output).run().get(0); long[] temp = new long[1]; out.copyTo(temp); short reskey = (short) temp[0];
调用线上模型:
// 输入长度---并转化成向量 List<Object> requestData = new ArrayList<>(Collections.nCopies(输入向量长度,0));
// 预测结果
Object responseData = iWpaiDLPredictOnlineService.tensorflowServingPredictOnline(线上服务编号,requestData);