keras_model.h
Go to the documentation of this file.
1 //////////////////////////////////////////////////////////////////////////////////////////////////
2 // Class: KerasModel, plus all the data and network layer classes
3 // Authors: P.Plonski, from DUNE, WUT, since 2016
4 // R.Sulej: adopt to LArSoft, vectorize dense, from DUNE, FNAL/NCBJ, since 2016
5 // D.Smith: optimize Conv2D compute from LArIAT, BU, 2017
6 //
7 //
8 // Simple implementation of running Keras models in the inference mode, see README.md.
9 //
10 //////////////////////////////////////////////////////////////////////////////////////////////////
11 
12 #ifndef KERAS_MODEL__H
13 #define KERAS_MODEL__H
14 
15 #include <string>
16 #include <vector>
17 #include <fstream>
18 #include <iostream>
19 
20 namespace keras
21 {
22  std::vector<float> read_1d_array(std::ifstream &fin, int cols);
23  void missing_activation_impl(const std::string &act);
24  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);
25  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);
26 
27  class DataChunk;
28  class DataChunk2D;
29  class DataChunkFlat;
30 
31  class Layer;
32  class LayerFlatten;
33  class LayerMaxPooling;
34  class LayerActivation;
35  class LayerConv2D;
36  class LayerDense;
37 
38  class KerasModel;
39 }
40 
42 public:
43  virtual ~DataChunk() {}
44  virtual size_t get_data_dim(void) const { return 0; }
45  virtual std::vector<float> const & get_1d() const { throw "not implemented"; };
46  virtual std::vector<std::vector<std::vector<float> > > const & get_3d() const { throw "not implemented"; };
47  virtual void set_data(std::vector<std::vector<std::vector<float> > > const &) {};
48  virtual void set_data(std::vector<float> const &) {};
49  //virtual unsigned int get_count();
50  virtual void read_from_file(const std::string &fname) {};
51  virtual void show_name() = 0;
52  virtual void show_values() = 0;
53 };
54 
56 public:
57  DataChunk2D(size_t depth, size_t rows, size_t cols, float init) :
58  data(depth, std::vector< std::vector<float> >(rows, std::vector<float>(cols, init)))
59  { }
60  DataChunk2D(void) { }
61 
62  std::vector< std::vector< std::vector<float> > > & get_3d_rw() { return data; };
63  std::vector< std::vector< std::vector<float> > > const & get_3d() const { return data; };
64  virtual void set_data(std::vector<std::vector<std::vector<float> > > const & d) { data = d; };
65  size_t get_data_dim(void) const { return 3; }
66 
67  void show_name() {
68  std::cout << "DataChunk2D " << data.size() << "x" << data[0].size() << "x" << data[0][0].size() << std::endl;
69  }
70 
71  void show_values() {
72  std::cout << "DataChunk2D values:" << std::endl;
73  for(size_t i = 0; i < data.size(); ++i) {
74  std::cout << "Kernel " << i << std::endl;
75  for(size_t j = 0; j < data[0].size(); ++j) {
76  for(size_t k = 0; k < data[0][0].size(); ++k) {
77  std::cout << data[i][j][k] << " ";
78  }
79  std::cout << std::endl;
80  }
81  }
82  }
83  //unsigned int get_count() {
84  // return data.size()*data[0].size()*data[0][0].size();
85  //}
86 
87  void read_from_file(const std::string &fname);
88  std::vector<std::vector<std::vector<float> > > data; // depth, rows, cols
89 
90  int m_depth;
91  int m_rows;
92  int m_cols;
93 };
94 
96 public:
97  DataChunkFlat(size_t size) : f(size) { }
98  DataChunkFlat(size_t size, float init) : f(size, init) { }
99  DataChunkFlat(void) { }
100 
101  std::vector<float> f;
102  std::vector<float> & get_1d_rw() { return f; }
103  std::vector<float> const & get_1d() const { return f; }
104  void set_data(std::vector<float> const & d) { f = d; };
105  size_t get_data_dim(void) const { return 1; }
106 
107  void show_name() {
108  std::cout << "DataChunkFlat " << f.size() << std::endl;
109  }
110  void show_values() {
111  std::cout << "DataChunkFlat values:" << std::endl;
112  for(size_t i = 0; i < f.size(); ++i) std::cout << f[i] << " ";
113  std::cout << std::endl;
114  }
116  //unsigned int get_count() { return f.size(); }
117 };
118 
120 public:
121  virtual void load_weights(std::ifstream &fin) = 0;
122  virtual keras::DataChunk* compute_output(keras::DataChunk*) = 0;
123 
124  Layer(std::string name) : m_name(name) {}
125  virtual ~Layer() {}
126 
127  virtual unsigned int get_input_rows() const = 0;
128  virtual unsigned int get_input_cols() const = 0;
129  virtual unsigned int get_output_units() const = 0;
130 
131  std::string get_name() { return m_name; }
133 };
134 
135 
136 class keras::LayerFlatten : public Layer {
137 public:
138  LayerFlatten() : Layer("Flatten") {}
139  void load_weights(std::ifstream &fin) {};
140  keras::DataChunk* compute_output(keras::DataChunk*);
141 
142  virtual unsigned int get_input_rows() const { return 0; } // look for the value in the preceding layer
143  virtual unsigned int get_input_cols() const { return 0; } // same as for rows
144  virtual unsigned int get_output_units() const { return 0; }
145 };
146 
147 
149 public:
150  LayerMaxPooling() : Layer("MaxPooling2D") {};
151 
152  void load_weights(std::ifstream &fin);
153  keras::DataChunk* compute_output(keras::DataChunk*);
154 
155  virtual unsigned int get_input_rows() const { return 0; } // look for the value in the preceding layer
156  virtual unsigned int get_input_cols() const { return 0; } // same as for rows
157  virtual unsigned int get_output_units() const { return 0; }
158 
159  int m_pool_x;
160  int m_pool_y;
161 
162 };
163 
165 public:
166  LayerActivation() : Layer("Activation") {}
167  void load_weights(std::ifstream &fin);
168  keras::DataChunk* compute_output(keras::DataChunk*);
169 
170  virtual unsigned int get_input_rows() const { return 0; } // look for the value in the preceding layer
171  virtual unsigned int get_input_cols() const { return 0; } // same as for rows
172  virtual unsigned int get_output_units() const { return 0; }
173 
175 };
176 
177 class keras::LayerConv2D : public Layer {
178 public:
179  LayerConv2D() : Layer("Conv2D") {}
180 
181  void load_weights(std::ifstream &fin);
182  keras::DataChunk* compute_output(keras::DataChunk*);
183  std::vector<std::vector<std::vector<std::vector<float> > > > m_kernels; // kernel, depth, rows, cols
184  std::vector<float> m_bias; // kernel
185 
186  virtual unsigned int get_input_rows() const { return m_rows; }
187  virtual unsigned int get_input_cols() const { return m_cols; }
188  virtual unsigned int get_output_units() const { return m_kernels_cnt; }
189 
192  int m_depth;
193  int m_rows;
194  int m_cols;
195 };
196 
197 class keras::LayerDense : public Layer {
198 public:
199  LayerDense() : Layer("Dense") {}
200 
201  void load_weights(std::ifstream &fin);
202  keras::DataChunk* compute_output(keras::DataChunk*);
203  std::vector<std::vector<float> > m_weights; //input, neuron
204  std::vector<float> m_bias; // neuron
205 
206  virtual unsigned int get_input_rows() const { return 1; } // flat, just one row
207  virtual unsigned int get_input_cols() const { return m_input_cnt; }
208  virtual unsigned int get_output_units() const { return m_neurons; }
209 
212 };
213 
215 public:
216  KerasModel(const std::string &input_fname);
217  ~KerasModel();
218  std::vector<float> compute_output(keras::DataChunk *dc);
219 
220  unsigned int get_input_rows() const { return m_layers.front()->get_input_rows(); }
221  unsigned int get_input_cols() const { return m_layers.front()->get_input_cols(); }
222  int get_output_length() const;
223 
224 private:
225 
226  void load_weights(const std::string &input_fname);
227  int m_layers_cnt; // number of layers
228  std::vector<Layer *> m_layers; // container with layers
229 
230 };
231 
232 #endif
static QCString name
Definition: declinfo.cpp:673
virtual unsigned int get_output_units() const
Definition: keras_model.h:144
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
virtual unsigned int get_output_units() const
Definition: keras_model.h:172
std::vector< std::vector< std::vector< std::vector< float > > > > m_kernels
Definition: keras_model.h:183
std::string m_name
Definition: keras_model.h:132
virtual unsigned int get_input_rows() const
Definition: keras_model.h:155
std::vector< std::vector< std::vector< float > > > const & get_3d() const
Definition: keras_model.h:63
std::string string
Definition: nybbler.cc:12
DataChunk2D(size_t depth, size_t rows, size_t cols, float init)
Definition: keras_model.h:57
struct vector vector
virtual void read_from_file(const std::string &fname)
Definition: keras_model.h:50
virtual unsigned int get_input_cols() const
Definition: keras_model.h:187
virtual void set_data(std::vector< std::vector< std::vector< float > > > const &)
Definition: keras_model.h:47
STL namespace.
unsigned int get_input_cols() const
Definition: keras_model.h:221
virtual std::vector< std::vector< std::vector< float > > > const & get_3d() const
Definition: keras_model.h:46
virtual size_t get_data_dim(void) const
Definition: keras_model.h:44
init
Definition: train.py:42
Layer(std::string name)
Definition: keras_model.h:124
size_t get_data_dim(void) const
Definition: keras_model.h:105
virtual void show_values()=0
std::vector< std::vector< float > > m_weights
Definition: keras_model.h:203
virtual unsigned int get_output_units() const
Definition: keras_model.h:188
std::string get_name()
Definition: keras_model.h:131
virtual unsigned int get_output_units() const
Definition: keras_model.h:157
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
virtual void show_name()=0
virtual void set_data(std::vector< float > const &)
Definition: keras_model.h:48
std::string m_activation_type
Definition: keras_model.h:174
virtual void set_data(std::vector< std::vector< std::vector< float > > > const &d)
Definition: keras_model.h:64
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
unsigned int get_input_rows() const
Definition: keras_model.h:220
virtual ~DataChunk()
Definition: keras_model.h:43
void missing_activation_impl(const std::string &act)
Definition: keras_model.cc:175
void load_weights(std::ifstream &fin)
Definition: keras_model.h:139
std::vector< Layer * > m_layers
Definition: keras_model.h:228
virtual unsigned int get_input_cols() const
Definition: keras_model.h:143
DataChunkFlat(size_t size, float init)
Definition: keras_model.h:98
virtual unsigned int get_input_rows() const
Definition: keras_model.h:142
std::vector< std::vector< std::vector< float > > > data
Definition: keras_model.h:88
virtual unsigned int get_output_units() const
Definition: keras_model.h:208
void read_from_file(const std::string &fname)
Definition: keras_model.h:115
std::vector< float > const & get_1d() const
Definition: keras_model.h:103
std::vector< float > & get_1d_rw()
Definition: keras_model.h:102
virtual ~Layer()
Definition: keras_model.h:125
DataChunkFlat(size_t size)
Definition: keras_model.h:97
void set_data(std::vector< float > const &d)
Definition: keras_model.h:104
std::vector< float > m_bias
Definition: keras_model.h:204
size_t get_data_dim(void) const
Definition: keras_model.h:65
virtual unsigned int get_input_rows() const
Definition: keras_model.h:170
virtual unsigned int get_input_cols() const
Definition: keras_model.h:171
virtual unsigned int get_input_cols() const
Definition: keras_model.h:207
virtual unsigned int get_input_rows() const
Definition: keras_model.h:186
virtual std::vector< float > const & get_1d() const
Definition: keras_model.h:45
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
std::vector< float > f
Definition: keras_model.h:101
virtual unsigned int get_input_cols() const
Definition: keras_model.h:156
std::vector< float > read_1d_array(std::ifstream &fin, int cols)
Definition: keras_model.cc:11
QTextStream & endl(QTextStream &s)
virtual unsigned int get_input_rows() const
Definition: keras_model.h:206