tflite

package module
v1.0.2 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: May 17, 2021 License: MIT Imports: 10 Imported by: 0

README

go-tflite

Go binding for TensorFlow Lite

Usage

model := tflite.NewModelFromFile("sin_model.tflite")
if model == nil {
	log.Fatal("cannot load model")
}
defer model.Delete()

options := tflite.NewInterpreterOptions()
defer options.Delete()

interpreter := tflite.NewInterpreter(model, options)
defer interpreter.Delete()

interpreter.AllocateTensors()

v := float64(1.2) * math.Pi / 180.0
input := interpreter.GetInputTensor(0)
input.Float32s()[0] = float32(v)
interpreter.Invoke()
got := float64(interpreter.GetOutputTensor(0).Float32s()[0])

See _example for more examples

Requirements

  • TensorFlow Lite - This release requires 2.2.0-rc3

Tensorflow Installation

You must install Tensorflow Lite C API. Assuming the source is under /source/directory/tensorflow

$ cd /source/directory/tensorflow
$ bazel build --config opt --config monolithic tensorflow:libtensorflow_c.so

Or to just compile the tensorflow lite libraries:

$ cd /some/path/tensorflow
$ bazel build --config opt --config monolithic //tensorflow/lite:libtensorflowlite.so
$ bazel build --config opt --config monolithic //tensorflow/lite/c:libtensorflowlite_c.so

In order for go to find the headers you must set the CGO_CFLAGS environment variable for the source and libraries of tensorflow. If your libraries are not installed in a standard location, you must also give the go linker the path to the shared librares with the CGO_LDFLAGS environment variable.

$ export CGO_CFLAGS=-I/source/directory/tensorflow
$ export CGO_LDFLAGS=-L/path/to/tensorflow/libaries

If you don't love bazel, you can try Makefile.tflite. Put this file as Makefile in tensorflow/lite/c, and run make. Sorry, this has not been test for Linux or Mac

Then run go build on some of the examples.

Edge TPU

To be able to compile and use the EdgeTPU delegate, you need to install the libraries from here: https://github.com/google-coral/edgetpu

There is also a deb package here: https://coral.withgoogle.com/docs/accelerator/get-started/#1-install-the-edge-tpu-runtime

The libraries from should be installed in a system wide library path like /usr/local/lib The include files should be installed somewhere that is accesable from your CGO include path

For x86:

cd /tmp && git clone https://github.com/google-coral/edgetpu.git && \
cp edgetpu/libedgetpu/direct/k8/libedgetpu.so.1.0 /usr/local/lib/libedgetpu.so.1.0 && \
ln -rs /usr/local/lib/libedgetpu.so.1.0 /usr/local/lib/libedgetpu.so.1 && \
ln -rs /usr/local/lib/libedgetpu.so.1.0 /usr/local/lib/libedgetpu.so && \
mkdir -p /usr/local/include/libedgetpu && \
cp edgetpu/libedgetpu/edgetpu.h /usr/local/include/edgetpu.h && \
cp edgetpu/libedgetpu/edgetpu_c.h /usr/local/include/edgetpu_c.h && \
rm -Rf edgetpu

License

MIT

Author

Yasuhrio Matsumoto (a.k.a. mattn)

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	// ErrTypeMismatch is type mismatch.
	ErrTypeMismatch = errors.New("type mismatch")
	// ErrBadTensor is bad tensor.
	ErrBadTensor = errors.New("bad tensor")
)

Functions

This section is empty.

Types

type BuiltinOperator

