https://github.com/nbortolotti/tflitego
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.
https://github.com/nbortolotti/tflitego
edge-computing go golang interpreter tensorflow tensorflow-lite tflite
Last synced: about 1 year ago
JSON representation
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.
- Host: GitHub
- URL: https://github.com/nbortolotti/tflitego
- Owner: nbortolotti
- License: apache-2.0
- Created: 2020-12-06T19:08:08.000Z (over 5 years ago)
- Default Branch: main
- Last Pushed: 2021-05-24T12:28:34.000Z (about 5 years ago)
- Last Synced: 2025-03-31T01:51:15.064Z (about 1 year ago)
- Topics: edge-computing, go, golang, interpreter, tensorflow, tensorflow-lite, tflite
- Language: Go
- Homepage: https://tflitego.nicolasbortolotti.com/
- Size: 3.13 MB
- Stars: 17
- Watchers: 4
- Forks: 3
- Open Issues: 1
-
Metadata Files:
- Readme: README.md
- License: LICENSE.md
- Code of conduct: CODE_OF_CONDUCT.md
Awesome Lists containing this project
README

[](https://coveralls.io/github/nbortolotti/tflitego?branch=main)
[](https://godoc.org/github.com/nbortolotti/tflitego)
[](https://gitter.im/tflitego/community?utm_source=badge&utm_medium=badge&utm_campaign=pr-badge)

# 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:
```shell script
bazel build -c opt //tensorflow/lite/c:tensorflowlite_c
```
more details [here](https://github.com/tensorflow/tensorflow/tree/master/tensorflow/lite/c)
Alternativally, if your prefer a simplification to use TensorFlow Lite C API, I prepared a a package here:
* [linux/X86_64, 2.4.0](https://storage.googleapis.com/clitelibrary/ctflitelib_2.4.0.tar.gz). Tested ubuntu 18.04
```shell script
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:
```shell script
wget https://storage.googleapis.com/clitelibrary/ctflitelib_2.4.0.tar.gz
```
* [raspberrypi_linux/ARMv7, 2.4.0](https://storage.googleapis.com/clitelibrary/ctflitelib_2.4.0_ARMv7.tar.gz)
```shell script
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
```shell script
go get github.com/nbortolotti/tflitego
```
## How to use
1. Create the model, here using the method from file.
```go
model, err := tflite.NewTFLiteModelFromFile("iris_lite.tflite")
defer model.Delete()
if err != nil {
log.Fatal("cannot load model", err)
}
```
2. Set Interpreter options
```go
options, err := tflite.NewInterpreterOptions()
defer options.Delete()
if err != nil {
log.Fatal("cannot initialize interpreter options", err)
}
options.SetNumThread(4)
```
3. Create Interpreter
```go
interpreter, err := tflite.NewInterpreter(model, options)
defer interpreter.Delete()
if err != nil {
log.Fatal("cannot create interpreter", err)
}
```
4. Allocate Tensors
```go
status := interpreter.AllocateTensors()
if status != tflite.TfLiteOk {
log.Println("allocate Tensors failed")
}
```
5. Input Tensor/s
```go
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)
```
6. Interpreter Invoke
```go
status = interpreter.Invoke()
if status != tflite.StatusOk {
log.Println("invoke interpreter failed")
}
```
7. Outputs/Results
```go
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:
* [tflite examples](https://github.com/nbortolotti/tflitego_examples)