-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathpredict.go
More file actions
160 lines (150 loc) · 5.23 KB
/
predict.go
File metadata and controls
160 lines (150 loc) · 5.23 KB
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
package godd
import (
"bytes"
"encoding/json"
"io/ioutil"
"net/http"
"time"
)
// PredictResult struct storing predictions result
type PredictResult struct {
Status struct {
Code int `json:"code,omitempty"`
Msg string `json:"msg,omitempty"`
} `json:"status,omitempty"`
Head struct {
Method string `json:"method,omitempty"`
Service string `json:"service,omitempty"`
Time float64 `json:"time,omitempty"`
} `json:"head,omitempty"`
Body struct {
Predictions []struct {
Classes []struct {
Prob float64 `json:"prob,omitempty"`
Last bool `json:"last,omitempty"`
Bbox struct {
Ymax float64 `json:"ymax,omitempty"`
Xmax float64 `json:"xmax,omitempty"`
Ymin float64 `json:"ymin,omitempty"`
Xmin float64 `json:"xmin,omitempty"`
} `json:"bbox,omitempty"`
Mask struct {
Format string `json:"format,omitempty"`
Width int `json:"width,omitempty"`
Height int `json:"height,omitempty"`
Data []int `json:"data,omitempty"`
} `json:"mask,omitempty"`
Cat string `json:"cat,omitempty"`
}
URI string `json:"uri,omitempty"`
} `json:"predictions,omitempty"`
} `json:"body,omitempty"`
}
// PredictRequest hold data for the
// prediction request
type PredictRequest struct {
// General parameters
Service string `json:"service,omitempty"`
Data []string `json:"data,omitempty"`
Parameters struct {
Input struct {
// Image - image
Width int `json:"width,omitempty"`
Height int `json:"height,omitempty"`
CropWidth int `json:"crop_width,omitempty"`
CropHeight int `json:"crop_height,omitempty"`
BW bool `json:"bw,omitempty"`
MeanTF float64 `json:"mean,omitempty"`
Mean []int `json:"mean,omitempty"`
STD float64 `json:"std,omitempty"`
// CSV - csv
Ignore []string `json:"ignore,omitempty"`
Separator string `json:"separator,omitempty"`
ID string `json:"id,omitempty"`
Scale bool `json:"scale,omitempty"`
MinVals []float64 `json:"min_vals,omitempty"`
MaxVals []float64 `json:"max_vals,omitempty"`
// MISSING: categoricals_mapping
// Text - txt
Count int `json:"count,omitempty"`
MinCount int `json:"min_count,omitempty"`
MinWordLength int `json:"min_word_length,omitempty"`
TFIDF bool `json:"tfidf,omitempty"`
Sentences bool `json:"sentences,omitempty"`
Characters bool `json:"characters,omitempty"`
Sequence int `json:"sequence,omitempty"`
ReadForward bool `json:"read_forward,omitempty"`
Alphabet string `json:"alphabet,omitempty"`
Sparse bool `json:"sparse,omitempty"`
Segmentation bool `json:"segmentation,omitempty"`
} `json:"input,omitempty"`
Output struct {
Best int `json:"best,omitempty"`
Template string `json:"template,omitempty"`
Network *struct {
URL string `json:"url,omitempty"`
HTTPMethod string `json:"http_method,omitempty"`
ContentType string `json:"content_type,omitempty"`
} `json:"network,omitempty"`
Measure []float64 `json:"measure,omitempty"`
ConfidenceThreshold float64 `json:"confidence_threshold,omitempty"`
Bbox bool `json:"bbox,omitempty"`
Mask bool `json:"mask,omitempty"`
Rois string `json:"rois,omitempty"`
Index bool `json:"index,omitempty"`
BuildIndex bool `json:"build_index,omitempty"`
Search bool `json:"search,omitempty"`
MultiboxRois bool `json:"multibox_rois,omitempty"`
CTC bool `json:"ctc,omitempty"`
BlankLabel int `json:"blank_label,omitempty"`
} `json:"output,omitempty"`
Mllib struct {
// Caffe / Caffe2
GPU bool `json:"gpu,omitempty"`
GPUID []int `json:"gpuid,omitempty"`
ExtractLayer string `json:"extract_layer,omitempty"`
// Net or TF
TestBatchSize int `json:"test_batch_size,omitempty"`
// Tensorflow
InputLayer string `json:"inputlayer,omitempty"`
OutputLayer string `json:"outputlayer,omitempty"`
} `json:"mllib,omitempty"`
} `json:"parameters,omitempty"`
}
// Predict perform a /predict call and
// return a PredictResult structure
func Predict(host string, predictRequest *PredictRequest) (result PredictResult, err error) {
// Turn requestPredict structure into a map for request
requestPredict := map[string]interface{}{
"service": predictRequest.Service,
"data": predictRequest.Data,
"parameters": predictRequest.Parameters,
}
// Marshal data
bytesReq, err := json.Marshal(requestPredict)
if err != nil {
return result, err
}
// Send HTTP request
req, err := http.NewRequest("POST", host+"/predict", bytes.NewBuffer(bytesReq))
req.Close = true
req.Header.Set("Content-Type", "application/json")
// Execute request
client := http.Client{Timeout: 500 * time.Second}
resp, err := client.Do(req)
if err != nil {
return result, err
}
defer resp.Body.Close()
// Read response
body, err := ioutil.ReadAll(resp.Body)
if err != nil {
return result, err
}
// Fill info structure with response data
err = json.Unmarshal(body, &result)
if err != nil {
return result, err
}
return result, err
}