Needing help with Tensorflow Lite on Android


#1

I’m working on a very simple app: It takes a model I trained in Keras on the MNIST dataset and tries to
recognize handwritten digits from pictures you take with the app.

I take the picture, resize it to 28x28 and turn it to black and white. Then I create a ByteBuffer to receive the pixels and pass it to the TensorFlow Lite interpreter. However the intepreter always returns the same values for whatever input I use, returning always the same probabilities, which leads me to think this is an input generation problem.

I know some of you out there have used TensorFlow Lite before (@rheza_h) so it would be great if someone could take a look at the repo and see if you spot any problems.

Here’s where the code is at:

and here’s an an issue I created


#2

Hmmmm interesting!
Not really sure, but I guess the problem is around here.

val postValue = if ((0.2989*(v shr 16 and 0xFF) +
            0.5870*(v shr 8 and 0xFF) + 0.1140*(v and 0xFF)) > 127) 255 else 0

How about

    (((v >> 16) & 0xFF) + ((v >> 8) & 0xFF) + (v & 0xFF)) / 3.0f / 255.0f;

“>>” seems not available in Kotlin…

 (((v shr 16) and 0xFF) + ((v shr 8) and 0xFF) + (v and 0xFF)) / 3.0f / 255.0f;

Thanks to https://kotlinlang.org/docs/reference/basic-types.html#operations :smiley:


#3

2 cent from me, just in case you didn’t check for this:
When I tried to do a “quick and dirty” mnist trained digit recognition software, the black and white values were the opposite in the camera and in the database. E.g. in the mnist database black was 0 and white was 255, but the image I was feeding the network was 0 for white and 255 for black.