LArSoft  v06_85_00
Liquid Argon Software toolkit - http://larsoft.org/
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 119 of file keras_model.cc.

119  {
120  load_weights(input_fname);
121 }
void load_weights(const std::string &input_fname)
Definition: keras_model.cc:433
keras::KerasModel::~KerasModel ( )

Definition at line 472 of file keras_model.cc.

472  {
473  for(int i = 0; i < (int)m_layers.size(); ++i) {
474  delete m_layers[i];
475  }
476 }
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 403 of file keras_model.cc.

References keras::DataChunk::get_1d().

Referenced by main(), and nnet::KerasModelInterface::Run().

403  {
404  //cout << endl << "KerasModel compute output" << endl;
405  //cout << "Input data size:" << endl;
406  //dc->show_name();
407 
408  keras::DataChunk *inp = dc;
409  keras::DataChunk *out = 0;
410  for(int l = 0; l < (int)m_layers.size(); ++l) {
411  //cout << "Processing layer " << m_layers[l]->get_name() << endl;
412  out = m_layers[l]->compute_output(inp);
413 
414  //cout << "Input" << endl;
415  //inp->show_name();
416  //cout << "Output" << endl;
417  //out->show_name();
418 
419  if (inp != dc) delete inp;
420  inp = 0L;
421  inp = out;
422  }
423 
424  //cout << "Output: ";
425  //out->show_values();
426 
427  std::vector<float> flat_out = out->get_1d();
428  delete out;
429 
430  return flat_out;
431 }
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 478 of file keras_model.cc.

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

Definition at line 433 of file keras_model.cc.

References fin, and keras::Layer::load_weights().

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

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: