Running Tensorflow Classifier Model In C++/Obj-C++ Results In Different Result Than Python
Solution 1:
My usual method for debugging issues like this is:
- First save out a raw C array of values from an example input that I know works. For example, make sure that label_image works with your newly-trained model, and then write out the float* array you get from
input_layer->flat<float>().data()
, using pseudo-code like this:
float* input_data = input_layer->flat<float>().data();
int input_data_count = input_layer->flat<float>().size();
printf("float g_test_input[]={\n");
for (int i = 0; i < input_data_count; ++i) {
printf(" %f,\n", input_data[i]);
}
printf("};\n");
You should end up with a big array that you can copy into your new code. Overwrite whatever input you have in the code you want to test. Now run it, and you should see the same output that you saw from label_image. If you don't, you know there's something different about the model you're loading. If the output is identical, then you know that the input preprocessing is different.
Assuming that it's the preprocessing that's wrong, my next step is to try loading an image from disk. The iOS example code does that in the simple example. Save out some of your expected input into an image file, and then make sure that both label_image and your code produce the same result.
Solution 2:
So this one is tricky.
I failed to mention I was running the graph_transform tool on my retrained graph - and was running quantize weights to lower my graphs size. In the past, I've not had an issue with this messing up classification scores at all, but apparently that caused an issue.
Running the above code with a graph transform call without quantize weights fixed the issue.
Post a Comment for "Running Tensorflow Classifier Model In C++/Obj-C++ Results In Different Result Than Python"