13 #include "tensorflow/core/platform/env.h" 15 #include "tensorflow/core/public/session_options.h" 23 tensorflow::SessionOptions
options;
24 tensorflow::ConfigProto &
config = options.config;
25 config.set_inter_op_parallelism_threads(1);
26 config.set_intra_op_parallelism_threads(1);
27 config.set_use_per_session_threads(
false);
36 tensorflow::GraphDef graph_def;
44 size_t ng = graph_def.node().size();
48 if (outputs.empty()) {
fOutputNames.push_back(graph_def.node()[ng - 1].name()); }
52 for (
size_t n = 0;
n < ng; ++
n)
54 name = graph_def.node()[
n].name();
55 auto pos = name.find(
"/");
56 if (
pos != std::string::npos) { basename = name.substr(0,
pos); }
60 for (
const auto &
s : outputs)
62 if (name.find(
s) != std::string::npos) { found =
true;
break; }
66 if (!last.empty() && (basename !=
current))
78 std::cout <<
"Output nodes not found in the graph." <<
std::endl;
101 if (
x.empty() ||
x.front().empty()) {
return std::vector<float>(); }
103 long long int rows =
x.size(), cols =
x.front().size();
105 tensorflow::Tensor _x(tensorflow::DT_FLOAT, tensorflow::TensorShape({ 1, rows, cols, 1 }));
106 auto input_map = _x.tensor<
float, 4>();
108 for (
long long int r = 0;
r < rows; ++
r) {
109 const auto &
row =
x[
r];
110 for (
long long int c = 0;
c < cols; ++
c) {
111 input_map(0,
r,
c, 0) =
row[
c];
117 else {
return std::vector<float>(); }
123 long long int samples)
125 if ((samples == 0) ||
x.empty() ||
x.front().empty() ||
x.front().front().empty() ||
x.front().front().front().empty())
128 if ((samples == -1) || (samples > (
long long int)
x.size())) { samples =
x.size(); }
131 rows =
x.front().size(),
132 cols =
x.front().front().size(),
133 depth =
x.front().front().front().size();
135 tensorflow::Tensor _x(tensorflow::DT_FLOAT, tensorflow::TensorShape({ samples, rows, cols, depth }));
136 auto input_map = _x.tensor<
float, 4>();
137 for (
long long int s = 0;
s < samples; ++
s) {
138 const auto & sample =
x[
s];
139 for (
long long int r = 0;
r < rows; ++
r) {
140 const auto &
row = sample[
r];
141 for (
long long int c = 0;
c < cols; ++
c) {
142 const auto & col =
row[
c];
143 for (
long long int d = 0;
d < depth; ++
d) {
144 input_map(
s,
r,
c,
d) = col[
d];
156 std::vector< std::pair<std::string, tensorflow::Tensor> >
inputs = {
162 std::vector<tensorflow::Tensor>
outputs;
169 size_t samples = 0, nouts = 0;
170 for (
size_t o = 0; o < outputs.size(); ++o)
172 if (o == 0) { samples = outputs[o].dim_size(0); }
173 else if ((
int)samples != outputs[o].dim_size(0))
175 throw std::string(
"TF outputs size inconsistent.");
177 nouts += outputs[o].dim_size(1);
181 std::vector< std::vector< float > >
result;
182 result.resize(samples, std::vector< float >(nouts));
185 for (
size_t o = 0; o < outputs.size(); ++o)
187 auto output_map = outputs[o].tensor<
float, 2>();
189 size_t n = outputs[o].dim_size(1);
190 for (
size_t s = 0;
s < samples; ++
s) {
191 std::vector< float > & vs = result[
s];
192 for (
size_t i = 0; i <
n; ++i) {
193 vs[idx0 + i] = output_map(
s, i);
203 return std::vector< std::vector< float > >();
std::vector< float > run(const std::vector< std::vector< float > > &x)
tensorflow::Session * fSession
std::vector< std::string > fOutputNames
Graph(const char *graph_file_name, const std::vector< std::string > &outputs, bool &success, int ninputs, int noutputs)
Not-throwing constructor.
QTextStream & endl(QTextStream &s)