LArSoft  v09_90_00
Liquid Argon Software toolkit - https://larsoft.org/
keras_model.h
Go to the documentation of this file.
1 // 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 //
11 
12 #ifndef KERAS_MODEL__H
13 #define KERAS_MODEL__H
14 
15 #include <fstream>
16 #include <iostream>
17 #include <string>
18 #include <vector>
19 
20 namespace keras {
21  std::vector<float> read_1d_array(std::ifstream& fin, int cols);
22  void missing_activation_impl(const std::string& act);
23  void conv_single_depth_valid(std::vector<std::vector<float>>& y,
24  std::vector<std::vector<float>> const& im,
25  std::vector<std::vector<float>> const& k);
26  void conv_single_depth_same(std::vector<std::vector<float>>& y,
27  std::vector<std::vector<float>> const& im,
28  std::vector<std::vector<float>> const& k);
29 
30  class DataChunk;
31  class DataChunk2D;
32  class DataChunkFlat;
33 
34  class Layer;
35  class LayerFlatten;
36  class LayerMaxPooling;
37  class LayerActivation;
38  class LayerConv2D;
39  class LayerDense;
40 
41  class KerasModel;
42 }
43 
45 public:
46  virtual ~DataChunk() {}
47  virtual size_t get_data_dim(void) const { return 0; }
48  virtual std::vector<float> const& get_1d() const { throw "not implemented"; };
49  virtual std::vector<std::vector<std::vector<float>>> const& get_3d() const
50  {
51  throw "not implemented";
52  };
53  virtual void set_data(std::vector<std::vector<std::vector<float>>> const&){};
54  virtual void set_data(std::vector<float> const&){};
55  //virtual unsigned int get_count();
56  virtual void read_from_file(const std::string& fname){};
57  virtual void show_name() = 0;
58  virtual void show_values() = 0;
59 };
60 
62 public:
63  DataChunk2D(size_t depth, size_t rows, size_t cols, float init)
64  : data(depth, std::vector<std::vector<float>>(rows, std::vector<float>(cols, init)))
65  {}
66  DataChunk2D(void) {}
67 
68  std::vector<std::vector<std::vector<float>>>& get_3d_rw() { return data; };
69  std::vector<std::vector<std::vector<float>>> const& get_3d() const { return data; };
70  virtual void set_data(std::vector<std::vector<std::vector<float>>> const& d) { data = d; };
71  size_t get_data_dim(void) const { return 3; }
72 
73  void show_name()
74  {
75  std::cout << "DataChunk2D " << data.size() << "x" << data[0].size() << "x" << data[0][0].size()
76  << std::endl;
77  }
78 
79  void show_values()
80  {
81  std::cout << "DataChunk2D values:" << std::endl;
82  for (size_t i = 0; i < data.size(); ++i) {
83  std::cout << "Kernel " << i << std::endl;
84  for (size_t j = 0; j < data[0].size(); ++j) {
85  for (size_t k = 0; k < data[0][0].size(); ++k) {
86  std::cout << data[i][j][k] << " ";
87  }
88  std::cout << std::endl;
89  }
90  }
91  }
92  //unsigned int get_count() {
93  // return data.size()*data[0].size()*data[0][0].size();
94  //}
95 
96  void read_from_file(const std::string& fname);
97  std::vector<std::vector<std::vector<float>>> data; // depth, rows, cols
98 
99  int m_depth;
100  int m_rows;
101  int m_cols;
102 };
103 
105 public:
106  DataChunkFlat(size_t size) : f(size) {}
107  DataChunkFlat(size_t size, float init) : f(size, init) {}
108  DataChunkFlat(void) {}
109 
110  std::vector<float> f;
111  std::vector<float>& get_1d_rw() { return f; }
112  std::vector<float> const& get_1d() const { return f; }
113  void set_data(std::vector<float> const& d) { f = d; };
114  size_t get_data_dim(void) const { return 1; }
115 
116  void show_name() { std::cout << "DataChunkFlat " << f.size() << std::endl; }
117  void show_values()
118  {
119  std::cout << "DataChunkFlat values:" << std::endl;
120  for (size_t i = 0; i < f.size(); ++i)
121  std::cout << f[i] << " ";
122  std::cout << std::endl;
123  }
124  void read_from_file(const std::string& fname){};
125  //unsigned int get_count() { return f.size(); }
126 };
127 
129 public:
130  virtual void load_weights(std::ifstream& fin) = 0;
131  virtual keras::DataChunk* compute_output(keras::DataChunk*) = 0;
132 
133  Layer(std::string name) : m_name(name) {}
134  virtual ~Layer() {}
135 
136  virtual unsigned int get_input_rows() const = 0;
137  virtual unsigned int get_input_cols() const = 0;
138  virtual unsigned int get_output_units() const = 0;
139 
140  std::string get_name() { return m_name; }
141  std::string m_name;
142 };
143 
144 class keras::LayerFlatten : public Layer {
145 public:
146  LayerFlatten() : Layer("Flatten") {}
147  void load_weights(std::ifstream& fin){};
148  keras::DataChunk* compute_output(keras::DataChunk*);
149 
150  virtual unsigned int get_input_rows() const
151  {
152  return 0;
153  } // look for the value in the preceding layer
154  virtual unsigned int get_input_cols() const { return 0; } // same as for rows
155  virtual unsigned int get_output_units() const { return 0; }
156 };
157 
159 public:
160  LayerMaxPooling() : Layer("MaxPooling2D"){};
161 
162  void load_weights(std::ifstream& fin);
163  keras::DataChunk* compute_output(keras::DataChunk*);
164 
165  virtual unsigned int get_input_rows() const
166  {
167  return 0;
168  } // look for the value in the preceding layer
169  virtual unsigned int get_input_cols() const { return 0; } // same as for rows
170  virtual unsigned int get_output_units() const { return 0; }
171 
172  int m_pool_x;
173  int m_pool_y;
174 };
175 
177 public:
178  LayerActivation() : Layer("Activation") {}
179  void load_weights(std::ifstream& fin);
180  keras::DataChunk* compute_output(keras::DataChunk*);
181 
182  virtual unsigned int get_input_rows() const
183  {
184  return 0;
185  } // look for the value in the preceding layer
186  virtual unsigned int get_input_cols() const { return 0; } // same as for rows
187  virtual unsigned int get_output_units() const { return 0; }
188 
189  std::string m_activation_type;
190 };
191 
192 class keras::LayerConv2D : public Layer {
193 public:
194  LayerConv2D() : Layer("Conv2D") {}
195 
196  void load_weights(std::ifstream& fin);
197  keras::DataChunk* compute_output(keras::DataChunk*);
198  std::vector<std::vector<std::vector<std::vector<float>>>> m_kernels; // kernel, depth, rows, cols
199  std::vector<float> m_bias; // kernel
200 
201  virtual unsigned int get_input_rows() const { return m_rows; }
202  virtual unsigned int get_input_cols() const { return m_cols; }
203  virtual unsigned int get_output_units() const { return m_kernels_cnt; }
204 
205  std::string m_border_mode;
207  int m_depth;
208  int m_rows;
209  int m_cols;
210 };
211 
212 class keras::LayerDense : public Layer {
213 public:
214  LayerDense() : Layer("Dense") {}
215 
216  void load_weights(std::ifstream& fin);
217  keras::DataChunk* compute_output(keras::DataChunk*);
218  std::vector<std::vector<float>> m_weights; //input, neuron
219  std::vector<float> m_bias; // neuron
220 
221  virtual unsigned int get_input_rows() const { return 1; } // flat, just one row
222  virtual unsigned int get_input_cols() const { return m_input_cnt; }
223  virtual unsigned int get_output_units() const { return m_neurons; }
224 
227 };
228 
230 public:
231  KerasModel(const std::string& input_fname);
232  ~KerasModel();
233  std::vector<float> compute_output(keras::DataChunk* dc);
234 
235  unsigned int get_input_rows() const { return m_layers.front()->get_input_rows(); }
236  unsigned int get_input_cols() const { return m_layers.front()->get_input_cols(); }
237  int get_output_length() const;
238 
239 private:
240  void load_weights(const std::string& input_fname);
241  int m_layers_cnt; // number of layers
242  std::vector<Layer*> m_layers; // container with layers
243 };
244 
245 #endif
virtual unsigned int get_output_units() const
Definition: keras_model.h:155
TString fin
Definition: Style.C:24
virtual unsigned int get_output_units() const
Definition: keras_model.h:187
std::vector< std::vector< std::vector< std::vector< float > > > > m_kernels
Definition: keras_model.h:198
std::string m_name
Definition: keras_model.h:141
virtual unsigned int get_input_rows() const
Definition: keras_model.h:165
DataChunk2D(size_t depth, size_t rows, size_t cols, float init)
Definition: keras_model.h:63
Float_t y
Definition: compare.C:6
virtual void set_data(std::vector< std::vector< std::vector< float >>> const &)
Definition: keras_model.h:53
virtual void read_from_file(const std::string &fname)
Definition: keras_model.h:56
virtual unsigned int get_input_cols() const
Definition: keras_model.h:202
STL namespace.
std::vector< std::vector< std::vector< float > > > const & get_3d() const
Definition: keras_model.h:69
unsigned int get_input_cols() const
Definition: keras_model.h:236
virtual std::vector< std::vector< std::vector< float > > > const & get_3d() const
Definition: keras_model.h:49
virtual size_t get_data_dim(void) const
Definition: keras_model.h:47
Layer(std::string name)
Definition: keras_model.h:133
size_t get_data_dim(void) const
Definition: keras_model.h:114
virtual void show_values()=0
std::vector< std::vector< float > > m_weights
Definition: keras_model.h:218
virtual unsigned int get_output_units() const
Definition: keras_model.h:203
std::string get_name()
Definition: keras_model.h:140
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
TFile f
Definition: plotHisto.C:6
virtual unsigned int get_output_units() const
Definition: keras_model.h:170
decltype(auto) constexpr size(T &&obj)
ADL-aware version of std::size.
Definition: StdUtils.h:101
auto vector(Vector const &v)
Returns a manipulator which will print the specified array.
Definition: DumpUtils.h:289
std::vector< float > m_bias
Definition: keras_model.h:199
virtual void show_name()=0
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::vector< Layer * > m_layers
Definition: keras_model.h:242
virtual void set_data(std::vector< float > const &)
Definition: keras_model.h:54
std::string m_activation_type
Definition: keras_model.h:189
std::string m_border_mode
Definition: keras_model.h:205
unsigned int get_input_rows() const
Definition: keras_model.h:235
Float_t d
Definition: plot.C:235
virtual ~DataChunk()
Definition: keras_model.h:46
void missing_activation_impl(const std::string &act)
Definition: keras_model.cc:183
void load_weights(std::ifstream &fin)
Definition: keras_model.h:147
virtual unsigned int get_input_cols() const
Definition: keras_model.h:154
DataChunkFlat(size_t size, float init)
Definition: keras_model.h:107
virtual unsigned int get_input_rows() const
Definition: keras_model.h:150
std::vector< std::vector< std::vector< float > > > data
Definition: keras_model.h:97
virtual unsigned int get_output_units() const
Definition: keras_model.h:223
void read_from_file(const std::string &fname)
Definition: keras_model.h:124
std::vector< std::vector< std::vector< float > > > & get_3d_rw()
Definition: keras_model.h:68
std::vector< float > const & get_1d() const
Definition: keras_model.h:112
virtual void set_data(std::vector< std::vector< std::vector< float >>> const &d)
Definition: keras_model.h:70
std::vector< float > & get_1d_rw()
Definition: keras_model.h:111
virtual ~Layer()
Definition: keras_model.h:134
DataChunkFlat(size_t size)
Definition: keras_model.h:106
void set_data(std::vector< float > const &d)
Definition: keras_model.h:113
std::vector< float > m_bias
Definition: keras_model.h:219
size_t get_data_dim(void) const
Definition: keras_model.h:71
virtual unsigned int get_input_rows() const
Definition: keras_model.h:182
virtual unsigned int get_input_cols() const
Definition: keras_model.h:186
virtual unsigned int get_input_cols() const
Definition: keras_model.h:222
virtual unsigned int get_input_rows() const
Definition: keras_model.h:201
virtual std::vector< float > const & get_1d() const
Definition: keras_model.h:48
std::vector< float > f
Definition: keras_model.h:110
virtual unsigned int get_input_cols() const
Definition: keras_model.h:169
std::vector< float > read_1d_array(std::ifstream &fin, int cols)
Definition: keras_model.cc:10
virtual unsigned int get_input_rows() const
Definition: keras_model.h:221