Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

tflite_runtime freezes while using this classifier outside Docker #4

Open
charon25 opened this issue Jun 2, 2021 · 0 comments
Open

Comments

@charon25
Copy link

charon25 commented Jun 2, 2021

My current project is quite similar to yours, but as I needed another way of getting and sending the audio, I chosed to pick parts of it, and not use it in a Docker container.

But as I use tflite_runtime in the same way as this project, it sometimes completely stops working on invoke() (and any subsequent method, for example if I try to recreate it with tflite.Intepreter()), and I can't even use Ctrl+C to stop the program, I need to kill it.

Do you have any idea why ? My test code is the following :

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, sr=sample_rate).T,axis=0)
    contrast = np.mean(librosa.feature.spectral_contrast(S=stft, sr=sample_rate).T,axis=0)
    tonnetz = np.mean(librosa.feature.tonnetz(y=librosa.effects.harmonic(X), sr=sample_rate).T,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))
    start_time = time.time()
    interpreter.invoke()
    end_time = time.time()
    duration = str(round(1000*(end_time - start_time), 1))
    print("Interpreter duration: {} ms".format(duration))
    tflite_model_predictions = interpreter.get_tensor(output_details[0]['index'])
    # print(tflite_model_predictions[0])
    # get the indices of the top 2 predictions, invert into descending order
    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(0.5)

It is supposed to classify the same sound over and over, but it will always freeze after a few iterations (around 10 in my tests). For example, here it stopped after 10 :

(classifier_env) mendel@fun-rabbit:~/udp_classifier$ python test_tflite.py 
Loaded 10 labels for model.
Interpreter duration: 10.3 ms
Top guess:  dog_bark  ( 50 %)
2nd guess:  gun_shot  ( 21 %)
1
Interpreter duration: 1.5 ms
Top guess:  dog_bark  ( 50 %)
2nd guess:  gun_shot  ( 21 %)
2
Interpreter duration: 1.5 ms
Top guess:  dog_bark  ( 50 %)
2nd guess:  gun_shot  ( 21 %)
3
Interpreter duration: 1.4 ms
Top guess:  dog_bark  ( 50 %)
2nd guess:  gun_shot  ( 21 %)
4
Interpreter duration: 1.3 ms
Top guess:  dog_bark  ( 50 %)
2nd guess:  gun_shot  ( 21 %)
5
Interpreter duration: 1.3 ms
Top guess:  dog_bark  ( 50 %)
2nd guess:  gun_shot  ( 21 %)
6
Interpreter duration: 1.4 ms
Top guess:  dog_bark  ( 50 %)
2nd guess:  gun_shot  ( 21 %)
7
Interpreter duration: 1.4 ms
Top guess:  dog_bark  ( 50 %)
2nd guess:  gun_shot  ( 21 %)
8
Interpreter duration: 1.4 ms
Top guess:  dog_bark  ( 50 %)
2nd guess:  gun_shot  ( 21 %)
9
Interpreter duration: 1.4 ms
Top guess:  dog_bark  ( 50 %)
2nd guess:  gun_shot  ( 21 %)
10
^C^C^C^C^C^C^C^C^C^C^C^C^C^C^C^C^C^C^C^C^C^C^C^C^C^C^C^C^C^C^C^C^C^C^C^C^C^CTerminated

Am I missing something obvious or a library to make it work ? Or maybe it is something in the containers themselves ?

Thank you very much in advance!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant