12 #include "tensorflow/core/public/session.h" 13 #include "tensorflow/core/platform/env.h" 16 tf::Graph::Graph(
const char* graph_file_name,
const std::vector<std::string> & outputs,
bool & success)
20 auto status = tensorflow::NewSession(tensorflow::SessionOptions(), &
fSession);
23 std::cout << status.ToString() << std::endl;
27 tensorflow::GraphDef graph_def;
28 status = tensorflow::ReadBinaryProto(tensorflow::Env::Default(), graph_file_name, &graph_def);
31 std::cout << status.ToString() << std::endl;
35 size_t ng = graph_def.node().size();
39 if (outputs.empty()) {
fOutputNames.push_back(graph_def.node()[ng - 1].name()); }
42 std::string last, current, basename, name;
43 for (
size_t n = 0;
n < ng; ++
n)
45 name = graph_def.node()[
n].name();
46 auto pos = name.find(
"/");
47 if (pos != std::string::npos) { basename = name.substr(0, pos); }
51 for (
const auto &
s : outputs)
53 if (name.find(
s) != std::string::npos) { found =
true;
break; }
57 if (!last.empty() && (basename != current))
69 std::cout <<
"Output nodes not found in the graph." << std::endl;
73 status =
fSession->Create(graph_def);
76 std::cout << status.ToString() << std::endl;
92 if (
x.empty() ||
x.front().empty()) {
return std::vector<float>(); }
94 long long int rows =
x.size(), cols =
x.front().size();
96 tensorflow::Tensor _x(tensorflow::DT_FLOAT, tensorflow::TensorShape({ 1, rows, cols, 1 }));
97 auto input_map = _x.tensor<float, 4>();
99 for (
long long int r = 0; r < rows; ++r) {
100 const auto & row =
x[r];
101 for (
long long int c = 0; c < cols; ++c) {
102 input_map(0, r, c, 0) = row[c];
106 auto result =
run(_x);
107 if (!result.empty()) {
return result.front(); }
108 else {
return std::vector<float>(); }
114 long long int samples)
116 if ((samples == 0) ||
x.empty() ||
x.front().empty() ||
x.front().front().empty() ||
x.front().front().front().empty())
119 if ((samples == -1) || (samples > (
long long int)
x.size())) { samples =
x.size(); }
122 rows =
x.front().size(),
123 cols =
x.front().front().size(),
124 depth =
x.front().front().front().size();
126 tensorflow::Tensor _x(tensorflow::DT_FLOAT, tensorflow::TensorShape({ samples, rows, cols, depth }));
127 auto input_map = _x.tensor<float, 4>();
128 for (
long long int s = 0;
s < samples; ++
s) {
129 const auto & sample =
x[
s];
130 for (
long long int r = 0; r < rows; ++r) {
131 const auto & row = sample[r];
132 for (
long long int c = 0; c < cols; ++c) {
133 const auto &
col = row[c];
134 for (
long long int d = 0;
d < depth; ++
d) {
135 input_map(
s, r, c,
d) =
col[
d];
147 std::vector< std::pair<std::string, tensorflow::Tensor> > inputs = {
153 std::vector<tensorflow::Tensor> outputs;
160 size_t samples = 0, nouts = 0;
161 for (
size_t o = 0; o < outputs.size(); ++o)
163 if (o == 0) { samples = outputs[o].dim_size(0); }
164 else if ((
int)samples != outputs[o].dim_size(0))
166 throw std::string(
"TF outputs size inconsistent.");
168 nouts += outputs[o].dim_size(1);
172 std::vector< std::vector< float > > result;
173 result.resize(samples, std::vector< float >(nouts));
176 for (
size_t o = 0; o < outputs.size(); ++o)
178 auto output_map = outputs[o].tensor<float, 2>();
180 size_t n = outputs[o].dim_size(1);
181 for (
size_t s = 0;
s < samples; ++
s) {
182 std::vector< float > & vs = result[
s];
183 for (
size_t i = 0; i <
n; ++i) {
184 vs[idx0 + i] = output_map(
s, i);
193 std::cout << status.ToString() << std::endl;
194 return std::vector< std::vector< float > >();
std::vector< float > run(const std::vector< std::vector< float > > &x)
Graph(const char *graph_file_name, const std::vector< std::string > &outputs, bool &success)
Not-throwing constructor.
auto vector(Vector const &v)
Returns a manipulator which will print the specified array.
std::vector< std::string > fOutputNames
tensorflow::Session * fSession