Public Member Functions | Private Attributes | List of all members
cvn::TFNetHandler Class Reference

Wrapper for caffe::Net which handles construction and prediction. More...

#include <TFNetHandler.h>

Public Member Functions

 TFNetHandler (const fhicl::ParameterSet &pset)
 Constructor which takes a pset with DeployProto and ModelFile fields. More...
 
int NOutput () const
 Number of outputs in neural net. More...
 
int NFeatures () const
 Number of outputs in neural net. More...
 
std::vector< std::vector< float > > Predict (const PixelMap &pm)
 Return prediction arrays for PixelMap. More...
 
std::vector< float > PredictFlavour (const PixelMap &pm)
 Return four element vector with summed numu, nue, nutau and NC elements. More...
 

Private Attributes

std::string fLibPath
 Library path (typically dune_pardata...) More...
 
std::string fTFProtoBuf
 location of the tf .pb file in the above path More...
 
bool fUseLogChargeScale
 Is the charge using a log scale? More...
 
unsigned int fImageWires
 Number of wires for the network to classify. More...
 
unsigned int fImageTDCs
 Number of tdcs for the network to classify. More...
 
std::vector< boolfReverseViews
 Do we need to reverse any views? More...
 
std::unique_ptr< tf::GraphfTFGraph
 Tensorflow graph. More...
 

Detailed Description

Wrapper for caffe::Net which handles construction and prediction.

Definition at line 22 of file TFNetHandler.h.

Constructor & Destructor Documentation

cvn::TFNetHandler::TFNetHandler ( const fhicl::ParameterSet pset)

Constructor which takes a pset with DeployProto and ModelFile fields.

Definition at line 22 of file TFNetHandler.cxx.

22  :
23  fLibPath(cet::getenv(pset.get<std::string>("LibPath", ""))),
24  fTFProtoBuf (fLibPath+"/"+pset.get<std::string>("TFProtoBuf")),
25  fUseLogChargeScale(pset.get<bool>("ChargeLogScale")),
26  fImageWires(pset.get<unsigned int>("NImageWires")),
27  fImageTDCs(pset.get<unsigned int>("NImageTDCs")),
28  fReverseViews(pset.get<std::vector<bool> >("ReverseViews"))
29  {
30 
31  // Construct the TF Graph object. The empty vector {} is used since the protobuf
32  // file gives the names of the output layer nodes
33  mf::LogInfo("TFNetHandler") << "Loading network: " << fTFProtoBuf << std::endl;
34  fTFGraph = tf::Graph::create(fTFProtoBuf.c_str(),{},pset.get<int>("NInputs"),pset.get<int>("NOutputs"));
35  if(!fTFGraph){
36  art::Exception(art::errors::Unknown) << "Tensorflow model not found or incorrect";
37  }
38 
39  }
std::string string
Definition: nybbler.cc:12
MaybeLogger_< ELseverityLevel::ELsev_info, false > LogInfo
unsigned int fImageWires
Number of wires for the network to classify.
Definition: TFNetHandler.h:46
std::unique_ptr< tf::Graph > fTFGraph
Tensorflow graph.
Definition: TFNetHandler.h:49
unsigned int fImageTDCs
Number of tdcs for the network to classify.
Definition: TFNetHandler.h:47
std::string getenv(std::string const &name)
Definition: getenv.cc:15
std::string fLibPath
Library path (typically dune_pardata...)
Definition: TFNetHandler.h:43
T get(std::string const &key) const
Definition: ParameterSet.h:271
static std::unique_ptr< Graph > create(const char *graph_file_name, const std::vector< std::string > &outputs={}, int ninputs=1, int noutputs=1)
Definition: tf_graph.h:32
cet::coded_exception< errors::ErrorCodes, ExceptionDetail::translate > Exception
Definition: Exception.h:66
bool fUseLogChargeScale
Is the charge using a log scale?
Definition: TFNetHandler.h:45
std::vector< bool > fReverseViews
Do we need to reverse any views?
Definition: TFNetHandler.h:48
QTextStream & endl(QTextStream &s)
std::string fTFProtoBuf
location of the tf .pb file in the above path
Definition: TFNetHandler.h:44

Member Function Documentation

