forked from serizba/cppflow
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathModel.cpp
More file actions
261 lines (192 loc) · 7.86 KB
/
Model.cpp
File metadata and controls
261 lines (192 loc) · 7.86 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
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
//
// Created by sergio on 12/05/19.
//
#include "../include/Model.h"
Model::Model(const std::string& model_filename) {
this->status = TF_NewStatus();
this->graph = TF_NewGraph();
// Create the session.
TF_SessionOptions* sess_opts = TF_NewSessionOptions();
this->session = TF_NewSession(this->graph, sess_opts, this->status);
TF_DeleteSessionOptions(sess_opts);
// Check the status
this->status_check(true);
// Create the graph
TF_Graph* g = this->graph;
// Import the graph definition
TF_Buffer* def = read(model_filename);
this->error_check(def != nullptr, "An error occurred reading the model");
TF_ImportGraphDefOptions* graph_opts = TF_NewImportGraphDefOptions();
TF_GraphImportGraphDef(g, def, graph_opts, this->status);
TF_DeleteImportGraphDefOptions(graph_opts);
TF_DeleteBuffer(def);
this->status_check(true);
}
Model::~Model() {
TF_DeleteSession(this->session, this->status);
TF_DeleteGraph(this->graph);
this->status_check(true);
TF_DeleteStatus(this->status);
}
void Model::init() {
TF_Operation* init_op[1] = {TF_GraphOperationByName(this->graph, "init")};
this->error_check(init_op[0]!= nullptr, "Error: No operation named \"init\" exists");
TF_SessionRun(this->session, nullptr, nullptr, nullptr, 0, nullptr, nullptr, 0, init_op, 1, nullptr, this->status);
this->status_check(true);
}
void Model::save(const std::string &ckpt) {
// Encode file_name to tensor
size_t size = 8 + TF_StringEncodedSize(ckpt.length());
TF_Tensor* t = TF_AllocateTensor(TF_STRING, nullptr, 0, size);
char* data = static_cast<char *>(TF_TensorData(t));
for (int i=0; i<8; i++) {data[i]=0;}
TF_StringEncode(ckpt.c_str(), ckpt.size(), data + 8, size - 8, status);
memset(data, 0, 8); // 8-byte offset of first string.
TF_StringEncode(ckpt.c_str(), ckpt.length(), (char*)(data + 8), size - 8, status);
// Check errors
if (!this->status_check(false)) {
TF_DeleteTensor(t);
std::cerr << "Error during filename " << ckpt << " encoding" << std::endl;
this->status_check(true);
}
TF_Output output_file;
output_file.oper = TF_GraphOperationByName(this->graph, "save/Const");
output_file.index = 0;
TF_Output inputs[1] = {output_file};
TF_Tensor* input_values[1] = {t};
const TF_Operation* restore_op[1] = {TF_GraphOperationByName(this->graph, "save/control_dependency")};
if (!restore_op[0]) {
TF_DeleteTensor(t);
this->error_check(false, "Error: No operation named \"save/control_dependencyl\" exists");
}
TF_SessionRun(this->session, nullptr, inputs, input_values, 1, nullptr, nullptr, 0, restore_op, 1, nullptr, this->status);
TF_DeleteTensor(t);
this->status_check(true);
}
void Model::restore(const std::string& ckpt) {
// Encode file_name to tensor
size_t size = 8 + TF_StringEncodedSize(ckpt.size());
TF_Tensor* t = TF_AllocateTensor(TF_STRING, nullptr, 0, size);
char* data = static_cast<char *>(TF_TensorData(t));
for (int i=0; i<8; i++) {data[i]=0;}
TF_StringEncode(ckpt.c_str(), ckpt.size(), data + 8, size - 8, status);
// Check errors
if (!this->status_check(false)) {
TF_DeleteTensor(t);
std::cerr << "Error during filename " << ckpt << " encoding" << std::endl;
this->status_check(true);
}
TF_Output output_file;
output_file.oper = TF_GraphOperationByName(this->graph, "save/Const");
output_file.index = 0;
TF_Output inputs[1] = {output_file};
TF_Tensor* input_values[1] = {t};
const TF_Operation* restore_op[1] = {TF_GraphOperationByName(this->graph, "save/restore_all")};
if (!restore_op[0]) {
TF_DeleteTensor(t);
this->error_check(false, "Error: No operation named \"save/restore_all\" exists");
}
TF_SessionRun(this->session, nullptr, inputs, input_values, 1, nullptr, nullptr, 0, restore_op, 1, nullptr, this->status);
TF_DeleteTensor(t);
this->status_check(true);
}
TF_Buffer *Model::read(const std::string& filename) {
std::ifstream file (filename, std::ios::binary | std::ios::ate);
// Error opening the file
if (!file.is_open()) {
std::cerr << "Unable to open file: " << filename << std::endl;
return nullptr;
}
// Cursor is at the end to get size
auto size = file.tellg();
// Move cursor to the beginning
file.seekg (0, std::ios::beg);
// Read
auto data = new char [size];
file.seekg (0, std::ios::beg);
file.read (data, size);
// Error reading the file
if (!file) {
std::cerr << "Unable to read the full file: " << filename << std::endl;
return nullptr;
}
// Create tensorflow buffer from read data
TF_Buffer* buffer = TF_NewBufferFromString(data, size);
// Close file and remove data
file.close();
delete[] data;
return buffer;
}
std::vector<std::string> Model::get_operations() const {
std::vector<std::string> result;
size_t pos = 0;
TF_Operation* oper;
// Iterate through the operations of a graph
while ((oper = TF_GraphNextOperation(this->graph, &pos)) != nullptr) {
result.emplace_back(TF_OperationName(oper));
}
return result;
}
void Model::run(const std::vector<Tensor*>& inputs, const std::vector<Tensor*>& outputs) {
this->error_check(std::all_of(inputs.begin(), inputs.end(), [](const Tensor* i){return i->flag == 1;}),
"Error: Not all elements from the inputs are full");
this->error_check(std::all_of(outputs.begin(), outputs.end(), [](const Tensor* o){return o->flag != -1;}),
"Error: Not all outputs Tensors are valid");
// Clean previous stored outputs
std::for_each(outputs.begin(), outputs.end(), [](Tensor* o){o->clean();});
// Get input operations
std::vector<TF_Output> io(inputs.size());
std::transform(inputs.begin(), inputs.end(), io.begin(), [](const Tensor* i) {return i->op;});
// Get input values
std::vector<TF_Tensor*> iv(inputs.size());
std::transform(inputs.begin(), inputs.end(), iv.begin(), [](const Tensor* i) {return i->val;});
// Get output operations
std::vector<TF_Output> oo(outputs.size());
std::transform(outputs.begin(), outputs.end(), oo.begin(), [](const Tensor* o) {return o->op;});
// Prepare output recipients
auto ov = new TF_Tensor*[outputs.size()];
TF_SessionRun(this->session, nullptr, io.data(), iv.data(), inputs.size(), oo.data(), ov, outputs.size(), nullptr, 0, nullptr, this->status);
this->status_check(true);
// Save results on outputs and mark as full
for (std::size_t i=0; i<outputs.size(); i++) {
outputs[i]->val = ov[i];
outputs[i]->flag = 1;
outputs[i]->deduce_shape();
}
// Mark input as empty
std::for_each(inputs.begin(), inputs.end(), [] (Tensor* i) {i->clean();});
delete[] ov;
}
void Model::run(Tensor &input, Tensor &output) {
this->run(&input, &output);
}
void Model::run(const std::vector<Tensor*> &inputs, Tensor &output) {
this->run(inputs, &output);
}
void Model::run(Tensor &input, const std::vector<Tensor*> &outputs) {
this->run(&input, outputs);
}
void Model::run(Tensor *input, Tensor *output) {
this->run(std::vector<Tensor*>({input}), std::vector<Tensor*>({output}));
}
void Model::run(const std::vector<Tensor*> &inputs, Tensor *output) {
this->run(inputs, std::vector<Tensor*>({output}));
}
void Model::run(Tensor *input, const std::vector<Tensor*> &outputs) {
this->run(std::vector<Tensor*>({input}), outputs);
}
bool Model::status_check(bool throw_exc) const {
if (TF_GetCode(this->status) != TF_OK) {
if (throw_exc) {
throw std::runtime_error(TF_Message(status));
} else {
return false;
}
}
return true;
}
void Model::error_check(bool condition, const std::string &error) const {
if (!condition) {
throw std::runtime_error(error);
}
}