首页 > 解决方案 > 在 Android 中调用 tflite 模型的问题

问题描述

我正在尝试在 android 中使用已经训练好的模型作为 tflite 模型,但是在执行输出的 tflite 模型时出现以下错误:

**A/libc: Fatal signal 8 (SIGFPE), code 1 (FPE_INTDIV), fault addr 0xb7bd4543 in tid 12009 (ing.tensorflow3), pid 12009 (ing.tensorflow3)**

下面是代码:

//calling

bitmap = getBitmapFromAsset("aval1.png");
        imageViewInput.setImageBitmap(bitmap);
        
testFunctionInference(bitmap);


//method body
public void testFunctionInference(Bitmap strName){
        try {
          
            //____________________________________
            ImageProcessor imageProcessor =
                    new ImageProcessor.Builder()
                            .add(new ResizeOp(1, 1, ResizeOp.ResizeMethod.BILINEAR))
                            .build();

            Log.w("testFunc:","after image processor");
// Create a TensorImage object. This creates the tensor of the corresponding
// tensor type (uint8 in this case) that the TensorFlow Lite interpreter needs.
            TensorImage tensorImage = new TensorImage(DataType.FLOAT32);

// Analysis code for every frame
// Preprocess the image
            tensorImage.load(strName);
            Log.w("testFunc:","265 L no.");
            tensorImage = imageProcessor.process(tensorImage);



            Log.w("testFunc:","before inputBuffer0");

            // Creates inputs for reference.
            TensorBuffer inputFeature0 = TensorBuffer.createFixedSize(new int[]{1, 640*480*3}, DataType.FLOAT32);

            MappedByteBuffer tfliteModel
                    = FileUtil.loadMappedFile(this,"converted_model.tflite");
            Interpreter tflite = new Interpreter(tfliteModel);
            Object a=tensorImage.getBuffer();
            Log.w("testFunc:","278");
            tflite.run(tensorImage.getBuffer(), inputFeature0.getBuffer());



            
        } catch (IOException e) {
            // TODO Handle the exception
        }
    }

请任何人协助解决此问题。

标签: androidandroid-studiomachine-learningtensorflow-lite

解决方案


要获取详细日志,您可以使用 nightly-SNAPSHOT 的调试版本。

https://www.tensorflow.org/lite/guide/android#use_the_tensorflow_lite_aar_from_mavencentral

dependencies {
    implementation 'org.tensorflow:tensorflow-lite:0.0.0-nightly-debug-SNAPSHOT'
}

但也许最好检查一下你是否正确提供了输入,因为你使用了 DataType.FLOAT32,你的模型应该有 float32 的输入。


推荐阅读