-
-
Notifications
You must be signed in to change notification settings - Fork 44
/
tflite_type.go
198 lines (182 loc) · 3.63 KB
/
tflite_type.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
package tflite
/*
#ifndef GO_TFLITE_H
#include "tflite.go.h"
#endif
*/
import "C"
import "errors"
var (
// ErrTypeMismatch is type mismatch.
ErrTypeMismatch = errors.New("type mismatch")
// ErrBadTensor is bad tensor.
ErrBadTensor = errors.New("bad tensor")
)
// SetInt32s sets int32s.
func (t *Tensor) SetInt32s(v []int32) error {
if t.Type() != Int32 {
return ErrTypeMismatch
}
ptr := C.TfLiteTensorData(t.t)
if ptr == nil {
return ErrBadTensor
}
n := t.ByteSize() / 4
to := (*((*[1<<29 - 1]int32)(ptr)))[:n]
copy(to, v)
return nil
}
// Int32s returns int32s.
func (t *Tensor) Int32s() []int32 {
if t.Type() != Int32 {
return nil
}
ptr := C.TfLiteTensorData(t.t)
if ptr == nil {
return nil
}
n := t.ByteSize() / 4
return (*((*[1<<29 - 1]int32)(ptr)))[:n]
}
// SetFloat32s sets float32s.
func (t *Tensor) SetFloat32s(v []float32) error {
if t.Type() != Float32 {
return ErrTypeMismatch
}
ptr := C.TfLiteTensorData(t.t)
if ptr == nil {
return ErrBadTensor
}
n := t.ByteSize() / 4
to := (*((*[1<<29 - 1]float32)(ptr)))[:n]
copy(to, v)
return nil
}
// Float32s returns float32s.
func (t *Tensor) Float32s() []float32 {
if t.Type() != Float32 {
return nil
}
ptr := C.TfLiteTensorData(t.t)
if ptr == nil {
return nil
}
n := t.ByteSize() / 4
return (*((*[1<<29 - 1]float32)(ptr)))[:n]
}
// Float32At returns float32 value located in the dimension.
func (t *Tensor) Float32At(at ...int) float32 {
pos := 0
for i := 0; i < t.NumDims(); i++ {
pos = pos*t.Dim(i) + at[i]
}
return t.Float32s()[pos]
}
// SetUint8s sets uint8s.
func (t *Tensor) SetUint8s(v []uint8) error {
if t.Type() != UInt8 {
return ErrTypeMismatch
}
ptr := C.TfLiteTensorData(t.t)
if ptr == nil {
return ErrBadTensor
}
n := t.ByteSize()
to := (*((*[1<<29 - 1]uint8)(ptr)))[:n]
copy(to, v)
return nil
}
// UInt8s returns uint8s.
func (t *Tensor) UInt8s() []uint8 {
if t.Type() != UInt8 {
return nil
}
ptr := C.TfLiteTensorData(t.t)
if ptr == nil {
return nil
}
n := t.ByteSize()
return (*((*[1<<29 - 1]uint8)(ptr)))[:n]
}
// SetInt64s sets int64s.
func (t *Tensor) SetInt64s(v []int64) error {
if t.Type() != Int64 {
return ErrTypeMismatch
}
ptr := C.TfLiteTensorData(t.t)
if ptr == nil {
return ErrBadTensor
}
n := t.ByteSize() / 8
to := (*((*[1<<28 - 1]int64)(ptr)))[:n]
copy(to, v)
return nil
}
// Int64s returns int64s.
func (t *Tensor) Int64s() []int64 {
if t.Type() != Int64 {
return nil
}
ptr := C.TfLiteTensorData(t.t)
if ptr == nil {
return nil
}
n := t.ByteSize() / 8
return (*((*[1<<28 - 1]int64)(ptr)))[:n]
}
// SetInt16s sets int16s.
func (t *Tensor) SetInt16s(v []int16) error {
if t.Type() != Int16 {
return ErrTypeMismatch
}
ptr := C.TfLiteTensorData(t.t)
if ptr == nil {
return ErrBadTensor
}
n := t.ByteSize() / 2
to := (*((*[1<<29 - 1]int16)(ptr)))[:n]
copy(to, v)
return nil
}
// Int16s returns int16s.
func (t *Tensor) Int16s() []int16 {
if t.Type() != Int16 {
return nil
}
ptr := C.TfLiteTensorData(t.t)
if ptr == nil {
return nil
}
n := t.ByteSize() / 2
return (*((*[1<<29 - 1]int16)(ptr)))[:n]
}
// SetInt8s sets int8s.
func (t *Tensor) SetInt8s(v []int8) error {
if t.Type() != Int8 {
return ErrTypeMismatch
}
ptr := C.TfLiteTensorData(t.t)
if ptr == nil {
return ErrBadTensor
}
n := t.ByteSize()
to := (*((*[1<<29 - 1]int8)(ptr)))[:n]
copy(to, v)
return nil
}
// Int8s returns int8s.
func (t *Tensor) Int8s() []int8 {
if t.Type() != Int8 {
return nil
}
ptr := C.TfLiteTensorData(t.t)
if ptr == nil {
return nil
}
n := t.ByteSize()
return (*((*[1<<29 - 1]int8)(ptr)))[:n]
}
// String returns name of tensor.
func (t *Tensor) String() string {
return t.Name()
}