我想检查加载图是否正确 .
我通过python保存学习的协议缓冲区文件 . 并且,我通过c加载协议缓冲区文件 .
但是当会话运行时我无法获得输出张量 .
我想输出并检查图形信息 .
Saveing code by python
with tf.Graph().as_default() as graph:
input_data = tf.placeholder(tf.float32, shape=train_data.shape, name="input")
keep_prob = tf.placeholder(tf.float32)
answer = net.inference(input_data, units, io_data_dim, keep_prob, output_net=True)
saver = tf.train.Saver()
with tf.Session() as sess:
# Load model file
sess.run(tf.initialize_all_variables())
ckpt = tf.train.get_checkpoint_state(ckpt_dir_name)
if ckpt: # checkpoint is exist
last_model = ckpt.model_checkpoint_path # last model path
saver.restore(sess, last_model)
else:
print("There is no training network...")
exit()
# check the saveing graph
for v in sess.graph.get_operations():
print(v.name)
graph_def = graph_util.convert_variables_to_constants(sess, graph.as_graph_def(), ['output_lay/output_lay'])
tf.train.write_graph(graph_def, '.', pb_name, as_text=False)
Loading code by C++
Status LoadGraph(string graph_file_name, std::unique_ptr<tensorflow::Session>* session) {
tensorflow::GraphDef graph_def;
Status graph_status = ReadBinaryProto(tensorflow::Env::Default(), graph_file_name, &graph_def);
if (!graph_status.ok())
{
return graph_status;
}
session->reset(tensorflow::NewSession(tensorflow::SessionOptions()));
Status create_status = (*session)->Create(graph_def);
if (!create_status.ok())
{
return create_status;
}
return Status::OK();
}
Runing code by c++
input_node_name =“输入”
output_node_name =“output_lay / output_lay”
Status Infer(std::unique_ptr<tensorflow::Session>* session,
tensorflow::Tensor* input,
string* input_node_name,
string* output_node_name,
tensorflow::Tensor* output)
{
tensorflow::Tensor input_object = *input;
// input
// I am not confident here
tensorflow::Tensor keep_prob(tensorflow::DT_FLOAT, tensorflow::TensorShape());
keep_prob.scalar<float>()() = 1.0;
std::vector<std::pair<string, tensorflow::Tensor>> inputs = {
{"Placeholder", keep_prob},
{*input_node_name, *input}
};
// ouput
std::vector<tensorflow::Tensor> outputs;
std::cout << "Runnning network..." << std::endl;
Status result = (*session)->Run(
inputs,
{*output_node_name},
{},
&outputs
);
// output 0 (no reply?)
std::cout << "outputs size " << outputs.size() << std::endl;
if (!result.ok())
{
LOG(ERROR) << "Failure: " << result;
}
(*output) = outputs[0];
//std::cout << "take first output" << std::endl;
return result;
}
result 是
无效参数:不兼容的形状: [77,1,513,16] vs. [13780,1,513,16] [[节点:conv1 / dropout / mul = Mul [T = DT_FLOAT,_device = "/job:localhost/replica:0/task:0/cpu:0"](conv1 / dropout / Div,conv1 / dropout / Floor)]]
ex)如果不使用cnn(3层感知器网络)无效参数:不兼容的形状: [77,600] vs. [10242,600] [[Node:hidden1 / dropout / mul = Mul [T = DT_FLOAT,_device = "/job:localhost/replica:0/task:0/cpu:0"](hidden1 / dropout / Div,hidden1 /差/地板)]]
pickup of the output 1st python code
-
输入
-
占位符
-
重塑/形状
-
重塑
-
conv1 / weights
-
conv1 /偏见
-
conv1 / Conv2D
-
conv1 / MaxPool
-
conv1 / Add
-
conv1 / conv1
-
conv1 / dropout / Shape
-
conv1 / dropout / add
-
conv1 / dropout / Floor
-
conv1 / dropout / Div
-
conv1 / dropout / mul
-
conv2 / weights
-
conv2 /偏见
-
conv2 / Conv2D
-
......
-
Reshape_1 / shape
-
Reshape_1
-
hidden1 / weights
-
hidden1 /偏见
-
hidden1 / MatMul
-
hidden1 /添加
-
hidden1 / hidden1
-
hidden1 / dropout / add
-
hidden1 / dropout / Floor
-
hidden1 / dropout / Div
-
hidden1 / dropout / mul
-
hidden2 / weights
-
......
-
output_lay / weights
-
output_lay /偏差
-
output_lay / MatMul
-
output_lay / Add
-
output_lay / output_lay