LArSoft  v09_90_00
Liquid Argon Software toolkit - https://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 192 of file keras_model.h.

Constructor & Destructor Documentation

keras::LayerConv2D::LayerConv2D ( )
inline

Definition at line 194 of file keras_model.h.

References fin.

194 : Layer("Conv2D") {}
Layer(std::string name)
Definition: keras_model.h:133

Member Function Documentation

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

Implements keras::Layer.

Definition at line 323 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(), util::size(), x, and y.

324 {
325  unsigned int st_x = (m_kernels[0][0].size() - 1) >> 1;
326  unsigned int st_y = (m_kernels[0][0][0].size() - 1) >> 1;
327  auto const& im = dc->get_3d();
328 
329  size_t size_x = (m_border_mode == "valid") ? im[0].size() - 2 * st_x : im[0].size();
330  size_t size_y = (m_border_mode == "valid") ? im[0][0].size() - 2 * st_y : im[0][0].size();
331 
332  // depth rows cols
333  keras::DataChunk2D* out = new keras::DataChunk2D(m_kernels.size(), size_x, size_y, 0);
334  auto& y_ret = out->get_3d_rw();
335 
336  //auto t1 = std::chrono::high_resolution_clock::now();
337 
338  // Parallelize the kernal calculation
339  tbb::parallel_for(size_t(0), size_t(m_kernels.size()), [&](size_t j) {
340  for (unsigned int m = 0; m < im.size(); ++m) { // loop over image depth
341  if (m_border_mode == "valid")
342  keras::conv_single_depth_valid(y_ret[j], im[m], m_kernels[j][m]);
343  else
344  keras::conv_single_depth_same(y_ret[j], im[m], m_kernels[j][m]);
345  }
346 
347  for (unsigned int x = 0; x < y_ret[0].size(); ++x) {
348 
349  size_t size = y_ret[0][0].size();
350  float bias = m_bias[j];
351  size_t k = 0;
352 
353  for (unsigned int y = 0; y < size / 8; ++y) {
354  y_ret[j][x][k] += bias;
355  y_ret[j][x][k + 1] += bias;
356  y_ret[j][x][k + 2] += bias;
357  y_ret[j][x][k + 3] += bias;
358  y_ret[j][x][k + 4] += bias;
359  y_ret[j][x][k + 5] += bias;
360  y_ret[j][x][k + 6] += bias;
361  y_ret[j][x][k + 7] += bias;
362  k += 8;
363  }
364  while (k < size) {
365  y_ret[j][x][k] += bias;
366  ++k;
367  }
368  }
369  });
370 
371  //auto t2 = std::chrono::high_resolution_clock::now();
372  /*
373  cout << "Parallal : "
374  << std::chrono::duration_cast<std::chrono::microseconds>(t2-t1).count()
375  << " microseconds." << endl;
376  */
377 
378  return out;
379 }
Float_t x
Definition: compare.C:6
std::vector< std::vector< std::vector< std::vector< float > > > > m_kernels
Definition: keras_model.h:198
Float_t y
Definition: compare.C:6
virtual std::vector< std::vector< std::vector< float > > > const & get_3d() const
Definition: keras_model.h:49
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:290
decltype(auto) constexpr size(T &&obj)
ADL-aware version of std::size.
Definition: StdUtils.h:101
std::vector< float > m_bias
Definition: keras_model.h:199
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:264
std::string m_border_mode
Definition: keras_model.h:205
std::vector< std::vector< std::vector< float > > > & get_3d_rw()
Definition: keras_model.h:68
virtual unsigned int keras::LayerConv2D::get_input_cols ( ) const
inlinevirtual

Implements keras::Layer.

Definition at line 202 of file keras_model.h.

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

Implements keras::Layer.

Definition at line 201 of file keras_model.h.

201 { return m_rows; }
std::string keras::Layer::get_name ( )
inlineinherited

Definition at line 140 of file keras_model.h.

140 { return m_name; }
std::string m_name
Definition: keras_model.h:141
virtual unsigned int keras::LayerConv2D::get_output_units ( ) const
inlinevirtual

Implements keras::Layer.

Definition at line 203 of file keras_model.h.

203 { 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.

References d, and r.

41 {
42  char tmp_char = ' ';
43  string tmp_str = "";
44  float tmp_float;
45  bool skip = false;
47  if (m_border_mode == "[") {
48  m_border_mode = "valid";
49  skip = true;
50  }
51 
52  cout << "LayerConv2D " << m_kernels_cnt << "x" << m_depth << "x" << m_rows << "x" << m_cols
53  << " border_mode " << m_border_mode << endl;
54  // reading kernel weights
55  for (int k = 0; k < m_kernels_cnt; ++k) {
56  vector<vector<vector<float>>> tmp_depths;
57  for (int d = 0; d < m_depth; ++d) {
58  vector<vector<float>> tmp_single_depth;
59  for (int r = 0; r < m_rows; ++r) {
60  if (!skip) { fin >> tmp_char; } // for '['
61  else {
62  skip = false;
63  }
64  vector<float> tmp_row;
65  for (int c = 0; c < m_cols; ++c) {
66  fin >> tmp_float;
67  //cout << tmp_float << " ";
68  tmp_row.push_back(tmp_float);
69  }
70  fin >> tmp_char; // for ']'
71  tmp_single_depth.push_back(tmp_row);
72  }
73  tmp_depths.push_back(tmp_single_depth);
74  }
75  m_kernels.push_back(tmp_depths);
76  }
77  // reading kernel biases
78  fin >> tmp_char; // for '['
79  for (int k = 0; k < m_kernels_cnt; ++k) {
80  fin >> tmp_float;
81  m_bias.push_back(tmp_float);
82  }
83  fin >> tmp_char; // for ']'
84 }
TRandom r
Definition: spectrum.C:23
TString fin
Definition: Style.C:24
std::vector< std::vector< std::vector< std::vector< float > > > > m_kernels
Definition: keras_model.h:198
std::vector< float > m_bias
Definition: keras_model.h:199
std::string m_border_mode
Definition: keras_model.h:205
Float_t d
Definition: plot.C:235

Member Data Documentation

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

Definition at line 199 of file keras_model.h.

std::string keras::LayerConv2D::m_border_mode

Definition at line 205 of file keras_model.h.

int keras::LayerConv2D::m_cols

Definition at line 209 of file keras_model.h.

int keras::LayerConv2D::m_depth

Definition at line 207 of file keras_model.h.

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

Definition at line 198 of file keras_model.h.

int keras::LayerConv2D::m_kernels_cnt

Definition at line 206 of file keras_model.h.

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

Definition at line 141 of file keras_model.h.

int keras::LayerConv2D::m_rows

Definition at line 208 of file keras_model.h.


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