forked from jolibrain/deepdetect
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathut-xgbapi.cc
314 lines (292 loc) · 13.5 KB
/
ut-xgbapi.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
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
/**
* DeepDetect
* Copyright (c) 2016 Emmanuel Benazera
* Author: Emmanuel Benazera <[email protected]>
*
* This file is part of deepdetect.
*
* deepdetect is free software: you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* deepdetect is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with deepdetect. If not, see <http://www.gnu.org/licenses/>.
*/
#include "deepdetect.h"
#include "jsonapi.h"
#include <gtest/gtest.h>
#include <stdio.h>
#include <unistd.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <iostream>
using namespace dd;
static std::string ok_str = "{\"status\":{\"code\":200,\"msg\":\"OK\"}}";
static std::string created_str
= "{\"status\":{\"code\":201,\"msg\":\"Created\"}}";
static std::string bad_param_str
= "{\"status\":{\"code\":400,\"msg\":\"BadRequest\"}}";
static std::string not_found_str
= "{\"status\":{\"code\":404,\"msg\":\"NotFound\"}}";
static std::string forest_repo = "../examples/all/forest_type/";
static std::string n20_repo = "../examples/all/n20/";
static std::string sflare_repo = "../examples/all/sflare/";
static std::string iterations_forest = "10";
static std::string iterations_n20 = "10";
static std::string iterations_sflare = "100";
TEST(xgbapi, service_train_csv)
{
JsonAPI japi;
std::string sname = "my_service";
std::string jstr
= "{\"mllib\":\"xgboost\",\"description\":\"my "
"classifier\",\"type\":\"supervised\",\"model\":{\"repository\":\""
+ forest_repo
+ "\"},\"parameters\":{\"input\":{\"connector\":\"csv\"},\"mllib\":{"
"\"nclasses\":7}}}";
std::string joutstr = japi.jrender(japi.service_create(sname, jstr));
ASSERT_EQ(created_str, joutstr);
// assert json blob file
std::cerr << forest_repo + "/" + JsonAPI::_json_blob_fname << std::endl;
ASSERT_TRUE(
fileops::file_exists(forest_repo + "/" + JsonAPI::_json_blob_fname));
// train
std::string jtrainstr
= "{\"service\":\"" + sname
+ "\",\"async\":false,\"parameters\":{\"input\":{\"label\":\"Cover_"
"Type\",\"id\":\"Id\",\"test_split\":0.1,\"label_offset\":-1,"
"\"shuffle\":true},\"mllib\":{\"iterations\":"
+ iterations_forest
+ ",\"objective\":\"multi:softprob\",\"gpu\":true},\"output\":{"
"\"measure\":[\"acc\",\"mcll\",\"f1\",\"cmdiag\",\"cmfull\"]}},"
"\"data\":[\""
+ forest_repo + "train.csv\"]}";
joutstr = japi.jrender(japi.service_train(jtrainstr));
std::cout << "joutstr=" << joutstr << std::endl;
JDoc jd;
jd.Parse<rapidjson::kParseNanAndInfFlag>(joutstr.c_str());
ASSERT_TRUE(!jd.HasParseError());
ASSERT_TRUE(jd.HasMember("status"));
ASSERT_EQ(201, jd["status"]["code"].GetInt());
ASSERT_EQ("Created", jd["status"]["msg"]);
ASSERT_TRUE(jd.HasMember("head"));
ASSERT_EQ("/train", jd["head"]["method"]);
ASSERT_TRUE(jd["head"]["time"].GetDouble() >= 0);
ASSERT_TRUE(jd.HasMember("body"));
ASSERT_TRUE(jd["body"].HasMember("measure"));
ASSERT_TRUE(jd["body"]["measure"].HasMember("f1"));
ASSERT_TRUE(jd["body"]["measure"]["f1"].GetDouble() > 0.7);
ASSERT_EQ(jd["body"]["measure"]["accp"].GetDouble(),
jd["body"]["measure"]["acc"].GetDouble());
ASSERT_TRUE(jd["body"]["measure"].HasMember("cmdiag"));
ASSERT_EQ(7, jd["body"]["measure"]["cmdiag"].Size());
ASSERT_TRUE(jd["body"]["measure"]["cmdiag"][0].GetDouble() >= 0);
ASSERT_TRUE(jd["body"]["measure"]["cmfull"][1]["1"].Size());
// predict from data, with header and id
std::string mem_data_head
= "Id,Elevation,Aspect,Slope,Horizontal_Distance_To_Hydrology,Vertical_"
"Distance_To_Hydrology,Horizontal_Distance_To_Roadways,Hillshade_9am,"
"Hillshade_Noon,Hillshade_3pm,Horizontal_Distance_To_Fire_Points,"
"Wilderness_Area1,Wilderness_Area2,Wilderness_Area3,Wilderness_Area4,"
"Soil_Type1,Soil_Type2,Soil_Type3,Soil_Type4,Soil_Type5,Soil_Type6,"
"Soil_Type7,Soil_Type8,Soil_Type9,Soil_Type10,Soil_Type11,Soil_Type12,"
"Soil_Type13,Soil_Type14,Soil_Type15,Soil_Type16,Soil_Type17,Soil_"
"Type18,Soil_Type19,Soil_Type20,Soil_Type21,Soil_Type22,Soil_Type23,"
"Soil_Type24,Soil_Type25,Soil_Type26,Soil_Type27,Soil_Type28,Soil_"
"Type29,Soil_Type30,Soil_Type31,Soil_Type32,Soil_Type33,Soil_Type34,"
"Soil_Type35,Soil_Type36,Soil_Type37,Soil_Type38,Soil_Type39,Soil_"
"Type40";
std::string mem_data
= "0,2499,326,7,300,88,480,202,232,169,1676,0,0,0,1,0,0,0,0,0,1,0,0,0,0,"
"0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0";
std::string jpredictstr
= "{\"service\":\"" + sname
+ "\",\"parameters\":{\"input\":{\"connector\":\"csv\",\"id\":\"Id\","
"\"scale\":false},\"output\":{\"best\":3}},\"data\":[\""
+ mem_data_head + "\",\"" + mem_data + "\"]}";
joutstr = japi.jrender(japi.service_predict(jpredictstr));
jd.Parse<rapidjson::kParseNanAndInfFlag>(joutstr.c_str());
ASSERT_TRUE(!jd.HasParseError());
ASSERT_TRUE(jd.HasMember("status"));
ASSERT_EQ(200, jd["status"]["code"].GetInt());
std::string cat0 = jd["body"]["predictions"][0]["classes"][0]["cat"]
.GetString(); // XXX: true cat is 3, which is 2 here
// with the label offset
std::string cat1
= jd["body"]["predictions"][0]["classes"][1]["cat"].GetString();
std::string cat2
= jd["body"]["predictions"][0]["classes"][2]["cat"].GetString();
ASSERT_TRUE("2" == cat0 || "2" == cat1 || "2" == cat2);
// predict from data, omitting header and sample id
std::string mem_data2
= "2499,326,7,300,88,480,202,232,169,1676,0,0,0,1,0,0,0,0,0,1,0,0,0,0,0,"
"0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0";
jpredictstr = "{\"service\":\"" + sname
+ "\",\"parameters\":{\"input\":{\"connector\":\"csv\","
"\"scale\":false},\"output\":{\"best\":3}},\"data\":[\""
+ mem_data2 + "\"]}";
joutstr = japi.jrender(japi.service_predict(jpredictstr));
std::cout << "joutstr=" << joutstr << std::endl;
jd.Parse<rapidjson::kParseNanAndInfFlag>(joutstr.c_str());
ASSERT_TRUE(!jd.HasParseError());
ASSERT_TRUE(jd.HasMember("status"));
ASSERT_EQ(200, jd["status"]["code"].GetInt());
cat0 = jd["body"]["predictions"][0]["classes"][0]["cat"]
.GetString(); // XXX: true cat is 3, which is 2 here with the
// label offset
cat1 = jd["body"]["predictions"][0]["classes"][1]["cat"].GetString();
cat2 = jd["body"]["predictions"][0]["classes"][2]["cat"].GetString();
ASSERT_TRUE("2" == cat0 || "2" == cat1 || "2" == cat2);
// remove service
jstr = "{\"clear\":\"lib\"}";
joutstr = japi.jrender(japi.service_delete(sname, jstr));
ASSERT_EQ(ok_str, joutstr);
// assert json blob file is still there (or gone if clear=full)
ASSERT_TRUE(
fileops::file_exists(forest_repo + "/" + JsonAPI::_json_blob_fname));
}
TEST(xgbapi, service_train_txt)
{
// create service
JsonAPI japi;
std::string n20_repo_loc = "n20";
mkdir(n20_repo_loc.c_str(), 0777);
std::string sname = "my_service";
std::string jstr
= "{\"mllib\":\"xgboost\",\"description\":\"my "
"classifier\",\"type\":\"supervised\",\"model\":{\"repository\":\""
+ n20_repo_loc
+ "\"},\"parameters\":{\"input\":{\"connector\":\"txt\"},\"mllib\":{"
"\"nclasses\":20}}}";
std::string joutstr = japi.jrender(japi.service_create(sname, jstr));
ASSERT_EQ(created_str, joutstr);
// train
std::string jtrainstr
= "{\"service\":\"" + sname
+ "\",\"async\":false,\"parameters\":{\"input\":{\"test_split\":0.2,"
"\"shuffle\":true,\"min_count\":10,\"min_word_length\":3,\"count\":"
"false},\"mllib\":{\"iterations\":"
+ iterations_n20
+ ",\"objective\":\"multi:softprob\",\"gpu\":true},\"output\":{"
"\"measure\":[\"acc\",\"mcll\",\"f1\"]}},\"data\":[\""
+ n20_repo + "news20\"]}";
joutstr = japi.jrender(japi.service_train(jtrainstr));
std::cout << "joutstr=" << joutstr << std::endl;
JDoc jd;
jd.Parse<rapidjson::kParseNanAndInfFlag>(joutstr.c_str());
ASSERT_TRUE(!jd.HasParseError());
ASSERT_TRUE(jd.HasMember("status"));
ASSERT_EQ(201, jd["status"]["code"].GetInt());
ASSERT_EQ("Created", jd["status"]["msg"]);
ASSERT_TRUE(jd.HasMember("head"));
ASSERT_EQ("/train", jd["head"]["method"]);
ASSERT_TRUE(jd["head"]["time"].GetDouble() >= 0);
ASSERT_TRUE(jd.HasMember("body"));
ASSERT_TRUE(jd["body"]["measure"].HasMember("f1"));
ASSERT_TRUE(jd["body"]["measure"]["acc"].GetDouble() >= 0.7);
ASSERT_EQ(jd["body"]["measure"]["accp"].GetDouble(),
jd["body"]["measure"]["acc"].GetDouble());
// predict with measure
std::string jpredictstr = "{\"service\":\"" + sname
+ "\",\"parameters\":{\"mllib\":{},\"output\":{"
"\"measure\":[\"f1\"]}},\"data\":[\""
+ n20_repo + "news20\"]}";
joutstr = japi.jrender(japi.service_predict(jpredictstr));
std::cout << "joutstr=" << joutstr << std::endl;
jd.Parse<rapidjson::kParseNanAndInfFlag>(joutstr.c_str());
ASSERT_TRUE(!jd.HasParseError());
ASSERT_TRUE(jd.HasMember("status"));
ASSERT_EQ(200, jd["status"]["code"].GetInt());
ASSERT_TRUE(jd.HasMember("body"));
ASSERT_TRUE(jd["body"].HasMember("measure"));
ASSERT_TRUE(jd["body"]["measure"]["f1"].GetDouble() >= 0.6);
// remove service
jstr = "{\"clear\":\"full\"}";
joutstr = japi.jrender(japi.service_delete(sname, jstr));
ASSERT_EQ(ok_str, joutstr);
rmdir(n20_repo_loc.c_str());
}
TEST(xgbapi, service_train_csv_mt_regression)
{
// create service
JsonAPI japi;
std::string sflare_repo_loc = "sflare";
mkdir(sflare_repo_loc.c_str(), 0777);
std::string sname = "my_service";
std::string jstr
= "{\"mllib\":\"xgboost\",\"description\":\"my "
"classifier\",\"type\":\"supervised\",\"model\":{\"repository\":\""
+ sflare_repo_loc
+ "\"},\"parameters\":{\"input\":{\"connector\":\"csv\"},\"mllib\":{"
"\"regression\":true,\"ntargets\":1}}}";
std::string joutstr = japi.jrender(japi.service_create(sname, jstr));
ASSERT_EQ(created_str, joutstr);
// train
std::string jtrainstr
= "{\"service\":\"" + sname
+ "\",\"async\":false,\"parameters\":{\"input\":{\"test_split\":0.1,"
"\"shuffle\":true,\"label\":[\"x_class\"],\"separator\":\",\","
"\"scale\":true,\"categoricals\":[\"class_code\",\"code_spot\","
"\"code_spot_distr\"]},\"mllib\":{\"objective\":\"reg:linear\","
"\"gpu\":true,\"iterations\":"
+ iterations_sflare
+ "},\"output\":{\"measure\":[\"eucll\"]}},\"data\":[\"" + sflare_repo
+ "flare.csv\"]}";
std::cerr << "jtrainstr=" << jtrainstr << std::endl;
joutstr = japi.jrender(japi.service_train(jtrainstr));
std::cout << "joutstr=" << joutstr << std::endl;
JDoc jd;
jd.Parse<rapidjson::kParseNanAndInfFlag>(joutstr.c_str());
ASSERT_TRUE(!jd.HasParseError());
ASSERT_TRUE(jd.HasMember("status"));
ASSERT_EQ(201, jd["status"]["code"].GetInt());
ASSERT_EQ("Created", jd["status"]["msg"]);
ASSERT_TRUE(jd.HasMember("head"));
ASSERT_EQ("/train", jd["head"]["method"]);
ASSERT_TRUE(jd["head"]["time"].GetDouble() >= 0);
ASSERT_TRUE(jd.HasMember("body"));
ASSERT_TRUE(jd["body"]["measure"].HasMember("eucll"));
ASSERT_TRUE(jd["body"]["measure"]["eucll"].GetDouble() > 0.0);
ASSERT_TRUE(jd["body"]["parameters"]["input"].HasMember("max_vals"));
ASSERT_TRUE(jd["body"]["parameters"]["input"].HasMember("min_vals"));
std::string str_min_vals
= japi.jrender(jd["body"]["parameters"]["input"]["min_vals"]);
std::string str_max_vals
= japi.jrender(jd["body"]["parameters"]["input"]["max_vals"]);
std::string str_categoricals = japi.jrender(
jd["body"]["parameters"]["input"]["categoricals_mapping"]);
std::cerr << "categoricals=" << str_categoricals << std::endl;
// predict
std::string sflare_data_head = "class_code,code_spot,code_spot_distr,act,"
"evo,prev_act,hist,reg,area,larg_area,x,y,z";
std::string sflare_data = "B,X,O,1,2,1,1,2,1,1,0,0,0";
std::string jpredictstr = "{\"service\":\"" + sname
+ "\",\"parameters\":{\"input\":{\"connector\":"
"\"csv\",\"scale\":true,\"min_vals\":"
+ str_min_vals + ",\"max_vals\":" + str_max_vals
+ ",\"categoricals_mapping\":" + str_categoricals
+ "},\"output\":{}},\"data\":[\""
+ sflare_data_head + "\",\"" + sflare_data
+ "\"]}";
joutstr = japi.jrender(japi.service_predict(jpredictstr));
std::cout << "joutstr=" << joutstr << std::endl;
jd.Parse<rapidjson::kParseNanAndInfFlag>(joutstr.c_str());
ASSERT_TRUE(!jd.HasParseError());
ASSERT_EQ(200, jd["status"]["code"]);
std::string uri = jd["body"]["predictions"][0]["uri"].GetString();
ASSERT_EQ("1", uri);
ASSERT_TRUE(
fabs(jd["body"]["predictions"][0]["vector"][0]["val"].GetDouble())
> 0.0);
// remove service
jstr = "{\"clear\":\"full\"}";
joutstr = japi.jrender(japi.service_delete(sname, jstr));
ASSERT_EQ(ok_str, joutstr);
rmdir(sflare_repo_loc.c_str());
}