-
Notifications
You must be signed in to change notification settings - Fork 2
/
infer.go
249 lines (214 loc) · 6.32 KB
/
infer.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
package infer
import (
"bytes"
"context"
"errors"
"image"
"io"
"sort"
tf "github.com/tensorflow/tensorflow/tensorflow/go"
"github.com/tensorflow/tensorflow/tensorflow/go/op"
// Required to support image decoding.
_ "image/jpeg"
_ "image/png"
)
var (
// ErrValidInputOutputRequired occurs if an invalid Input or Output is provided.
ErrValidInputOutputRequired = errors.New("a valid Input/Output is required")
)
// Model is an ML model. It's composed of a computation graph and
// an Input and Output. It provides methods for running inferences
// usnig it's Graph, abiding by it's Input/Output.
type Model struct {
Graph *tf.Graph
Classes []string
Input *Input
Output *Output
}
// Input is an ML layer. It is identified by a key and has dimensions
// The dimensions are used to augment or resize the output as appropriate.
type Input struct {
// Key represents a layer in a TensorFlow model. The selection of a Key
// determines "where" the Input/Output occurs in the Graph.
Key string
// Dimensions represents the size of the input. It can be of any type but
// must contains the values expected by the layer. It may be used to
// augment or resize the input so that it conforms to the specified layer.
Dimensions interface{}
}
// Output is an ML layer. It is identified by a key and has dimensions
// The dimensions are used to augment or resize the output as appropriate.
type Output struct {
// Key represents a layer in a TensorFlow model. The selection of a Key
// determines "where" the Input/Output occurs in the Graph.
Key string
// Dimensions represents the size of the input. It can be of any type but
// must contains the values expected by the layer. It may be used to
// augment or resize the input so that it conforms to the specified layer.
Dimensions interface{}
}
// New returns a new Model.
func New(model *Model) (*Model, error) {
if model.Input == nil || model.Output == nil {
return nil, ErrValidInputOutputRequired
}
if model.Input.Key == "" || model.Output.Key == "" {
return nil, ErrValidInputOutputRequired
}
return model, nil
}
// ImageOptions represent configurable options when evaluating images.
// Note: for now it is sparse, but included to keep the method signature
// consistent as new options become available.
type ImageOptions struct {
// IsGray represents whether the Model expects the input image
// to be grayscale or not. Specifically, whether the image has
// 3 channels or 1 channel.
IsGray bool
}
// FromImageWithContext evaluates an image with context. Optional ImageOptions
// can be included to dictate the pre-processing of the input image. The method
// returns an interface of results which can be cast to the appropriate type.
func (m *Model) FromImageWithContext(ctx context.Context, r io.Reader, opts *ImageOptions) ([]*Prediction, error) {
if ctx == nil {
panic("nil context")
}
c := make(chan error)
var p []*Prediction
go func() {
var err error
p, err = m.fromImage(r, opts)
c <- err
}()
for {
select {
case <-ctx.Done():
return p, ctx.Err()
case err := <-c:
return p, err
}
}
}
// FromImage evaluates an image.
func (m *Model) FromImage(r io.Reader, opts *ImageOptions) ([]*Prediction, error) {
return m.FromImageWithContext(context.Background(), r, opts)
}
func (m *Model) fromImage(r io.Reader, opts *ImageOptions) ([]*Prediction, error) {
var imgBuf, tensorBuf bytes.Buffer
w := io.MultiWriter(&imgBuf, &tensorBuf)
_, err := io.Copy(w, r)
if err != nil {
return nil, err
}
// Determine image type.
_, typ, err := image.Decode(&imgBuf)
if err != nil {
return nil, err
}
// Create tensor from image as string. DecodePng/Jpeg expects this.
tensor, err := tf.NewTensor(tensorBuf.String())
if err != nil {
return nil, err
}
scope := op.NewScope()
input := op.Placeholder(scope, tf.String)
var channels int64 = 3
if opts.IsGray {
channels = 1
}
// Create a decoder operation based on image type.
var decoder tf.Output
switch typ {
case "png":
decoder = op.DecodePng(scope, input, op.DecodePngChannels(channels))
break
case "jpeg":
decoder = op.DecodeJpeg(scope, input, op.DecodeJpegChannels(channels))
break
default:
return nil, errors.New("invalid image")
}
// Crop/resize image. This bilinearly resizes the input image.
// op.CropAndResize provides support for cropping/resizing multiple "boxes"
// from the input image, however we only require one. Linked below
// are the python docs, which are more thorough:
// https://www.tensorflow.org/api_docs/python/tf/image/crop_and_resize
cropSize := op.Const(scope.SubScope("crop_size"), m.Input.Dimensions)
boxes := op.Const(scope.SubScope("boxes"), [][]float32{{0, 0, 1, 1}})
boxInd := op.Const(scope.SubScope("box_ind"), []int32{0})
images := op.ExpandDims(
scope,
op.Cast(scope, decoder, tf.Float),
op.Const(scope.SubScope("batch"), int32(0)),
)
cropAndResize := op.CropAndResize(
scope,
images,
boxes,
boxInd,
cropSize,
)
// Create graph for cropping, initiate session, and execute.
graph, err := scope.Finalize()
if err != nil {
return nil, err
}
session, err := tf.NewSession(graph, nil)
if err != nil {
return nil, err
}
defer session.Close()
cropped, err := session.Run(
map[tf.Output]*tf.Tensor{
input: tensor,
},
[]tf.Output{cropAndResize},
nil)
if err != nil {
return nil, err
}
// Pass cropped image to primary model for evaluation.
result, err := m.eval(cropped[0])
if err != nil {
return nil, err
}
// Convert result to Predictions.
var scores []float32
if len(result[0].Shape()) == 1 {
scores = result[0].Value().([]float32)
} else {
scores = result[0].Value().([][]float32)[0]
}
predictions := make([]*Prediction, len(scores))
for i := range predictions {
var c interface{}
if len(m.Classes) > i {
c = m.Classes[i]
} else {
c = i
}
predictions[i] = &Prediction{
Class: c,
Score: scores[i],
}
}
sort.Sort(sort.Reverse(Predictions(predictions)))
return predictions, nil
}
// eval executes the inference using an input against the model graph.
func (m *Model) eval(input *tf.Tensor) ([]*tf.Tensor, error) {
session, err := tf.NewSession(m.Graph, nil)
if err != nil {
return nil, err
}
defer session.Close()
return session.Run(
map[tf.Output]*tf.Tensor{
m.Graph.Operation(m.Input.Key).Output(0): input,
},
[]tf.Output{
m.Graph.Operation(m.Output.Key).Output(0),
},
nil,
)
}