我们正在使用tensorflow- tflite在安卓系统中开发语义分割应用程序。使用的'.tflite‘deeplabv3模型的输入类型为(ImageTensor) uint81,300,300,3,输出类型(SemanticPredictions) uint8300,300,300。我们能够成功地运行该模型,并在tflite.run method.But的帮助下获得ByteBuffer格式的输出。我们无法从java.The模型中的此输出中提取图像,该模型使用pascal voc数据集进行训练,并且实际上是从TF模型转换为tflite格式:'mobilenetv2_dm05_coco_voc_trainval’。
这个问题看起来类似于下面的堆栈溢出问题:tensorflow-lite - using tflite Interpreter to get an image in the output
处理浮点数据类型转换的相同问题似乎在github问题中得到了修复:https://github.com/tensorflow/tensorflow/issues/23483
那么,我们如何从UINT8模型的输出中正确地提取分割掩码呢?
发布于 2019-02-23 00:07:17
尝试以下代码:
/**
* Converts ByteBuffer with segmentation mask to the Bitmap
*
* @param byteBuffer Output ByteBuffer from Interpreter.run
* @param imgSizeX Model output image width
* @param imgSizeY Model output image height
* @return Mono color Bitmap mask
*/
private Bitmap convertByteBufferToBitmap(ByteBuffer byteBuffer, int imgSizeX, int imgSizeY){
byteBuffer.rewind();
byteBuffer.order(ByteOrder.nativeOrder());
Bitmap bitmap = Bitmap.createBitmap(imgSizeX , imgSizeY, Bitmap.Config.ARGB_4444);
int[] pixels = new int[imgSizeX * imgSizeY];
for (int i = 0; i < imgSizeX * imgSizeY; i++)
if (byteBuffer.getFloat()>0.5)
pixels[i]= Color.argb(100, 255, 105, 180);
else
pixels[i]=Color.argb(0, 0, 0, 0);
bitmap.setPixels(pixels, 0, imgSizeX, 0, 0, imgSizeX, imgSizeY);
return bitmap;
}它适用于具有单色输出的模型。
发布于 2018-12-16 18:04:03
大致是这样的:
Byte[][] output = new Byte[300][300];
Bitmap bitmap = Bitmap.createBitmap(300,300,Bitmap.Config.ARGB_8888);
for (int row = 0; row < output.length ; row++) {
for (int col = 0; col < output[0].length ; col++) {
int pixelIntensity = output[col][row];
bitmap.setPixel(col,row,Color.rgb(pixelIntensity,pixelIntensity,pixelIntensity));
}https://stackoverflow.com/questions/53801064
复制相似问题