Java 为量化Tensorflow Lite模型创建位图ByteBuffer

Java 为量化Tensorflow Lite模型创建位图ByteBuffer,java,floating-point,pixel,tensorflow-lite,bytebuffer,Java,Floating Point,Pixel,Tensorflow Lite,Bytebuffer,我想使用一个量化的tensorflow lite模型,但是我现在使用的ByteBuffer使用的是浮点。我希望这是整数表示。现在这个模型需要270000字节,我正在尝试传递1080000字节。它是否像将float转换为int一样简单 public ByteBuffer convertBitmapToByteBuffer(Bitmap bitmap) { // Preallocate memory for bytebuffer ByteBuffer byteBuffer = By

我想使用一个量化的tensorflow lite模型,但是我现在使用的ByteBuffer使用的是浮点。我希望这是整数表示。现在这个模型需要270000字节,我正在尝试传递1080000字节。它是否像将float转换为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;
}
public ByteBuffer convertBitmapToByteBuffer(位图){
//为bytebuffer预分配内存
ByteBuffer ByteBuffer=ByteBuffer.allocate(inputSize*inputSize*pixelSize);
byteBuffer.order(ByteOrder.nativeOrder());
//初始化像素数据数组并从位图填充
int[]intArray=新int[inputSize*inputSize];
bitmap.getPixels(intArray,0,bitmap.getWidth(),0,0,
bitmap.getWidth(),bitmap.getHeight();
int pixel=0;//像素索引器
对于(int i=0;i 16&0x000000FF)-imageMean)/imageStd);
byteBuffer.putfloat(((输入>>8&0x000000FF)-imageMean)/imageStd);
byteBuffer.putfloat(((输入&0x000000FF)-imageMean)/imageStd));
}
}
返回缓冲区;
}

感谢您提供的提示。

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

以下是您可以尝试的方法。(我假设
pixelSize
3

int pixel=0;//像素索引器
对于(int i=0;i 16)&0xFF))//R
.put((字节)((输入>>8)&0xFF))//G
.put((字节)((输入)&0xFF));//B
}
}