TFNetHandler.cxx
Go to the documentation of this file.
1 ////////////////////////////////////////////////////////////////////////
2 /// \file TFNetHandler.cxx
3 /// \brief TFNetHandler for CVN
4 /// \author Alexander Radovic - a.radovic@gmail.com
5 /// Leigh Whitehead - leigh.howard.whitehead@cern.ch
6 /// Saul Alonso Monsalve - saul.alonso.monsalve@cern.ch
7 ////////////////////////////////////////////////////////////////////////
8 
9 #include <iostream>
10 #include <string>
11 #include "cetlib/getenv.h"
12 
15 
18 
19 namespace cvn
20 {
21 
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  }
40 
41  // Check the network outputs
42  bool check(const std::vector< std::vector< float > > & outputs)
43  {
44  if (outputs.size() == 1) return true;
45  size_t aux = 0;
46  for (size_t o = 0; o < outputs.size(); ++o)
47  {
48  size_t aux2 = 0;
49 
50  for (size_t i = 0; i < outputs[o].size(); ++i)
51  if (outputs[o][i] == 0.0 || outputs[o][i] == 1.0)
52  aux2++;
53  if (aux2 == outputs[o].size()) aux++;
54  }
55  return aux == outputs.size() ? false : true;
56  }
57 
58  // Fill outputs with value -3
59  void fillEmpty(std::vector< std::vector< float > > & outputs)
60  {
61  for (size_t o = 0; o < outputs.size(); ++o)
62  {
63  for (size_t i = 0; i < outputs[o].size(); ++i)
64  outputs[o][i] = -3.0;
65  }
66  return;
67  }
68 
69  std::vector< std::vector<float> > TFNetHandler::Predict(const PixelMap& pm)
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  }
118 
119  /*
120  // The standard output has 13 elements, this function sums the convenient ones
121  std::vector<float> TFNetHandler::PredictFlavour(const PixelMap& pm){
122 
123  std::vector<float> fullResults = this->Predict(pm);
124 
125  std::vector<float> flavourResults;
126 
127  // First element is CC numu
128  float sumNumu = fullResults[0] + fullResults[1] + fullResults[2] + fullResults[3];
129  // Then CC nue
130  float sumNue = fullResults[4] + fullResults[5] + fullResults[6] + fullResults[7];
131  // Then CC nutau
132  float sumNutau = fullResults[8] + fullResults[9] + fullResults[10] + fullResults[11];
133  // End with NC
134  float sumNC = fullResults[12];
135 
136  flavourResults.push_back(sumNumu);
137  flavourResults.push_back(sumNue);
138  flavourResults.push_back(sumNutau);
139  flavourResults.push_back(sumNC);
140 
141  return flavourResults;
142  }
143  */
144 
145 }
146 
std::vector< ViewVectorF > ImageVectorF
Definition: CVNImageUtils.h:21
TFNetHandler(const fhicl::ParameterSet &pset)
Constructor which takes a pset with DeployProto and ModelFile fields.
std::vector< std::vector< float > > Predict(const PixelMap &pm)
Return prediction arrays for PixelMap.
std::string string
Definition: nybbler.cc:12
MaybeLogger_< ELseverityLevel::ELsev_info, false > LogInfo
void SetViewReversal(bool reverseX, bool reverseY, bool reverseZ)
Function to set any views that need reversing.
void SetLogScale(bool setLog)
Set the log scale for charge.
unsigned int fImageWires
Number of wires for the network to classify.
Definition: TFNetHandler.h:46
struct vector vector
STL namespace.
Utility class for truth labels.
bool check(const std::vector< std::vector< float > > &outputs)
std::unique_ptr< tf::Graph > fTFGraph
Tensorflow graph.
Definition: TFNetHandler.h:49
decltype(auto) constexpr size(T &&obj)
ADL-aware version of std::size.
Definition: StdUtils.h:92
unsigned int fImageTDCs
Number of tdcs for the network to classify.
Definition: TFNetHandler.h:47
Utilities for producing images for the CVN.
std::string getenv(std::string const &name)
Definition: getenv.cc:15
T get(std::string const &key) const
Definition: ParameterSet.h:271
TFNetHandler for CVN.
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
void ConvertPixelMapToImageVectorF(const PixelMap &pm, ImageVectorF &imageVec)
Convert a pixel map into an image vector (float version)
Class containing some utility functions for all things CVN.
Definition: CVNImageUtils.h:24
cet::coded_exception< errors::ErrorCodes, ExceptionDetail::translate > Exception
Definition: Exception.h:66
void SetImageSize(unsigned int nWires, unsigned int nTDCs, unsigned int nViews)
Set up the image size that we want to have.
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
PixelMap, basic input to CVN neural net.
Definition: PixelMap.h:22
void fillEmpty(std::vector< std::vector< float > > &outputs)
auto const & get(AssnsNode< L, R, D > const &r)
Definition: AssnsNode.h:115
int bool
Definition: qglobal.h:345
QTextStream & endl(QTextStream &s)
std::string fTFProtoBuf
location of the tf .pb file in the above path
Definition: TFNetHandler.h:44