【问题标题】:Creating a Bitmap ByteBuffer for quantized Tensorflow Lite Model为量化的 Tensorflow Lite 模型创建位图 ByteBuffer
【发布时间】:2020-05-28 16:06:44
【问题描述】:

我想使用量化的 tensorflow lite 模型,但我拥有的当前 ByteBuffer 使用的是浮点数。我希望这是整数表示。现在模型需要 270000 字节,我正在尝试传递 1080000 字节。是否像将浮点数转换为 int 一样简单?

public ByteBuffer convertBitmapToByteBuffer(Bitmap bitmap) {

    // Preallocate memory for bytebuffer
    ByteBuffer byteBuffer = ByteBuffer.allocate(inputSize*inputSize*pixelSize);
    byteBuffer.order(ByteOrder.nativeOrder());

    // Initialize pixel data array and populate from bitmap
    int [] intArray = new int[inputSize*inputSize];
    bitmap.getPixels(intArray, 0, bitmap.getWidth(), 0 , 0,
            bitmap.getWidth(), bitmap.getHeight());

    int pixel = 0;      // pixel indexer
    for (int i=0; i<inputSize; i++) {
        for (int j=0; j<inputSize; j++) {
            int input = intArray[pixel++];

            byteBuffer.putfloat((((input >> 16 & 0x000000FF) - imageMean) / imageStd));
            byteBuffer.putfloat((((input >> 8 & 0x000000FF) - imageMean) / imageStd));
            byteBuffer.putfloat((((input & 0x000000FF) - imageMean) / imageStd));
        }
    }
    return byteBuffer;
}

感谢您提供的任何提示。

【问题讨论】:

    标签: java floating-point pixel tensorflow-lite bytebuffer


    【解决方案1】:

    将 float 转换为 int 不是正确的方法。好消息是模型期望的量化输入值(依次为 8 位 r、g、b 值)与 Bitmap 像素表示完全匹配,只是模型不期望 alpha 通道,因此转换过程实际上应该比使用浮点输入时更容易。

    您可以尝试以下方法。 (我假设pixelSize3

    int pixel = 0;      // pixel indexer
    for (int i=0; i<inputSize; i++) {
        for (int j=0; j<inputSize; j++) {
            int input = intArray[pixel++];   // pixel containing ARGB.
            byteBuffer
                .put((byte)((input >> 16) & 0xFF))    // R
                .put((byte)((input >>  8) & 0xFF))    // G
                .put((byte)((input      ) & 0xFF));   // B
        }
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2019-05-16
      • 2019-09-10
      • 2019-03-31
      • 1970-01-01
      • 2018-09-07
      • 2020-06-22
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多