type BuiltinOperator int
const (
	BuiltinOperator_ADD                          BuiltinOperator = 0
	BuiltinOperator_AVERAGE_POOL_2D              BuiltinOperator = 1
	BuiltinOperator_CONCATENATION                BuiltinOperator = 2
	BuiltinOperator_CONV_2D                      BuiltinOperator = 3
	BuiltinOperator_DEPTHWISE_CONV_2D            BuiltinOperator = 4
	BuiltinOperator_DEQUANTIZE                   BuiltinOperator = 6
	BuiltinOperator_EMBEDDING_LOOKUP             BuiltinOperator = 7
	BuiltinOperator_FLOOR                        BuiltinOperator = 8
	BuiltinOperator_FULLY_CONNECTED              BuiltinOperator = 9
	BuiltinOperator_HASHTABLE_LOOKUP             BuiltinOperator = 10
	BuiltinOperator_L2_NORMALIZATION             BuiltinOperator = 11
	BuiltinOperator_L2_POOL_2D                   BuiltinOperator = 12
	BuiltinOperator_LOCAL_RESPONSE_NORMALIZATION BuiltinOperator = 13
	BuiltinOperator_LOGISTIC                     BuiltinOperator = 14
	BuiltinOperator_LSH_PROJECTION               BuiltinOperator = 15
	BuiltinOperator_LSTM                         BuiltinOperator = 16
	BuiltinOperator_MAX_POOL_2D                  BuiltinOperator = 17
	BuiltinOperator_MUL                          BuiltinOperator = 18
	BuiltinOperator_RELU                         BuiltinOperator = 19
	BuiltinOperator_RELU_N1_TO_1                 BuiltinOperator = 20
	BuiltinOperator_RELU6                        BuiltinOperator = 21
	BuiltinOperator_RESHAPE                      BuiltinOperator = 22
	BuiltinOperator_RESIZE_BILINEAR              BuiltinOperator = 23
	BuiltinOperator_RNN                          BuiltinOperator = 24
	BuiltinOperator_SOFTMAX                      BuiltinOperator = 25
	BuiltinOperator_SPACE_TO_DEPTH               BuiltinOperator = 26
	BuiltinOperator_SVDF                         BuiltinOperator = 27
	BuiltinOperator_TANH                         BuiltinOperator = 28
	BuiltinOperator_CONCAT_EMBEDDINGS            BuiltinOperator = 29
	BuiltinOperator_SKIP_GRAM                    BuiltinOperator = 30
	BuiltinOperator_CALL                         BuiltinOperator = 31
	BuiltinOperator_CUSTOM                       BuiltinOperator = 32
	BuiltinOperator_EMBEDDING_LOOKUP_SPARSE      BuiltinOperator = 33
	BuiltinOperator_PAD                          BuiltinOperator = 34
	BuiltinOperator_UNIDIRECTIONAL_SEQUENCE_RNN  BuiltinOperator = 35
	BuiltinOperator_GATHER                       BuiltinOperator = 36
	BuiltinOperator_BATCH_TO_SPACE_ND            BuiltinOperator = 37
	BuiltinOperator_SPACE_TO_BATCH_ND            BuiltinOperator = 38
	BuiltinOperator_TRANSPOSE                    BuiltinOperator = 39
	BuiltinOperator_MEAN                         BuiltinOperator = 40
	BuiltinOperator_SUB                          BuiltinOperator = 41
	BuiltinOperator_DIV                          BuiltinOperator = 42
	BuiltinOperator_SQUEEZE                      BuiltinOperator = 43
	BuiltinOperator_UNIDIRECTIONAL_SEQUENCE_LSTM BuiltinOperator = 44
	BuiltinOperator_STRIDED_SLICE                BuiltinOperator = 45
	BuiltinOperator_BIDIRECTIONAL_SEQUENCE_RNN   BuiltinOperator = 46
	BuiltinOperator_EXP                          BuiltinOperator = 47
	BuiltinOperator_TOPK_V2                      BuiltinOperator = 48
	BuiltinOperator_SPLIT                        BuiltinOperator = 49
	BuiltinOperator_LOG_SOFTMAX                  BuiltinOperator = 50
	BuiltinOperator_DELEGATE                     BuiltinOperator = 51
	BuiltinOperator_BIDIRECTIONAL_SEQUENCE_LSTM  BuiltinOperator = 52
	BuiltinOperator_CAST                         BuiltinOperator = 53
	BuiltinOperator_PRELU                        BuiltinOperator = 54
	BuiltinOperator_MAXIMUM                      BuiltinOperator = 55
	BuiltinOperator_ARG_MAX                      BuiltinOperator = 56
	BuiltinOperator_MINIMUM                      BuiltinOperator = 57
	BuiltinOperator_LESS                         BuiltinOperator = 58
	BuiltinOperator_NEG                          BuiltinOperator = 59
	BuiltinOperator_PADV2                        BuiltinOperator = 60
	BuiltinOperator_GREATER                      BuiltinOperator = 61
	BuiltinOperator_GREATER_EQUAL                BuiltinOperator = 62
	BuiltinOperator_LESS_EQUAL                   BuiltinOperator = 63
	BuiltinOperator_SELECT                       BuiltinOperator = 64
	BuiltinOperator_SLICE                        BuiltinOperator = 65
	BuiltinOperator_SIN                          BuiltinOperator = 66
	BuiltinOperator_TRANSPOSE_CONV               BuiltinOperator = 67
	BuiltinOperator_SPARSE_TO_DENSE              BuiltinOperator = 68
	BuiltinOperator_TILE                         BuiltinOperator = 69
	BuiltinOperator_EXPAND_DIMS                  BuiltinOperator = 70
	BuiltinOperator_EQUAL                        BuiltinOperator = 71
	BuiltinOperator_NOT_EQUAL                    BuiltinOperator = 72
	BuiltinOperator_LOG                          BuiltinOperator = 73
	BuiltinOperator_SUM                          BuiltinOperator = 74
	BuiltinOperator_SQRT                         BuiltinOperator = 75
	BuiltinOperator_RSQRT                        BuiltinOperator = 76
	BuiltinOperator_SHAPE                        BuiltinOperator = 77
	BuiltinOperator_POW                          BuiltinOperator = 78
	BuiltinOperator_ARG_MIN                      BuiltinOperator = 79
	BuiltinOperator_FAKE_QUANT                   BuiltinOperator = 80
	BuiltinOperator_REDUCE_PROD                  BuiltinOperator = 81
	BuiltinOperator_REDUCE_MAX                   BuiltinOperator = 82
	BuiltinOperator_PACK                         BuiltinOperator = 83
	BuiltinOperator_LOGICAL_OR                   BuiltinOperator = 84
	BuiltinOperator_ONE_HOT                      BuiltinOperator = 85
	BuiltinOperator_LOGICAL_AND                  BuiltinOperator = 86
	BuiltinOperator_LOGICAL_NOT                  BuiltinOperator = 87
	BuiltinOperator_UNPACK                       BuiltinOperator = 88
	BuiltinOperator_REDUCE_MIN                   BuiltinOperator = 89
	BuiltinOperator_FLOOR_DIV                    BuiltinOperator = 90
	BuiltinOperator_REDUCE_ANY                   BuiltinOperator = 91
	BuiltinOperator_SQUARE                       BuiltinOperator = 92
	BuiltinOperator_ZEROS_LIKE                   BuiltinOperator = 93
	BuiltinOperator_FILL                         BuiltinOperator = 94
	BuiltinOperator_FLOOR_MOD                    BuiltinOperator = 95
	BuiltinOperator_RANGE                        BuiltinOperator = 96
	BuiltinOperator_RESIZE_NEAREST_NEIGHBOR      BuiltinOperator = 97
	BuiltinOperator_LEAKY_RELU                   BuiltinOperator = 98
	BuiltinOperator_SQUARED_DIFFERENCE           BuiltinOperator = 99
	BuiltinOperator_MIRROR_PAD                   BuiltinOperator = 100
	BuiltinOperator_ABS                          BuiltinOperator = 101
	BuiltinOperator_SPLIT_V                      BuiltinOperator = 102
	BuiltinOperator_UNIQUE                       BuiltinOperator = 103
	BuiltinOperator_CEIL                         BuiltinOperator = 104
	BuiltinOperator_REVERSE_V2                   BuiltinOperator = 105
	BuiltinOperator_ADD_N                        BuiltinOperator = 106
	BuiltinOperator_GATHER_ND                    BuiltinOperator = 107
	BuiltinOperator_COS                          BuiltinOperator = 108
	BuiltinOperator_WHERE                        BuiltinOperator = 109
	BuiltinOperator_RANK                         BuiltinOperator = 110
	BuiltinOperator_ELU                          BuiltinOperator = 111
	BuiltinOperator_REVERSE_SEQUENCE             BuiltinOperator = 112
	BuiltinOperator_MATRIX_DIAG                  BuiltinOperator = 113
	BuiltinOperator_QUANTIZE                     BuiltinOperator = 114
	BuiltinOperator_MATRIX_SET_DIAG              BuiltinOperator = 115
	BuiltinOperator_MIN                          BuiltinOperator = BuiltinOperator_ADD
	BuiltinOperator_MAX                          BuiltinOperator = BuiltinOperator_MATRIX_SET_DIAG
)

type DynamicBuffer

type DynamicBuffer struct {
	// contains filtered or unexported fields
}

DynamicBuffer is buffer hold multiple strings.

func (*DynamicBuffer) AddString

func (d *DynamicBuffer) AddString(s string)

AddString append to the dynamic buffer.

func (*DynamicBuffer) WriteToTensorAsVector

func (d *DynamicBuffer) WriteToTensorAsVector(t *Tensor)

WriteToTensorAsVector write buffer into the tensor as vector.

type ExpRegistration

type ExpRegistration struct {
	Init            unsafe.Pointer
	Free            unsafe.Pointer
	Prepare         unsafe.Pointer
	Invoke          unsafe.Pointer
	ProfilingString unsafe.Pointer
}

ExtRegistration indicate registration structure.

type Interpreter

type Interpreter struct {
	// contains filtered or unexported fields
}

Interpreter implement TfLiteInterpreter.

func NewInterpreter

func NewInterpreter(model *Model, options *InterpreterOptions) *Interpreter

NewInterpreter create new Interpreter.

func (*Interpreter) AllocateTensors

func (i *Interpreter) AllocateTensors() Status

AllocateTensor allocate tensors for the interpreter.

func (*Interpreter) Delete

func (i *Interpreter) Delete()

Delete delete instance of Interpreter.

func (*Interpreter) GetInputTensor

func (i *Interpreter) GetInputTensor(index int) *Tensor

GetInputTensor return input tensor specified by index.

func (*Interpreter) GetInputTensorCount

func (i *Interpreter) GetInputTensorCount() int

GetInputTensorCount return number of input tensors.

func (*Interpreter) GetOutputTensor

func (i *Interpreter) GetOutputTensor(index int) *Tensor

GetOutputTensor return output tensor specified by index.

func (*Interpreter) GetOutputTensorCount

func (i *Interpreter) GetOutputTensorCount() int

GetOutputTensorCount return number of output tensors.

func (*Interpreter) Invoke

func (i *Interpreter) Invoke() Status

Invoke invoke the task.

func (*Interpreter) ResetVariableTensors

func (i *Interpreter) ResetVariableTensors() Status

ResetVariableTensors resets variable tensors.

func (*Interpreter) ResizeInputTensor

func (i *Interpreter) ResizeInputTensor(index int, dims []int32) Status

ResizeInputTensor resize the tensor specified by index with dims.

type InterpreterOptions

type InterpreterOptions struct {
	// contains filtered or unexported fields
}

InterpreterOptions implement TfLiteInterpreterOptions.

func NewInterpreterOptions

func NewInterpreterOptions() *InterpreterOptions

NewInterpreterOptions create new InterpreterOptions.

func (*InterpreterOptions) AddDelegate

func (o *InterpreterOptions) AddDelegate(d delegates.Delegater)

func (*InterpreterOptions) Delete

func (o *InterpreterOptions) Delete()

Delete delete instance of InterpreterOptions.

func (*InterpreterOptions) ExpAddBuiltinOp

func (o *InterpreterOptions) ExpAddBuiltinOp(op BuiltinOperator, reg *ExpRegistration, minVersion, maxVersion int)

ExpAddBuiltinOp add builtin op specified by code and registration. Current implementation is work in progress.

func (*InterpreterOptions) ExpAddCustomOp

func (o *InterpreterOptions) ExpAddCustomOp(name string, reg *ExpRegistration, minVersion, maxVersion int)

ExpAddCustomOp add custom op specified by name and registration. Current implementation is work in progress.

func (*InterpreterOptions) SetErrorReporter

func (o *InterpreterOptions) SetErrorReporter(f func(string, interface{}), user_data interface{})

SetErrorRepoter set a function of reporter.

func (*InterpreterOptions) SetNumThread

func (o *InterpreterOptions) SetNumThread(num_threads int)

SetNumThread set number of threads.

func (*InterpreterOptions) SetUseNNAPI

func (o *InterpreterOptions) SetUseNNAPI(enable bool)

SetUseNNAPI enable or disable the NN API for the interpreter (true to enable).

