|
| 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 | +} |
0 commit comments