KerasModel.h
Go to the documentation of this file.
1 #ifndef KERAS_MODEL__H
2 #define KERAS_MODEL__H
3 
4 #include <string>
5 #include <vector>
6 #include <fstream>
7 #include <iostream>
8 
9 //Modified from https://github.com/pplonski/keras2cpp
10 
11 namespace keras
12 {
13  std::vector<float> read_1d_array(std::ifstream &fin, int cols);
14  void missing_activation_impl(const std::string &act);
15  std::vector< std::vector<float> > conv_single_depth_valid(std::vector< std::vector<float> > const & im, std::vector< std::vector<float> > const & k);
16  std::vector< std::vector<float> > conv_single_depth_same(std::vector< std::vector<float> > const & im, std::vector< std::vector<float> > const & k);
17 
18  class DataChunk;
19  class DataChunk2D;
20  class DataChunkFlat;
21 
22  class Layer;
23  class LayerFlatten;
24  class LayerMaxPooling;
25  class LayerActivation;
26  class LayerConv2D;
27  class LayerDense;
28 
29  class KerasModel;
30 }
31 
33 public:
34  virtual ~DataChunk() {}
35  virtual size_t get_data_dim(void) const { return 0; }
36  virtual std::vector<float> const & get_1d() const { throw "not implemented"; };
37  virtual std::vector<std::vector<std::vector<float> > > const & get_3d() const { throw "not implemented"; };
38  virtual void set_data(std::vector<std::vector<std::vector<float> > > const &) {};
39  virtual void set_data(std::vector<float> const &) {};
40  //virtual unsigned int get_count();
41  virtual void read_from_file(const std::string &fname) {};
42  virtual void show_name() = 0;
43  virtual void show_values() = 0;
44 };
45 
47 public:
49  std::vector< std::vector< std::vector<float> > > const & get_3d() const { return data; };
50 
51  virtual void set_data(std::vector<std::vector<std::vector<float> > > const & d) { data = d; };
52  size_t get_data_dim(void) const { return 3; }
53 
54  void show_name() {
55  // std::cout << "DataChunk2D " << data.size() << "x" << data[0].size() << "x" << data[0][0].size() << std::endl;
56  }
57 
58  void show_values() {
59  //std::cout << "DataChunk2D values:" << std::endl;
60  for(size_t i = 0; i < data.size(); ++i) {
61  // std::cout << "Kernel " << i << std::endl;
62  for(size_t j = 0; j < data[0].size(); ++j) {
63  for(size_t k = 0; k < data[0][0].size(); ++k) {
64  // std::cout << data[i][j][k] << " ";
65  }
66  // std::cout << std::endl;
67  }
68  }
69  }
70  //unsigned int get_count() {
71  // return data.size()*data[0].size()*data[0][0].size();
72  //}
73 
74  void read_from_file(const std::string &fname);
75  std::vector<std::vector<std::vector<float> > > data; // depth, rows, cols
76 
77  int m_depth;
78  int m_rows;
79  int m_cols;
80 };
81 
83  public:
85  DataChunkFlat(size_t size) : f(size) { }
86  DataChunkFlat(size_t size, float init) : f(size, init) { }
87  DataChunkFlat(void) { }
88  std::vector<float> f;
89  std::vector<float> & get_1d_rw() { return f; }
90  std::vector<float> const & get_1d() const { return f; }
91  void set_data(std::vector<float> const & d) { f = d; };
92  size_t get_data_dim(void) const { return 1; }
93 
94  void show_name() {
95  // std::cout << "DataChunkFlat " << f.size() << std::endl;
96  }
97  void show_values() {
98  // std::cout << "DataChunkFlat values:" << std::endl;
99  // for(size_t i = 0; i < f.size(); ++i) std::cout << f[i] << " ";
100  // std::cout << std::endl;
101  }
103  //unsigned int get_count() { return f.size(); }
104 };
105 
107 public:
108  virtual void load_weights(std::ifstream &fin) = 0;
109  virtual keras::DataChunk* compute_output(keras::DataChunk*) = 0;
110 
111  Layer(std::string name) : m_name(name) {}
112  virtual ~Layer() {}
113 
114  virtual unsigned int get_input_rows() const = 0;
115  virtual unsigned int get_input_cols() const = 0;
116  virtual unsigned int get_output_units() const = 0;
117 
118  std::string get_name() { return m_name; }
120 };
121 
122 
123 class keras::LayerFlatten : public Layer {
124 public:
125  LayerFlatten() : Layer("Flatten") {}
126  void load_weights(std::ifstream &fin) {};
127  keras::DataChunk* compute_output(keras::DataChunk*);
128 
129  virtual unsigned int get_input_rows() const { return 0; } // look for the value in the preceding layer
130  virtual unsigned int get_input_cols() const { return 0; } // same as for rows
131  virtual unsigned int get_output_units() const { return 0; }
132 };
133 
134 
136 public:
137  LayerMaxPooling() : Layer("MaxPooling2D") {};
138 
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  int m_pool_x;
147  int m_pool_y;
148 
149 };
150 
152 public:
153  LayerActivation() : Layer("Activation") {}
154  void load_weights(std::ifstream &fin);
155  keras::DataChunk* compute_output(keras::DataChunk*);
156 
157  virtual unsigned int get_input_rows() const { return 0; } // look for the value in the preceding layer
158  virtual unsigned int get_input_cols() const { return 0; } // same as for rows
159  virtual unsigned int get_output_units() const { return 0; }
160 
162 };
163 
164 class keras::LayerConv2D : public Layer {
165 public:
166  LayerConv2D() : Layer("Conv2D") {}
167 
168  void load_weights(std::ifstream &fin);
169  keras::DataChunk* compute_output(keras::DataChunk*);
170  std::vector<std::vector<std::vector<std::vector<float> > > > m_kernels; // kernel, depth, rows, cols
171  std::vector<float> m_bias; // kernel
172 
173  virtual unsigned int get_input_rows() const { return m_rows; }
174  virtual unsigned int get_input_cols() const { return m_cols; }
175  virtual unsigned int get_output_units() const { return m_kernels_cnt; }
176 
179  int m_depth;
180  int m_rows;
181  int m_cols;
182 };
183 
184 class keras::LayerDense : public Layer {
185 public:
186  LayerDense() : Layer("Dense") {}
187 
188  void load_weights(std::ifstream &fin);
189  keras::DataChunk* compute_output(keras::DataChunk*);
190  std::vector<std::vector<float> > m_weights; //input, neuron
191  std::vector<float> m_bias; // neuron
192 
193  virtual unsigned int get_input_rows() const { return 1; } // flat, just one row
194  virtual unsigned int get_input_cols() const { return m_input_cnt; }
195  virtual unsigned int get_output_units() const { return m_neurons; }
196 
199 };
200 
202 public:
203  KerasModel(const std::string &input_fname);
204  ~KerasModel();
205  std::vector<float> compute_output(keras::DataChunk *dc);
206 
207  unsigned int get_input_rows() const { return m_layers.front()->get_input_rows(); }
208  unsigned int get_input_cols() const { return m_layers.front()->get_input_cols(); }
209  int get_output_length() const;
210 
211 private:
212 
213  void load_weights(const std::string &input_fname);
214  int m_layers_cnt; // number of layers
215  std::vector<Layer *> m_layers; // container with layers
216 
217 };
218 
219 #endif
virtual unsigned int get_output_units() const
Definition: KerasModel.h:131
TString fin
Definition: Style.C:24
const XML_Char * name
Definition: expat.h:151
virtual unsigned int get_output_units() const
Definition: KerasModel.h:159
std::string m_name
Definition: KerasModel.h:119
virtual unsigned int get_input_rows() const
Definition: KerasModel.h:142
std::vector< std::vector< std::vector< float > > > const & get_3d() const
Definition: KerasModel.h:49
std::vector< std::vector< float > > conv_single_depth_same(std::vector< std::vector< float > > const &im, std::vector< std::vector< float > > const &k)
Definition: KerasModel.cxx:250
std::vector< Layer * > m_layers
Definition: KerasModel.h:215
virtual void read_from_file(const std::string &fname)
Definition: KerasModel.h:41
virtual unsigned int get_input_cols() const
Definition: KerasModel.h:174
std::vector< float > f
Definition: KerasModel.h:88
virtual void set_data(std::vector< std::vector< std::vector< float > > > const &)
Definition: KerasModel.h:38
unsigned int get_input_cols() const
Definition: KerasModel.h:208
virtual std::vector< std::vector< std::vector< float > > > const & get_3d() const
Definition: KerasModel.h:37
virtual size_t get_data_dim(void) const
Definition: KerasModel.h:35
Layer(std::string name)
Definition: KerasModel.h:111
size_t get_data_dim(void) const
Definition: KerasModel.h:92
virtual void show_values()=0
virtual unsigned int get_output_units() const
Definition: KerasModel.h:175
std::string get_name()
Definition: KerasModel.h:118
std::vector< float > m_bias
Definition: KerasModel.h:171
std::vector< std::vector< std::vector< std::vector< float > > > > m_kernels
Definition: KerasModel.h:170
const XML_Char const XML_Char * data
Definition: expat.h:268
std::vector< std::vector< float > > m_weights
Definition: KerasModel.h:190
virtual unsigned int get_output_units() const
Definition: KerasModel.h:144
virtual void show_name()=0
const int cols[3]
virtual void set_data(std::vector< float > const &)
Definition: KerasModel.h:39
std::string m_activation_type
Definition: KerasModel.h:161
virtual void set_data(std::vector< std::vector< std::vector< float > > > const &d)
Definition: KerasModel.h:51
std::string m_border_mode
Definition: KerasModel.h:177
unsigned int get_input_rows() const
Definition: KerasModel.h:207
Float_t d
Definition: plot.C:236
virtual ~DataChunk()
Definition: KerasModel.h:34
const double j
Definition: BetheBloch.cxx:29
void missing_activation_impl(const std::string &act)
Definition: KerasModel.cxx:176
std::vector< float > m_bias
Definition: KerasModel.h:191
void load_weights(std::ifstream &fin)
Definition: KerasModel.h:126
virtual unsigned int get_input_cols() const
Definition: KerasModel.h:130
DataChunkFlat(size_t size, float init)
Definition: KerasModel.h:86
virtual unsigned int get_input_rows() const
Definition: KerasModel.h:129
std::vector< std::vector< float > > conv_single_depth_valid(std::vector< std::vector< float > > const &im, std::vector< std::vector< float > > const &k)
Definition: KerasModel.cxx:225
std::vector< std::vector< std::vector< float > > > data
Definition: KerasModel.h:75
virtual unsigned int get_output_units() const
Definition: KerasModel.h:195
::xsd::cxx::tree::string< char, simple_type > string
Definition: Database.h:154
void read_from_file(const std::string &fname)
Definition: KerasModel.h:102
std::vector< float > const & get_1d() const
Definition: KerasModel.h:90
std::vector< float > & get_1d_rw()
Definition: KerasModel.h:89
virtual ~Layer()
Definition: KerasModel.h:112
DataChunkFlat(size_t size)
Definition: KerasModel.h:85
void set_data(std::vector< float > const &d)
Definition: KerasModel.h:91
size_t get_data_dim(void) const
Definition: KerasModel.h:52
virtual unsigned int get_input_rows() const
Definition: KerasModel.h:157
virtual unsigned int get_input_cols() const
Definition: KerasModel.h:158
virtual unsigned int get_input_cols() const
Definition: KerasModel.h:194
virtual unsigned int get_input_rows() const
Definition: KerasModel.h:173
virtual std::vector< float > const & get_1d() const
Definition: KerasModel.h:36
virtual unsigned int get_input_cols() const
Definition: KerasModel.h:143
std::vector< float > read_1d_array(std::ifstream &fin, int cols)
Definition: KerasModel.cxx:11
virtual unsigned int get_input_rows() const
Definition: KerasModel.h:193