Skip to content

Reuse TensorFlow protobuf messages #1728

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wangkuiyi opened this issue Feb 13, 2020 · 0 comments · Fixed by #1729
Closed

Reuse TensorFlow protobuf messages #1728

wangkuiyi opened this issue Feb 13, 2020 · 0 comments · Fixed by #1729
Assignees

Comments

@wangkuiyi
Copy link
Collaborator

We are considering reusing the TensorProto message defined in TensorFlow. This requires us to be able to build TensorFlow's .proto files into Go source code. This guide explains how to do this.

Install Protobuf Tools

To convert .proto files into .go and .py files, we need protoc, the protobuf compilers, and its Go code generator.

Compile Protobuf Messages

  1. We can check out the TensorFlow source tree to any local directory, say, /tmp/tensorflow, by running the following command:

    git clone https://github.com/tensorflow/tensorflow /tmp/tensorflow
  2. Make sure that we set the environment variable $GOPATH, say, ~/go.

  3. To compiler all .proto files, which reside in tensorflow/core/framework, please type the following command.

    for i in tensorflow/core/framework/*.proto; do 
        protoc --go_out=$GOPATH/src -I $PWD $i; 
    done

    The generated Go source files are in the directory $GOPATH/src/github.com/tensorflow/tensorflow/tensorflow/go/core/framework/, because of in all these .proto files, TensorFlow authors put lines like the following:

    option go_package = "github.com/tensorflow/tensorflow/tensorflow/go/core/framework/tensor_go_proto";

Use the Protobuf Messages in Go Code

Let us create an example project in $GOPATH/src/github.com/wangkuiyi/aaa or anywhere in the $GOPATH/src directory, and put in there the following Go source file a.go.

package main

import (
	"fmt"

	"github.com/tensorflow/tensorflow/tensorflow/go/core/framework/tensor_shape_go_proto"
)

func main() {
	var a tensor_shape_go_proto.TensorShapeProto
	fmt.Println(a.GetDim())
}

We can now install dependencies and build this program.

cd $GOPATH/src/github.com/wangkuiyi/aaa
go get ./...
go install
$GOPATH/bin/aaa
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants