Skip to content

tflitego provide a simple and clear solution to use TensorFlow lite in Go. Our objective is provide a cohesive API, simplicity related to TensorFlow Lite C API connection and maintainability.

License

Notifications You must be signed in to change notification settings

nbortolotti/tflitego

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Go Coverage Status GoDoc Gitter Subreddit subscribers

tflitego: TensorFlow Lite for Go

tflitego provide a simple and clear solution to use TensorFlow lite in Go. Our objective is provide a cohesive API, simplicity related to TensorFlow Lite C API connection and maintainability.

Requeriments

TensorFlow Lite C API. A native shared library target that contains the C API for inference has been provided. Assuming a working bazel configuration, this can be built as follows:

bazel build -c opt //tensorflow/lite/c:tensorflowlite_c

more details here

Alternativally, if your prefer a simplification to use TensorFlow Lite C API, I prepared a a package here:

wget https://storage.googleapis.com/clitelibrary/ctflitelib_[version].tar.gz
sudo tar -C /usr/local -xzf ctflitelib_[version].tar.gz
sudo ldconfig

Replaces version for the available package. Example:

wget https://storage.googleapis.com/clitelibrary/ctflitelib_2.4.0.tar.gz
wget https://storage.googleapis.com/clitelibrary/ctflitelib_2.4.0_ARMv7.tar.gz
sudo tar -C /usr/local -xzf ctflitelib_2.4.0_ARMv7.tar.gz

Installation

go get github.com/nbortolotti/tflitego

How to use

  1. Create the model, here using the method from file.
model, err := tflite.NewTFLiteModelFromFile("iris_lite.tflite")
defer model.Delete()
if err != nil {
    log.Fatal("cannot load model", err)
}
  1. Set Interpreter options
options, err := tflite.NewInterpreterOptions()
defer options.Delete()
if err != nil {
    log.Fatal("cannot initialize interpreter options", err)
}
options.SetNumThread(4)
  1. Create Interpreter
interpreter, err := tflite.NewInterpreter(model, options)
defer interpreter.Delete()
if err != nil {
    log.Fatal("cannot create interpreter", err)
}
  1. Allocate Tensors
status := interpreter.AllocateTensors()
if status != tflite.TfLiteOk {
    log.Println("allocate Tensors failed")
}
  1. Input Tensor/s
newspecie := []float32{7.9, 3.8, 6.4, 2.0}
input, err := interpreter.GetInputTensor(0)
if err != nil {
	log.Println("cannot get input tensor", err)
}
input.SetFloat32(newspecie)
  1. Interpreter Invoke
status = interpreter.Invoke()
if status != tflite.StatusOk {
    log.Println("invoke interpreter failed")
}
  1. Outputs/Results
output, err := interpreter.GetOutputTensor(0)
if err != nil {
	log.Println("cannot get output tensor", err)
}
out := output.OperateFloat32()
fmt.Println(topSpecie(out))

Also here is possible view examples about tflitego in action:

About

tflitego provide a simple and clear solution to use TensorFlow lite in Go. Our objective is provide a cohesive API, simplicity related to TensorFlow Lite C API connection and maintainability.

Topics

Resources

License

Code of conduct

Stars

Watchers

Forks

Packages

No packages published

Languages