forked from TheFoundryVisionmongers/nuke-ML-server
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMLClientModelManager.cpp
348 lines (306 loc) · 9.78 KB
/
MLClientModelManager.cpp
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
348
// Copyright (c) 2019 Foundry.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//*************************************************************************
#include "MLClientModelManager.h"
#include "DDImage/Knob.h"
#include "MLClient.h"
MLClientModelKnob::MLClientModelKnob(DD::Image::Knob_Closure* kc, DD::Image::Op* op, const char* name)
: DD::Image::Knob(kc, name)
, _op(op)
, _model("")
{ }
const char* MLClientModelKnob::Class() const
{
return "MLClientModelKnob";
}
bool MLClientModelKnob::not_default () const
{
// Always flag as not default, so it's always serialised.
return true;
}
std::string MLClientModelKnob::getModel() const
{
return _model;
}
const std::map<std::string, std::string>& MLClientModelKnob::getParameters() const
{
return _parameters;
}
void MLClientModelKnob::to_script (std::ostream &out, const DD::Image::OutputContext *, bool quote) const
{
std::string saveString;
std::stringstream ss;
if (_op != nullptr) {
DD::Image::Knob* k = _op->knob("models");
if(k != nullptr) {
const int modelIndex = k->get_value();
DD::Image::Enumeration_KnobI* eKnob = k->enumerationKnob();
if(eKnob != nullptr) {
ss << "model:" << eKnob->getItemValueString(modelIndex) << ";";
MLClient* mlClient = dynamic_cast<MLClient*>(_op);
if(mlClient != nullptr) {
MLClientModelManager& mlManager = mlClient->getModelManager();
toScriptT(mlManager, ss, &MLClientModelManager::getNumOfInts, &MLClientModelManager::getDynamicIntName);
toScriptT(mlManager, ss, &MLClientModelManager::getNumOfFloats, &MLClientModelManager::getDynamicFloatName);
toScriptT(mlManager, ss, &MLClientModelManager::getNumOfBools, &MLClientModelManager::getDynamicBoolName);
toScriptStrings(mlManager, ss);
}
}
}
}
saveString = ss.str();
if(quote) {
saveString.insert(saveString.begin(),'{');
saveString+='}';
}
out << saveString;
}
bool MLClientModelKnob::from_script(const char * src)
{
std::string loadString(src);
if ((_op != nullptr) && (loadString!="")) {
bool success = false;
// We parse the serialised string to extract the pairs of key:val;
const std::string delimiter = ";";
const std::string keyValDelimiter = ":";
_parameters.clear();
size_t pos = 0;
std::string token;
while ((pos = loadString.find(delimiter)) != std::string::npos) {
token = loadString.substr(0, pos);
std::cout << token << std::endl;
// We further split the key:value pair
std::string key = token.substr(0, token.find(keyValDelimiter));
std::string val = token.substr(token.find(keyValDelimiter) + keyValDelimiter.length(), token.length() - key.length() - keyValDelimiter.length());
if(key == "model") {
_model = val;
} else {
_parameters.insert(std::make_pair(key, val));
}
loadString.erase(0, pos + delimiter.length());
}
return success;
}
return true;
}
void MLClientModelKnob::toScriptT(MLClientModelManager& mlManager, std::ostream &out,
int (MLClientModelManager::*getNum)() const,
std::string (MLClientModelManager::*getDynamicName)(int)) const
{
const int num = (mlManager.*getNum)();
for(int i = 0; i < num; i++) {
DD::Image::Knob* k = _op->knob((mlManager.*getDynamicName)(i).c_str());
if(k != nullptr) {
std::stringstream ss;
k->to_script(ss, nullptr, false);
out << (mlManager.*getDynamicName)(i) << ":" << ss.str() << ";";
}
}
}
void MLClientModelKnob::toScriptStrings(MLClientModelManager& mlManager, std::ostream &out) const
{
const int numFloats = mlManager.getNumOfStrings();
for(int i = 0; i < numFloats; i++) {
DD::Image::Knob* k = _op->knob(mlManager.getDynamicStringName(i).c_str());
if(k != nullptr) {
out << mlManager.getDynamicStringName(i) << ":" << k->get_text() << ";";
}
}
}
MLClientModelManager::MLClientModelManager(DD::Image::Op* parent)
: _parent(parent)
{ }
MLClientModelManager::~MLClientModelManager()
{ }
//! Parse options from the server model /m to the MLClientModelManager
void MLClientModelManager::parseOptions(const mlserver::Model& m)
{
clear();
for (int i = 0, endI = m.bool_options_size(); i < endI; i++) {
mlserver::BoolAttrib option;
option = m.bool_options(i);
if (option.values(0)) {
_dynamicBoolValues.push_back(1);
}
else {
_dynamicBoolValues.push_back(0);
}
_dynamicBoolNames.push_back(option.name());
}
for (int i = 0, endI = m.int_options_size(); i < endI; i++) {
mlserver::IntAttrib option;
option = m.int_options(i);
_dynamicIntValues.push_back(option.values(0));
_dynamicIntNames.push_back(option.name());
}
for (int i = 0, endI = m.float_options_size(); i < endI; i++) {
mlserver::FloatAttrib option;
option = m.float_options(i);
_dynamicFloatValues.push_back(option.values(0));
_dynamicFloatNames.push_back(option.name());
}
for (int i = 0, endI = m.string_options_size(); i < endI; i++) {
mlserver::StringAttrib option;
option = m.string_options(i);
_dynamicStringValues.push_back(option.values(0));
_dynamicStringNames.push_back(option.name());
}
for (int i = 0, endI = m.button_options_size(); i < endI; i++) {
mlserver::BoolAttrib option;
option = m.button_options(i);
if (option.values(0)) {
_dynamicButtonValues.push_back(1);
}
else {
_dynamicButtonValues.push_back(0);
}
_dynamicButtonNames.push_back(option.name());
}
}
//! Use current knob values to update options on the server model /m
//! in order to later request inference on this model
void MLClientModelManager::updateOptions(mlserver::Model& m)
{
m.clear_bool_options();
for (int i = 0; i < _dynamicBoolValues.size(); i++) {
mlserver::BoolAttrib* option = m.add_bool_options();
option->set_name(_dynamicBoolNames[i]);
DD::Image::Knob* k = _parent->knob(_dynamicBoolNames[i].c_str());
bool val = false;
if (k != nullptr) {
val = k->get_value();
}
option->add_values(val);
}
m.clear_int_options();
for (int i = 0; i < _dynamicIntValues.size(); i++) {
mlserver::IntAttrib* option = m.add_int_options();
option->set_name(_dynamicIntNames[i]);
DD::Image::Knob* k = _parent->knob(_dynamicIntNames[i].c_str());
int val = 0;
if (k != nullptr) {
val = k->get_value();
}
option->add_values(val);
}
m.clear_float_options();
for (int i = 0; i < _dynamicFloatValues.size(); i++) {
mlserver::FloatAttrib* option = m.add_float_options();
option->set_name(_dynamicFloatNames[i]);
DD::Image::Knob* k = _parent->knob(_dynamicFloatNames[i].c_str());
float val = 0.0f;
if (k != nullptr) {
val = k->get_value();
}
option->add_values(val);
}
m.clear_string_options();
for (int i = 0; i < _dynamicStringValues.size(); i++) {
mlserver::StringAttrib* option = m.add_string_options();
option->set_name(_dynamicStringNames[i]);
DD::Image::Knob* k = _parent->knob(_dynamicStringNames[i].c_str());
const char* val = "";
if(k != nullptr) {
val = k->get_text();
if (val==nullptr) {
val = "";
}
}
option->add_values(val);
}
m.clear_button_options();
for (int i = 0; i < _dynamicButtonValues.size(); i++) {
mlserver::BoolAttrib* option = m.add_button_options();
option->set_name(_dynamicButtonNames[i]);
// Get member value instead of knob value to catch button push
option->add_values(_dynamicButtonValues[i]);
}
}
int MLClientModelManager::getNumOfFloats() const
{
return _dynamicFloatValues.size();
}
int MLClientModelManager::getNumOfInts() const
{
return _dynamicIntValues.size();
}
int MLClientModelManager::getNumOfBools() const
{
return _dynamicBoolValues.size();
}
int MLClientModelManager::getNumOfStrings() const
{
return _dynamicStringValues.size();
}
int MLClientModelManager::getNumOfButtons() const
{
return _dynamicButtonValues.size();
}
std::string MLClientModelManager::getDynamicBoolName(int idx)
{
return _dynamicBoolNames[idx];
}
std::string MLClientModelManager::getDynamicFloatName(int idx)
{
return _dynamicFloatNames[idx];
}
std::string MLClientModelManager::getDynamicIntName(int idx)
{
return _dynamicIntNames[idx];
}
std::string MLClientModelManager::getDynamicStringName(int idx)
{
return _dynamicStringNames[idx];
}
std::string MLClientModelManager::getDynamicButtonName(int idx)
{
return _dynamicButtonNames[idx];
}
float* MLClientModelManager::getDynamicFloatValue(int idx)
{
return &_dynamicFloatValues[idx];
}
int* MLClientModelManager::getDynamicIntValue(int idx)
{
return &_dynamicIntValues[idx];
}
bool* MLClientModelManager::getDynamicBoolValue(int idx)
{
return (bool*)&_dynamicBoolValues[idx];
}
std::string* MLClientModelManager::getDynamicStringValue(int idx)
{
return &_dynamicStringValues[idx];
}
bool* MLClientModelManager::getDynamicButtonValue(int idx)
{
return (bool*)&_dynamicButtonValues[idx];
}
void MLClientModelManager::setDynamicButtonValue(int idx, int value)
{
_dynamicButtonValues[idx] = value;
}
void MLClientModelManager::clear()
{
_dynamicBoolValues.clear();
_dynamicIntValues.clear();
_dynamicFloatValues.clear();
_dynamicStringValues.clear();
_dynamicButtonValues.clear();
_dynamicBoolNames.clear();
_dynamicIntNames.clear();
_dynamicFloatNames.clear();
_dynamicStringNames.clear();
_dynamicButtonNames.clear();
}