<jupyter_start><jupyter_text>for reference:- anger 0- disgust 1- fear 2- happy 3- sad 4- neutral 5<jupyter_code>ravdess_file = []
ravdess_label = []

ravdess_dict = {5:0, 7:1, 6:2, 3:3, 4:4, 1:5}

for i in range(0, 24):
    # Get all file names in the dir
    actor = "Actor_%02d" % (i + 1)
    inputs = os.listdir('./data/RAV/' + actor)
    
    for ele in inputs:
        file_name = ele.split('-')


        if int(file_name[2]) in [5,7,6,3,4,1]:
            ravdess_file.append('./data/RAV/' + actor + '/' + ele) # Form paths to the files
            ravdess_label.append(ravdess_dict[int(file_name[2])])                    # Get emotion label
 
ravdess = list(zip(ravdess_file,ravdess_label))
ravdess_df = pd.DataFrame(ravdess, columns=['file', 'label'])

ravdess_df.head()
emodb_file = []
emodb_label = []

emodb_dict = {'W':0, 'E':1, 'A':2, 'F':3, 'T':4, 'N':5}

inputs = os.listdir('./data/EmoDB/wav/')

for ele in inputs:
    if ele[5] in ['W', 'E', 'A', 'F', 'T', 'N']:
        emodb_file.append('./data/EmoDB/wav/' + ele)
    
        emodb_label.append(emodb_dict[ele[5]])
    
emodb = list(zip(emodb_file,emodb_label))

emodb_df = pd.DataFrame(emodb, columns=['file', 'label'])

emodb_df.head()
tess_file = []
tess_label = []

tess_dict = {'angry':0, 'disgust':1, 'fear':2, 'happy':3, 'sad':4, 'neutral':5}

temp = os.listdir('./data/TESS')
inputs = []

for ele in temp:
    if '.wav' in ele:
        inputs.append(ele)
        
for ele in inputs:
    file_name = ele.replace('.', '_')
    file_name = file_name.split('_')   
    if file_name[2] in ['angry', 'disgust', 'fear', 'happy', 'sad', 'neutral']:
        tess_file.append('./data/TESS/' + ele)
        tess_label.append(tess_dict[file_name[2]])

tess = list(zip(tess_file,tess_label))

tess_df = pd.DataFrame(tess, columns=['file', 'label'])

tess_df.head()
savee_file = []
savee_label = []

savee_dict = {'a':0, 'd':1, 'f':2, 'h':3, 'sa':4, 'n':5}

actors = ['DC', 'JE', 'JK', 'KL']

for actor in actors:
    inputs = os.listdir('./data/SAVEE/' + actor)
    
    for ele in inputs:
        if(ele.split('.')[0][:-2] in ['a', 'd', 'f', 'h', 'sa', 'n']):
            savee_file.append('./data/SAVEE/' + actor + '/' + ele)
            savee_label.append(savee_dict[ele.split('.')[0][:-2]])
        
savee = list(zip(savee_file,savee_label))

savee_df = pd.DataFrame(savee, columns=['file', 'label'])

savee_df.head()       
main = pd.concat([ravdess_df, savee_df, emodb_df, tess_df])
main.head()
main.shape
file = list(main['file'])
label = list(main['label'])
one_hot_label = []
for l in label:
    one_hot_label.append(tf.one_hot(l, 6))
one_hot_label[:10]
label[:10]
# Runs for a LOOONGGGGGGG Time
lbr_data = []

for ele in file:
    temp, sfreq = librosa.load(ele)
    lbr_data.append(np.pad(temp, (0, 197971 - len(temp))))
mfcc_data = []
for ele in lbr_data:
    mfcc_data.append(librosa.feature.mfcc(ele))
mfcc_data = np.array(mfcc_data).reshape(4330, 20, 387)
mfcc_data.shape
mel_data = []
for ele in lbr_data:
    mel_data.append(librosa.feature.melspectrogram(ele))
mel_data = np.array(mel_data).reshape(4330, 128, 387)
# Mel spectrum takes much longer than MFCC
train_X, test_X, train_y, test_y = train_test_split(mel_data, one_hot_label, test_size=0.15, shuffle=True)
model = Sequential()

# Change input shape when using different features
model.add(Conv1D(228, 1, input_shape = (128, 387), activation='tanh'))
model.add(Conv1D(128, 3, activation='tanh'))
model.add(Conv1D(64, 3, activation='tanh'))

#print(model.output_shape)

model.add(LSTM(256, return_sequences=True))
model.add(LSTM(128, return_sequences=True))

model.add(Flatten())

model.add(Dense(128, activation="tanh"))
model.add(Dense(64, activation="tanh"))
model.add(Dense(6))
train_y = np.array(train_y)
from keras.callbacks import EarlyStopping

earlystop_callback = EarlyStopping(
  monitor='val_accuracy', min_delta=0.0001,
  patience=8, restore_best_weights=True)
model.compile(loss=tf.nn.softmax_cross_entropy_with_logits, optimizer="Adam", metrics=["accuracy"])

history = model.fit(train_X, train_y, batch_size=16, epochs=100, validation_split=0.1, callbacks=[earlystop_callback])
plt.plot(history.history['loss'], label="train_loss")
plt.plot(history.history['val_loss'], label="val_loss")

plt.legend()
test_pred_y = model.predict(test_X)
corr = 0
for i in range (0, len(test_pred_y)):
    if np.argmax(test_pred_y[i]) == np.argmax(test_y[i]):
        corr += 1
corr / len(test_pred_y)
temp, sfreq = librosa.load(file[0])

librosa.display.waveplot(temp, x_axis='time')
S = librosa.feature.melspectrogram(temp)
S_DB = librosa.power_to_db(S, ref=np.max)
librosa.display.specshow(S_DB, x_axis='time', y_axis='mel');
S = librosa.stft(temp)
Y = np.abs(S) ** 2
librosa.display.specshow(Y, x_axis='time', y_axis='linear')
S = librosa.feature.mfcc(temp)
librosa.display.specshow(S, x_axis='time');<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.18465326220763234, 'fraction_numerical': 0.029134181370537547, 'mean_word_length': 4.453020134228188, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 5, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 1, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '1', 'dir': 'jupyter-structured-clean-dedup', 'id': '397417', 'license': 'CC-BY-4.0', 'n_tokens_mistral': 2078, 'n_tokens_neox': 1972, 'n_words': 370, 'path': 'EmotionClassifierCNN_LSTM.ipynb', 'repo_name': 'OwenPi314/EDM', 'size': '4874'}