【发布时间】:2018-10-18 18:53:51
【问题描述】:
我在使用 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 以接受任意数量的样本,就像输入平面一样)。我从以下链接获得了转换器代码: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;
}
}
结果应该在 0 和 1 之间的每个样本的示例输出 (p) 如下所示。只有 I0 是正确的,而 I16 和 I18 的值非常大。 我认为问题是运行会话后输出的维度仍然是 1,应该是 20。是否有可能使用 c++ api 对多个样本进行推理?
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
【问题讨论】:
-
能否给出整个源代码,或者引用两个代码,我怀疑它与具有额外尺寸的data().shape有关,您也可以粘贴pb作为输出节点
-
我已经用输出节点和用于将 keras hd5 文件转换为 pb 的代码更新了我的帖子。谢谢。
-
因为它的输出是 float ,所以你应该循环输出而不是数据,然后 float p = utputs[i].flat
().data();让我知道进展如何 -
取决于你的模型定义,输出可能有一些额外的值,例如与你的输出无关的索引,我怀疑零可能是索引
-
不幸的是,这不起作用。它似乎只识别输出 [0] 并且在输出 [1] 上失败并显示消息“F tensorflow/core/framework/tensor.cc:617] 检查失败:dtype() == expected_dtype (0 vs. 1)”
标签: c++ tensorflow neural-network keras inference