LArSoft  v06_85_00
Liquid Argon Software toolkit - http://larsoft.org/
nnet::TfModelInterface Class Reference

#include "PointIdAlg.h"

Inheritance diagram for nnet::TfModelInterface:
nnet::ModelInterface

Public Member Functions

 TfModelInterface (const char *modelFileName)
 
std::vector< std::vector< float > > Run (std::vector< std::vector< std::vector< float > > > const &inps, int samples=-1) override
 
std::vector< float > Run (std::vector< std::vector< float > > const &inp2d) override
 

Protected Member Functions

std::string findFile (const char *fileName) const
 

Private Attributes

std::unique_ptr< tf::Graphg
 

Detailed Description

Definition at line 76 of file PointIdAlg.h.

Constructor & Destructor Documentation

nnet::TfModelInterface::TfModelInterface ( const char *  modelFileName)

Definition at line 97 of file PointIdAlg.cxx.

References tf::Graph::create(), nnet::ModelInterface::findFile(), and art::errors::Unknown.

98 {
99  g = tf::Graph::create(nnet::ModelInterface::findFile(modelFileName).c_str(), {"cnn_output", "_netout"});
100  if (!g) { throw art::Exception(art::errors::Unknown) << "TF model failed."; }
101 
102  mf::LogInfo("TfModelInterface") << "TF model loaded.";
103 }
MaybeLogger_< ELseverityLevel::ELsev_info, false > LogInfo
static std::unique_ptr< Graph > create(const char *graph_file_name, const std::vector< std::string > &outputs={})
Definition: tf_graph.h:29
cet::coded_exception< errors::ErrorCodes, ExceptionDetail::translate > Exception
Definition: Exception.h:66
std::string findFile(const char *fileName) const
Definition: PointIdAlg.cxx:51
std::unique_ptr< tf::Graph > g
Definition: PointIdAlg.h:85

Member Function Documentation

std::string nnet::ModelInterface::findFile ( const char *  fileName) const
protectedinherited

Definition at line 51 of file PointIdAlg.cxx.

References art::errors::NotFound.

Referenced by nnet::ModelInterface::ModelInterface(), and TfModelInterface().

52 {
53  std::string fname_out;
54  cet::search_path sp("FW_SEARCH_PATH");
55  if (!sp.find_file(fileName, fname_out))
56  {
57  struct stat buffer;
58  if (stat(fileName, &buffer) == 0) { fname_out = fileName; }
59  else
60  {
62  << "Could not find the model file " << fileName;
63  }
64  }
65  return fname_out;
66 }
cet::coded_exception< errors::ErrorCodes, ExceptionDetail::translate > Exception
Definition: Exception.h:66
std::vector< std::vector< float > > nnet::TfModelInterface::Run ( std::vector< std::vector< std::vector< float > > > const &  inps,
int  samples = -1 
)
overridevirtual

Reimplemented from nnet::ModelInterface.

Definition at line 106 of file PointIdAlg.cxx.

References s, and lar::dump::vector().

107 {
108  if ((samples == 0) || inps.empty() || inps.front().empty() || inps.front().front().empty())
109  return std::vector< std::vector<float> >();
110 
111  if ((samples == -1) || (samples > (long long int)inps.size())) { samples = inps.size(); }
112 
113  long long int rows = inps.front().size(), cols = inps.front().front().size();
114 
115  tensorflow::Tensor _x(tensorflow::DT_FLOAT, tensorflow::TensorShape({ samples, rows, cols, 1 }));
116  auto input_map = _x.tensor<float, 4>();
117  for (long long int s = 0; s < samples; ++s) {
118  const auto & sample = inps[s];
119  for (long long int r = 0; r < rows; ++r) {
120  const auto & row = sample[r];
121  for (long long int c = 0; c < cols; ++c) {
122  input_map(s, r, c, 0) = row[c];
123  }
124  }
125  }
126 
127  return g->run(_x);
128 }
Float_t s
Definition: plot.C:23
auto vector(Vector const &v)
Returns a manipulator which will print the specified array.
Definition: DumpUtils.h:265
std::unique_ptr< tf::Graph > g
Definition: PointIdAlg.h:85
std::vector< float > nnet::TfModelInterface::Run ( std::vector< std::vector< float > > const &  inp2d)
overridevirtual

Implements nnet::ModelInterface.

Definition at line 131 of file PointIdAlg.cxx.

132 {
133  long long int rows = inp2d.size(), cols = inp2d.front().size();
134 
135  tensorflow::Tensor _x(tensorflow::DT_FLOAT, tensorflow::TensorShape({ 1, rows, cols, 1 }));
136  auto input_map = _x.tensor<float, 4>();
137  for (long long int r = 0; r < rows; ++r) {
138  const auto & row = inp2d[r];
139  for (long long int c = 0; c < cols; ++c) {
140  input_map(0, r, c, 0) = row[c];
141  }
142  }
143 
144  auto out = g->run(_x);
145  if (!out.empty()) return out.front();
146  else return std::vector<float>();
147 }
std::unique_ptr< tf::Graph > g
Definition: PointIdAlg.h:85

Member Data Documentation

std::unique_ptr<tf::Graph> nnet::TfModelInterface::g
private

Definition at line 85 of file PointIdAlg.h.


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