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.
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:
- linux/X86_64, 2.4.0. Tested ubuntu 18.04
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
go get github.com/nbortolotti/tflitego
- 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)
}
- Set Interpreter options
options, err := tflite.NewInterpreterOptions()
defer options.Delete()
if err != nil {
log.Fatal("cannot initialize interpreter options", err)
}
options.SetNumThread(4)
- Create Interpreter
interpreter, err := tflite.NewInterpreter(model, options)
defer interpreter.Delete()
if err != nil {
log.Fatal("cannot create interpreter", err)
}
- Allocate Tensors
status := interpreter.AllocateTensors()
if status != tflite.TfLiteOk {
log.Println("allocate Tensors failed")
}
- 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)
- Interpreter Invoke
status = interpreter.Invoke()
if status != tflite.StatusOk {
log.Println("invoke interpreter failed")
}
- 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: