tensorflow C++ 批量推理
tensorflow c++ batch inference
我在使用 c++ tensorflow api 对大于 1 的批量大小进行推断时遇到问题。网络输入平面为 8x8x13,输出为单个浮点数。当我尝试按如下方式推断多个样本时,结果仅对第一个样本是正确的。我使用 keras2tensorflow 工具将图形转换为 .pb 格式。
node {
name: "main_input"
op: "Placeholder"
attr {
key: "dtype"
value {
type: DT_FLOAT
}
}
attr {
key: "shape"
value {
shape {
dim {
size: -1
}
dim {
size: 8
}
dim {
size: 8
}
dim {
size: 12
}
}
}
}
}
编辑: 输出节点是一个标量。罪魁祸首可能是我用来将 keras hdf5 文件转换为 pb 的 keras2tensorflow 代码吗?也许输出应该是 -1x1 来接受任意数量的样本,就像输入平面一样)。我从以下 link 获得了转换器代码:keras_to_tensorflow
node {
name: "value_0"
op: "Identity"
input: "strided_slice"
attr {
key: "T"
value {
type: DT_FLOAT
}
}
}
输入平面尺寸正确设置为-1 x 8 x 8 x 13。
void test() {
//input planes
const int nmoves = pstack->count; //This is the number of samples
TensorShape input_shape({nmoves, 8, 8, CHANNELS});
Tensor inputs(DT_FLOAT, input_shape);
//.... Initialize input planes
//output
std::vector<Tensor> outputs;
//run session
TF_CHECK_OK( session->Run(
{{input_layer, inputs}}, {output_layer}, {}, &outputs)
);
//get results
auto outd = outputs[0].flat<float>().data(); //is this correct way to access the data for multiple samples ?
for(int i = 0;i < nmoves; i++) {
float p = outd[i]; //The value of p is wrong for all but the first one
std::cout << "I" << i << " == " << p << std::endl;
}
}
每个样本的示例输出 (p) 如下所示,其中结果应介于 0 和 1 之间。只有 I0 是正确的,而 I16 和 I18 的值非常大。 我认为问题是在 运行 会话之后输出的维度仍然是 1,应该是 20。是否有可能使用 c++ [=27= 对多个样本进行推理] ?
I0 == 0.434162
I1 == 0
I2 == 0
I3 == 0.0640963
I4 == 0.0718748
I5 == 0.325485
I6 == 0
I7 == 0
I8 == 0
I9 == 0
I10 == 0.141193
I11 == 0.398055
I12 == 0.237758
I13 == 0.530693
I14 == 2.44527e-42
I15 == 0
I16 == -5.62959e+14
I17 == 4.56697e-41
I18 == -5.62959e+14
I19 == 4.56697e-41
问题原来是由于 keras_to_tensorflow 我用于转换的错误引起的。我报告了这个问题 here. The bug is still there in keras_to_tensorflow
第 68 行:
pred[i] = tf.identity(net_model.output[i], name=pred_node_names[i])
"output"应该是"outputs"
pred[i] = tf.identity(net_model.outputs[i], name=pred_node_names[i])
我在使用 c++ tensorflow api 对大于 1 的批量大小进行推断时遇到问题。网络输入平面为 8x8x13,输出为单个浮点数。当我尝试按如下方式推断多个样本时,结果仅对第一个样本是正确的。我使用 keras2tensorflow 工具将图形转换为 .pb 格式。
node {
name: "main_input"
op: "Placeholder"
attr {
key: "dtype"
value {
type: DT_FLOAT
}
}
attr {
key: "shape"
value {
shape {
dim {
size: -1
}
dim {
size: 8
}
dim {
size: 8
}
dim {
size: 12
}
}
}
}
}
编辑: 输出节点是一个标量。罪魁祸首可能是我用来将 keras hdf5 文件转换为 pb 的 keras2tensorflow 代码吗?也许输出应该是 -1x1 来接受任意数量的样本,就像输入平面一样)。我从以下 link 获得了转换器代码:keras_to_tensorflow
node {
name: "value_0"
op: "Identity"
input: "strided_slice"
attr {
key: "T"
value {
type: DT_FLOAT
}
}
}
输入平面尺寸正确设置为-1 x 8 x 8 x 13。
void test() {
//input planes
const int nmoves = pstack->count; //This is the number of samples
TensorShape input_shape({nmoves, 8, 8, CHANNELS});
Tensor inputs(DT_FLOAT, input_shape);
//.... Initialize input planes
//output
std::vector<Tensor> outputs;
//run session
TF_CHECK_OK( session->Run(
{{input_layer, inputs}}, {output_layer}, {}, &outputs)
);
//get results
auto outd = outputs[0].flat<float>().data(); //is this correct way to access the data for multiple samples ?
for(int i = 0;i < nmoves; i++) {
float p = outd[i]; //The value of p is wrong for all but the first one
std::cout << "I" << i << " == " << p << std::endl;
}
}
每个样本的示例输出 (p) 如下所示,其中结果应介于 0 和 1 之间。只有 I0 是正确的,而 I16 和 I18 的值非常大。 我认为问题是在 运行 会话之后输出的维度仍然是 1,应该是 20。是否有可能使用 c++ [=27= 对多个样本进行推理] ?
I0 == 0.434162
I1 == 0
I2 == 0
I3 == 0.0640963
I4 == 0.0718748
I5 == 0.325485
I6 == 0
I7 == 0
I8 == 0
I9 == 0
I10 == 0.141193
I11 == 0.398055
I12 == 0.237758
I13 == 0.530693
I14 == 2.44527e-42
I15 == 0
I16 == -5.62959e+14
I17 == 4.56697e-41
I18 == -5.62959e+14
I19 == 4.56697e-41
问题原来是由于 keras_to_tensorflow 我用于转换的错误引起的。我报告了这个问题 here. The bug is still there in keras_to_tensorflow
第 68 行:
pred[i] = tf.identity(net_model.output[i], name=pred_node_names[i])
"output"应该是"outputs"
pred[i] = tf.identity(net_model.outputs[i], name=pred_node_names[i])