我想使用量化的张量流精简模型,但是我当前使用的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;
}
感谢您提供的任何提示。
将float转换为int是不正确的方法。好消息是,模型期望的量化输入值(顺序为8位r,g,b值)与位图像素表示完全匹配,只是模型不希望有alpha通道,因此转换过程实际上应该比使用浮点输入时更容易。
这是您可以尝试的方法。 (我假设pixelSize
为3
)
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
}
}