-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathclient.cc
210 lines (150 loc) · 6.92 KB
/
client.cc
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
/*******************************************
Author : Jun Zhang
Email : [email protected]
Last Modified : 2019-09-03 18:28
Filename : client.cc
Description :
*******************************************/
#include "client.h"
int ServingClient::Predict(const PredictRequest& request, PredictResponse& response) {
brpc::Controller cntl;
stub_ -> Predict(&cntl, &request, &response, NULL);
if(cntl.Failed()) {
LOG(INFO) << "Predict success, latecy: " << cntl.latency_us() << " us";
} else {
LOG(WARNING) << cntl.ErrorText();
return -1;
}
return 0;
}
int ServingTask::Init() {
client_ = new ServingClient(channel_);
if(nullptr == client_) {
LOG(INFO) << "failed to init ServingClient.";
return -1;
}
LOG(INFO) << "Init ServingClient success.";
return 0;
}
int ServingTask::ShutDown() {
if(nullptr != client_) {
delete client_;
client_ = nullptr;
}
LOG(INFO) << "ServingClient shutdown.";
}
int ServingTask::Process(const std::string &model_name, const std::string &model_sig_name, const void* in_data, const size_t in_size, int &idx, float& prob) {
if(nullptr == client_)
return -1;
PredictRequest request;
PredictResponse response;
request.mutable_model_spec() -> set_name(model_name);
request.mutable_model_spec() -> set_signature_name(model_sig_name);
tensorflow::TensorProto proto;
proto.set_dtype(tensorflow::DataType::DT_STRING);
proto.add_string_val(in_data, in_size);
proto.mutable_tensor_shape() -> add_dim() -> set_size(1);
MapProto& inputs = *request.mutable_inputs();
inputs["input"] = proto;
int ret = client_ -> Predict(request, response);
LOG(INFO) << "outputs size is " << response.outputs_size();
MapProto& map_outputs = *response.mutable_outputs();
MapProto::iterator iter;
int output_idx = 0;
for(iter = map_outputs.begin(); iter != map_outputs.end(); ++iter) {
tensorflow::TensorProto& result_tensor_proto = iter -> second;
LOG(INFO) << "num of probs: " << result_tensor_proto.float_val_size();
for(int i = 0; i < result_tensor_proto.float_val_size(); i ++) {
LOG(INFO) << "idx: " << i << ", prob: " << result_tensor_proto.float_val(i);
}
}
return ret;
}
int ServingTask::Process(const std::string &model_name, const std::string &model_sig_name, const void* in_data, const size_t in_size, std::vector<float> &bbox) {
if(nullptr == client_)
return -1;
PredictRequest request;
PredictResponse response;
request.mutable_model_spec() -> set_name(model_name);
request.mutable_model_spec() -> set_signature_name(model_sig_name);
tensorflow::TensorProto proto;
proto.set_dtype(tensorflow::DataType::DT_STRING);
proto.add_string_val(in_data, in_size);
proto.mutable_tensor_shape() -> add_dim() -> set_size(1);
MapProto& inputs = *request.mutable_inputs();
inputs["inputs"] = proto;
int ret = client_ -> Predict(request, response);
LOG(INFO) << "outputs size is " << response.outputs_size();
MapProto& map_outputs = *response.mutable_outputs();
MapProto::iterator iter;
int output_idx = 0;
auto it = map_outputs.find("detection_boxes");
if(it == map_outputs.end()){
return -2;
}
std::vector<float> data((it -> second).float_val().begin(), (it -> second).float_val().begin() + 4);
LOG(INFO) << "data size: " << (it -> second).float_val_size();
LOG(INFO) << "data : " << data[0] << ", " << data[1] << ", " << data[2] << ", " << data[3];
//<< "," << (it -> second).float_val(1) << ", " << (it -> second).float_val(2) << ", " < (it -> second).float_val(3);
if((it -> second).has_tensor_shape()) {
LOG(INFO) << "has tensor shape";
//tensorflow::TensorShape& shape_data = *(iter -> second).mutable_tensor_shape();
//LOG(INFO) << "tensorshape size is " << (iter -> second).tensor_shape_size();
}
//tensorflow::TensorProto& result_tensor_proto = it -> second;
//LOG(INFO) << "num of probs: " << result_tensor_proto.float_val_size();
//for(int i = 0; i < result_tensor_proto.float_val_size(); i ++) {
// LOG(INFO) << "idx: " << i << ", prob: " << result_tensor_proto.float_val(i);
//}
//for(iter = map_outputs.begin(); iter != map_outputs.end(); ++iter) {
// tensorflow::TensorProto& result_tensor_proto = iter -> second;
// LOG(INFO) << "num of probs: " << result_tensor_proto.float_val_size();
// for(int i = 0; i < result_tensor_proto.float_val_size(); i ++) {
// LOG(INFO) << "idx: " << i << ", prob: " << result_tensor_proto.float_val(i);
// }
//}
return ret;
}
int ServingTask::Process(const std::string &model_name, const std::string &model_sig_name, const void* in_data, const size_t in_size, const int height, const int width, std::vector<float> &detect_fea){
if(nullptr == client_)
return -1;
PredictRequest request;
PredictResponse response;
request.mutable_model_spec() -> set_name(model_name);
request.mutable_model_spec() -> set_signature_name(model_sig_name);
tensorflow::TensorProto proto;
proto.set_dtype(tensorflow::DataType::DT_STRING);
proto.add_string_val(in_data, in_size);
proto.mutable_tensor_shape() -> add_dim() -> set_size(1);
MapProto& inputs = *request.mutable_inputs();
inputs["input"] = proto;
int ret = client_ -> Predict(request, response);
LOG(INFO) << "outputs size is " << response.outputs_size();
MapProto& map_outputs = *response.mutable_outputs();
MapProto::iterator iter;
int output_idx = 0;
auto it = map_outputs.find("output");
if(it == map_outputs.end()){
return -2;
}
//std::vector<float> data((it -> second).float_val().begin(), (it -> second).float_val().begin() + 4);
LOG(INFO) << "data size: " << (it -> second).float_val_size();
//LOG(INFO) << "data : " << data[0] << ", " << data[1] << ", " << data[2] << ", " << data[3];
//<< "," << (it -> second).float_val(1) << ", " << (it -> second).float_val(2) << ", " < (it -> second).float_val(3);
if((it -> second).has_tensor_shape()) {
LOG(INFO) << "has tensor shape";
}
//tensorflow::TensorProto& result_tensor_proto = it -> second;
//LOG(INFO) << "num of probs: " << result_tensor_proto.float_val_size();
//for(int i = 0; i < result_tensor_proto.float_val_size(); i ++) {
// LOG(INFO) << "idx: " << i << ", prob: " << result_tensor_proto.float_val(i);
//}
//for(iter = map_outputs.begin(); iter != map_outputs.end(); ++iter) {
// tensorflow::TensorProto& result_tensor_proto = iter -> second;
// LOG(INFO) << "num of probs: " << result_tensor_proto.float_val_size();
// for(int i = 0; i < result_tensor_proto.float_val_size(); i ++) {
// LOG(INFO) << "idx: " << i << ", prob: " << result_tensor_proto.float_val(i);
// }
//}
return ret;
}