tf.signal.rFFT 在 TFLITE 中无法在 JAV 中运行

tf.signal.rFFT in TFLITE is not working in JAV

系统信息

我只是想建立一个模型,将时间序列数据作为输入并计算 rFFT/irFFT 和 return 时间序列数据。 我使用 tf.signal.rfft 和 tf.signal.irfft 进行计算。我已将它转换为 TFLITE,并且它在 python 中工作得非常好。但是当我将它加载到 android studio 时,它在解释器中给了我 Null。以下是 python 的代码:

from tensorflow.keras.layers import  Lambda, Input
import tensorflow as tf

inp = keras.Input(shape=((197429)))
O = Lambda(stftLayer)(inp)
Z = Lambda(istftLayer)(O)
model = keras.Model(inputs=inp, outputs=Z, name="fft_model")

converter = tf.lite.TFLiteConverter.from_keras_model(model)
converter.target_spec.supported_ops = [tf.lite.OpsSet.TFLITE_BUILTINS,tf.lite.OpsSet.SELECT_TF_OPS ]
tflite_model = converter.convert()
with open('FFT.tflite', 'wb') as f:
    f.write(tflite_model)

当我在 python 上向这个 TFLITE 模型发出语音信号时,它 return 完全是相同的信号。但是当我将它加载到 Android 工作室和 运行 以下代码时,它没有正确加载 TFLITE 模型并在解释器中显示 NULL :

private MappedByteBuffer loadModelFile() throws IOException {
AssetFileDescriptor fileDescriptor = this.getAssets().openFd("FFT.tflite");
FileInputStream fileInputStream = new FileInputStream(fileDescriptor.getFileDescriptor());
FileChannel fileChannel = fileInputStream.getChannel();
long startOffSets = fileDescriptor.getStartOffset();
long declaredLength = fileDescriptor.getDeclaredLength();
return fileChannel.map(FileChannel.MapMode.READ_ONLY, startOffSets, declaredLength);

}
import org.tensorflow.lite.Interpreter;
Interpreter tflite;

try {
tflite = new Interpreter(loadModelFile());

    } catch (Exception e) {
        e.printStackTrace();
    }

当我运行建模时它给出 原因:java.lang.NullPointerException:尝试在空对象引用上调用虚方法'void org.tensorflow.lite.Interpreter.run(java.lang.Object, java.lang.Object)'

以下是gradle设置:

android {
    compileSdkVersion 30
    buildToolsVersion "30.0.3"

    defaultConfig {
        applicationId "com.example.dtln_test"
        minSdkVersion 27
        targetSdkVersion 30
        versionCode 1
        versionName "1.0"

        testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
    }

    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
        }
    }
    compileOptions {
        sourceCompatibility JavaVersion.VERSION_1_8
        targetCompatibility JavaVersion.VERSION_1_8
    }
    aaptOptions{
        noCompress = "tflite"

    }
    buildFeatures {
        mlModelBinding true
    }
}

dependencies {
    implementation fileTree(dir: 'libs', include: ['*.jar'])

    //implementation 'org.tensorflow:tensorflow-lite:0.0.0-nightly'
    implementation 'androidx.appcompat:appcompat:1.1.0'
    implementation 'androidx.constraintlayout:constraintlayout:1.1.3'
    implementation 'org.tensorflow:tensorflow-lite:+'
    implementation 'org.tensorflow:tensorflow-lite-support:+'
    ///////////////////////////////////////////////////////////
   // implementation 'org.tensorflow:tensorflow-lite-support:0.1.0-rc1'
  //  implementation 'org.tensorflow:tensorflow-lite-metadata:0.1.0-rc1'
//    implementation 'org.tensorflow:tensorflow-lite-gpu:2.2.0'
    testImplementation 'junit:junit:4.12'
    implementation 'com.google.android.material:material:1.2.0-alpha03'
    androidTestImplementation 'androidx.test.ext:junit:1.1.1'
    androidTestImplementation 'androidx.test.espresso:espresso-core:3.2.0'
}

任何帮助将不胜感激!! 谢谢

似乎引发了 null 异常,因为 TFLite 解释器创建因某种原因失败。最好先验证解释器对象是否初始化好。

我建议在创建解释器实例时添加一个语句来记录错误,如下面的代码片段:

try {
    tflite = new Interpreter(loadModelFile());
} catch (Exception e) {
    Log.e("TFLite", "failed to create TFLite interpreter instance", e);
}

如果在Interpreter创建过程中出现错误,您可以在Android工作室的logcat部分找到相应的错误。

然后,我们可以调试一下初始化失败的原因

问题已解决。 在使用自定义 functions/Lambda 层制作 TFLITE 时,在制作 tflite 时执行这些步骤。

converter = tf.lite.TFLiteConverter.from_keras_model(model)
converter.target_spec.supported_ops = [tf.lite.OpsSet.TFLITE_BUILTINS,tf.lite.OpsSet.SELECT_TF_OPS ]
tflite_model = converter.convert()

并在 android studio 中将这些添加到依赖项中:

实施'org.tensorflow:tensorflow-lite:0.0.0-nightly-SNAPSHOT'

实施'org.tensorflow:tensorflow-lite-select-tf-ops:0.0.0-nightly-SNAPSHOT'