Public Member Functions | Public Attributes | List of all members
keras::LayerConv2D Class Reference

#include <keras_model.h>

Inheritance diagram for keras::LayerConv2D:
keras::Layer

Public Member Functions

 LayerConv2D ()
 
void load_weights (std::ifstream &fin)
 
keras::DataChunkcompute_output (keras::DataChunk *)
 
virtual unsigned int get_input_rows () const
 
virtual unsigned int get_input_cols () const
 
virtual unsigned int get_output_units () const
 
- Public Member Functions inherited from keras::Layer
 Layer (std::string name)
 
virtual ~Layer ()
 
std::string get_name ()
 

Public Attributes

std::vector< std::vector< std::vector< std::vector< float > > > > m_kernels
 
std::vector< float > m_bias
 
std::string m_border_mode
 
int m_kernels_cnt
 
int m_depth
 
int m_rows
 
int m_cols
 
- Public Attributes inherited from keras::Layer
std::string m_name
 

Detailed Description

Definition at line 177 of file keras_model.h.

Constructor & Destructor Documentation

keras::LayerConv2D::LayerConv2D ( )
inline

Definition at line 179 of file keras_model.h.

179 : Layer("Conv2D") {}
Layer(std::string name)
Definition: keras_model.h:124

Member Function Documentation

keras::DataChunk * keras::LayerConv2D::compute_output ( keras::DataChunk dc)
virtual

Implements keras::Layer.

Definition at line 307 of file keras_model.cc.

307  {
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;
310  auto const & im = dc->get_3d();
311 
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();
314 
315  // depth rows cols
316  keras::DataChunk2D *out = new keras::DataChunk2D(m_kernels.size(), size_x, size_y, 0);
317  auto & y_ret = out->get_3d_rw();
318 
319  //auto t1 = std::chrono::high_resolution_clock::now();
320 
321  // Parallelize the kernal calculation
322  tbb::parallel_for( size_t(0), size_t(m_kernels.size()), [&]( size_t j ) {
323 
324  for(unsigned int m = 0; m < im.size(); ++m) { // loop over image depth
325  if (m_border_mode == "valid")
326  keras::conv_single_depth_valid(y_ret[j], im[m], m_kernels[j][m]);
327  else
328  keras::conv_single_depth_same(y_ret[j], im[m], m_kernels[j][m]);
329  }
330 
331  for(unsigned int x = 0; x < y_ret[0].size(); ++x) {
332 
333  size_t size = y_ret[0][0].size();
334  float bias = m_bias[j];
335  size_t k = 0;
336 
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;
346  k += 8;
347  }
348  while (k < size) { y_ret[j][x][k] += bias; ++k; }
349 
350  }
351  });
352 
353  //auto t2 = std::chrono::high_resolution_clock::now();
354  /*
355  cout << "Parallal : "
356  << std::chrono::duration_cast<std::chrono::microseconds>(t2-t1).count()
357  << " microseconds." << endl;
358  */
359 
360  return out;
361 }
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)
Definition: keras_model.cc:246
std::vector< std::vector< std::vector< std::vector< float > > > > m_kernels
Definition: keras_model.h:183
virtual std::vector< std::vector< std::vector< float > > > const & get_3d() const
Definition: keras_model.h:46
decltype(auto) constexpr size(T &&obj)
ADL-aware version of std::size.
Definition: StdUtils.h:92
std::vector< float > m_bias
Definition: keras_model.h:184
std::string m_border_mode
Definition: keras_model.h:190
std::vector< std::vector< std::vector< float > > > & get_3d_rw()
Definition: keras_model.h:62
list x
Definition: train.py:276
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)
Definition: keras_model.cc:273
virtual unsigned int keras::LayerConv2D::get_input_cols ( ) const
inlinevirtual

Implements keras::Layer.

Definition at line 187 of file keras_model.h.

187 { return m_cols; }
virtual unsigned int keras::LayerConv2D::get_input_rows ( ) const
inlinevirtual

Implements keras::Layer.

Definition at line 186 of file keras_model.h.

186 { return m_rows; }
virtual unsigned int keras::LayerConv2D::get_output_units ( ) const
inlinevirtual

Implements keras::Layer.

Definition at line 188 of file keras_model.h.

188 { return m_kernels_cnt; }
void keras::LayerConv2D::load_weights ( std::ifstream &  fin)
virtual

Implements keras::Layer.

Definition at line 40 of file keras_model.cc.

40  {
41  char tmp_char = ' ';
42  string tmp_str = "";
43  float tmp_float;
44  bool skip = false;
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; }
47 
48  cout << "LayerConv2D " << m_kernels_cnt
49  << "x" << m_depth << "x" << m_rows << "x" << m_cols << " border_mode " << m_border_mode << endl;
50  // reading kernel weights
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; } // for '['
57  else { skip = false; }
58  vector<float> tmp_row;
59  for(int c = 0; c < m_cols; ++c) {
60  fin >> tmp_float;
61  //cout << tmp_float << " ";
62  tmp_row.push_back(tmp_float);
63  }
64  fin >> tmp_char; // for ']'
65  tmp_single_depth.push_back(tmp_row);
66  }
67  tmp_depths.push_back(tmp_single_depth);
68  }
69  m_kernels.push_back(tmp_depths);
70  }
71  // reading kernel biases
72  fin >> tmp_char; // for '['
73  for(int k = 0; k < m_kernels_cnt; ++k) {
74  fin >> tmp_float;
75  m_bias.push_back(tmp_float);
76  }
77  fin >> tmp_char; // for ']'
78 
79 }
std::vector< std::vector< std::vector< std::vector< float > > > > m_kernels
Definition: keras_model.h:183
std::vector< float > m_bias
Definition: keras_model.h:184
std::string m_border_mode
Definition: keras_model.h:190
QTextStream & endl(QTextStream &s)

Member Data Documentation

std::vector<float> keras::LayerConv2D::m_bias

Definition at line 184 of file keras_model.h.

std::string keras::LayerConv2D::m_border_mode

Definition at line 190 of file keras_model.h.

int keras::LayerConv2D::m_cols

Definition at line 194 of file keras_model.h.

int keras::LayerConv2D::m_depth

Definition at line 192 of file keras_model.h.

std::vector<std::vector<std::vector<std::vector<float> > > > keras::LayerConv2D::m_kernels

Definition at line 183 of file keras_model.h.

int keras::LayerConv2D::m_kernels_cnt

Definition at line 191 of file keras_model.h.

int keras::LayerConv2D::m_rows

Definition at line 193 of file keras_model.h.


The documentation for this class was generated from the following files: