Skip to content

Commit 90340a7

Browse files
committed
Merge branch 'develop' into main
2 parents d182686 + bee23a4 commit 90340a7

7 files changed

Lines changed: 271 additions & 16 deletions

File tree

interpreter.go

Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
1+
package tflitego
2+
3+
/*
4+
#include <stdio.h>
5+
#include <tensorflow/lite/c/c_api.h>
6+
#cgo LDFLAGS: -ltensorflowlite_c
7+
#cgo linux LDFLAGS: -lm -ldl -lrt
8+
*/
9+
import "C"
10+
import (
11+
"fmt"
12+
)
13+
14+
// TfLiteStatus represents TFLiteStatus
15+
type TfLiteStatus int
16+
17+
// options to represent TFLiteStatus
18+
const (
19+
TfLiteOk TfLiteStatus = iota
20+
TfLiteError
21+
TfLiteDelegateError
22+
TfLiteApplicationError
23+
)
24+
25+
// TfLiteInterpreter represents a TensorFlow Lite Interpreter .
26+
type TfLiteInterpreter struct {
27+
interpreter *C.TfLiteInterpreter
28+
}
29+
30+
// NewInterpreter create new TfLiteInterpreter.
31+
func NewInterpreter(tfmodel *TFLiteModel, opts *InterpreterOptions) (*TfLiteInterpreter, error) {
32+
var o *C.TfLiteInterpreterOptions
33+
if opts != nil {
34+
o = opts.options
35+
}
36+
i := C.TfLiteInterpreterCreate(tfmodel.model, o)
37+
if i == nil {
38+
return nil, fmt.Errorf("unable to create new TFLiteInterpreter")
39+
}
40+
return &TfLiteInterpreter{interpreter: i}, nil
41+
}
42+
43+
// Delete represents the delete instance of Interpreter.
44+
func (TfLiteInterpreter *TfLiteInterpreter) Delete() {
45+
if TfLiteInterpreter != nil {
46+
C.TfLiteInterpreterDelete(TfLiteInterpreter.interpreter)
47+
}
48+
49+
}
50+
51+
// GetInputTensor return tfLiteTensor using index.
52+
func (TfLiteInterpreter *TfLiteInterpreter) GetInputTensor(index int) (*TfLiteTensor, error) {
53+
t := C.TfLiteInterpreterGetInputTensor(TfLiteInterpreter.interpreter, C.int32_t(index))
54+
if t == nil {
55+
return nil, fmt.Errorf("unable to retrieve Input Tensor")
56+
}
57+
return &TfLiteTensor{tensor: t}, nil
58+
}
59+
60+
// AllocateTensors allocate tensors for the interpreter.
61+
func (TfLiteInterpreter *TfLiteInterpreter) AllocateTensors() TfLiteStatus {
62+
if TfLiteInterpreter != nil {
63+
s := C.TfLiteInterpreterAllocateTensors(TfLiteInterpreter.interpreter)
64+
return TfLiteStatus(s)
65+
}
66+
return TfLiteError
67+
}
68+
69+
// Invoke invoke interpreter
70+
func (TfLiteInterpreter *TfLiteInterpreter) Invoke() TfLiteStatus {
71+
s := C.TfLiteInterpreterInvoke(TfLiteInterpreter.interpreter)
72+
return TfLiteStatus(s)
73+
}
74+
75+
// GetOutputTensor return output TfLiteTensor specified by index.
76+
func (TfLiteInterpreter *TfLiteInterpreter) GetOutputTensor(index int) *TfLiteTensor {
77+
t := C.TfLiteInterpreterGetOutputTensor(TfLiteInterpreter.interpreter, C.int32_t(index))
78+
if t == nil {
79+
return nil
80+
}
81+
return &TfLiteTensor{tensor: t}
82+
}

model.go

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
package tflitego
2+
3+
/*
4+
#include <stdio.h>
5+
#include <tensorflow/lite/c/c_api.h>
6+
#cgo LDFLAGS: -ltensorflowlite_c
7+
#cgo linux LDFLAGS: -lm -ldl -lrt
8+
*/
9+
import "C"
10+
import (
11+
"fmt"
12+
)
13+
14+
// TFLiteModel represents a TensorFlow Lite Model.
15+
type TFLiteModel struct {
16+
model *C.TfLiteModel
17+
}
18+
19+
// NewTFLiteModelFromFile creates a new TensorFlow Lite Model from File.
20+
func NewTFLiteModelFromFile(modelPath string) (*TFLiteModel, error) {
21+
m := C.TfLiteModelCreateFromFile(C.CString(modelPath))
22+
if m == nil {
23+
return nil, fmt.Errorf("unable to create the model")
24+
}
25+
return &TFLiteModel{model: m}, nil
26+
}
27+
28+
// Delete delete instance of TF Lite model.
29+
func (TFLiteModel *TFLiteModel) Delete() {
30+
if TFLiteModel != nil {
31+
C.TfLiteModelDelete(TFLiteModel.model)
32+
}
33+
}

