我目前正在研究歌唱語言識別問題(以及機器學習的基礎知識)。我在互聯網上發現了很多關于此的作品,但其中一些不提供任何代碼(甚至偽代碼),這就是為什么我試圖使用他們的機器學習模型描述來重現它們。
一個很好的例子是LISTEN, READ, AND IDENTIFY: MULTIMODAL SINGING LANGUAGE IDENTIFICATION OF MUSICKeunwoo Choi 和 Yuxuan Wang 寫的。
總而言之,它們連接了兩層:音頻層(以頻譜圖的形式)、文本層(使用 langdetect 的元資料上的語言概率向量,56 維向量)。
The text branch is a 3-layer MLP where each layer consists of a 128-unit fully-connected layer, a batch normalization layer, and a ReLU activation [22].
對于文本模型,我得到了這樣的東西:
text_model = Sequential()
text_model.add(Input((56,), name='input'))
text_model.add(BatchNormalization())
text_model.add(Dense(128, activation='relu'))
langdetect.detect_langs(metadata)回報[de:0.8571399874707945, en:0.14285867860989504]。
我不確定我是否正確描述了我的模型,我無法理解如何將它正確地(langdetect 概率向量)放入 keras 模型中。
uj5u.com熱心網友回復:
首先,您需要將langdetect輸出轉換為恒定長度的向量。庫中有 55 種語言,因此我們需要創建長度為 55 的向量,其中第 i 個元素表示文本來自第 i 種語言的概率。你可以這樣做:
import tensorflow as tf
import numpy as np
import langdetect
langdetect.detector_factory.init_factory()
LANGUAGES_LIST = langdetect.detector_factory._factory.langlist
def get_probabilities_vector(text):
predictions = langdetect.detect_langs(text)
output = np.zeros(len(LANGUAGES_LIST))
for p in predictions:
output[LANGUAGES_LIST.index(p.lang)] = p.prob
return tf.constant(output)
然后您需要創建一個具有多個輸入的模型。這可以使用函式式 API來完成,例如像這樣(根據您的用例更改您的輸入):
def create_model():
audio_input = tf.keras.Input(shape=(256,))
langdetect_input = tf.keras.Input(shape=(55,))
x = tf.keras.layers.concatenate([audio_input, langdetect_input])
x = tf.keras.layers.Dense(128, activation='relu')(x)
output = tf.keras.layers.Dense(55)(x)
model = tf.keras.Model(
inputs={
'audio': audio_input,
'text': langdetect_input
},
outputs=output)
return model
在一些輸入上測驗模型:
model = create_model()
audio_input = tf.constant(np.random.rand(256))
langdetect_input = get_probabilities_vector('This is just a test input')
model({
'audio': tf.expand_dims(audio_input, 0),
'text': tf.expand_dims(langdetect_input, 0)
})
>>> <tf.Tensor: shape=(1, 55), dtype=float32, numpy=
array([[ 0.23361185, 0.19011918, -0.45230836, -0.0602392 , -0.20067683,
0.9698535 , -1.0724173 , 0.08978442, 0.052798 , -0.16554174,
0.9238764 , 1.0331644 , 0.4508734 , -0.2450786 , -1.0605856 ,
0.3239496 , -1.0073977 , -0.2129285 , -0.6817296 , 0.05288622,
0.9089616 , -0.11521344, 0.25696573, -0.07688305, -0.36123943,
-0.0317415 , -0.18303779, 0.13786468, 0.88620317, 0.11393422,
-0.5215691 , -0.28585738, 0.54988045, -0.02300271, -0.4347821 ,
-0.57744324, 0.14031887, 0.8255624 , -0.13157232, -1.1060234 ,
-0.24097277, 0.12950295, 0.4586677 , 0.37702668, 0.7558856 ,
-0.05933011, 0.53903174, 0.27433476, -0.18464057, 1.0673125 ,
-0.05723387, -0.03429477, 0.4431308 , -0.14510366, -0.28087378]],
dtype=float32)>
我正在使用expand_dims函式擴展輸入的維度,以便輸入具有形狀(1, 256)和(1, 55)(這類似于輸入(batch_size, 256)以及(batch_size, 55)模型在訓練期間期望的形狀)。
這只是一個草稿,但大致可以解決您的問題。
轉載請註明出處,本文鏈接:https://www.uj5u.com/qiye/536696.html
上一篇:如何拆分影像取決于它的標簽,因此每個標簽都會有它的影像檔案夾
下一篇:如何獲取一批中每張影像的平均值?
