3 #include "tbb/parallel_for.h" 16 for(
int n = 0;
n < cols; ++
n) {
18 arr.push_back(tmp_float);
25 ifstream fin(fname.c_str());
26 fin >> m_depth >> m_rows >> m_cols;
28 for(
int d = 0;
d < m_depth; ++
d) {
29 vector<vector<float> > tmp_single_depth;
30 for(
int r = 0;
r < m_rows; ++
r) {
32 tmp_single_depth.push_back(tmp_row);
34 data.push_back(tmp_single_depth);
45 fin >> m_kernels_cnt >> m_depth >> m_rows >> m_cols >> m_border_mode;
46 if (m_border_mode ==
"[") { m_border_mode =
"valid"; skip =
true; }
48 cout <<
"LayerConv2D " << m_kernels_cnt
49 <<
"x" << m_depth <<
"x" << m_rows <<
"x" << m_cols <<
" border_mode " << m_border_mode <<
endl;
51 for(
int k = 0;
k < m_kernels_cnt; ++
k) {
52 vector<vector<vector<float> > > tmp_depths;
53 for(
int d = 0;
d < m_depth; ++
d) {
54 vector<vector<float> > tmp_single_depth;
55 for(
int r = 0;
r < m_rows; ++
r) {
56 if (!skip) { fin >> tmp_char; }
57 else { skip =
false; }
58 vector<float> tmp_row;
59 for(
int c = 0;
c < m_cols; ++
c) {
62 tmp_row.push_back(tmp_float);
65 tmp_single_depth.push_back(tmp_row);
67 tmp_depths.push_back(tmp_single_depth);
69 m_kernels.push_back(tmp_depths);
73 for(
int k = 0;
k < m_kernels_cnt; ++
k) {
75 m_bias.push_back(tmp_float);
82 fin >> m_activation_type;
83 cout <<
"Activation type " << m_activation_type <<
endl;
87 fin >> m_pool_x >> m_pool_y;
88 cout <<
"MaxPooling " << m_pool_x <<
"x" << m_pool_y <<
endl;
92 fin >> m_input_cnt >> m_neurons;
95 for(
int i = 0; i < m_input_cnt; ++i) {
98 for(
int n = 0;
n < m_neurons; ++
n) {
100 tmp_n.push_back(tmp_float);
103 m_weights.push_back(tmp_n);
105 cout <<
"weights " << m_weights.size() <<
endl;
107 for(
int n = 0;
n < m_neurons; ++
n) {
109 m_bias.push_back(tmp_float);
112 cout <<
"bias " << m_bias.size() <<
endl;
117 load_weights(input_fname);
122 vector<vector<vector<float> > >
im = dc->
get_3d();
124 size_t csize = im[0].size();
125 size_t rsize = im[0][0].size();
126 size_t size = im.size() * csize * rsize;
129 for(
size_t i = 0, dst = 0; i < im.size(); ++i) {
130 for(
size_t j = 0; j < csize; ++j) {
131 float *
row = im[i][j].data();
132 for(
size_t k = 0;
k < rsize; ++
k) {
133 y_ret[dst++] = row[
k];
143 vector<vector<vector<float> > >
im = dc->
get_3d();
144 vector<vector<vector<float> > > y_ret;
145 for(
unsigned int i = 0; i < im.size(); ++i) {
146 vector<vector<float> > tmp_y;
147 for(
unsigned int j = 0; j < (
unsigned int)(im[0].
size()/m_pool_x); ++j) {
148 tmp_y.push_back(vector<float>((
int)(im[0][0].
size()/m_pool_y), 0.0));
150 y_ret.push_back(tmp_y);
152 for(
unsigned int d = 0;
d < y_ret.size(); ++
d) {
153 for(
unsigned int x = 0;
x < y_ret[0].size(); ++
x) {
154 unsigned int start_x =
x*m_pool_x;
155 unsigned int end_x = start_x + m_pool_x;
156 for(
unsigned int y = 0;
y < y_ret[0][0].size(); ++
y) {
157 unsigned int start_y =
y*m_pool_y;
158 unsigned int end_y = start_y + m_pool_y;
161 for(
unsigned int i = start_x; i < end_x; ++i) {
162 for(
unsigned int j = start_y; j < end_y; ++j) {
163 values.push_back(im[
d][i][j]);
166 y_ret[
d][
x][
y] = *max_element(values.begin(), values.end());
171 out->set_data(y_ret);
176 cout <<
"Activation " << act <<
" not defined!" <<
endl;
177 cout <<
"Please add its implementation before use." <<
endl;
184 vector<vector<vector<float> > >
y = dc->
get_3d();
185 if(m_activation_type ==
"relu") {
186 for(
unsigned int i = 0; i < y.size(); ++i) {
187 for(
unsigned int j = 0; j < y[0].size(); ++j) {
188 for(
unsigned int k = 0;
k < y[0][0].size(); ++
k) {
189 if(y[i][j][
k] < 0) y[i][j][
k] = 0;
193 }
else if(m_activation_type ==
"tanh") {
194 for(
unsigned int i = 0; i < y.size(); ++i) {
195 for(
unsigned int j = 0; j < y[0].size(); ++j) {
196 for(
unsigned int k = 0;
k < y[0][0].size(); ++
k) {
197 y[i][j][
k] = tanh(y[i][j][
k]);
210 vector<float>
y = dc->
get_1d();
211 if(m_activation_type ==
"relu") {
212 for(
unsigned int k = 0;
k < y.size(); ++
k) {
213 if(y[
k] < 0) y[
k] = 0;
215 }
else if(m_activation_type ==
"softmax") {
217 for(
unsigned int k = 0;
k < y.size(); ++
k) {
221 for(
unsigned int k = 0;
k < y.size(); ++
k) {
224 }
else if(m_activation_type ==
"tanh") {
225 for(
unsigned int k = 0;
k < y.size(); ++
k) {
228 }
else if(m_activation_type ==
"sigmoid") {
229 for(
unsigned int k = 0;
k < y.size(); ++
k) {
230 y[
k] = 1.0F / (1.0F + exp(-y[
k]));
240 }
else {
throw "data dim not supported"; }
251 size_t k1_size =
k.size(), k2_size =
k[0].size();
252 unsigned int st_x = (k1_size - 1) >> 1;
253 unsigned int st_y = (k2_size - 1) >> 1;
255 for(
unsigned int i = st_x; i <
im.size()-st_x; ++i) {
256 for(
unsigned int j = st_y; j <
im[0].size()-st_y; ++j) {
259 for(
unsigned int k1 = 0; k1 < k1_size; ++k1) {
262 for(
unsigned int k2 = 0; k2 < k2_size; ++k2) {
264 sum +=
k[k1_size-k1-1][k2_size-k2-1] *
im[i-st_x+k1][j-st_y+k2];
267 y[i-st_x][j-st_y] += sum;
278 size_t k1_size =
k.size(), k2_size =
k[0].size();
279 unsigned int st_x = (k1_size - 1) >> 1;
280 unsigned int st_y = (k2_size - 1) >> 1;
282 size_t max_imc =
im.size() - 1;
283 size_t max_imr =
im[0].size() - 1;
285 for(
unsigned int i = 0; i <
im.size(); ++i) {
286 for(
unsigned int j = 0; j <
im[0].size(); ++j) {
289 for(
unsigned int k1 = 0; k1 < k1_size; ++k1) {
292 for(
unsigned int k2 = 0; k2 < k2_size; ++k2) {
293 if(i+k1 < st_x)
continue;
294 if(i+k1 > max_imc+st_x)
continue;
295 if(j+k2 < st_y)
continue;
296 if(j+k2 > max_imr+st_y)
continue;
299 sum +=
k[k1_size-k1-1][k2_size-k2-1] *
im[i-st_x+k1][j-st_y+k2];
308 unsigned int st_x = (m_kernels[0][0].size()-1) >> 1;
309 unsigned int st_y = (m_kernels[0][0][0].size()-1) >> 1;
312 size_t size_x = (m_border_mode ==
"valid")?
im[0].
size() - 2 * st_x :
im[0].size();
313 size_t size_y = (m_border_mode ==
"valid")?
im[0][0].
size() - 2 * st_y:
im[0][0].size();
322 tbb::parallel_for(
size_t(0),
size_t(m_kernels.size()), [&](
size_t j ) {
324 for(
unsigned int m = 0;
m <
im.size(); ++
m) {
325 if (m_border_mode ==
"valid")
331 for(
unsigned int x = 0;
x < y_ret[0].size(); ++
x) {
333 size_t size = y_ret[0][0].size();
334 float bias = m_bias[j];
337 for(
unsigned int y = 0;
y < size/8; ++
y) {
338 y_ret[j][
x][
k] += bias;
339 y_ret[j][
x][k+1] += bias;
340 y_ret[j][
x][k+2] += bias;
341 y_ret[j][
x][k+3] += bias;
342 y_ret[j][
x][k+4] += bias;
343 y_ret[j][
x][k+5] += bias;
344 y_ret[j][
x][k+6] += bias;
345 y_ret[j][
x][k+7] += bias;
348 while (k < size) { y_ret[j][
x][
k] += bias; ++
k; }
367 size_t size = m_weights[0].size();
368 size_t size8 = size >> 3;
375 for (
size_t j = 0; j < m_weights.size(); ++j) {
376 const float *
w = m_weights[j].data();
379 for (
size_t i = 0; i < size8; ++i) {
380 y_ret[
k] += w[
k] *
p;
381 y_ret[k+1] += w[k+1] *
p;
382 y_ret[k+2] += w[k+2] *
p;
383 y_ret[k+3] += w[k+3] *
p;
384 y_ret[k+4] += w[k+4] *
p;
385 y_ret[k+5] += w[k+5] *
p;
386 y_ret[k+6] += w[k+6] *
p;
387 y_ret[k+7] += w[k+7] *
p;
390 while (k < size) { y_ret[
k] += w[
k] *
p; ++
k; }
392 for (
size_t i = 0; i <
size; ++i) {
393 y_ret[i] += m_bias[i];
407 for(
int l = 0;
l < (
int)m_layers.size(); ++
l) {
409 out = m_layers[
l]->compute_output(inp);
416 if (inp != dc)
delete inp;
424 std::vector<float> flat_out = out->
get_1d();
431 cout <<
"Reading model from " << input_fname <<
endl;
432 ifstream fin(input_fname.c_str());
433 string layer_type =
"";
437 fin >> tmp_str >> m_layers_cnt;
438 cout <<
"Layers " << m_layers_cnt <<
endl;
440 for(
int layer = 0; layer < m_layers_cnt; ++layer) {
441 fin >> tmp_str >> tmp_int >> layer_type;
442 cout <<
"Layer " << tmp_int <<
" " << layer_type <<
endl;
445 if(layer_type ==
"Convolution2D") {
447 }
else if(layer_type ==
"Activation") {
449 }
else if(layer_type ==
"MaxPooling2D") {
451 }
else if(layer_type ==
"Flatten") {
453 }
else if(layer_type ==
"Dense") {
455 }
else if(layer_type ==
"Dropout") {
459 cout <<
"Layer is empty, maybe it is not defined? Cannot define network." <<
endl;
463 m_layers.push_back(l);
470 for(
int i = 0; i < (
int)m_layers.size(); ++i) {
477 int i = m_layers.size() - 1;
478 while ((i > 0) && (m_layers[i]->get_output_units() == 0)) --i;
479 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)
decltype(auto) constexpr size(T &&obj)
ADL-aware version of std::size.
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)
QTextStream & endl(QTextStream &s)