forked from viamrobotics/rdk
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathml.go
217 lines (204 loc) · 6.61 KB
/
ml.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
// Package ml provides some fundamental machine learning primitives.
package ml
import (
"math"
"unsafe"
"github.com/pkg/errors"
pb "go.viam.com/api/service/mlmodel/v1"
"golang.org/x/exp/constraints"
"gorgonia.org/tensor"
)
// ProtoToTensors takes pb.FlatTensors and turns it into a Tensors map.
func ProtoToTensors(pbft *pb.FlatTensors) (Tensors, error) {
if pbft == nil {
return nil, errors.New("protobuf FlatTensors is nil")
}
tensors := Tensors{}
for name, ftproto := range pbft.Tensors {
t, err := CreateNewTensor(ftproto)
if err != nil {
return nil, err
}
tensors[name] = t
}
return tensors, nil
}
// CreateNewTensor turns a proto FlatTensor into a *tensor.Dense.
func CreateNewTensor(pft *pb.FlatTensor) (*tensor.Dense, error) {
shape := make([]int, 0, len(pft.Shape))
for _, s := range pft.Shape {
shape = append(shape, int(s))
}
pt := pft.Tensor
switch t := pt.(type) {
case *pb.FlatTensor_Int8Tensor:
data := t.Int8Tensor
if data == nil {
return nil, errors.New("tensor of type Int8Tensor is nil")
}
dataSlice := data.GetData()
unsafeInt8Slice := *(*[]int8)(unsafe.Pointer(&dataSlice)) //nolint:gosec
int8Slice := make([]int8, 0, len(dataSlice))
int8Slice = append(int8Slice, unsafeInt8Slice...)
return tensor.New(tensor.WithShape(shape...), tensor.WithBacking(int8Slice)), nil
case *pb.FlatTensor_Uint8Tensor:
data := t.Uint8Tensor
if data == nil {
return nil, errors.New("tensor of type Uint8Tensor is nil")
}
return tensor.New(tensor.WithShape(shape...), tensor.WithBacking(data.GetData())), nil
case *pb.FlatTensor_Int16Tensor:
data := t.Int16Tensor
if data == nil {
return nil, errors.New("tensor of type Int16Tensor is nil")
}
int16Data := uint32ToInt16(data.GetData())
return tensor.New(tensor.WithShape(shape...), tensor.WithBacking(int16Data)), nil
case *pb.FlatTensor_Uint16Tensor:
data := t.Uint16Tensor
if data == nil {
return nil, errors.New("tensor of type Uint16Tensor is nil")
}
uint16Data := uint32ToUint16(data.GetData())
return tensor.New(tensor.WithShape(shape...), tensor.WithBacking(uint16Data)), nil
case *pb.FlatTensor_Int32Tensor:
data := t.Int32Tensor
if data == nil {
return nil, errors.New("tensor of type Int32Tensor is nil")
}
return tensor.New(tensor.WithShape(shape...), tensor.WithBacking(data.GetData())), nil
case *pb.FlatTensor_Uint32Tensor:
data := t.Uint32Tensor
if data == nil {
return nil, errors.New("tensor of type Uint32Tensor is nil")
}
return tensor.New(tensor.WithShape(shape...), tensor.WithBacking(data.GetData())), nil
case *pb.FlatTensor_Int64Tensor:
data := t.Int64Tensor
if data == nil {
return nil, errors.New("tensor of type Int64Tensor is nil")
}
return tensor.New(tensor.WithShape(shape...), tensor.WithBacking(data.GetData())), nil
case *pb.FlatTensor_Uint64Tensor:
data := t.Uint64Tensor
if data == nil {
return nil, errors.New("tensor of type Uint64Tensor is nil")
}
return tensor.New(tensor.WithShape(shape...), tensor.WithBacking(data.GetData())), nil
case *pb.FlatTensor_FloatTensor:
data := t.FloatTensor
if data == nil {
return nil, errors.New("tensor of type FloatTensor is nil")
}
return tensor.New(tensor.WithShape(shape...), tensor.WithBacking(data.GetData())), nil
case *pb.FlatTensor_DoubleTensor:
data := t.DoubleTensor
if data == nil {
return nil, errors.New("tensor of type DoubleTensor is nil")
}
return tensor.New(tensor.WithShape(shape...), tensor.WithBacking(data.GetData())), nil
default:
return nil, errors.Errorf("don't know how to create tensor.Dense from proto type %T", pt)
}
}
func uint32ToInt16(uint32Slice []uint32) []int16 {
int16Slice := make([]int16, len(uint32Slice))
for i, value := range uint32Slice {
int16Slice[i] = int16(value)
}
return int16Slice
}
func uint32ToUint16(uint32Slice []uint32) []uint16 {
uint16Slice := make([]uint16, len(uint32Slice))
for i, value := range uint32Slice {
uint16Slice[i] = uint16(value)
}
return uint16Slice
}
// number interface for converting between numbers.
type number interface {
constraints.Integer | constraints.Float
}
// convertNumberSlice converts any number slice into another number slice.
func convertNumberSlice[T1, T2 number](t1 []T1) []T2 {
t2 := make([]T2, len(t1))
for i := range t1 {
t2[i] = T2(t1[i])
}
return t2
}
// ConvertToFloat64Slice converts any numbers or slice of numbers into a float64 slice.
func ConvertToFloat64Slice(slice interface{}) ([]float64, error) {
switch v := slice.(type) {
case []float64:
return v, nil
case float64:
return []float64{v}, nil
case []float32:
return convertNumberSlice[float32, float64](v), nil
case float32:
return convertNumberSlice[float32, float64]([]float32{v}), nil
case []int:
return convertNumberSlice[int, float64](v), nil
case int:
return convertNumberSlice[int, float64]([]int{v}), nil
case []uint:
return convertNumberSlice[uint, float64](v), nil
case uint:
return convertNumberSlice[uint, float64]([]uint{v}), nil
case []int8:
return convertNumberSlice[int8, float64](v), nil
case int8:
return convertNumberSlice[int8, float64]([]int8{v}), nil
case []int16:
return convertNumberSlice[int16, float64](v), nil
case int16:
return convertNumberSlice[int16, float64]([]int16{v}), nil
case []int32:
return convertNumberSlice[int32, float64](v), nil
case int32:
return convertNumberSlice[int32, float64]([]int32{v}), nil
case []int64:
return convertNumberSlice[int64, float64](v), nil
case int64:
return convertNumberSlice[int64, float64]([]int64{v}), nil
case []uint8:
return convertNumberSlice[uint8, float64](v), nil
case uint8:
return convertNumberSlice[uint8, float64]([]uint8{v}), nil
case []uint16:
return convertNumberSlice[uint16, float64](v), nil
case uint16:
return convertNumberSlice[uint16, float64]([]uint16{v}), nil
case []uint32:
return convertNumberSlice[uint32, float64](v), nil
case uint32:
return convertNumberSlice[uint32, float64]([]uint32{v}), nil
case []uint64:
return convertNumberSlice[uint64, float64](v), nil
case uint64:
return convertNumberSlice[uint64, float64]([]uint64{v}), nil
default:
return nil, errors.Errorf("dont know how to convert slice of %T into a []float64", slice)
}
}
// softmax takes the input slice and applies the softmax function.
func softmax(in []float64) []float64 {
out := make([]float64, 0, len(in))
bigSum := 0.0
for _, x := range in {
bigSum += math.Exp(x)
}
for _, x := range in {
out = append(out, math.Exp(x)/bigSum)
}
return out
}
// TensorNames returns all the names of the tensors.
func TensorNames(t Tensors) []string {
names := []string{}
for name := range t {
names = append(names, name)
}
return names
}