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