LArSoft  v09_90_00
Liquid Argon Software toolkit - https://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 229 of file keras_model.h.

Constructor & Destructor Documentation

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

Definition at line 123 of file keras_model.cc.

124 {
125  load_weights(input_fname);
126 }
void load_weights(const std::string &input_fname)
Definition: keras_model.cc:452
keras::KerasModel::~KerasModel ( )

Definition at line 495 of file keras_model.cc.

496 {
497  for (int i = 0; i < (int)m_layers.size(); ++i) {
498  delete m_layers[i];
499  }
500 }
std::vector< Layer * > m_layers
Definition: keras_model.h:242

Member Function Documentation

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

Definition at line 421 of file keras_model.cc.

References keras::DataChunk::get_1d().

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

422 {
423  //cout << endl << "KerasModel compute output" << endl;
424  //cout << "Input data size:" << endl;
425  //dc->show_name();
426 
427  keras::DataChunk* inp = dc;
428  keras::DataChunk* out = 0;
429  for (int l = 0; l < (int)m_layers.size(); ++l) {
430  //cout << "Processing layer " << m_layers[l]->get_name() << endl;
431  out = m_layers[l]->compute_output(inp);
432 
433  //cout << "Input" << endl;
434  //inp->show_name();
435  //cout << "Output" << endl;
436  //out->show_name();
437 
438  if (inp != dc) delete inp;
439  inp = 0L;
440  inp = out;
441  }
442 
443  //cout << "Output: ";
444  //out->show_values();
445 
446  std::vector<float> flat_out = out->get_1d();
447  delete out;
448 
449  return flat_out;
450 }
std::vector< Layer * > m_layers
Definition: keras_model.h:242
virtual std::vector< float > const & get_1d() const
Definition: keras_model.h:48
unsigned int keras::KerasModel::get_input_cols ( ) const
inline

Definition at line 236 of file keras_model.h.

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

Definition at line 235 of file keras_model.h.

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

Definition at line 502 of file keras_model.cc.

503 {
504  int i = m_layers.size() - 1;
505  while ((i > 0) && (m_layers[i]->get_output_units() == 0))
506  --i;
507  return m_layers[i]->get_output_units();
508 }
std::vector< Layer * > m_layers
Definition: keras_model.h:242
void keras::KerasModel::load_weights ( const std::string &  input_fname)
private

Definition at line 452 of file keras_model.cc.

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

453 {
454  cout << "Reading model from " << input_fname << endl;
455  ifstream fin(input_fname.c_str());
456  string layer_type = "";
457  string tmp_str = "";
458  int tmp_int = 0;
459 
460  fin >> tmp_str >> m_layers_cnt;
461  cout << "Layers " << m_layers_cnt << endl;
462 
463  for (int layer = 0; layer < m_layers_cnt; ++layer) { // iterate over layers
464  fin >> tmp_str >> tmp_int >> layer_type;
465  cout << "Layer " << tmp_int << " " << layer_type << endl;
466 
467  Layer* l = 0L;
468  if (layer_type == "Convolution2D") { l = new LayerConv2D(); }
469  else if (layer_type == "Activation") {
470  l = new LayerActivation();
471  }
472  else if (layer_type == "MaxPooling2D") {
473  l = new LayerMaxPooling();
474  }
475  else if (layer_type == "Flatten") {
476  l = new LayerFlatten();
477  }
478  else if (layer_type == "Dense") {
479  l = new LayerDense();
480  }
481  else if (layer_type == "Dropout") {
482  continue; // we dont need dropout layer in prediciton mode
483  }
484  if (l == 0L) {
485  cout << "Layer is empty, maybe it is not defined? Cannot define network." << endl;
486  return;
487  }
488  l->load_weights(fin);
489  m_layers.push_back(l);
490  }
491 
492  fin.close();
493 }
TString fin
Definition: Style.C:24
std::vector< Layer * > m_layers
Definition: keras_model.h:242

Member Data Documentation

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

Definition at line 242 of file keras_model.h.

int keras::KerasModel::m_layers_cnt
private

Definition at line 241 of file keras_model.h.


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