int cvn::TFNetHandler::NFeatures ( ) const

Number of outputs in neural net.

int cvn::TFNetHandler::NOutput ( ) const

Number of outputs in neural net.

std::vector< std::vector< float > > cvn::TFNetHandler::Predict ( const PixelMap pm)

Return prediction arrays for PixelMap.

Definition at line 69 of file TFNetHandler.cxx.

70  {
71 
72  CVNImageUtils imageUtils;
73 
74  // Configure the image utility
75  imageUtils.SetViewReversal(fReverseViews);
76  imageUtils.SetImageSize(fImageWires,fImageTDCs,3);
77  imageUtils.SetLogScale(fUseLogChargeScale);
78 
79  ImageVectorF thisImage;
80  imageUtils.ConvertPixelMapToImageVectorF(pm,thisImage);
81  std::vector<ImageVectorF> vecForTF;
82 
83  vecForTF.push_back(thisImage);
84 
85  std::vector< std::vector< std::vector< float > > > cvnResults; // shape(samples, #outputs, output_size)
86  bool status = false;
87 
88  int counter = 0;
89 
90  do{ // do until it gets a correct result
91  // std::cout << "Number of CVN result vectors " << cvnResults.size() << " with " << cvnResults[0].size() << " categories" << std::endl;
92  cvnResults = fTFGraph->run(vecForTF);
93  status = check(cvnResults[0]);
94  //std::cout << "Status: " << status << std::endl;
95  counter++;
96  if(counter==10){
97  std::cout << "Error, CVN never outputing a correct result. Filling result with zeros.";
98  std::cout << std::endl;
99  fillEmpty(cvnResults[0]);
100  break;
101  }
102  }while(status == false);
103 
104  std::cout << "Classifier summary: ";
105  std::cout << std::endl;
106  int output_index = 0;
107  for(auto const & output : cvnResults[0])
108  {
109  std::cout << "Output " << output_index++ << ": ";
110  for(auto const v : output)
111  std::cout << v << ", ";
112  std::cout << std::endl;
113  }
114  std::cout << std::endl;
115 
116  return cvnResults[0];
117  }
std::vector< ViewVectorF > ImageVectorF
Definition: CVNImageUtils.h:21
unsigned int fImageWires
Number of wires for the network to classify.
Definition: TFNetHandler.h:46
bool check(const std::vector< std::vector< float > > &outputs)
std::unique_ptr< tf::Graph > fTFGraph
Tensorflow graph.
Definition: TFNetHandler.h:49
unsigned int fImageTDCs
Number of tdcs for the network to classify.
Definition: TFNetHandler.h:47
bool fUseLogChargeScale
Is the charge using a log scale?
Definition: TFNetHandler.h:45
std::vector< bool > fReverseViews
Do we need to reverse any views?
Definition: TFNetHandler.h:48
void fillEmpty(std::vector< std::vector< float > > &outputs)
QTextStream & endl(QTextStream &s)
std::vector<float> cvn::TFNetHandler::PredictFlavour ( const PixelMap pm)

Return four element vector with summed numu, nue, nutau and NC elements.

Member Data Documentation

unsigned int cvn::TFNetHandler::fImageTDCs
private

Number of tdcs for the network to classify.

Definition at line 47 of file TFNetHandler.h.

unsigned int cvn::TFNetHandler::fImageWires
private

Number of wires for the network to classify.

Definition at line 46 of file TFNetHandler.h.

std::string cvn::TFNetHandler::fLibPath
private

Library path (typically dune_pardata...)

Definition at line 43 of file TFNetHandler.h.

std::vector<bool> cvn::TFNetHandler::fReverseViews
private

Do we need to reverse any views?

Definition at line 48 of file TFNetHandler.h.

std::unique_ptr<tf::Graph> cvn::TFNetHandler::fTFGraph
private

Tensorflow graph.

Definition at line 49 of file TFNetHandler.h.

std::string cvn::TFNetHandler::fTFProtoBuf
private

location of the tf .pb file in the above path

Definition at line 44 of file TFNetHandler.h.

bool cvn::TFNetHandler::fUseLogChargeScale
private

Is the charge using a log scale?

Definition at line 45 of file TFNetHandler.h.


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