options.go

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
package tflitego
2+
3+
/*
4+
#include <stdio.h>
5+
#include <tensorflow/lite/c/c_api.h>
6+
#cgo LDFLAGS: -ltensorflowlite_c
7+
#cgo linux LDFLAGS: -lm -ldl -lrt
8+
*/
9+
import "C"
10+
import (
11+
"fmt"
12+
)
13+
14+
// InterpreterOptions represents a TensorFlow Lite InterpreterOptions.
15+
type InterpreterOptions struct {
16+
options *C.TfLiteInterpreterOptions
17+
}
18+
19+
// NewInterpreterOptions creates new InterpreterOptions.
20+
func NewInterpreterOptions() (*InterpreterOptions, error) {
21+
o := C.TfLiteInterpreterOptionsCreate()
22+
if o == nil {
23+
return nil, fmt.Errorf("unable to create an InterpreterOptions")
24+
}
25+
return &InterpreterOptions{options: o}, nil
26+
}
27+
28+
// Delete delete instance of InterpreterOptions.
29+
func (InterpreterOptions *InterpreterOptions) Delete() {
30+
if InterpreterOptions != nil {
31+
C.TfLiteInterpreterOptionsDelete(InterpreterOptions.options)
32+
}
33+
}
34+
35+
// SetNumThread set number of threads.
36+
func (InterpreterOptions *InterpreterOptions) SetNumThread(numThreads int) {
37+
C.TfLiteInterpreterOptionsSetNumThreads(InterpreterOptions.options, C.int32_t(numThreads))
38+
}

tensor.go

Lines changed: 96 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,96 @@
1+
package tflitego
2+
3+
/*
4+
#include <stdio.h>
5+
#include <tensorflow/lite/c/c_api.h>
6+
#cgo LDFLAGS: -ltensorflowlite_c
7+
#cgo linux LDFLAGS: -lm -ldl -lrt
8+
*/
9+
import "C"
10+
import (
11+
"fmt"
12+
)
13+
14+
// TensorType is types of the tensor.
15+
type TensorType int
16+
17+
// Tensors type
18+
const (
19+
TfLiteNoType TensorType = iota
20+
TfLiteFloat32
21+
TfLiteInt32
22+
TfLiteUInt8
23+
TfLiteInt64
24+
TfLiteString
25+
TfLiteBool
26+
TfLiteInt16
27+
TfLiteComplex64
28+
TfLiteInt8
29+
)
30+
31+
// TfLiteTensor represents TensorFlow Lite Tensor.
32+
type TfLiteTensor struct {
33+
tensor *C.TfLiteTensor
34+
}
35+
36+
// Type return TensorType.
37+
func (TfLiteTensor *TfLiteTensor) Type() TensorType {
38+
return TensorType(C.TfLiteTensorType(TfLiteTensor.tensor))
39+
}
40+
41+
// NumDims return number of dimensions.
42+
func (TfLiteTensor *TfLiteTensor) NumDims() int {
43+
return int(C.TfLiteTensorNumDims(TfLiteTensor.tensor))
44+
}
45+
46+
// Dim return dimension of the element specified by index.
47+
func (TfLiteTensor *TfLiteTensor) Dim(index int) int {
48+
return int(C.TfLiteTensorDim(TfLiteTensor.tensor, C.int32_t(index)))
49+
}
50+
51+
// Shape return shape of the tensor.
52+
func (TfLiteTensor *TfLiteTensor) Shape() []int {
53+
shape := make([]int, TfLiteTensor.NumDims())
54+
for i := 0; i < TfLiteTensor.NumDims(); i++ {
55+
shape[i] = TfLiteTensor.Dim(i)
56+
}
57+
return shape
58+
}
59+
60+
// ByteSize return byte size of the tensor.
61+
func (TfLiteTensor *TfLiteTensor) ByteSize() uint {
62+
return uint(C.TfLiteTensorByteSize(TfLiteTensor.tensor))
63+
}
64+
65+
// Name return name of the tensor.
66+
func (TfLiteTensor *TfLiteTensor) Name() string {
67+
return C.GoString(C.TfLiteTensorName(TfLiteTensor.tensor))
68+
}
69+
70+
// SetFloat32 sets float32s.
71+
func (TfLiteTensor *TfLiteTensor) SetFloat32(v []float32) error {
72+
if TfLiteTensor.Type() != TfLiteFloat32 {
73+
return fmt.Errorf("type error")
74+
}
75+
ptr := C.TfLiteTensorData(TfLiteTensor.tensor)
76+
if ptr == nil {
77+
return fmt.Errorf("bad tensor")
78+
}
79+
n := TfLiteTensor.ByteSize() / 4
80+
to := (*((*[1<<29 - 1]float32)(ptr)))[:n]
81+
copy(to, v)
82+
return nil
83+
}
84+
85+
// OperateFloat32 returns float32.
86+
func (TfLiteTensor *TfLiteTensor) OperateFloat32() []float32 {
87+
if TfLiteTensor.Type() != TfLiteFloat32 {
88+
return nil
89+
}
90+
ptr := C.TfLiteTensorData(TfLiteTensor.tensor)
91+
if ptr == nil {
92+
return nil
93+
}
94+
n := TfLiteTensor.ByteSize() / 4
95+
return (*((*[1<<29 - 1]float32)(ptr)))[:n]
96+
}

tflite.go

Lines changed: 0 additions & 15 deletions
This file was deleted.

utils.go

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
package tflitego
2+
3+
/*
4+
#include <stdio.h>
5+
#include <tensorflow/lite/c/c_api.h>
6+
#cgo LDFLAGS: -ltensorflowlite_c
7+
#cgo linux LDFLAGS: -lm -ldl -lrt
8+
*/
9+
import "C"
10+
import (
11+
"fmt"
12+
)
13+
14+
// TFVersion return TensorFlow Lite version
15+
func TFVersion() (string, error) {
16+
x := C.TfLiteVersion()
17+
if x == nil {
18+
return "", fmt.Errorf("unable to retrieve TensorFlow Lite version")
19+
}
20+
return C.GoString(x), nil
21+
}

tflite_test.go renamed to utils_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
package gotflite
1+
package tflitego
22

33
import "testing"
44

0 commit comments

Comments
 (0)