13 #include "tensorflow/core/platform/env.h" 15 #include "tensorflow/core/public/session_options.h" 28 tensorflow::SessionOptions
options;
29 tensorflow::ConfigProto &
config = options.config;
30 config.set_inter_op_parallelism_threads(1);
31 config.set_intra_op_parallelism_threads(1);
32 config.set_use_per_session_threads(
false);
43 tensorflow::GraphDef graph_def;
52 size_t ng = graph_def.node().size();
73 for (
size_t n = 0;
n < ng; ++
n)
75 name = graph_def.node()[
n].name();
76 auto pos = name.find(
"/");
77 if (
pos != std::string::npos) { basename = name.substr(0,
pos); }
81 for (
const auto &
s : outputs)
83 if (name.find(
s) != std::string::npos) { found =
true;
break; }
87 if (!last.empty() && (basename !=
current))
99 std::cout <<
"Output nodes not found in the graph." <<
std::endl;
121 std::cout <<
"tf::CTPGraph::dtor: " <<
"Close failed." <<
std::endl;
132 const unsigned int nSamples =
input.size();
136 const unsigned int nEls =
input.front().at(0).size();
137 tensorflow::Tensor dEdxTensor(tensorflow::DT_FLOAT,tensorflow::TensorShape({nSamples,nEls,1}));
140 const unsigned int nVars =
input.front().at(1).size();
142 tensorflow::Tensor varsTensor(tensorflow::DT_FLOAT,tensorflow::TensorShape({nSamples,nVars}));
147 auto dedxInputMap = dEdxTensor.tensor<
float,3>();
148 auto varsInputMap = varsTensor.tensor<
float,2>();
149 for(
unsigned int s = 0;
s < nSamples; ++
s){
151 for(
unsigned int e = 0;
e < nEls; ++
e){
153 dedxInputMap(
s,
e,0) =
input.at(
s).at(0).at(
e);
156 for(
unsigned int v = 0; v < nVars; ++v){
158 varsInputMap(
s,v) =
input.at(
s).at(1).at(v);
162 std::vector<tensorflow::Tensor> inputTensors;
163 inputTensors.push_back(dEdxTensor);
164 inputTensors.push_back(varsTensor);
168 return run(inputTensors);
173 std::vector< std::vector< std::vector< float > > >
tf::CTPGraph::run(
const std::vector< tensorflow::Tensor > &
x)
176 std::vector< std::pair<std::string, tensorflow::Tensor> >
inputs;
183 std::vector<tensorflow::Tensor>
outputs;
189 std::vector< std::vector< std::vector<float> > >
result;
192 const unsigned int nOut = outputs.size();
193 unsigned int nSamples = 0;
195 for(
unsigned int o = 0; o < nOut; ++o){
197 nSamples = outputs[o].dim_size(0);
199 else if (nSamples != outputs[o].dim_size(0))
201 throw std::string(
"TF outputs size inconsistent.");
205 result.resize(nSamples,
std::vector< std::vector<float> >(nOut));
206 for(
unsigned int s = 0;
s < nSamples; ++
s){
207 for(
unsigned int o = 0; o < nOut; ++o){
209 auto output_map = outputs[o].tensor<
float,2>();
210 const unsigned int nNodes = outputs[o].dim_size(1);
211 result[
s][o].resize(nNodes);
212 for(
unsigned int n = 0;
n < nNodes; ++
n){
213 result[
s][o][
n] = output_map(
s,
n);
219 std::cout <<
"Processing error in Tensorflow. Returning empty output." <<
std::endl;
std::vector< float > run(const std::vector< std::vector< float > > &x)
CTPGraph(const char *graph_file_name, const std::vector< std::string > &outputs, bool &success, int ninputs, int noutputs)
Not-throwing constructor.
tensorflow::Session * fSession
std::vector< std::string > fInputNames
std::vector< std::string > fOutputNames
QTextStream & endl(QTextStream &s)