Get prediction values from tensorflow lite android
abdou31 opened this issue · 2 comments
System information
- Have I written custom code (as opposed to using a stock example script provided in TensorFlow):No
- OS Platform and Distribution (e.g., Linux Ubuntu 16.04): Windows 10 x64
- Mobile device (e.g. iPhone 8, Pixel 2, Samsung Galaxy) if the issue happens on a mobile device: Samsung A3 2016
- TensorFlow installed from (source or binary): binary
- TensorFlow version (use command below):1.13.1
- Python version:3.6.8
- Bazel version (if compiling from source):
- GCC/Compiler version (if compiling from source):
- CUDA/cuDNN version:9.0/7.4
- GPU model and memory: Nvidia Geforce 840m 3 Go
Describe the current behavior
I want to get values from output tensor with input an image to predict the eye region landmarks.
I have predicted image from frozen graph model and Tensorflow lite using python script and it works fine.
The problem is with the Android platform, I got false and different values compared to values from python script
Describe the expected behavior
Code to reproduce the issue
Python script
data = np.asarray( img, dtype="float32" )
# Inference on input data normalized to [0, 1]
inputImg = np.expand_dims(data,0).astype(np.float32)
input_details = interpreter.get_input_details()
interpreter.set_tensor(input_details[0]['index'], inputImg)
interpreter.invoke()
output_details = interpreter.get_output_details()
output_data = interpreter.get_tensor(output_details[0]['index'])
print(output_data)
Java code ( Android Environment )
private float[][][][] bitmapToInputArray() {
// [START mlkit_bitmap_input]
Bitmap bitmap= getYourInputImage();
bitmap = Bitmap.createScaledBitmap(bitmap, 112, 112, true);
int batchNum = 0;
float[][][][] input = new float[1][112][112][3];
for (int x = 0; x < 112; x++) {
for (int y = 0; y < 112; y++) {
int pixel = bitmap.getPixel(x, y);
// Normalize channel values to [-1.0, 1.0]. This requirement varies by
// model. For example, some models might require values to be normalized
// to the range [0.0, 1.0] instead.
input[batchNum][x][y][0] = (Color.red(pixel) - 127) / 128.0f;
input[batchNum][x][y][1] = (Color.green(pixel) - 127) / 128.0f;
input[batchNum][x][y][2] = (Color.blue(pixel) - 127) / 128.0f;
Log.i("Input","input"+input[batchNum][x][y]);
}
}
// [END mlkit_bitmap_input]
return input;
}
private void useInferenceResult(float[] probabilities) throws IOException {
String[] result=new String[80];
String x="";
String y="";
ArrayList<Point> listpoint= new ArrayList<Point>();
double viewWidth = canvas.getWidth();
double viewHeight = canvas.getHeight();
double imageWidth = mutableBitmap.getWidth();
double imageHeight = mutableBitmap.getHeight();
Log.i("viewWidth","viewwidth "+viewWidth);
Log.i("viewHeight","viewheight "+viewHeight);
Log.i("imagewidth","imagewidth "+imageWidth);
Log.i("imaageHeigh","imageheigh "+imageHeight);
double scale = Math.min(viewWidth / imageWidth, viewHeight / imageHeight);
Log.i("Scale","Scale"+scale);
try {
for (int i = 0; i < probabilities.length; i++) {
Log.i("MLKit", String.format("%1.8f", probabilities[i]));
float i1 = probabilities[i];
Log.i("floaaat", "" + i1);
}
}
Other info / logs
- That what I should get : ( this is from python script using Tensorflow lite model ):
[[0.3323875 0.19654518 0.3430611 0.17367488 0.3671013 0.16478491
0.39022946 0.17491257 0.399131 0.19814822 0.3900888 0.22294888
0.36448467 0.23006389 0.34123936 0.21979642 0.30286688 0.20466375
0.31792122 0.19202346 0.33909258 0.18460245 0.3652809 0.18118384
0.39127237 0.18584773 0.40995973 0.19605562 0.42233318 0.21089557
0.40651116 0.2182863 0.38454503 0.22073016 0.3601514 0.22152397
0.3375371 0.21898884 0.31742495 0.2137229 0.628366 0.21445222
0.63888156 0.19114998 0.6626687 0.18333182 0.6856534 0.1955274
0.69444335 0.22068903 0.6838143 0.2438955 0.66011566 0.2518409
0.63507193 0.2414121 0.60496974 0.22573914 0.6209617 0.21241865
0.6422507 0.20397455 0.6683734 0.20254172 0.6917013 0.2092008
0.7084733 0.21974358 0.7189961 0.23459744 0.70730895 0.24100597
0.69019526 0.24282111 0.6691431 0.24188581 0.6451422 0.23857626
0.6218671 0.2344214 ]]
- Those values are from Android Studio ( java using Log.i) :
2019-08-17 14:47:50.617 21349-21349/com.example.irisdetection I/MLKit: 0,23961355
2019-08-17 14:47:50.620 21349-21349/com.example.irisdetection I/MLKit: 0,25104424
2019-08-17 14:47:50.621 21349-21349/com.example.irisdetection I/MLKit: 0,28179651
2019-08-17 14:47:50.622 21349-21349/com.example.irisdetection I/MLKit: 0,31467810
2019-08-17 14:47:50.623 21349-21349/com.example.irisdetection I/MLKit: 0,33257431
2019-08-17 14:47:50.624 21349-21349/com.example.irisdetection I/MLKit: 0,32645294
2019-08-17 14:47:50.625 21349-21349/com.example.irisdetection I/MLKit: 0,29138848
2019-08-17 14:47:50.626 21349-21349/com.example.irisdetection I/MLKit: 0,25581932
2019-08-17 14:47:50.627 21349-21349/com.example.irisdetection I/MLKit: 0,19593856
2019-08-17 14:47:50.628 21349-21349/com.example.irisdetection I/MLKit: 0,21698779
2019-08-17 14:47:50.631 21349-21349/com.example.irisdetection I/MLKit: 0,24266151
2019-08-17 14:47:50.632 21349-21349/com.example.irisdetection I/MLKit: 0,27562365
2019-08-17 14:47:50.633 21349-21349/com.example.irisdetection I/MLKit: 0,30823168
2019-08-17 14:47:50.635 21349-21349/com.example.irisdetection I/MLKit: 0,33465266
2019-08-17 14:47:50.636 21349-21349/com.example.irisdetection I/MLKit: 0,35355449
2019-08-17 14:47:50.637 21349-21349/com.example.irisdetection I/MLKit: 0,34009647
2019-08-17 14:47:50.638 21349-21349/com.example.irisdetection I/MLKit: 0,31358159
2019-08-17 14:47:50.640 21349-21349/com.example.irisdetection I/MLKit: 0,28156102
2019-08-17 14:47:50.642 21349-21349/com.example.irisdetection I/MLKit: 0,25063315
2019-08-17 14:47:50.643 21349-21349/com.example.irisdetection I/MLKit: 0,21878451
2019-08-17 14:47:50.644 21349-21349/com.example.irisdetection I/MLKit: 0,69623101
2019-08-17 14:47:50.646 21349-21349/com.example.irisdetection I/MLKit: 0,70167470
2019-08-17 14:47:50.646 21349-21349/com.example.irisdetection I/MLKit: 0,73317540
2019-08-17 14:47:50.648 21349-21349/com.example.irisdetection I/MLKit: 0,76974392
2019-08-17 14:47:50.649 21349-21349/com.example.irisdetection I/MLKit: 0,79195201
2019-08-17 14:47:50.651 21349-21349/com.example.irisdetection I/MLKit: 0,78359401
2019-08-17 14:47:50.652 21349-21349/com.example.irisdetection I/MLKit: 0,75674009
2019-08-17 14:47:50.653 21349-21349/com.example.irisdetection I/MLKit: 0,71786618
2019-08-17 14:47:50.654 21349-21349/com.example.irisdetection I/MLKit: 0,66782737
2019-08-17 14:47:50.655 21349-21349/com.example.irisdetection I/MLKit: 0,68930006
2019-08-17 14:47:50.656 21349-21349/com.example.irisdetection I/MLKit: 0,71668541
2019-08-17 14:47:50.657 21349-21349/com.example.irisdetection I/MLKit: 0,75279719
2019-08-17 14:47:50.658 21349-21349/com.example.irisdetection I/MLKit: 0,78872705
2019-08-17 14:47:50.659 21349-21349/com.example.irisdetection I/MLKit: 0,81867975
2019-08-17 14:47:50.661 21349-21349/com.example.irisdetection I/MLKit: 0,83806717
2019-08-17 14:47:50.662 21349-21349/com.example.irisdetection I/MLKit: 0,82371044
2019-08-17 14:47:50.664 21349-21349/com.example.irisdetection I/MLKit: 0,79749656
2019-08-17 14:47:50.665 21349-21349/com.example.irisdetection I/MLKit: 0,76317006
2019-08-17 14:47:50.666 21349-21349/com.example.irisdetection I/MLKit: 0,72700304
2019-08-17 14:47:50.667 21349-21349/com.example.irisdetection I/MLKit: 0,69159627
How can I solve this problem??
@abdou31 really sorry for the slow response here, I was on vacation. I don't think this is the right place for you to get help with this issue. I would recommend posing on StackOverflow where people are always ready to help with detailed debugging like this!
The problem is related to firebase MLKit api. I have posted this issue on Stackoverflow and i didn't get any solution.
This problem can be only resolved in this repository