1
votes

I am testing Google Cloud AutoML vision, I have completed the training process, have an exported edge device tflite model, over 100k images, 25 labels.

Following the instructions in these two tutorials and code from the below repo: https://cloud.google.com/vision/automl/docs/edge-quickstart https://cloud.google.com/vision/automl/docs/tflite-android-tutorial https://github.com/googlecodelabs/tensorflow-for-poets-2/tree/master/android/tflite

I am assuming the problem is not with the custom tflite problem, but some sort of compatibility issue that the tutorial does not account for (either in error or because it is outdated?)

The sample app with pretrained model included in the github repo above works without any changes. When I swap out the tflite packages and make the code changes requested by the tutorial, I encounter a BufferOverflowException

I am assuming the problem is not with the custom tflite problem, but some sort of compatibility issue that the tutorial does not account for (either in error or because it is outdated?)

The sample app with pretrained model included in the github repo above works without any changes. When I swap out the tflite packages and make the code changes requested by the tutorial, I encounter a BufferOverflowException

Code crashes at the try/catch block (added by me to see more of what was going on)

'''
private void convertBitmapToByteBuffer(Bitmap bitmap) {
if (imgData == null) {
  return;
}
imgData.rewind();
bitmap.getPixels(intValues, 0, bitmap.getWidth(), 0, 0, bitmap.getWidth(), bitmap.getHeight());
// Convert the image to floating point.
int pixel = 0;
long startTime = SystemClock.uptimeMillis();
for (int i = 0; i < DIM_IMG_SIZE_X; ++i) {
  for (int j = 0; j < DIM_IMG_SIZE_Y; ++j) {
    final int val = intValues[pixel++];
    try
    {
      imgData.putFloat((((val >> 16) & 0xFF))/IMAGE_STD);
      imgData.putFloat((((val >> 8) & 0xFF))/IMAGE_STD);
      imgData.putFloat((((val) & 0xFF))/IMAGE_STD);
    }
    catch (BufferOverflowException e)
    {
      Log.e("TfLiteCameraDemo", "Exception caught: ", e);
    }
  }
}
long endTime = SystemClock.uptimeMillis();
Log.d(TAG, "Timecost to put values into ByteBuffer: " + Long.toString(endTime - startTime));
}
'''

Imported tflite model, following instruction by tutorials in link above, crashes.

1
Given the first link says *Beta* This product or feature is in a pre-release state and might change or have limited support. For more information, see Product launch stages. I would send feedback on the page and/or create an issue ticket. - Morrison Chang
I've already opened an issue for it on the git repo, just hoping there might be someone that has better insight to the issue - tvanfossen

1 Answers

0
votes

I realize this is an older post but I just ran into the same issue and found a solution. Maybe posting will help someone else out in the future.

The buffer the Tensorflow part wants is 150528 bytes. By using putFloat() above the code is trying to put 4x (float = 4 bytes) that much data into the imgData buffer. I tried just making the buffer bigger and that produced a different error. What finally worked was to convert the data from float to byte. See below.

Original: (crashed with buffer overflow error)

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

Modified: (this runs w/out crashing on my Galaxy Note9. It doesn't do a great job of accurate identification though.)

imgData.put((byte) ((byte) (((val >> 16) & 0xFF)- IMAGE_MEAN)/IMAGE_STD));
imgData.put((byte) ((byte) (((val >> 8) & 0xFF)-IMAGE_MEAN)/IMAGE_STD));
imgData.put((byte) (((byte) ((val) & 0xFF)-IMAGE_MEAN)/IMAGE_STD));