type Model

type Model struct {
	// contains filtered or unexported fields
}

Model is TfLiteModel.

func NewModel

func NewModel(model_data []byte) *Model

NewModel create new Model from buffer.

func NewModelFromFile

func NewModelFromFile(model_path string) *Model

NewModelFromFile create new Model from file data.

func (*Model) Delete

func (m *Model) Delete()

Delete delete instance of model.

type QuantizationParams

type QuantizationParams struct {
	Scale     float64
	ZeroPoint int
}

QuantizationParams implement TfLiteQuantizationParams.

type Status

type Status int

State implement TfLiteStatus.

const (
	OK Status = 0
	Error
)

func (Status) String

func (i Status) String() string

type Tensor

type Tensor struct {
	// contains filtered or unexported fields
}

Tensor implement TfLiteTensor.

func (*Tensor) ByteSize

func (t *Tensor) ByteSize() uint

ByteSize return byte size of the tensor.

func (*Tensor) CopyFromBuffer

func (t *Tensor) CopyFromBuffer(b interface{}) Status

CopyFromBuffer write buffer to the tensor.

func (*Tensor) CopyToBuffer

func (t *Tensor) CopyToBuffer(b interface{}) Status

CopyToBuffer write buffer from the tensor.

func (*Tensor) Data

func (t *Tensor) Data() unsafe.Pointer

Data return pointer of buffer.

func (*Tensor) Dim

func (t *Tensor) Dim(index int) int

Dim return dimension of the element specified by index.

func (*Tensor) Float32At

func (t *Tensor) Float32At(at ...int) float32

Float32At returns float32 value located in the dimension.

func (*Tensor) Float32s

func (t *Tensor) Float32s() []float32

Float32s returns float32s.

func (*Tensor) GetString

func (t *Tensor) GetString(index int) string

GetString returns string in the string buffer.

func (*Tensor) Int16s

func (t *Tensor) Int16s() []int16

Int16s returns int16s.

func (*Tensor) Int32s

func (t *Tensor) Int32s() []int32

Int32s returns int32s.

func (*Tensor) Int64s

func (t *Tensor) Int64s() []int64

Int64s returns int64s.

func (*Tensor) Int8s

func (t *Tensor) Int8s() []int8

Int8s returns int8s.

func (*Tensor) Name

func (t *Tensor) Name() string

Name return name of the tensor.

func (*Tensor) NumDims

func (t *Tensor) NumDims() int

NumDims return number of dimensions.

func (*Tensor) QuantizationParams

func (t *Tensor) QuantizationParams() QuantizationParams

QuantizationParams return quantization parameters of the tensor.

func (*Tensor) SetFloat32s

func (t *Tensor) SetFloat32s(v []float32) error

SetFloat32s sets float32s.

func (*Tensor) SetInt16s

func (t *Tensor) SetInt16s(v []int16) error

SetInt16s sets int16s.

func (*Tensor) SetInt32s

func (t *Tensor) SetInt32s(v []int32) error

SetInt32s sets int32s.

func (*Tensor) SetInt64s

func (t *Tensor) SetInt64s(v []int64) error

SetInt64s sets int64s.

func (*Tensor) SetInt8s

func (t *Tensor) SetInt8s(v []int8) error

SetInt8s sets int8s.

func (*Tensor) SetUint8s

func (t *Tensor) SetUint8s(v []uint8) error

SetUint8s sets uint8s.

func (*Tensor) Shape

func (t *Tensor) Shape() []int

Shape return shape of the tensor.

func (*Tensor) String

func (t *Tensor) String() string

String returns name of tensor.

func (*Tensor) Type

func (t *Tensor) Type() TensorType

Type return TensorType.

func (*Tensor) UInt8s

func (t *Tensor) UInt8s() []uint8

UInt8s returns uint8s.

type TensorType

type TensorType int

TensorType is types of the tensor.

const (
	NoType    TensorType = 0
	Float32   TensorType = 1
	Int32     TensorType = 2
	UInt8     TensorType = 3
	Int64     TensorType = 4
	String    TensorType = 5
	Bool      TensorType = 6
	Int16     TensorType = 7
	Complex64 TensorType = 8
	Int8      TensorType = 9
)

func (TensorType) String

func (i TensorType) String() string

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL