系统信息
操作系统平台和发行版:(Windows10Pro/Andriod 10,11)
移动设备:(Redmi注意事项8)
TensorFlow版本: 2.4
Python版本: 3.8
问题:我正在安卓工作室开发实时音频处理应用程序。我使用LSTM建立了一个模型,并将模型转换为TFLITE格式。在此之后,当我通过给出一些数据来测试模型时,每次都给出不一致的推理时间。模型推理时间为3-4 4ms。如果我在循环中运行推理,3-4ms是平均推理时间。如果我转向实时场景,我必须每10 If执行一次推理。在这种情况下,我的推理时间会增加。这可能是因为推断不是背靠背运行,而是实际上一直等到我们再次获得音频数据(10 7ms回调-3ms推理时间=7ms在线程中等待更多音频数据)。我在tensorflow站点(https://www.tensorflow.org/lite/performance/measurement)上读到,这是由于热身状态和稳态。对于热身状态,它给出了9ms的推理时间,在稳态时给出了几乎3ms的推理时间。但是在我的例子中,由于音频不断地出现,我希望模型在稳定的状态下运行。在应用程序中加载TFLITE模型后,是否有任何方法在稳定状态下运行该模型?
当前行为 Android每次调用推理函数时都会升温
预期的行为安卓tflite模型应该在一开始就热身,然后保持稳定状态。
非常感谢您的帮助,谢谢!
如果我使用loadModelFile (输入、输出),请在下面找到加载函数;它提供了较高的推理时间,即13-14 ms,但是当我在for循环中运行这一行时,它给我大约3-4ms的推断时间。我已经声明过了,在时间循环之外。
private MappedByteBuffer loadModelFile() throws IOException {
AssetFileDescriptor fileDescriptor = this.getAssets().openFd("lstm.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);
}
Interpreter tflite;
float[][][] model_output;
float[][][][] model_st;
private void foo() {
Thread bar = new Thread(new Runnable() {
@Override
public void run() {
try {
tflite = new Interpreter(loadModelFile());
} catch (Exception e) {
e.printStackTrace();
}
while (true) {
float[] data = network.getData(); // blocking call // Audiorecord is being used in the audioHandler Object // Data is returned every 10ms
Object[] inputs = {data, states};
outputs.put(0, model_output);
outputs.put(1, model_st);
/////////// Inference ////////////////
tfLiteLock.lock();
try {
tflite.runForMultipleInputsOutputs(inputs, outputs);
} finally {
tfLiteLock.unlock();
}
System.out.println("inference_time",""+tflite.getLastNativeInferenceDurationNanoseconds())
//Computing
//for(int i=0;i<500;i++){
//try {
// tflite.runForMultipleInputsOutputs(inputs, outputs);
//} finally {
// tfLiteLock.unlock();
//}
//System.out.println("inference_time",""+tflite.getLastNativeInferenceDurationNanoseconds())
//}
foo.bar(model_output);
}
}
});
bar.setPriority(Thread.MAX_PRIORITY);
bar.start();我在tensorflow基准测试工具上运行模型,没有延迟。当我毫不延迟地运行模型时,它给了avg 2ms的时间进行推理。当每次推理之间加5ms的延迟时,模型推理时间增加到6ms。请参阅以下截图:
发布于 2021-03-31 09:10:03
热身状态是指同一TFLite解释器的几次首次运行,而稳态是指热身状态之后的任何时刻。
在初始运行中,TFLite解释器可能需要花费一些时间来分配张量和临时内存以进行卷积优化,这意味着可能需要更多的时间。在几次初次运行之后,这些配置将已经解决了。
因此,如果您可以在每次运行中重用TFLite解释器对象,您可以假设即将进行的推理将在稳定状态下进行。
https://stackoverflow.com/questions/66882466
复制相似问题