-
Notifications
You must be signed in to change notification settings - Fork 110
/
tflite.go
334 lines (286 loc) · 9.38 KB
/
tflite.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
//go:build !arm && !windows && !no_tflite && (!no_cgo || android)
package inference
import (
"fmt"
"log"
"os"
"runtime"
"sync"
tflite "github.com/mattn/go-tflite"
"github.com/pkg/errors"
"gorgonia.org/tensor"
"go.viam.com/rdk/ml"
tfliteSchema "go.viam.com/rdk/ml/inference/tflite"
metadata "go.viam.com/rdk/ml/inference/tflite_metadata"
)
const tfLiteMetadataName string = "TFLITE_METADATA"
// TFLiteStruct holds information, model and interpreter of a tflite model in go.
type TFLiteStruct struct {
model *tflite.Model
interpreter Interpreter
interpreterOptions *tflite.InterpreterOptions
Info *TFLiteInfo
modelPath string
mu sync.Mutex
}
// Interpreter interface holds methods used by a tflite interpreter.
type Interpreter interface {
AllocateTensors() tflite.Status
Invoke() tflite.Status
GetOutputTensorCount() int
GetInputTensorCount() int
GetInputTensor(i int) *tflite.Tensor
GetOutputTensor(i int) *tflite.Tensor
Delete()
}
// TFLiteModelLoader holds functions that sets up a tflite model to be used.
type TFLiteModelLoader struct {
newModelFromFile func(path string) *tflite.Model
newInterpreter func(model *tflite.Model, options *tflite.InterpreterOptions) (Interpreter, error)
interpreterOptions *tflite.InterpreterOptions
getInfo func(inter Interpreter) *TFLiteInfo
}
// NewDefaultTFLiteModelLoader returns the default loader when using tflite.
func NewDefaultTFLiteModelLoader() (*TFLiteModelLoader, error) {
options, err := createTFLiteInterpreterOptions(runtime.NumCPU())
if err != nil {
return nil, err
}
loader := &TFLiteModelLoader{
newModelFromFile: tflite.NewModelFromFile,
newInterpreter: getInterpreter,
interpreterOptions: options,
getInfo: getInfo,
}
return loader, nil
}
// NewTFLiteModelLoader returns a loader that allows you to set threads when using tflite.
func NewTFLiteModelLoader(numThreads int) (*TFLiteModelLoader, error) {
if numThreads <= 0 {
return nil, errors.New("numThreads must be a positive integer")
}
options, err := createTFLiteInterpreterOptions(numThreads)
if err != nil {
return nil, err
}
loader := &TFLiteModelLoader{
newModelFromFile: tflite.NewModelFromFile,
newInterpreter: getInterpreter,
interpreterOptions: options,
getInfo: getInfo,
}
return loader, nil
}
// createTFLiteInterpreterOptions returns tflite interpreterOptions with settings.
func createTFLiteInterpreterOptions(numThreads int) (*tflite.InterpreterOptions, error) {
options := tflite.NewInterpreterOptions()
if options == nil {
return nil, FailedToLoadError("interpreter options")
}
options.SetNumThread(numThreads)
options.SetErrorReporter(func(msg string, userData interface{}) {
log.Println(msg)
}, nil)
return options, nil
}
// Load returns a TFLite struct that is ready to be used for inferences.
func (loader TFLiteModelLoader) Load(modelPath string) (*TFLiteStruct, error) {
tfLiteModel := loader.newModelFromFile(modelPath)
if tfLiteModel == nil {
return nil, FailedToLoadError("model")
}
interpreter, err := loader.newInterpreter(tfLiteModel, loader.interpreterOptions)
if err != nil {
return nil, err
}
status := interpreter.AllocateTensors()
if status != tflite.OK {
return nil, errors.New("failed to allocate tensors")
}
info := loader.getInfo(interpreter)
modelStruct := &TFLiteStruct{
model: tfLiteModel,
interpreter: interpreter,
interpreterOptions: loader.interpreterOptions,
Info: info,
modelPath: modelPath,
}
return modelStruct, nil
}
// InTensorType is a wrapper around a string that details the allowed input tensor types.
type InTensorType string
// UInt8 and Float32 are the currently supported input tensor types.
const (
UInt8 = InTensorType("UInt8")
Float32 = InTensorType("Float32")
)
// TFLiteInfo holds information about a model that are useful for creating input tensors bytes.
type TFLiteInfo struct {
InputHeight int
InputWidth int
InputChannels int
InputShape []int
InputTensorType InTensorType
InputTensorCount int
OutputTensorCount int
OutputTensorTypes []string
}
// getInfo provides some input and output tensor information based on a tflite interpreter.
func getInfo(inter Interpreter) *TFLiteInfo {
input := inter.GetInputTensor(0)
numOut := inter.GetOutputTensorCount()
var outTypes []string
for i := 0; i < numOut; i++ {
outTypes = append(outTypes, inter.GetOutputTensor(i).Type().String())
}
info := &TFLiteInfo{
InputHeight: input.Dim(1),
InputWidth: input.Dim(2),
InputChannels: input.Dim(3),
InputShape: input.Shape(),
InputTensorType: InTensorType(input.Type().String()),
InputTensorCount: inter.GetInputTensorCount(),
OutputTensorCount: numOut,
OutputTensorTypes: outTypes,
}
return info
}
// Infer takes an input map of tensors and returns an output map of tensors.
func (model *TFLiteStruct) Infer(inputTensors ml.Tensors) (ml.Tensors, error) {
model.mu.Lock()
defer model.mu.Unlock()
interpreter := model.interpreter
inputCount := interpreter.GetInputTensorCount()
if inputCount == 1 && len(inputTensors) == 1 { // convenience function for underspecified names
input := interpreter.GetInputTensor(0)
for _, inpTensor := range inputTensors { // there is only one element in this map
status := input.CopyFromBuffer(inpTensor.Data())
if status != tflite.OK {
return nil, errors.Errorf("copying from tensor buffer %q failed", input.Name())
}
}
} else {
for i := 0; i < inputCount; i++ {
input := interpreter.GetInputTensor(i)
inpTensor, ok := inputTensors[input.Name()]
if !ok {
return nil, errors.Errorf("tflite model expected a tensor named %q, but no such input tensor found", input.Name())
}
if inpTensor == nil {
continue
}
status := input.CopyFromBuffer(inpTensor.Data())
if status != tflite.OK {
return nil, errors.Errorf("copying from tensor buffer named %q failed", input.Name())
}
}
}
status := interpreter.Invoke()
if status != tflite.OK {
return nil, errors.New("tflite invoke failed")
}
output := ml.Tensors{}
for i := 0; i < interpreter.GetOutputTensorCount(); i++ {
t := interpreter.GetOutputTensor(i)
if t == nil {
continue
}
tType := TFliteTensorToGorgoniaTensor(t.Type())
outputTensor := tensor.New(
tensor.WithShape(t.Shape()...),
tensor.Of(tType),
tensor.FromMemory(uintptr(t.Data()), uintptr(t.ByteSize())),
)
outName := fmt.Sprintf("%s:%v", t.Name(), i)
output[outName] = outputTensor
}
return output, nil
}
// TFliteTensorToGorgoniaTensor converts the constants from one tensor library to another.
func TFliteTensorToGorgoniaTensor(t tflite.TensorType) tensor.Dtype {
switch t {
case tflite.NoType:
return tensor.Uintptr // just return is as a general pointer type...
case tflite.Float32:
return tensor.Float32
case tflite.Int32:
return tensor.Int32
case tflite.UInt8:
return tensor.Uint8
case tflite.Int64:
return tensor.Int64
case tflite.String:
return tensor.String
case tflite.Bool:
return tensor.Bool
case tflite.Int16:
return tensor.Int16
case tflite.Complex64:
return tensor.Complex64
case tflite.Int8:
return tensor.Int8
default: // shouldn't reach here unless tflite adds more types
return tensor.Uintptr
}
}
// Metadata provides the metadata information based on the model flatbuffer file.
func (model *TFLiteStruct) Metadata() (*metadata.ModelMetadataT, error) {
b, err := getTFLiteMetadataBytes(model.modelPath)
if err != nil {
return nil, err
}
if len(b) == 0 {
return nil, MetadataDoesNotExistError()
}
return getTFLiteMetadataAsStruct(b), nil
}
// getTFLiteMetadataBytes takes a model path of a tflite file and extracts the metadata buffer from the entire model.
func getTFLiteMetadataBytes(modelPath string) ([]byte, error) {
//nolint:gosec
buf, err := os.ReadFile(modelPath)
if err != nil {
return nil, err
}
model := tfliteSchema.GetRootAsModel(buf, 0)
metadataLen := model.MetadataLength()
if metadataLen == 0 {
return []byte{}, nil
}
for i := 0; i < metadataLen; i++ {
metadata := &tfliteSchema.Metadata{}
if success := model.Metadata(metadata, i); !success {
return nil, FailedToLoadError("metadata")
}
if tfLiteMetadataName == string(metadata.Name()) {
metadataBuffer := &tfliteSchema.Buffer{}
success := model.Buffers(metadataBuffer, int(metadata.Buffer()))
if !success {
return nil, FailedToLoadError("metadata buffer")
}
bufInBytes := metadataBuffer.DataBytes()
return bufInBytes, nil
}
}
return []byte{}, nil
}
// getTFLiteMetadataAsStruct takes the metadata buffer returns a readable struct based on the tflite flatbuffer schema.
func getTFLiteMetadataAsStruct(metaBytes []byte) *metadata.ModelMetadataT {
meta := metadata.GetRootAsModelMetadata(metaBytes, 0)
structMeta := meta.UnPack()
return structMeta
}
// Close should be called at the end of using the interpreter to delete related models and interpreters.
func (model *TFLiteStruct) Close() error {
model.model.Delete()
model.interpreterOptions.Delete()
model.interpreter.Delete()
return nil
}
// getInterpreter conforms a *tflite.Interpreter to the Interpreter interface.
func getInterpreter(model *tflite.Model, options *tflite.InterpreterOptions) (Interpreter, error) {
interpreter := tflite.NewInterpreter(model, options)
if interpreter == nil {
return nil, FailedToLoadError("interpreter")
}
return interpreter, nil
}