LArSoft  v07_13_02
Liquid Argon Software toolkit - http://larsoft.org/
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
 
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
 
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.

References fin.

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 310 of file keras_model.cc.

References keras::conv_single_depth_same(), keras::conv_single_depth_valid(), keras::DataChunk::get_3d(), keras::DataChunk2D::get_3d_rw(), x, and y.

310  {
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();
314 
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();
317 
318  // depth rows cols
319  keras::DataChunk2D *out = new keras::DataChunk2D(m_kernels.size(), size_x, size_y, 0);
320  auto & y_ret = out->get_3d_rw();
321 
322  //auto t1 = std::chrono::high_resolution_clock::now();
323 
324  // Parallelize the kernal calculation
325  tbb::parallel_for( size_t(0), size_t(m_kernels.size()), [&]( size_t j ) {
326 
327  for(unsigned int m = 0; m < im.size(); ++m) { // loop over image depth
328  if (m_border_mode == "valid")
329  keras::conv_single_depth_valid(y_ret[j], im[m], m_kernels[j][m]);
330  else
331  keras::conv_single_depth_same(y_ret[j], im[m], m_kernels[j][m]);
332  }
333 
334  for(unsigned int x = 0; x < y_ret[0].size(); ++x) {
335 
336  size_t size = y_ret[0][0].size();
337  float bias = m_bias[j];
338  size_t k = 0;
339 
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;
349  k += 8;
350  }
351  while (k < size) { y_ret[j][x][k] += bias; ++k; }
352 
353  }
354  });
355 
356  //auto t2 = std::chrono::high_resolution_clock::now();
357  /*
358  cout << "Parallal : "
359  << std::chrono::duration_cast<std::chrono::microseconds>(t2-t1).count()
360  << " microseconds." << endl;
361  */
362 
363  return out;
364 }
Float_t x
Definition: compare.C:6
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:249
std::vector< std::vector< std::vector< std::vector< float > > > > m_kernels
Definition: keras_model.h:183
Float_t y
Definition: compare.C:6
virtual std::vector< std::vector< std::vector< float > > > const & get_3d() const
Definition: keras_model.h:46
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
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:276
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; }
std::string keras::Layer::get_name ( )
inlineinherited

Definition at line 131 of file keras_model.h.

131 { return m_name; }
std::string m_name
Definition: keras_model.h:132
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 43 of file keras_model.cc.

References d.

43  {
44  char tmp_char = ' ';
45  string tmp_str = "";
46  float tmp_float;
47  bool skip = false;
49  if (m_border_mode == "[") { m_border_mode = "valid"; skip = true; }
50 
51  cout << "LayerConv2D " << m_kernels_cnt
52  << "x" << m_depth << "x" << m_rows << "x" << m_cols << " border_mode " << m_border_mode << endl;
53  // reading kernel weights
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; } // for '['
60  else { skip = false; }
61  vector<float> tmp_row;
62  for(int c = 0; c < m_cols; ++c) {
63  fin >> tmp_float;
64  //cout << tmp_float << " ";
65  tmp_row.push_back(tmp_float);
66  }
67  fin >> tmp_char; // for ']'
68  tmp_single_depth.push_back(tmp_row);
69  }
70  tmp_depths.push_back(tmp_single_depth);
71  }
72  m_kernels.push_back(tmp_depths);
73  }
74  // reading kernel biases
75  fin >> tmp_char; // for '['
76  for(int k = 0; k < m_kernels_cnt; ++k) {
77  fin >> tmp_float;
78  m_bias.push_back(tmp_float);
79  }
80  fin >> tmp_char; // for ']'
81 
82 }
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
TString fin
Definition: Style.C:24
std::string m_border_mode
Definition: keras_model.h:190
Float_t d
Definition: plot.C:237

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.

std::string keras::Layer::m_name
inherited

Definition at line 132 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: