3 #include "tbb/parallel_for.h" 19 for(
int n = 0;
n < cols; ++
n) {
21 arr.push_back(tmp_float);
28 ifstream
fin(fname.c_str());
29 fin >> m_depth >> m_rows >> m_cols;
31 for(
int d = 0;
d < m_depth; ++
d) {
32 vector<vector<float> > tmp_single_depth;
33 for(
int r = 0; r < m_rows; ++r) {
35 tmp_single_depth.push_back(tmp_row);
37 data.push_back(tmp_single_depth);
48 fin >> m_kernels_cnt >> m_depth >> m_rows >> m_cols >> m_border_mode;
49 if (m_border_mode ==
"[") { m_border_mode =
"valid"; skip =
true; }
51 cout <<
"LayerConv2D " << m_kernels_cnt
52 <<
"x" << m_depth <<
"x" << m_rows <<
"x" << m_cols <<
" border_mode " << m_border_mode << endl;
54 for(
int k = 0; k < m_kernels_cnt; ++k) {
55 vector<vector<vector<float> > > tmp_depths;
56 for(
int d = 0;
d < m_depth; ++
d) {
57 vector<vector<float> > tmp_single_depth;
58 for(
int r = 0; r < m_rows; ++r) {
59 if (!skip) { fin >> tmp_char; }
60 else { skip =
false; }
61 vector<float> tmp_row;
62 for(
int c = 0; c < m_cols; ++c) {
65 tmp_row.push_back(tmp_float);
68 tmp_single_depth.push_back(tmp_row);
70 tmp_depths.push_back(tmp_single_depth);
72 m_kernels.push_back(tmp_depths);
76 for(
int k = 0; k < m_kernels_cnt; ++k) {
78 m_bias.push_back(tmp_float);
85 fin >> m_activation_type;
86 cout <<
"Activation type " << m_activation_type << endl;
90 fin >> m_pool_x >> m_pool_y;
91 cout <<
"MaxPooling " << m_pool_x <<
"x" << m_pool_y << endl;
95 fin >> m_input_cnt >> m_neurons;
98 for(
int i = 0; i < m_input_cnt; ++i) {
101 for(
int n = 0;
n < m_neurons; ++
n) {
103 tmp_n.push_back(tmp_float);
106 m_weights.push_back(tmp_n);
108 cout <<
"weights " << m_weights.size() << endl;
110 for(
int n = 0;
n < m_neurons; ++
n) {
112 m_bias.push_back(tmp_float);
115 cout <<
"bias " << m_bias.size() << endl;
120 load_weights(input_fname);
125 vector<vector<vector<float> > > im = dc->
get_3d();
127 size_t csize = im[0].size();
128 size_t rsize = im[0][0].size();
129 size_t size = im.size() * csize * rsize;
132 for(
size_t i = 0, dst = 0; i < im.size(); ++i) {
133 for(
size_t j = 0; j < csize; ++j) {
134 float * row = im[i][j].data();
135 for(
size_t k = 0; k < rsize; ++k) {
136 y_ret[dst++] = row[k];
146 vector<vector<vector<float> > > im = dc->
get_3d();
147 vector<vector<vector<float> > > y_ret;
148 for(
unsigned int i = 0; i < im.size(); ++i) {
149 vector<vector<float> > tmp_y;
150 for(
unsigned int j = 0; j < (
unsigned int)(im[0].size()/m_pool_x); ++j) {
151 tmp_y.push_back(vector<float>((
int)(im[0][0].size()/m_pool_y), 0.0));
153 y_ret.push_back(tmp_y);
155 for(
unsigned int d = 0;
d < y_ret.size(); ++
d) {
156 for(
unsigned int x = 0;
x < y_ret[0].size(); ++
x) {
157 unsigned int start_x =
x*m_pool_x;
158 unsigned int end_x = start_x + m_pool_x;
159 for(
unsigned int y = 0;
y < y_ret[0][0].size(); ++
y) {
160 unsigned int start_y =
y*m_pool_y;
161 unsigned int end_y = start_y + m_pool_y;
163 vector<float> values;
164 for(
unsigned int i = start_x; i < end_x; ++i) {
165 for(
unsigned int j = start_y; j < end_y; ++j) {
166 values.push_back(im[
d][i][j]);
169 y_ret[
d][
x][
y] = *max_element(values.begin(), values.end());
174 out->set_data(y_ret);
179 cout <<
"Activation " << act <<
" not defined!" << endl;
180 cout <<
"Please add its implementation before use." << endl;
187 vector<vector<vector<float> > >
y = dc->
get_3d();
188 if(m_activation_type ==
"relu") {
189 for(
unsigned int i = 0; i < y.size(); ++i) {
190 for(
unsigned int j = 0; j < y[0].size(); ++j) {
191 for(
unsigned int k = 0; k < y[0][0].size(); ++k) {
192 if(y[i][j][k] < 0) y[i][j][k] = 0;
196 }
else if(m_activation_type ==
"tanh") {
197 for(
unsigned int i = 0; i < y.size(); ++i) {
198 for(
unsigned int j = 0; j < y[0].size(); ++j) {
199 for(
unsigned int k = 0; k < y[0][0].size(); ++k) {
200 y[i][j][k] = tanh(y[i][j][k]);
213 vector<float>
y = dc->
get_1d();
214 if(m_activation_type ==
"relu") {
215 for(
unsigned int k = 0; k < y.size(); ++k) {
216 if(y[k] < 0) y[k] = 0;
218 }
else if(m_activation_type ==
"softmax") {
220 for(
unsigned int k = 0; k < y.size(); ++k) {
224 for(
unsigned int k = 0; k < y.size(); ++k) {
227 }
else if(m_activation_type ==
"tanh") {
228 for(
unsigned int k = 0; k < y.size(); ++k) {
231 }
else if(m_activation_type ==
"sigmoid") {
232 for(
unsigned int k = 0; k < y.size(); ++k) {
233 y[k] = 1.0F / (1.0F + exp(-y[k]));
243 }
else {
throw "data dim not supported"; }
254 size_t k1_size = k.size(), k2_size = k[0].size();
255 unsigned int st_x = (k1_size - 1) >> 1;
256 unsigned int st_y = (k2_size - 1) >> 1;
258 for(
unsigned int i = st_x; i < im.size()-st_x; ++i) {
259 for(
unsigned int j = st_y; j < im[0].size()-st_y; ++j) {
262 for(
unsigned int k1 = 0; k1 < k1_size; ++k1) {
265 for(
unsigned int k2 = 0; k2 < k2_size; ++k2) {
267 sum += k[k1_size-k1-1][k2_size-k2-1] * im[i-st_x+k1][j-st_y+k2];
270 y[i-st_x][j-st_y] += sum;
281 size_t k1_size = k.size(), k2_size = k[0].size();
282 unsigned int st_x = (k1_size - 1) >> 1;
283 unsigned int st_y = (k2_size - 1) >> 1;
285 size_t max_imc = im.size() - 1;
286 size_t max_imr = im[0].size() - 1;
288 for(
unsigned int i = 0; i < im.size(); ++i) {
289 for(
unsigned int j = 0; j < im[0].size(); ++j) {
292 for(
unsigned int k1 = 0; k1 < k1_size; ++k1) {
295 for(
unsigned int k2 = 0; k2 < k2_size; ++k2) {
296 if(i+k1 < st_x)
continue;
297 if(i+k1 > max_imc+st_x)
continue;
298 if(j+k2 < st_y)
continue;
299 if(j+k2 > max_imr+st_y)
continue;
302 sum += k[k1_size-k1-1][k2_size-k2-1] * im[i-st_x+k1][j-st_y+k2];
311 unsigned int st_x = (m_kernels[0][0].size()-1) >> 1;
312 unsigned int st_y = (m_kernels[0][0][0].size()-1) >> 1;
313 auto const & im = dc->
get_3d();
315 size_t size_x = (m_border_mode ==
"valid")? im[0].size() - 2 * st_x : im[0].size();
316 size_t size_y = (m_border_mode ==
"valid")? im[0][0].size() - 2 * st_y: im[0][0].size();
325 tbb::parallel_for(
size_t(0),
size_t(m_kernels.size()), [&](
size_t j ) {
327 for(
unsigned int m = 0; m < im.size(); ++m) {
328 if (m_border_mode ==
"valid")
334 for(
unsigned int x = 0;
x < y_ret[0].size(); ++
x) {
336 size_t size = y_ret[0][0].size();
337 float bias = m_bias[j];
340 for(
unsigned int y = 0;
y < size/8; ++
y) {
341 y_ret[j][
x][k] += bias;
342 y_ret[j][
x][k+1] += bias;
343 y_ret[j][
x][k+2] += bias;
344 y_ret[j][
x][k+3] += bias;
345 y_ret[j][
x][k+4] += bias;
346 y_ret[j][
x][k+5] += bias;
347 y_ret[j][
x][k+6] += bias;
348 y_ret[j][
x][k+7] += bias;
351 while (k < size) { y_ret[j][
x][k] += bias; ++k; }
370 size_t size = m_weights[0].size();
371 size_t size8 = size >> 3;
376 auto const & im = dc->
get_1d();
378 for (
size_t j = 0; j < m_weights.size(); ++j) {
379 const float *
w = m_weights[j].data();
382 for (
size_t i = 0; i < size8; ++i) {
383 y_ret[k] += w[k] * p;
384 y_ret[k+1] += w[k+1] * p;
385 y_ret[k+2] += w[k+2] * p;
386 y_ret[k+3] += w[k+3] * p;
387 y_ret[k+4] += w[k+4] * p;
388 y_ret[k+5] += w[k+5] * p;
389 y_ret[k+6] += w[k+6] * p;
390 y_ret[k+7] += w[k+7] * p;
393 while (k < size) { y_ret[k] += w[k] * p; ++k; }
395 for (
size_t i = 0; i < size; ++i) {
396 y_ret[i] += m_bias[i];
410 for(
int l = 0; l < (int)m_layers.size(); ++l) {
412 out = m_layers[l]->compute_output(inp);
419 if (inp != dc)
delete inp;
427 std::vector<float> flat_out = out->
get_1d();
434 cout <<
"Reading model from " << input_fname << endl;
435 ifstream
fin(input_fname.c_str());
436 string layer_type =
"";
440 fin >> tmp_str >> m_layers_cnt;
441 cout <<
"Layers " << m_layers_cnt << endl;
443 for(
int layer = 0; layer < m_layers_cnt; ++layer) {
444 fin >> tmp_str >> tmp_int >> layer_type;
445 cout <<
"Layer " << tmp_int <<
" " << layer_type << endl;
448 if(layer_type ==
"Convolution2D") {
450 }
else if(layer_type ==
"Activation") {
452 }
else if(layer_type ==
"MaxPooling2D") {
454 }
else if(layer_type ==
"Flatten") {
456 }
else if(layer_type ==
"Dense") {
458 }
else if(layer_type ==
"Dropout") {
462 cout <<
"Layer is empty, maybe it is not defined? Cannot define network." << endl;
466 m_layers.push_back(l);
473 for(
int i = 0; i < (int)m_layers.size(); ++i) {
480 int i = m_layers.size() - 1;
481 while ((i > 0) && (m_layers[i]->get_output_units() == 0)) --i;
482 return m_layers[i]->get_output_units();
void load_weights(std::ifstream &fin)
void conv_single_depth_valid(std::vector< std::vector< float > > &y, std::vector< std::vector< float > > const &im, std::vector< std::vector< float > > const &k)
KerasModel(const std::string &input_fname)
keras::DataChunk * compute_output(keras::DataChunk *)
int get_output_length() const
virtual void set_data(std::vector< std::vector< std::vector< float > > > const &)
void load_weights(const std::string &input_fname)
virtual void load_weights(std::ifstream &fin)=0
virtual std::vector< std::vector< std::vector< float > > > const & get_3d() const
virtual size_t get_data_dim(void) const
keras::DataChunk * compute_output(keras::DataChunk *)
keras::DataChunk * compute_output(keras::DataChunk *)
std::vector< float > compute_output(keras::DataChunk *dc)
auto vector(Vector const &v)
Returns a manipulator which will print the specified array.
void read_from_file(const std::string &fname)
std::vector< std::vector< std::vector< float > > > & get_3d_rw()
keras::DataChunk * compute_output(keras::DataChunk *)
void missing_activation_impl(const std::string &act)
void load_weights(std::ifstream &fin)
keras::DataChunk * compute_output(keras::DataChunk *)
std::vector< float > & get_1d_rw()
void load_weights(std::ifstream &fin)
void load_weights(std::ifstream &fin)
virtual std::vector< float > const & get_1d() const
void conv_single_depth_same(std::vector< std::vector< float > > &y, std::vector< std::vector< float > > const &im, std::vector< std::vector< float > > const &k)
std::vector< float > read_1d_array(std::ifstream &fin, int cols)