Last active
April 2, 2022 08:43
-
-
Save mauri870/1f953a183ee6c186e70a0a72e78b088c to your computer and use it in GitHub Desktop.
Tensorflow Serving Go client for the inception model
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Tensorflow Serving Go client for the inception model | |
// go get github.com/golang/protobuf/ptypes/wrappers google.golang.org/grpc | |
// | |
// Compile the proto files: | |
// | |
// git clone https://github.com/tensorflow/serving.git | |
// git clone https://github.com/tensorflow/tensorflow.git | |
// | |
// mkdir -p vendor | |
// PROTOC_OPTS='-I tensorflow -I serving --go_out=plugins=grpc:vendor' | |
// eval "protoc $PROTOC_OPTS serving/tensorflow_serving/apis/*.proto" | |
// eval "protoc $PROTOC_OPTS serving/tensorflow_serving/config/*.proto" | |
// eval "protoc $PROTOC_OPTS serving/tensorflow_serving/util/*.proto" | |
// eval "protoc $PROTOC_OPTS serving/tensorflow_serving/sources/storage_path/*.proto" | |
// eval "protoc $PROTOC_OPTS tensorflow/tensorflow/core/framework/*.proto" | |
// eval "protoc $PROTOC_OPTS tensorflow/tensorflow/core/example/*.proto" | |
// eval "protoc $PROTOC_OPTS tensorflow/tensorflow/core/lib/core/*.proto" | |
// eval "protoc $PROTOC_OPTS tensorflow/tensorflow/core/protobuf/{saver,meta_graph}.proto" | |
package main | |
import ( | |
"context" | |
"flag" | |
"fmt" | |
"io/ioutil" | |
"log" | |
"os" | |
"path/filepath" | |
tf_core_framework "tensorflow/core/framework" | |
pb "tensorflow_serving/apis" | |
google_protobuf "github.com/golang/protobuf/ptypes/wrappers" | |
"google.golang.org/grpc" | |
) | |
func main() { | |
servingAddress := flag.String("serving-address", "localhost:10000", "The tensorflow serving address") | |
flag.Parse() | |
if flag.NArg() != 1 { | |
fmt.Println("Usage: " + os.Args[0] + " --serving-address localhost:10000 path/to/img.png") | |
os.Exit(1) | |
} | |
imgPath, err := filepath.Abs(flag.Arg(0)) | |
if err != nil { | |
log.Fatalln(err) | |
} | |
imageBytes, err := ioutil.ReadFile(imgPath) | |
if err != nil { | |
log.Fatalln(err) | |
} | |
request := &pb.PredictRequest{ | |
ModelSpec: &pb.ModelSpec{ | |
Name: "inception", | |
SignatureName: "predict_images", | |
Version: &google_protobuf.Int64Value{ | |
Value: int64(1), | |
}, | |
}, | |
Inputs: map[string]*tf_core_framework.TensorProto{ | |
"images": &tf_core_framework.TensorProto{ | |
Dtype: tf_core_framework.DataType_DT_STRING, | |
TensorShape: &tf_core_framework.TensorShapeProto{ | |
Dim: []*tf_core_framework.TensorShapeProto_Dim{ | |
&tf_core_framework.TensorShapeProto_Dim{ | |
Size: int64(1), | |
}, | |
}, | |
}, | |
StringVal: [][]byte{imageBytes}, | |
}, | |
}, | |
} | |
conn, err := grpc.Dial(*servingAddress, grpc.WithInsecure()) | |
if err != nil { | |
log.Fatalf("Cannot connect to the grpc server: %v\n", err) | |
} | |
defer conn.Close() | |
client := pb.NewPredictionServiceClient(conn) | |
resp, err := client.Predict(context.Background(), request) | |
if err != nil { | |
log.Fatalln(err) | |
} | |
log.Println(resp) | |
} |
eval "protoc $PROTOC_OPTS tensorflow/tensorflow/core/protobuf/{saver,meta_graph}.proto"
GOT an error message:
--go_out: protoc-gen-go: plugins are not supported; use 'protoc --go-grpc_out=...' to generate gRP
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Updated version here