我正在使用 Firebase MLKIT 在应用内本地实现“.tflite”模型。model.tflite 文件大小约为 132 MB。它显示了错误原因:
onFailure:java.lang.IllegalStateException:内部错误:准备张量分配时意外失败:此解释器不支持常规 TensorFlow 操作。确保在推理之前调用 Flex 委托。节点号 17 (Flex) 未能准备好。
和错误信息:
本地模型加载失败,模型选项:本地模型路径:model.tflite。远程模型名称:未指定。
该模型被用于将一张普通图像转换为黑白图像[出于某种目的]。
我也列出了依赖项,
- 实施 'com.google.firebase:firebase-ml-model-interpreter:22.0.3'
- 实施 'org.tensorflow:tensorflow-lite:1.13.1'
我也访问了该链接,但无法理解如何在我的情况下处理此问题:https ://github.com/tensorflow/tensorflow/blob/master/tensorflow/lite/java/src/test/java/org/tensorflow /lite/InterpreterFlexTest.java
我也附上代码片段:
'''
FirebaseCustomLocalModel localModel = null;
try {
localModel = new FirebaseCustomLocalModel.Builder()
.setAssetFilePath("model.tflite")
.build();
} catch (Exception e) {
e.printStackTrace();
Log.d(TAG, "onClick: "+e);
}
FirebaseModelInterpreter interpreter;
FirebaseModelInterpreterOptions options =
new FirebaseModelInterpreterOptions.Builder(localModel).build();
try {
interpreter = FirebaseModelInterpreter.getInstance(options);
FirebaseModelInputOutputOptions inputOutputOptions =
new FirebaseModelInputOutputOptions.Builder()
.setInputFormat(0, FirebaseModelDataType.FLOAT32, new int[]{1, 256, 256, 3})
.setOutputFormat(0, FirebaseModelDataType.FLOAT32, new int[]{420, 580})
.build();
Bitmap bitmap = getYourInputImage();
bitmap = Bitmap.createScaledBitmap(bitmap, 256, 256, true);
int batchNum = 0;
float[][][][] input = new float[1][256][256][3];
for (int x = 0; x < 256; x++) {
for (int y = 0; y < 256; y++) {
int pixel = bitmap.getPixel(x, y);
// Normalize channel values to [-1.0, 1.0]. This requirement varies by
// model. For example, some models might require values to be normalized
// to the range [0.0, 1.0] instead.
input[batchNum][x][y][0] = (Color.red(pixel) - 127) / 128.0f;
input[batchNum][x][y][1] = (Color.green(pixel) - 127) / 128.0f;
input[batchNum][x][y][2] = (Color.blue(pixel) - 127) / 128.0f;
}
}
FirebaseModelInputs inputs = new FirebaseModelInputs.Builder()
.add(input) // add() as many input arrays as your model requires
.build();
// Log.d(TAG, "onClick: "+inputs.toString()+"\n"+inputOutputOptions.toString());
interpreter.run(inputs, inputOutputOptions)
.addOnSuccessListener(
new OnSuccessListener<FirebaseModelOutputs>() {
@Override
public void onSuccess(FirebaseModelOutputs result) {
// ...
float[][] output = result.getOutput(0);
float[] probabilities = output[0];
Log.d(TAG, "onSuccess: "+result.getOutput(0).toString());
}
})
.addOnFailureListener(
new OnFailureListener() {
@Override
public void onFailure(@NonNull Exception e) {
// Task failed with an exception
// ...
Log.d(TAG, "onFailure: "+e.getCause()+"\n\n"+e.getLocalizedMessage()+"\n\n"+e.getMessage());
}
});
} catch (FirebaseMLException e) {
Log.d(TAG, "onClick: "+e);
// ...
}
'''
让我知道需要做什么。