#python #neural-network #tensorflow2.0 #regularized
#python #нейронная сеть #tensorflow2.0 #регуляризованный
Вопрос:
Я занимаюсь классификацией музыкальных жанров. Я создал файл .h5 с моей моделью, которая является нейронной сетью. И теперь я хочу его использовать. Вот код для прогнозирования музыкального жанра :
#%%
import librosa
import tensorflow as tf
import numpy as np
from collections import Counter
SAVED_MODEL_PATH = "modelLast.h5"
SAMPLES_TO_CONSIDER = 22050
DURATION = 30
SAMPLE_PER_TRACK = SAMPLES_TO_CONSIDER * DURATION
#%%
class _Keyword_Spotting_Service:
"""Singleton class for keyword spotting inference with trained models.
:param model: Trained model
"""
model = None
_mapping = [
"blues",
"classical",
"country",
"disco",
"hiphop",
"jazz",
"metal",
"pop",
"reggae",
"rock"
]
_instance = None
def predict(self, file_path, num_mfcc=13, n_fft=2048, hop_length=512):
"""Extract MFCCs from audio file.
:param file_path (str): Path of audio file
:param num_mfcc (int): # of coefficients to extract
:param n_fft (int): Interval we consider to apply STFT. Measured in # of samples
:param hop_length (int): Sliding window for STFT. Measured in # of samples
:return MFCCs (ndarray): 2-dim array with MFCC data of shape (# time steps, # coefficients)
"""
num_segments = 10
num_samples_per_segment = int(SAMPLE_PER_TRACK / num_segments) # num of segments
# load audio file
signal, sample_rate = librosa.load(file_path)
# a faire
predicted_indexes = [0] * num_segments
predicted_mfcc = [0] * num_segments
for s in range(num_segments):
start_sample = num_samples_per_segment * s # s=0 -> 0
finish_sample = start_sample num_samples_per_segment # s=0 -> num_samples_per_segment
mfcc = librosa.feature.mfcc(signal[start_sample:finish_sample],
sample_rate,
n_fft=n_fft, n_mfcc=num_mfcc,
hop_length=hop_length)
MFCCs = mfcc.T
MFCCs = MFCCs[np.newaxis, ..., np.newaxis]
# get the predicted label
predictions = self.model.predict(MFCCs)
print ("nPredictions: {}".format(predictions))
predicted_indexes [s] = np.argmax(predictions)
predicted_mfcc [s] = np.max(predictions)
print("nIndex list: {}".format(predicted_indexes))
print("nIndex list Mfccs : {}".format(predicted_mfcc))
#predicted_index = np.bincount(predicted_indexes).argmax() # Méthode pour avoir l'index qui se répète le plus de fois
# Ajout de précision du code :
"""
Nous ressort de la liste les indexs qui se répètent le plus de fois et s'il y a plusieurs doublons
triplés, compare la valeurs des indexs et choisi l'index à la valeur la plus élevée
Voir le code python Liste.py pour plus de précision
"""
indices = list(map(lambda x: x[0], Counter(predicted_indexes).most_common()))
counts = list(map(lambda x: x[1], Counter(predicted_indexes).most_common()))
print("nIndices présents dans la liste : ", indices)
print("nNombre d'apparition des indices : ", counts)
max_indices = [indices[i] for i, x in enumerate(counts) if x == max(counts)]
result_mcfccs = []
for idx, id in enumerate(predicted_indexes):
if id in max_indices:
result_mcfccs.append(predicted_mfcc[idx])
result = max(result_mcfccs)
print("n Indice se répétant le plus : ", max_indices)
print("nValeur maximale de l'indice se répétant le plus : ",result)
indice = predicted_mfcc.index(result)
print("nEmplacement de la valeur dans la lsite :",indice)
F= predicted_indexes.pop(indice)
print("nRésultat final : ", F)
predicted_keyword = self._mapping[F]
return predicted_keyword
def Keyword_Spotting_Service():
"""Factory function for Keyword_Spotting_Service class.
:return _Keyword_Spotting_Service._instance (_Keyword_Spotting_Service):
"""
# ensure an instance is created only the first time the factory function is called
if _Keyword_Spotting_Service._instance is None:
_Keyword_Spotting_Service._instance = _Keyword_Spotting_Service()
_Keyword_Spotting_Service.model = tf.keras.models.load_model(SAVED_MODEL_PATH)
return _Keyword_Spotting_Service._instance
if __name__ == "__main__":
# create 2 instances of the keyword spotting service
kss = Keyword_Spotting_Service()
kss1 = Keyword_Spotting_Service()
# check that different instances of the keyword spotting service point back to the same object (singleton)
assert kss is kss1
# make a prediction
keyword = kss.predict("discoTrain.wav") # Disco
#keyword = kss.predict("TheRiversGoingWildCUT.mp3") # Blues
#keyword = kss.predict("QuantumJazz.mp3") # Jazz
#keyword = kss.predict("QuantumJazzCUT.mp3") # Jazz
#keyword = kss.predict("AbsconseResilience.mp3") # Metal
#keyword = kss.predict("Nature.wav")
#keyword = kss.predict("elvis-presley-jailhouse-rock-music-video.mp3") # Rock
#keyword = kss.predict("bob-marley-no-woman-no-cry-official-video.mp3") # Reggae
#keyword = kss.predict("alan-jackson-chattahoochee-official-music-videoCUT.mp3") # Country
print(keyword)
Проблема в том, что он возвращает мне ошибку значения, которую я никогда не видел ни на одном форуме, которая :
File "C:ProgramDataAnaconda3envsPMIlibsite-packagestensorflow_corepythonkerasutilsgeneric_utils.py", line 165, in class_and_config_for_serialized_keras_object
raise ValueError('Unknown ' printable_module_name ': ' class_name)
ValueError: Unknown regularizer: L2
Как я могу это исправить?
Ответ №1:
Я, наконец, нашел, почему у меня возникла эта ошибка. Это пришло из моих переменных path. Мне нужно было добавить «ffmpeg» в мои переменные path только после того, как я загрузил папку из Интернета. Вот ссылка:https://ffmpeg.org/download.html Я скопировал папку непосредственно на свой диск «C» и добавил путь к своим переменным path.
Удачи!
Комментарии:
1. Как вы узнали, что это из-за «ffmpeg»? У меня такая же проблема, но это решение мне не помогло!
2. Привет! Я только что заглянул в свои переменные окружения и увидел, что пути ffmpeg здесь больше нет. Но ничто в моем python idle не подсказывало мне, что нужно смотреть в моей переменной окружения. Может быть, вы сможете посмотреть, есть ли у вас правильная версия tensorflow. Я знаю, что для корректной работы этой библиотеки необходимо установить правильную версию. Удачи 🙂