forked from cmars/tfclient
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathclient.go
347 lines (309 loc) · 8.32 KB
/
client.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
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
package tfclient
import (
"bytes"
"image"
"log"
"reflect"
"sync"
"time"
tf "github.com/figroc/tensorflow-serving-client/v2/go/tensorflow_serving/apis"
tfcore "github.com/figroc/tensorflow-serving-client/v2/go/tensorflow/core/framework"
meta_graph "github.com/figroc/tensorflow-serving-client/v2/go/tensorflow/core/protobuf"
proto "github.com/golang/protobuf/proto"
"golang.org/x/image/bmp"
"golang.org/x/image/draw"
"golang.org/x/net/context"
"google.golang.org/grpc"
)
type TensorProto = tfcore.TensorProto
type TensorShapeProto = tfcore.TensorShapeProto
type TensorShapeProto_Dim = tfcore.TensorShapeProto_Dim
type ModelSpec = tf.ModelSpec
type PredictionClient struct {
mu sync.RWMutex
rpcConn *grpc.ClientConn
svcConn tf.PredictionServiceClient
debug bool
inputConf map[string]*InputConfig
inputConfMutex sync.Mutex
}
type BoxPrediction struct {
Class string `json:"class"`
Score float32 `json:"score"`
Y1 float32 `json:"y1"`
X1 float32 `json:"x1"`
Y2 float32 `json:"y2"`
X2 float32 `json:"x2"`
}
type InputConfig struct {
SignatureName string
InputName string
Dtype tfcore.DataType
SequenceLength int64
Height int64
Width int64
Version int64
}
var classLabels = []string{
"signs",
"panels",
"vehicles",
"people",
"license-plates",
"traffic_light",
}
func NewClient(addr string) (*PredictionClient, error) {
conn, err := grpc.Dial(
addr,
grpc.WithDefaultCallOptions(grpc.MaxCallRecvMsgSize(100*1024*1024)),
grpc.WithInsecure(),
grpc.WithBlock(),
grpc.WithTimeout(5 * time.Minute),
)
if err != nil {
return nil, err
}
c := tf.NewPredictionServiceClient(conn)
return &PredictionClient{
rpcConn: conn,
svcConn: c,
debug: false,
inputConf: make(map[string]*InputConfig),
}, nil
}
func (c *PredictionClient) SetDebugging(debug bool) {
c.debug = debug
}
func (c *PredictionClient) FormatBoxes(resp map[string]*tfcore.TensorProto, minConfidence float32) []BoxPrediction {
var result []BoxPrediction
boxes := resp["detection_boxes"].FloatVal
classes := resp["detection_classes"].FloatVal
for i, score := range resp["detection_scores"].FloatVal {
if score < minConfidence {
break
}
classIndex := int(classes[i]) - 1
coordIndex := i * 4
p := BoxPrediction{
Class: classLabels[classIndex],
Score: score,
Y1: boxes[coordIndex],
X1: boxes[coordIndex+1],
Y2: boxes[coordIndex+2],
X2: boxes[coordIndex+3],
}
result = append(result, p)
}
return result
}
func (c *PredictionClient) GetInputConfig(modelName string) (*InputConfig, error) {
c.inputConfMutex.Lock()
conf, ok := c.inputConf[modelName]
c.inputConfMutex.Unlock()
if ok {
return conf, nil
}
modelSpec := &tf.ModelSpec{
Name: modelName,
}
resp, err := c.svcConn.GetModelMetadata(context.Background(), &tf.GetModelMetadataRequest{
ModelSpec: modelSpec,
MetadataField: []string{"signature_def"},
})
if err != nil {
return nil, err
}
var ret InputConfig
modelSpec = resp.GetModelSpec()
ret.Version = modelSpec.GetVersion().GetValue()
sgDefMap := tf.SignatureDefMap{}
if err := proto.Unmarshal(resp.GetMetadata()["signature_def"].Value, &sgDefMap); err != nil {
return nil, err
}
sgDef := sgDefMap.GetSignatureDef()
var inputs map[string]*meta_graph.TensorInfo
if _, ok := sgDef["predict_images"]; ok {
ret.SignatureName = "predict_images"
inputs = sgDef["predict_images"].Inputs
} else {
inputs = sgDef["serving_default"].Inputs
}
for key, val := range inputs {
ret.InputName = key
ret.Dtype = val.Dtype
if val.Dtype != tfcore.DataType_DT_STRING {
if len(val.TensorShape.Dim) < 5 { // batch, height, width, channels
ret.Height = val.TensorShape.Dim[1].Size
ret.Width = val.TensorShape.Dim[2].Size
} else { // batch, seq_len, height, width, channels
ret.SequenceLength = val.TensorShape.Dim[1].Size
ret.Height = val.TensorShape.Dim[2].Size
ret.Width = val.TensorShape.Dim[3].Size
}
}
break
}
c.inputConfMutex.Lock()
c.inputConf[modelName] = &ret
c.inputConfMutex.Unlock()
return &ret, nil
}
func (c *PredictionClient) FormatInputImages(images []image.Image, inputConf *InputConfig, scaler draw.Scaler) (*tfcore.TensorProto, error) {
var inputProto *tfcore.TensorProto
var tfshape *tfcore.TensorShapeProto
if inputConf.Dtype == tfcore.DataType_DT_STRING {
tfshape = &tfcore.TensorShapeProto{
Dim: []*tfcore.TensorShapeProto_Dim{{Size: int64(len(images))}},
}
content := make([][]byte, len(images))
for i, img := range images {
buf := new(bytes.Buffer)
if err := bmp.Encode(buf, img); err != nil {
return nil, err
}
content[i] = buf.Bytes()
}
inputProto = &tfcore.TensorProto{
Dtype: inputConf.Dtype,
StringVal: content,
TensorShape: tfshape,
}
} else {
mustResize := true
w := inputConf.Width
h := inputConf.Height
bounds := images[0].Bounds()
if w <= 0 || h <= 0 {
w = int64(bounds.Max.X)
h = int64(bounds.Max.Y)
mustResize = false
}
tfshape = &tfcore.TensorShapeProto{
Dim: []*tfcore.TensorShapeProto_Dim{
{Size: int64(len(images))},
{Size: h},
{Size: w},
{Size: 3},
},
}
content := make([]byte, int64(len(images))*w*h*3)
rect := image.Rect(0, 0, int(w), int(h))
i := 0
for _, img := range images {
bounds = img.Bounds()
if mustResize && !bounds.Eq(rect) {
dst := image.NewRGBA(rect)
scaler.Scale(dst, rect, img, bounds, draw.Over, nil)
img = dst
}
var pix []uint8
switch v := img.(type) {
case *image.RGBA:
pix = v.Pix
case *image.NRGBA:
pix = v.Pix
}
if pix == nil {
// very slow fallback for non-RGBA images
for y := int64(0); y < h; y++ {
for x := int64(0); x < w; x++ {
c := img.At(int(x), int(y))
r, g, b, _ := c.RGBA()
content[i] = byte(r)
i++
content[i] = byte(g)
i++
content[i] = byte(b)
i++
}
}
} else {
nPix := len(pix)
for j := 0; j < nPix; j += 4 {
content[i] = pix[j]
content[i+1] = pix[j+1]
content[i+2] = pix[j+2]
i += 3
}
}
}
inputProto = &tfcore.TensorProto{
Dtype: inputConf.Dtype,
TensorContent: content,
TensorShape: tfshape,
}
}
return inputProto, nil
}
func (c *PredictionClient) GetModelSpec(modelName, signatureName string, inputConf *InputConfig) *tf.ModelSpec {
modelSpec := &tf.ModelSpec{
Name: modelName,
}
if signatureName != "" {
modelSpec.SignatureName = signatureName
} else if inputConf.SignatureName != "" {
modelSpec.SignatureName = inputConf.SignatureName
}
return modelSpec
}
func (c *PredictionClient) PredictRaw(modelSpec *tf.ModelSpec, inputConf *InputConfig, inputProto *tfcore.TensorProto) (map[string]*tfcore.TensorProto, error) {
resp, err := c.svcConn.Predict(context.Background(), &tf.PredictRequest{
ModelSpec: modelSpec,
Inputs: map[string]*tfcore.TensorProto{
inputConf.InputName: inputProto,
},
})
if err != nil {
return nil, err
}
if c.debug {
log.Println("Output format:", reflect.TypeOf(resp.Outputs))
log.Println("Output:", resp.Outputs)
}
return resp.Outputs, nil
}
func (c *PredictionClient) PredictImages(modelName, signatureName string, images []image.Image, scaler draw.Scaler) (map[string]*tfcore.TensorProto, error) {
inputConf, err := c.GetInputConfig(modelName)
if err != nil {
return nil, err
}
modelSpec := &tf.ModelSpec{
Name: modelName,
}
if signatureName != "" {
modelSpec.SignatureName = signatureName
} else if inputConf.SignatureName != "" {
modelSpec.SignatureName = inputConf.SignatureName
}
inputProto, err := c.FormatInputImages(images, inputConf, scaler)
if err != nil {
return nil, err
}
return c.PredictRaw(modelSpec, inputConf, inputProto)
}
func (c *PredictionClient) GetOutput(modelName, signatureName string) (map[string]*tfcore.TensorProto, error) {
modelSpec := &tf.ModelSpec{
Name: modelName,
}
if signatureName != "" {
modelSpec.SignatureName = signatureName
}
resp, err := c.svcConn.Predict(context.Background(), &tf.PredictRequest{
ModelSpec: modelSpec,
Inputs: nil,
})
if err != nil {
return nil, err
}
if c.debug {
log.Println("Output format:", reflect.TypeOf(resp.Outputs))
log.Println("Output:", resp.Outputs)
}
return resp.Outputs, nil
}
func (c *PredictionClient) Close() error {
c.mu.Lock()
defer c.mu.Unlock()
c.svcConn = nil
return c.rpcConn.Close()
}