I have a problem with making inference on a batchsize greater than 1 using the c++ tensorflow api. The network input planes are 8x8x13 and the output is a single float. When I try to infer on multiple samples as follows, the result is correct only for the first sample. I used keras2tensorflow tool for converting the graph to .pb format.
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
}
}
}
}
}
Edit: Output node is a scalar. Could the culprit be the keras2tensorflow code that I used to convert keras hdf5 file to pb ? Maybe the output should have been a -1x1 to accept any number of samples just like the input planes). I got the converter code from the following link: keras_to_tensorflow
node {
name: "value_0"
op: "Identity"
input: "strided_slice"
attr {
key: "T"
value {
type: DT_FLOAT
}
}
}
The input plane dimension is correctly set to -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;
}
}
Example output (p) for each sample where the result is supposed to be between 0 and 1 is shown below. Only I0 is correct while I16 and I18 have very large values. I think the problem is that after running the session the dimension of outputs is still 1, should have been 20. Is it possible at all to do inference on multiple samples using the 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