Skip to content

Lightweight tool that generates Go structures and Tensorflow inference execution code from SavedModel

License

Notifications You must be signed in to change notification settings

Unity-Technologies/go-tensorgen

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

1 Commit
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Go Report Card

Tensorgen

Lightweight tool that generates Go structures and Tensorflow inference execution code from SavedModel. The tool is designed to save the developer from the headache of tensors construction when working with Tensorflow from Go.

Usage

There are two steps that you need to perform to use this tool:

  • Generate Go code from SavedModel.
  • Perform model execution using generated code.

Code Generating

The simplest way is to use Docker image that has libtensorflow dependencies inside. But if you have libtensorflow locally — sky is the limit.

With Docker

TBD

Without Docker

To run Go code libtensorflow must be available as dynamic library. It means that .so and .h files, compatible with used version of tensorflow/go package, must be provided. More info: Install TensorFlow for C

Install tool with go get

go get -u github.com/Unity-Technologies/go-tensorgen

Exec go-tensorgen with defined SavedModel files directory and output dir, where generated code will be placed.

go-tensorgen -model-dir=./savedmodel -output-dir=./mymodel

Inference execution

For each available signature separate "Runner" will be generated. You can find example, how to use it, below.

import (
	"github.com/user/project/mymodel"
	tf "github.com/tensorflow/tensorflow/tensorflow/go"
)

dir := "./savedmodel"
metaGraph := "serve"
model, err := tf.LoadSavedModel(dir, []string{metaGraph}, &tf.SessionOptions{})
if err != nil { ... }

runner := mymodel.NewSignatureRunner(model.Graph, model.Session)

err = runner.LoadOperations()
if err != nil { ... }

req := mymodel.SignatureRequest{
	Feature: []float32{0.5},
}

resp, err := runner.Run(ctx, req)
if err != nil { ... }

// resp - is ready to use go struct

TODO:

  • Prepare Docker image that includes libtensorflow.
  • Explain how to use binary distributions of libtensorflow for local development

Contributing

Pull requests are very much welcomed. Create your pull request, make sure a test or example is included that covers your change and your commits represent coherent changes that include a reason for the change.

Use golangci-lint to check code with linters:

golangci-lint run ./...

About

Lightweight tool that generates Go structures and Tensorflow inference execution code from SavedModel

Topics

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages