【发布时间】: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