Public Member Functions | Private Member Functions | Private Attributes | List of all members
keras::KerasModel Class Reference

#include <keras_model.h>

Public Member Functions

 KerasModel (const std::string &input_fname)
 
 ~KerasModel ()
 
std::vector< float > compute_output (keras::DataChunk *dc)
 
unsigned int get_input_rows () const
 
unsigned int get_input_cols () const
 
int get_output_length () const
 

Private Member Functions

void load_weights (const std::string &input_fname)
 

Private Attributes

int m_layers_cnt
 
std::vector< Layer * > m_layers
 

Detailed Description

Definition at line 214 of file keras_model.h.

Constructor & Destructor Documentation

keras::KerasModel::KerasModel ( const std::string input_fname)

Definition at line 116 of file keras_model.cc.

116  {
117  load_weights(input_fname);
118 }
void load_weights(const std::string &input_fname)
Definition: keras_model.cc:430
keras::KerasModel::~KerasModel ( )

Definition at line 469 of file keras_model.cc.

469  {
470  for(int i = 0; i < (int)m_layers.size(); ++i) {
471  delete m_layers[i];
472  }
473 }
std::vector< Layer * > m_layers
Definition: keras_model.h:228

Member Function Documentation

std::vector< float > keras::KerasModel::compute_output ( keras::DataChunk dc)

Definition at line 400 of file keras_model.cc.

400  {
401  //cout << endl << "KerasModel compute output" << endl;
402  //cout << "Input data size:" << endl;
403  //dc->show_name();
404 
405  keras::DataChunk *inp = dc;
406  keras::DataChunk *out = 0;
407  for(int l = 0; l < (int)m_layers.size(); ++l) {
408  //cout << "Processing layer " << m_layers[l]->get_name() << endl;
409  out = m_layers[l]->compute_output(inp);
410 
411  //cout << "Input" << endl;
412  //inp->show_name();
413  //cout << "Output" << endl;
414  //out->show_name();
415 
416  if (inp != dc) delete inp;
417  inp = 0L;
418  inp = out;
419  }
420 
421  //cout << "Output: ";
422  //out->show_values();
423 
424  std::vector<float> flat_out = out->get_1d();
425  delete out;
426 
427  return flat_out;
428 }
static QStrList * l
Definition: config.cpp:1044
std::vector< Layer * > m_layers
Definition: keras_model.h:228
virtual std::vector< float > const & get_1d() const
Definition: keras_model.h:45
unsigned int keras::KerasModel::get_input_cols ( ) const
inline

Definition at line 221 of file keras_model.h.

221 { return m_layers.front()->get_input_cols(); }
std::vector< Layer * > m_layers
Definition: keras_model.h:228
unsigned int keras::KerasModel::get_input_rows ( ) const
inline

Definition at line 220 of file keras_model.h.

220 { return m_layers.front()->get_input_rows(); }
std::vector< Layer * > m_layers
Definition: keras_model.h:228
int keras::KerasModel::get_output_length ( ) const

Definition at line 475 of file keras_model.cc.

476 {
477  int i = m_layers.size() - 1;
478  while ((i > 0) && (m_layers[i]->get_output_units() == 0)) --i;
479  return m_layers[i]->get_output_units();
480 }
std::vector< Layer * > m_layers
Definition: keras_model.h:228
void keras::KerasModel::load_weights ( const std::string input_fname)
private

Definition at line 430 of file keras_model.cc.

430  {
431  cout << "Reading model from " << input_fname << endl;
432  ifstream fin(input_fname.c_str());
433  string layer_type = "";
434  string tmp_str = "";
435  int tmp_int = 0;
436 
437  fin >> tmp_str >> m_layers_cnt;
438  cout << "Layers " << m_layers_cnt << endl;
439 
440  for(int layer = 0; layer < m_layers_cnt; ++layer) { // iterate over layers
441  fin >> tmp_str >> tmp_int >> layer_type;
442  cout << "Layer " << tmp_int << " " << layer_type << endl;
443 
444  Layer *l = 0L;
445  if(layer_type == "Convolution2D") {
446  l = new LayerConv2D();
447  } else if(layer_type == "Activation") {
448  l = new LayerActivation();
449  } else if(layer_type == "MaxPooling2D") {
450  l = new LayerMaxPooling();
451  } else if(layer_type == "Flatten") {
452  l = new LayerFlatten();
453  } else if(layer_type == "Dense") {
454  l = new LayerDense();
455  } else if(layer_type == "Dropout") {
456  continue; // we dont need dropout layer in prediciton mode
457  }
458  if(l == 0L) {
459  cout << "Layer is empty, maybe it is not defined? Cannot define network." << endl;
460  return;
461  }
462  l->load_weights(fin);
463  m_layers.push_back(l);
464  }
465 
466  fin.close();
467 }
static QStrList * l
Definition: config.cpp:1044
std::vector< Layer * > m_layers
Definition: keras_model.h:228
QTextStream & endl(QTextStream &s)

Member Data Documentation

std::vector<Layer *> keras::KerasModel::m_layers
private

Definition at line 228 of file keras_model.h.

int keras::KerasModel::m_layers_cnt
private

Definition at line 227 of file keras_model.h.


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