Mor*_*itz 9 java android tensorflow tensorflow-lite
当运行 TFLite 解释器将包含浮点数的 ByteBuffer 作为输入时,应用程序抛出异常:
“数据类型错误:无法解析 java.lang.Float 的数据类型”
模型本身在 Keras 上进行训练,然后转换为 TF,然后转换为 TFLite。
对于转换,我使用了 TF(1.5.0 版)toco 转换器。
toco 的输入参数:
toco --input_file=converted.pb --output_file=model.tflite --input_format=TENSORFLOW_GRAPHDEF --input_shape=1,224,224,3 --input_array=main_input --output_array=main_output/Sigmoid --inference_type=FLOAT --output_format=TFLITE - -input_type=浮动
我还手动创建了一个 float[][][][] 数组,而不是 ByteBuffer,其维度是模型的预期输入:[1,224,224,3]
导致与 ByteBuffer 相同的错误。
请注意,我将浮点数除以 255 以获得 [0,1] 范围内的像素值。
import org.tensorflow.lite.Interpreter;
import java.nio.ByteBuffer;
public Interpreter tflite;
tflite = new Interpreter(loadModelFile(Test_TFLite.this,modelFile));
ByteBuffer bytebuffer_float = convertBitmapToByteBuffer_float(image, 1,
224, 3);
float out = 0;
tflite.run(bytebuffer_float,out);
private ByteBuffer convertBitmapToByteBuffer_float(Bitmap bitmap, int
BATCH_SIZE, int inputSize, int PIXEL_SIZE) {
ByteBuffer byteBuffer = ByteBuffer.allocateDirect(4 * BATCH_SIZE *
inputSize * inputSize * PIXEL_SIZE); //float_size = 4 bytes
byteBuffer.order(ByteOrder.nativeOrder());
int[] intValues = new int[inputSize * inputSize];
bitmap.getPixels(intValues, 0, bitmap.getWidth(), 0, 0,
bitmap.getWidth(), bitmap.getHeight());
int pixel = 0;
for (int i = 0; i < inputSize; ++i) {
for (int j = 0; j < inputSize; ++j) {
final int val = intValues[pixel++];
byteBuffer.putFloat( ((val >> 16) & 0xFF)* (1.f/255.f));
byteBuffer.putFloat( ((val >> 8) & 0xFF)* (1.f/255.f));
byteBuffer.putFloat( (val & 0xFF)* (1.f/255.f));
}
}
return byteBuffer;
}
Run Code Online (Sandbox Code Playgroud)
我希望将 [0,1] 范围内的单个浮点值作为输出。由于解释器抛出异常,因此没有实际输出。
“数据类型错误:无法解析 java.lang.Float 的数据类型”
| 归档时间: |
|
| 查看次数: |
3399 次 |
| 最近记录: |