I have a SavedModel in a folder (generator_model_final
) with the following content:
- saved_model.pb
- variables
|- variables.data-00000-of-00002
|- variables.data-00001-of-00002
|- variables.index
In the root of the directory, I have my .cc
and BUILD
files:
- gan_loader.cc
- BUILD
- generator_model_final
I want to load a SavedModel using the C++ API for Tensorflow. My C++ code is the following:
#include <fstream>
#include <utility>
#include <vector>
#include "tensorflow/cc/ops/const_op.h"
#include "tensorflow/cc/ops/image_ops.h"
#include "tensorflow/cc/ops/standard_ops.h"
#include "tensorflow/core/framework/graph.pb.h"
#include "tensorflow/core/framework/tensor.h"
#include "tensorflow/core/graph/default_device.h"
#include "tensorflow/core/graph/graph_def_builder.h"
#include "tensorflow/core/lib/core/errors.h"
#include "tensorflow/core/lib/core/stringpiece.h"
#include "tensorflow/core/lib/core/threadpool.h"
#include "tensorflow/core/lib/io/path.h"
#include "tensorflow/core/lib/strings/str_util.h"
#include "tensorflow/core/lib/strings/stringprintf.h"
#include "tensorflow/core/platform/env.h"
#include "tensorflow/core/platform/init_main.h"
#include "tensorflow/core/platform/logging.h"
#include "tensorflow/core/platform/types.h"
#include "tensorflow/core/public/session.h"
#include "tensorflow/core/util/command_line_flags.h"
#include "tensorflow/cc/saved_model/loader.h"
#include "tensorflow/cc/saved_model/tag_constants.h"
// These are all common classes it's handy to reference with no namespace.
using tensorflow::Flag;
using tensorflow::int32;
using tensorflow::Status;
using tensorflow::string;
using tensorflow::Tensor;
using tensorflow::tstring;
using tensorflow::SavedModelBundle;
using tensorflow::SessionOptions;
using tensorflow::RunOptions;
using tensorflow::kSavedModelTagServe;
int main(int argc, char* argv[]) {
// These are the command-line flags the program can understand.
// They define where the graph and input data is located, and what kind of
// input the model expects.
// Input/Output names
string input_layer = "dense_1_input";
string output_layer = "conv2d_2";
string root_dir = "";
// Arguments
std::vector<Flag> flag_list = {
Flag("input_layer", &input_layer, "name of input layer"),
Flag("output_layer", &output_layer, "name of output layer"),
Flag("root_dir", &root_dir, "interpret image and graph file names relative to this directory"),
};
string usage = tensorflow::Flags::Usage(argv[0], flag_list);
const bool parse_result = tensorflow::Flags::Parse(&argc, argv, flag_list);
if (!parse_result) {
LOG(ERROR) << usage;
return -1;
}
// We need to call this to set up global state for TensorFlow.
tensorflow::port::InitMain(argv[0], &argc, &argv);
if (argc > 1) {
LOG(ERROR) << "Unknown argument " << argv[1] << "\n" << usage;
return -1;
}
// TODO: First we load and initialize the model.
SavedModelBundle model;
SessionOptions session_options;
RunOptions run_options;
auto status = tensorflow::LoadSavedModel(session_options, run_options, "generator_model_final/", {kSavedModelTagServe}, &model);
if (!status.ok()) {
std::cerr << "Failed: " << status;
return -1;
}
return 0;
}
In the last part of the code, I used the loader.h provided by TF to load a SavedModel using C++. I believe it should load already correctly a SavedModel. When I build it with Bazel (bazel build tensorflow/gan_loader/...
), it builds fine. However, when running the executable generated (./bazel-bin/tensorflow/gan_loader/gan_loader
), I get the following error:
2020-06-20 11:12:45.925247: I tensorflow/cc/saved_model/reader.cc:31] Reading SavedModel from: generator_model_final/
2020-06-20 11:12:45.925312: I tensorflow/cc/saved_model/loader.cc:364] SavedModel load for tags { serve }; Status: fail: Not found: Could not find SavedModel .pb or .pbtxt at supplied export directory path: generator_model_final/. Took 77 microseconds.
Failed: Not found: Could not find SavedModel .pb or .pbtxt at supplied export directory path: generator_model_final/(base)
It is strange, because there is a .pb file, and it contains the tag serve.
Some info about my SavedModel:
Running $ saved_model_cli show --dir <path_to_saved_model_folder>
it provides:
The given SavedModel contains the following tag-sets:
serve
Running $ saved_model_cli show --dir <path_to_saved_model_folder> --tag_set serve
it provides:
The given SavedModel MetaGraphDef contains SignatureDefs with the following keys:
SignatureDef key: "__saved_model_init_op"
SignatureDef key: "serving_default"
Finally, using $ saved_model_cli show --dir <path_to_saved_model_folder> --tag_set serve --signature_def serving_default
provides with:
The given SavedModel SignatureDef contains the following input(s):
inputs['dense_1_input'] tensor_info:
dtype: DT_FLOAT
shape: (-1, 100)
name: serving_default_dense_1_input:0
The given SavedModel SignatureDef contains the following output(s):
outputs['conv2d_2'] tensor_info:
dtype: DT_FLOAT
shape: (-1, 28, 28, 1)
name: StatefulPartitionedCall:0
Method name is: tensorflow/serving/predict
Do you have an idea about why this is happening? Is maybe the path to the directory wrong? Does the SavedModel miss something?
Thank you!