/
mlmodel.h
209 lines (189 loc) · 5.1 KB
/
mlmodel.h
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
/**
* DeepDetect
* Copyright (c) 2014-2015 Emmanuel Benazera
* Author: Emmanuel Benazera <beniz@droidnik.fr>
*
* 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/>.
*/
#ifndef MODEL_H
#define MODEL_H
#ifdef USE_SIMSEARCH
#include "simsearch.h"
#endif
#include <string>
#include <fstream>
#include <iostream>
#include <unordered_map>
#include "apidata.h"
#include "utils/fileops.hpp"
#include "mllibstrategy.h"
namespace dd
{
class MLModel
{
public:
MLModel() {}
MLModel(const APIData &ad, APIData &adg,
const std::shared_ptr<spdlog::logger> &logger) {
init_repo_dir(ad);
read_config_json(adg,logger);
}
MLModel(const APIData &ad)
{
init_repo_dir(ad);
}
MLModel(const std::string &repo)
:_repo(repo) {}
MLModel(const APIData &ad, const std::string &repo)
:_repo(repo)
{
init_repo_dir(ad);
}
~MLModel() {
#ifdef USE_SIMSEARCH
delete _se;
#endif
}
void read_corresp_file()
{
if (!_corresp.empty())
{
std::ifstream ff(_corresp);
if (!ff.is_open())
std::cerr << "cannot open model corresp file=" << _corresp << std::endl;
else{
std::string line;
while(!ff.eof())
{
std::getline(ff,line);
std::string key = line.substr(0,line.find(' '));
if (!key.empty())
{
std::string value = line.substr(line.find(' ')+1);
_hcorresp.insert(std::pair<int,std::string>(std::stoi(key),value));
}
}
}
}
}
inline std::string get_hcorresp(const int &i)
{
if (_hcorresp.empty())
return std::to_string(i);
else return _hcorresp[i];
}
#ifdef USE_SIMSEARCH
/**
* \brief create similarity search engine
*/
void create_sim_search(const int &dim)
{
if (!_se)
{
_se = new SearchEngine<AnnoySE>(dim,_repo);
_se->_tse->_map_populate = _index_preload;
_se->create_index();
}
}
/**
* \brief create similarity search index
*/
void create_index()
{
if (_se)
_se->create_index();
}
/**
* \brief build similarity search index
*/
void build_index()
{
if (_se)
_se->update_index();
}
/**
* \brief remove similarity search index
*/
void remove_index()
{
if (_se)
_se->remove_index();
}
#endif
std::string _repo; /**< model repository. */
std::string _mlmodel_template_repo = "templates/";
std::unordered_map<int,std::string> _hcorresp; /**< table of class correspondences. */
std::string _corresp; /**< file name of the class correspondences (e.g. house / 23) */
std::string _best_model_filename = "/best_model.txt";
#ifdef USE_SIMSEARCH
SearchEngine<AnnoySE> *_se = nullptr;
bool _index_preload = true;
#endif
private:
void init_repo_dir(const APIData &ad)
{
// auto-creation of model directory
_repo = ad.get("repository").get<std::string>();
bool create = ad.has("create_repository") && ad.get("create_repository").get<bool>();
bool isDir;
bool exists = fileops::file_exists(_repo, isDir);
if (exists && !isDir)
throw MLLibBadParamException("file exists with same name as repository");
if (!exists && create)
fileops::create_dir(_repo,0775);
#ifdef USE_SIMSEARCH
_index_preload = ad.has("index_preload") && ad.get("index_preload").get<bool>();
#endif
// auto-install from model archive
if (ad.has("init"))
{
std::string compressedf = ad.get("init").get<std::string>();
if (fileops::uncompress(compressedf,_repo))
throw MLLibBadParamException("failed installing model from archive, check 'init' argument to model");
}
}
void read_config_json(APIData &adg,
const std::shared_ptr<spdlog::logger> &logger)
{
const std::string cf = _repo + "/config.json";
if (!fileops::file_exists(cf))
return;
std::ifstream is(cf);
std::stringstream jbuf;
jbuf << is.rdbuf();
rapidjson::Document d;
d.Parse(jbuf.str().c_str());
if (d.HasParseError())
{
logger->error("config.json parsing error on string: {}",jbuf.str());
throw MLLibBadParamException("Failed parsing config file " + cf);
}
APIData adcj;
try
{
adcj = APIData(d);
}
catch(RapidjsonException &e)
{
logger->error("JSON error {}",e.what());
throw MLLibBadParamException("Failed converting JSON file to internal data format");
}
APIData adcj_parameters = adcj.getobj("parameters");
adg.add("parameters",adcj_parameters);
}
};
}
#endif