TensorflowHandler.cxx
Go to the documentation of this file.
1 ////////////////////////////////////////////////////////////////////////
2 /// \file TensorflowHandler.h
3 /// \brief TensorflowHandler for ShowerLID
4 /// \author Andrew Vold - voldx034@umn.edu
5 ////////////////////////////////////////////////////////////////////////
6 
7 #include <iostream>
8 #include <string>
11 #include "ShowerLID/ShowerLID.h"
13 #include "tensorflow/core/public/session.h"
14 #include "tensorflow/core/platform/env.h"
15 #include "TVector3.h"
16 
17 namespace tensorflow
18 {
20  {
21  }
22 
23  //Much of code written by Jim Fleming
24  //https://medium.com/jim-fleming/loading-a-tensorflow-graph-with-the-c-api-4caaff88463f
25  float TensorflowHandler::Predict(float sliceE,std::map<int,std::vector<float>> featuremap,bool fRHC)
26  {
27 
28  //Initialize session
29  Session* session;
30  Status status = NewSession(SessionOptions(), &session);
31  if (!status.ok()) {
32  std::cout << status.ToString() << "\n";
33  return -5.0;
34  }
35 
36  std::string Model = "/nova/app/users/vold/ART2/ShowerLID/FHC_Model.pb";
37  if(fRHC) Model = "/nova/app/users/vold/ART2/ShowerLID/RHC_Model.pb";
38 
39  GraphDef graph_def;
40  status = ReadBinaryProto(Env::Default(),Model, &graph_def);
41  if (!status.ok()){
42  std::cout << status.ToString() << "\n";
43  return -5.0;
44  }
45 
46  //Add graph to session
47  status = session->Create(graph_def);
48  if (!status.ok()) {
49  std::cout << status.ToString() << "\n";
50  return -5.0;
51  }
52 
53  //Set inputs and outputs
54  Tensor aux_input(DT_FLOAT, TensorShape({1,1}));
55  Tensor main_input(DT_FLOAT, TensorShape({1,7,19}));
56  auto Slc = aux_input.tensor<float,2>();
57  auto Shw = main_input.tensor<float,3>();
58 
59  Slc(0,0) = sliceE;
60 
61  int num1 = 0;
62  for(std::map<int,std::vector<float>>::iterator i = featuremap.begin(); i != featuremap.end(); i++){
63  std::vector<float> vec = i->second;
64  int num2 = 0;
65  for(std::vector<float>::iterator j = vec.begin(); j != vec.end(); j++){
66  Shw(0,num1,num2) = *j;
67  num2++;
68  }
69  num1++;
70  }
71 /*
72  for(int i = 0; i < 7; i++){
73  for(int j = 0; j < 19; j++){
74  Shw(0,i,j) = featuremap[i][j];
75  }
76  }
77 */
78  std::vector<std::pair<string,Tensor>> inputs = {
79  { "main_input", main_input },
80  { "aux_input", aux_input },
81  };
82 
83  //Initialize outputs
84  std::vector<Tensor> outputs;
85 
86  //Run session
87  status = session->Run(inputs,{"output_node0"},{},&outputs);
88  if (!status.ok()){
89  std::cout << status.ToString() << "\n";
90  return -5.0;
91  }
92 
93  //Graph output and convert output to scalar
94  auto output_c = outputs[0].scalar<float>();
95 
96  //Print for debugging purposes
97 // std::cout << outputs[0].DebugString() << "\n";
98 // std::cout << output_c() << "\n";
99 
100  //Free up resources
101  session->Close();
102 
103  return output_c();
104 
105  }//End Predict
106 
107 }//End namespace slid
TensorflowHandler(fhicl::ParameterSet const &pset)
Basic constructor, takes path to deploy model pb.
int status
Definition: fabricate.py:1613
rosenbrock_model_namespace::rosenbrock_model Model
Int_t num1[12]
Definition: f2_nu.C:142
TensorflowHandler for ShowerLID.
const double j
Definition: BetheBloch.cxx:29
Eigen::VectorXd vec
OStream cout
Definition: OStream.cxx:6
::xsd::cxx::tree::string< char, simple_type > string
Definition: Database.h:154
float Predict(float sliceE, std::map< int, std::vector< float >> featuremap, bool fRHC)