saved_model_cli show --dir 15/ --all 用于查看tensorflowd export 模型的输入输出。
    其中包含SignatureName,input 和output 维度和类型,在golang 侧代码会用到

    1. (base) dh@DHMacBook risk_rec % saved_model_cli show --dir 15/ --all
    2. /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'.
    3. _np_qint8 = np.dtype([("qint8", np.int8, 1)])
    4. /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'.
    5. _np_quint8 = np.dtype([("quint8", np.uint8, 1)])
    6. /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'.
    7. _np_qint16 = np.dtype([("qint16", np.int16, 1)])
    8. /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'.
    9. _np_quint16 = np.dtype([("quint16", np.uint16, 1)])
    10. /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'.
    11. _np_qint32 = np.dtype([("qint32", np.int32, 1)])
    12. /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'.
    13. np_resource = np.dtype([("resource", np.ubyte, 1)])
    14. /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'.
    15. _np_qint8 = np.dtype([("qint8", np.int8, 1)])
    16. /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'.
    17. _np_quint8 = np.dtype([("quint8", np.uint8, 1)])
    18. /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'.
    19. _np_qint16 = np.dtype([("qint16", np.int16, 1)])
    20. /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'.
    21. _np_quint16 = np.dtype([("quint16", np.uint16, 1)])
    22. /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'.
    23. _np_qint32 = np.dtype([("qint32", np.int32, 1)])
    24. /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'.
    25. np_resource = np.dtype([("resource", np.ubyte, 1)])
    26. MetaGraphDef with tag-set: 'serve' contains the following SignatureDefs:
    27. signature_def['predict_images']:
    28. The given SavedModel SignatureDef contains the following input(s):
    29. inputs['dropout_keep_prob'] tensor_info:
    30. dtype: DT_FLOAT
    31. shape: unknown_rank
    32. name: keep_prob:0
    33. inputs['images'] tensor_info:
    34. dtype: DT_INT32
    35. shape: (-1, 256)
    36. name: input_x:0
    37. The given SavedModel SignatureDef contains the following output(s):
    38. outputs['predictions'] tensor_info:
    39. dtype: DT_FLOAT
    40. shape: (-1, 3)
    41. name: score/y_out:0
    42. outputs['scores'] tensor_info:
    43. dtype: DT_INT64
    44. shape: (-1)
    45. name: score/predictions:0
    46. Method name is: tensorflow/serving/predict
    47. signature_def['serving_default']:
    48. The given SavedModel SignatureDef contains the following input(s):
    49. inputs['inputs'] tensor_info:
    50. dtype: DT_STRING
    51. shape: unknown_rank
    52. name: tf_example:0
    53. The given SavedModel SignatureDef contains the following output(s):
    54. outputs['classes'] tensor_info:
    55. dtype: DT_FLOAT
    56. shape: (-1, 3)
    57. name: input_y:0
    58. outputs['scores'] tensor_info:
    59. dtype: DT_INT64
    60. shape: (-1)
    61. name: score/predictions:0
    62. Method name is: tensorflow/serving/classify
    1. 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源码
    初始化

    1. RpcAddress="localhost:8801"
    2. // 如果是docker 部署服务,RpcAddress="RiskPredict:8500"
    3. var eventCnnServerConn, eventCnnServerConnErr = grpc.Dial(RpcAddress, grpc.WithInsecure())
    4. if eventCnnServerConnErr != nil {
    5. beego.Error("did not connect: %v", eventCnnServerConnErr)
    6. os.Exit(1)
    7. }

    classificationCNNModelName必须与docker 部署的环境变量 MODEL_NAME一致

    1. classificationCNNModelName="RiskPredict"
    2. classificationCNNSignatureName="predict_images"
    3. beego.Info("server conn:", classificationCNNServerConn.GetState().String())
    4. if classificationCNNServerConn.GetState().String() == "TRANSIENT_FAILURE" {
    5. return nil, errors.New("model is TRANSIENT_FAILURE")
    6. }
    7. classificationServer := pb.NewPredictionServiceClient(classificationCNNServerConn)
    8. modelSpec := pb.ModelSpec{
    9. Name: classificationCNNModelName,
    10. SignatureName: classificationCNNSignatureName,
    11. }
    12. dim11 := pb.TensorShapeProto_Dim{
    13. Size: 1,
    14. }
    15. dim12 := pb.TensorShapeProto_Dim{
    16. Size: reflect.ValueOf(len(image)).Int(),
    17. }
    18. shape1 := pb.TensorShapeProto{
    19. Dim: []*pb.TensorShapeProto_Dim{&dim11, &dim12},
    20. }
    21. tensorProto1 := pb.TensorProto{
    22. Dtype: pb.DataType_DT_INT32,
    23. IntVal: image,
    24. TensorShape: &shape1,
    25. }
    26. dim2 := pb.TensorShapeProto_Dim{
    27. Size: 1,
    28. }
    29. shape2 := pb.TensorShapeProto{
    30. Dim: []*pb.TensorShapeProto_Dim{&dim2},
    31. }
    32. tensorProto2 := pb.TensorProto{
    33. Dtype: pb.DataType_DT_FLOAT,
    34. FloatVal: []float32{1.0},
    35. TensorShape: &shape2,
    36. }
    37. inputs := map[string]*pb.TensorProto{
    38. "images": &tensorProto1,
    39. "dropout_keep_prob": &tensorProto2,
    40. }
    41. //beego.Debug("inputs:", inputs)
    42. request := pb.PredictRequest{
    43. ModelSpec: &modelSpec,
    44. Inputs: inputs,
    45. }
    46. r, err := classificationServer.Predict(context.Background(), &request)
    47. if err != nil {
    48. beego.Error("predict error: ", err)
    49. }
    50. //scores := r.Outputs["scores"].Int64Val
    51. scores :=r.Outputs["predictions"].FloatVal