微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

tflite_runtime 有时会在我的 Coral Dev Board Mini 上冻结

如何解决tflite_runtime 有时会在我的 Coral Dev Board Mini 上冻结

我尝试在我的 Coral Dev Board Mini 上使用 tflite_runtime,从 this repo 中汲取灵感,但没有使用 Docker。但是,有时对解释器的任何函数调用都会冻结并停止执行任何操作。执行此操作时,我什至无法 Ctrl+C 我的程序,我需要终止该进程。我试图在互联网上搜索但无济于事,我试图修复它也失败了。当 invoke() 函数花费太多时间时,我尝试重新创建解释器,但它以同样的方式冻结。当我尝试重新加载包时发生了同样的事情。

这是我使用的代码。冻结发生在 interpreter.invoke() 行上,而且永远不会同时发生。有时10次迭代就够了,有时300次就可以正常运行。tflite模型是repo中的模型。

import tflite_runtime.interpreter as tflite
import glob
import os
from os import path
import librosa
import numpy as np
import sqlite3
import time
from datetime import datetime,timedelta

DB_FILE = os.getenv("DB_PATH","/home/mendel/udp/audio_db.db")  # path and filename of database
WAV_PATH = os.getenv("WAV_PATH","/home/mendel/udp/audio/") # folder with wav files
MODEL_FILE = os.getenv("MODEL_FILE","/home/mendel/classifier/sound_edgetpu.tflite") # path and filename  with model file
LABEL_FILE = os.getenv("LABEL_FILE","/home/mendel/classifier/labels.txt") #path and filename of associated model class names
AUTO_DELETE = os.getenv("AUTO_DELETE","false") #if equal to true,files above the threshold will automatically be deleted 
ct = os.getenv("CERTAINTY_THRESHOLD","50") # minimum value to consider prediction to be acceptable
if ct.isnumeric():
    CERTAINTY_THRESHOLD = int(ct)
else:
    CERTAINTY_THRESHOLD = 70

# Load labels from a file
sound_names = [line.rstrip('\n') for line in open(LABEL_FILE)]
print("Loaded {0} labels for model.".format(len(sound_names)))

# just extract the features
def extract_features_only(filename):
    features = np.empty((0,193))
    X,sample_rate = librosa.load(filename)
    stft = np.abs(librosa.stft(X))
    mfccs = np.mean(librosa.feature.mfcc(y=X,sr=sample_rate,n_mfcc=40).T,axis=0)
    chroma = np.mean(librosa.feature.chroma_stft(S=stft,sr=sample_rate).T,axis=0)
    mel = np.mean(librosa.feature.melspectrogram(X,axis=0)
    contrast = np.mean(librosa.feature.spectral_contrast(S=stft,axis=0)
    tonnetz = np.mean(librosa.feature.tonnetz(y=librosa.effects.harmonic(X),axis=0)
    ext_features = np.hstack([mfccs,chroma,mel,contrast,tonnetz])
    features = np.vstack([features,ext_features])
    return features

try:
    interpreter = tflite.Interpreter(model_path=MODEL_FILE,experimental_delegates=[tflite.load_delegate('libedgetpu.so.1')])
except ValueError:
    print("Interpreter error. Is Edge TPU plugged in?")

input_details = interpreter.get_input_details()
output_details = interpreter.get_output_details()
interpreter.allocate_tensors()

FILE = "/home/mendel/udp_classifier/audio/1622637563.wav"

n = 0
while True:
    predict_x = extract_features_only(FILE)
    interpreter.set_tensor(input_details[0]['index'],predict_x.astype(np.float32))
    interpreter.invoke()
    tflite_model_predictions = interpreter.get_tensor(output_details[0]['index'])

    # Get the top 2 predictions
    ind = np.argpartition(tflite_model_predictions[0],-2)[-2:]
    ind[np.argsort(tflite_model_predictions[0][ind])]
    ind = ind[::-1]
    top_certainty = int(tflite_model_predictions[0,ind[0]] * 100)
    second_certainty = int(tflite_model_predictions[0,ind[1]] * 100)

    print("Top guess: ",sound_names[ind[0]]," (",top_certainty,"%)")
    print("2nd guess: ",sound_names[ind[1]],second_certainty,"%)")
    n += 1
    print(n)
    time.sleep(1)

您对正在发生的事情以及如何解决问题有任何想法吗?

非常感谢!

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。