13 #include "tensorflow/core/platform/env.h" 15 #include "tensorflow/core/public/session_options.h" 18 tf::Graph::Graph(
const char* graph_file_name,
const std::vector<std::string> &
outputs,
bool & success,
int ninputs,
int noutputs)
26 tensorflow::SessionOptions
options;
27 tensorflow::ConfigProto &
config = options.config;
28 config.set_inter_op_parallelism_threads(1);
29 config.set_intra_op_parallelism_threads(1);
30 config.set_use_per_session_threads(
false);
39 tensorflow::GraphDef graph_def;
47 size_t ng = graph_def.node().size();
78 for (
size_t n = 0;
n < ng; ++
n)
80 name = graph_def.node()[
n].name();
81 auto pos = name.find(
"/");
82 if (
pos != std::string::npos) { basename = name.substr(0,
pos); }
86 for (
const auto &
s : outputs)
88 if (name.find(
s) != std::string::npos) { found =
true;
break; }
92 if (!last.empty() && (basename !=
current))
104 std::cout <<
"Output nodes not found in the graph." <<
std::endl;
121 std::cout <<
"tf::Graph::dtor: " <<
"Close failed." <<
std::endl;
131 long long int samples)
133 if ((samples == 0) ||
x.empty() ||
x.front().empty() ||
x.front().front().empty() ||
x.front().front().front().empty())
136 if ((samples == -1) || (samples > (
long long int)
x.size())) { samples =
x.size(); }
139 rows =
x.front().size(),
140 cols =
x.front().front().size(),
141 depth =
x.front().front().front().size();
143 std::vector< tensorflow::Tensor > _x;
148 _x.push_back(tensorflow::Tensor(tensorflow::DT_FLOAT, tensorflow::TensorShape({ samples, rows, cols, depth })));
149 auto input_map = _x[0].tensor<
float, 4>();
150 for (
long long int s = 0;
s < samples; ++
s) {
151 const auto & sample =
x[
s];
152 for (
long long int r = 0;
r < rows; ++
r) {
153 const auto &
row = sample[
r];
154 for (
long long int c = 0;
c < cols; ++
c) {
155 const auto & col =
row[
c];
156 for (
long long int d = 0;
d < depth; ++
d) {
157 input_map(
s,
r,
c,
d) = col[
d];
166 for(
int i=0; i<depth; ++i){
167 _x.push_back(tensorflow::Tensor(tensorflow::DT_FLOAT, tensorflow::TensorShape({ samples, rows, cols, 1 })));
172 for(
int view=0; view<depth; ++view){
173 auto input_map = _x[view].tensor<
float, 4>();
174 for (
long long int s = 0;
s < samples; ++
s) {
175 const auto & sample =
x[
s];
176 for (
long long int r = 0;
r < rows; ++
r) {
177 const auto &
row = sample[
r];
178 for (
long long int c = 0;
c < cols; ++
c) {
179 const auto & col =
row[
c];
180 long long int d = view;
181 input_map(
s,
r,
c, 0) = col[
d];
193 std::vector< std::vector< std::vector< float > > >
tf::Graph::run(
const std::vector< tensorflow::Tensor > &
x)
195 std::vector< std::pair<std::string, tensorflow::Tensor> >
inputs;
209 std::vector<tensorflow::Tensor>
outputs;
218 for (
size_t o = 0; o < outputs.size(); ++o)
220 if (o == 0) { samples = outputs[o].dim_size(0); }
221 else if ((
int)samples != outputs[o].dim_size(0))
223 throw std::string(
"TF outputs size inconsistent.");
227 std::vector< std::vector< std::vector< float > > >
result;
228 result.resize(samples,
std::vector< std::vector< float > >(outputs.size()));
230 for (
size_t s = 0;
s < samples; ++
s)
232 for (
size_t o = 0; o < outputs.size(); ++o)
234 size_t n = outputs[o].dim_size(1);
235 auto output_map = outputs[o].tensor<
float, 2>();
237 result[
s][o].resize(outputs[o].dim_size(1));
239 std::vector< float > & vs = result[
s][o];
240 for (
size_t i = 0; i <
n; ++i)
242 vs[i] = output_map(
s, i);
252 return std::vector< std::vector< std::vector< float > > >();
std::vector< float > run(const std::vector< std::vector< float > > &x)
tensorflow::Session * fSession
std::vector< std::string > fOutputNames
std::vector< std::string > fInputNames
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)