saved_model_cli show --dir 15/ --all 用于查看tensorflowd export 模型的输入输出。
其中包含SignatureName,input 和output 维度和类型,在golang 侧代码会用到
(base) dh@DHMacBook risk_rec % saved_model_cli show --dir 15/ --all/Users/dh/miniconda3/lib/python3.7/site-packages/tensorflow/python/framework/dtypes.py:516: FutureWarning: Passing (type, 1) or '1type' as a synonym of type is deprecated; in a future version of numpy, it will be understood as (type, (1,)) / '(1,)type'._np_qint8 = np.dtype([("qint8", np.int8, 1)])/Users/dh/miniconda3/lib/python3.7/site-packages/tensorflow/python/framework/dtypes.py:517: FutureWarning: Passing (type, 1) or '1type' as a synonym of type is deprecated; in a future version of numpy, it will be understood as (type, (1,)) / '(1,)type'._np_quint8 = np.dtype([("quint8", np.uint8, 1)])/Users/dh/miniconda3/lib/python3.7/site-packages/tensorflow/python/framework/dtypes.py:518: FutureWarning: Passing (type, 1) or '1type' as a synonym of type is deprecated; in a future version of numpy, it will be understood as (type, (1,)) / '(1,)type'._np_qint16 = np.dtype([("qint16", np.int16, 1)])/Users/dh/miniconda3/lib/python3.7/site-packages/tensorflow/python/framework/dtypes.py:519: FutureWarning: Passing (type, 1) or '1type' as a synonym of type is deprecated; in a future version of numpy, it will be understood as (type, (1,)) / '(1,)type'._np_quint16 = np.dtype([("quint16", np.uint16, 1)])/Users/dh/miniconda3/lib/python3.7/site-packages/tensorflow/python/framework/dtypes.py:520: FutureWarning: Passing (type, 1) or '1type' as a synonym of type is deprecated; in a future version of numpy, it will be understood as (type, (1,)) / '(1,)type'._np_qint32 = np.dtype([("qint32", np.int32, 1)])/Users/dh/miniconda3/lib/python3.7/site-packages/tensorflow/python/framework/dtypes.py:525: FutureWarning: Passing (type, 1) or '1type' as a synonym of type is deprecated; in a future version of numpy, it will be understood as (type, (1,)) / '(1,)type'.np_resource = np.dtype([("resource", np.ubyte, 1)])/Users/dh/miniconda3/lib/python3.7/site-packages/tensorboard/compat/tensorflow_stub/dtypes.py:541: FutureWarning: Passing (type, 1) or '1type' as a synonym of type is deprecated; in a future version of numpy, it will be understood as (type, (1,)) / '(1,)type'._np_qint8 = np.dtype([("qint8", np.int8, 1)])/Users/dh/miniconda3/lib/python3.7/site-packages/tensorboard/compat/tensorflow_stub/dtypes.py:542: FutureWarning: Passing (type, 1) or '1type' as a synonym of type is deprecated; in a future version of numpy, it will be understood as (type, (1,)) / '(1,)type'._np_quint8 = np.dtype([("quint8", np.uint8, 1)])/Users/dh/miniconda3/lib/python3.7/site-packages/tensorboard/compat/tensorflow_stub/dtypes.py:543: FutureWarning: Passing (type, 1) or '1type' as a synonym of type is deprecated; in a future version of numpy, it will be understood as (type, (1,)) / '(1,)type'._np_qint16 = np.dtype([("qint16", np.int16, 1)])/Users/dh/miniconda3/lib/python3.7/site-packages/tensorboard/compat/tensorflow_stub/dtypes.py:544: FutureWarning: Passing (type, 1) or '1type' as a synonym of type is deprecated; in a future version of numpy, it will be understood as (type, (1,)) / '(1,)type'._np_quint16 = np.dtype([("quint16", np.uint16, 1)])/Users/dh/miniconda3/lib/python3.7/site-packages/tensorboard/compat/tensorflow_stub/dtypes.py:545: FutureWarning: Passing (type, 1) or '1type' as a synonym of type is deprecated; in a future version of numpy, it will be understood as (type, (1,)) / '(1,)type'._np_qint32 = np.dtype([("qint32", np.int32, 1)])/Users/dh/miniconda3/lib/python3.7/site-packages/tensorboard/compat/tensorflow_stub/dtypes.py:550: FutureWarning: Passing (type, 1) or '1type' as a synonym of type is deprecated; in a future version of numpy, it will be understood as (type, (1,)) / '(1,)type'.np_resource = np.dtype([("resource", np.ubyte, 1)])MetaGraphDef with tag-set: 'serve' contains the following SignatureDefs:signature_def['predict_images']:The given SavedModel SignatureDef contains the following input(s):inputs['dropout_keep_prob'] tensor_info:dtype: DT_FLOATshape: unknown_rankname: keep_prob:0inputs['images'] tensor_info:dtype: DT_INT32shape: (-1, 256)name: input_x:0The given SavedModel SignatureDef contains the following output(s):outputs['predictions'] tensor_info:dtype: DT_FLOATshape: (-1, 3)name: score/y_out:0outputs['scores'] tensor_info:dtype: DT_INT64shape: (-1)name: score/predictions:0Method name is: tensorflow/serving/predictsignature_def['serving_default']:The given SavedModel SignatureDef contains the following input(s):inputs['inputs'] tensor_info:dtype: DT_STRINGshape: unknown_rankname: tf_example:0The given SavedModel SignatureDef contains the following output(s):outputs['classes'] tensor_info:dtype: DT_FLOATshape: (-1, 3)name: input_y:0outputs['scores'] tensor_info:dtype: DT_INT64shape: (-1)name: score/predictions:0Method name is: tensorflow/serving/classify
docker run -t --rm -h RiskPredict -d -p 8801:8500 -v /root/***/risk_rec:/models/RiskPredict --name RiskPredict -e MODEL_NAME=RiskPredict tensorflow/serving
备注:MODEL_NAME必须与映射的路径/models/RiskPredict 最后一层目录名一致。
golang侧下载github上的tensorflow下的tensorflow和serving源码
初始化
RpcAddress="localhost:8801"// 如果是docker 部署服务,RpcAddress="RiskPredict:8500"var eventCnnServerConn, eventCnnServerConnErr = grpc.Dial(RpcAddress, grpc.WithInsecure())if eventCnnServerConnErr != nil {beego.Error("did not connect: %v", eventCnnServerConnErr)os.Exit(1)}
classificationCNNModelName必须与docker 部署的环境变量 MODEL_NAME一致
classificationCNNModelName="RiskPredict"classificationCNNSignatureName="predict_images"beego.Info("server conn:", classificationCNNServerConn.GetState().String())if classificationCNNServerConn.GetState().String() == "TRANSIENT_FAILURE" {return nil, errors.New("model is TRANSIENT_FAILURE")}classificationServer := pb.NewPredictionServiceClient(classificationCNNServerConn)modelSpec := pb.ModelSpec{Name: classificationCNNModelName,SignatureName: classificationCNNSignatureName,}dim11 := pb.TensorShapeProto_Dim{Size: 1,}dim12 := pb.TensorShapeProto_Dim{Size: reflect.ValueOf(len(image)).Int(),}shape1 := pb.TensorShapeProto{Dim: []*pb.TensorShapeProto_Dim{&dim11, &dim12},}tensorProto1 := pb.TensorProto{Dtype: pb.DataType_DT_INT32,IntVal: image,TensorShape: &shape1,}dim2 := pb.TensorShapeProto_Dim{Size: 1,}shape2 := pb.TensorShapeProto{Dim: []*pb.TensorShapeProto_Dim{&dim2},}tensorProto2 := pb.TensorProto{Dtype: pb.DataType_DT_FLOAT,FloatVal: []float32{1.0},TensorShape: &shape2,}inputs := map[string]*pb.TensorProto{"images": &tensorProto1,"dropout_keep_prob": &tensorProto2,}//beego.Debug("inputs:", inputs)request := pb.PredictRequest{ModelSpec: &modelSpec,Inputs: inputs,}r, err := classificationServer.Predict(context.Background(), &request)if err != nil {beego.Error("predict error: ", err)}//scores := r.Outputs["scores"].Int64Valscores :=r.Outputs["predictions"].FloatVal
