| import tensorflow as tf |
| import numpy as np |
| import os |
| import librosa |
| import pandas as pd |
|
|
| |
| AUDIO_PATH = 'data/train/' |
| CSV_PATH = 'data/train/transcriptions.csv' |
|
|
| |
| df = pd.read_csv(CSV_PATH) |
| filenames = df['filename'].values |
| texts = df['text'].values |
|
|
| |
| def preprocess_audio(filename): |
| file_path = os.path.join(AUDIO_PATH, filename) |
| y, sr = librosa.load(file_path, sr=None) |
| mel_spec = librosa.feature.melspectrogram(y, sr=sr, n_mels=128) |
| mel_spec = librosa.power_to_db(mel_spec, ref=np.max) |
| return mel_spec |
|
|
| |
| X = np.array([preprocess_audio(f) for f in filenames]) |
|
|
| |
| y = texts |
|
|
| |
| model = tf.keras.Sequential([ |
| tf.keras.layers.InputLayer(input_shape=(None, 128)), |
| tf.keras.layers.Bidirectional(tf.keras.layers.LSTM(256, return_sequences=True)), |
| tf.keras.layers.Dense(256, activation='relu'), |
| tf.keras.layers.Dense(len(np.unique(texts)), activation='softmax') |
| ]) |
|
|
| |
| model.compile(optimizer='adam', loss='sparse_categorical_crossentropy', metrics=['accuracy']) |
|
|
| |
| model.fit(X, y, epochs=10, batch_size=32) |
|
|
| |
| model.save('model/tts_model.h5') |