Android-TensorFlow-Lite-Example icon indicating copy to clipboard operation
Android-TensorFlow-Lite-Example copied to clipboard

Code is not working with inceptionV3 model [cannot convert an tensorflowlite tensor with type float32 to a java object of type [[b (which is compatible with the tensorflowlite type uint8)]

Open prince-kumar-15 opened this issue 7 years ago • 4 comments

I have changed the required things for inceptionV3 as mentioned below but still its not working. its giving me an error : cannot convert an tensorflowlite tensor with type float32 to a java object of type [[b (which is compatible with the tensorflowlite type uint8)

Changes that I did :

private static final int INPUT_SIZE = 299; /** * The inception net requires additional normalization of the used input. */ private static final int IMAGE_MEAN = 128; private static final float IMAGE_STD = 128.0f; private ByteBuffer convertBitmapToByteBufferForInceptionV3(Bitmap bitmap) { ByteBuffer byteBuffer = ByteBuffer.allocateDirect(BATCH_SIZE * inputSize * inputSize * PIXEL_SIZE * 4); byteBuffer.order(ByteOrder.nativeOrder()); int[] intValues = new int[inputSize * inputSize]; bitmap.getPixels(intValues, 0, bitmap.getWidth(), 0, 0, bitmap.getWidth(), bitmap.getHeight()); int pixel = 0; for (int i = 0; i < inputSize; ++i) { for (int j = 0; j < inputSize; ++j) { final int val = intValues[pixel++];

                byteBuffer.putFloat((((val >> 16) & 0xFF) - IMAGE_MEAN) / IMAGE_STD);
                byteBuffer.putFloat((((val >> 8) & 0xFF) - IMAGE_MEAN) / IMAGE_STD);
                byteBuffer.putFloat(((val & 0xFF) - IMAGE_MEAN) / IMAGE_STD);
            
        }
    }
    return byteBuffer;
}

prince-kumar-15 avatar Apr 24 '18 07:04 prince-kumar-15

I changed some byte data type to float type. and it worked well!

ZZANZU avatar May 15 '18 04:05 ZZANZU

Which ones exactly? @ZZANZU

aashutoshrathi avatar Jul 10 '18 19:07 aashutoshrathi

@aashutoshrathi

https://github.com/COSE471/COSE471_android/blob/master/app/src/main/java/com/example/android/alarmapp/tflite/TensorFlowImageClassifier.java

I think this is not real solution, but it worked well(maybe...?)

@Override
    public List<Recognition> recognizeImage(Bitmap bitmap) {
        ByteBuffer byteBuffer = convertBitmapToByteBuffer(bitmap);
        float[][] result = new float[1][labelList.size()];

        long startTime = SystemClock.uptimeMillis();

        interpreter.run(byteBuffer, result);

        return getSortedResult(result);
    }

I changed the 'result''s type to float, and

@SuppressLint("DefaultLocale")
    private List<Recognition> getSortedResult(float[][] labelProbArray) {

        PriorityQueue<Recognition> pq =
                new PriorityQueue<>(
                        MAX_RESULTS,
                        new Comparator<Recognition>() {
                            @Override
                            public int compare(Recognition lhs, Recognition rhs) {
                                return Float.compare(rhs.getConfidence(), lhs.getConfidence());
                            }
                        });

        for (int i = 0; i < labelList.size(); ++i) {
            float confidence = (labelProbArray[0][i] * 100) / 127.0f; // deleted '& 0xff' and multiplied by 100(but I'm not sure, but it showed me some cool accuracy value...

            if (confidence > THRESHOLD) {
                pq.add(new Recognition("" + i,
                        labelList.size() > i ? labelList.get(i) : "unknown",
                        confidence));
            }
        }

'getSortedResult' function gets float value, and it calculates the 'confidenc'e value.

This is what I did temporarily just for deleting the errors. If you have any solution, please let me know.

ZZANZU avatar Jul 12 '18 07:07 ZZANZU

hy i did this but confidence value not increase from 0

waleedkalyar avatar May 29 '19 12:05 waleedkalyar