-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmain.py
108 lines (86 loc) · 4.42 KB
/
main.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
# External Imports
import numpy as np
import itertools
# Project Level Imports
import datasource_analysis
import feature_extraction_audio
import io_operations
import model_creation_audio
import model_evaluation_audio
import config
def experiment_1():
""" Function running experiment_1: raw audio labelling, audio feature extraction, audio model training & testing"""
"""
Raw Audio Data Labelling
"""
# Define parameters to use for labelling
labellingFilename = "Labelled_SUBESCO_AUDIO"
featureOutputFilename = "mfcc(12)_zcr_cc_plus48khz"
dataOriginName = "SUBESCO"
# Check for presence or absence of the specified file (create or load file)
if io_operations.checkIfFileExists(labellingFilename+".csv", dataOriginName):
dataDF = io_operations.loadDataset(labellingFilename, dataOriginName)
print(dataDF)
else:
# Load, Label and if needed transcribe an audio dataset
dataDF = datasource_analysis.identifyData(dataOriginName, "AUDIO", ".wav")
print()
# Persist the found files and associated values to disk
io_operations.saveDataset(dataDF, labellingFilename, dataOriginName)
print()
"""
Audio Feature Extraction
"""
# Define the list of features, and the required arguments (Originates from Librosa)
featureSet = ["mfcc", "zero_crossing_rate", "chroma_cens", "chroma_stft", "melspectrogram", "spectral_contrast", "tonnetz"]
argDict = {'mfcc': {'n_mfcc': 12, 'sr': 48000}, 'chroma_cens': {'sr': 48000}, "chroma_stft": {'sr': 48000}, "melspectrogram": {'sr': 48000}, "spectral_contrast": {'sr': 48000}, "tonnetz": {'sr': 48000}}
# Check for presence or absence of the specified file (create or load file)
if io_operations.checkIfFileExists(featureOutputFilename+".pickle", dataOriginName):
dataDF = io_operations.loadPickle(featureOutputFilename, dataOriginName)
print(dataDF)
else:
# Run the feature extraction loop function
dataDF = feature_extraction_audio.extractFeatures(dataDF, featureSet, argDict, True, 48000, 3)
print()
# Persist the features to disk, in a loadable pickle form, and viewable csv
io_operations.savePickle(dataDF, featureOutputFilename, dataOriginName)
io_operations.saveDataset(dataDF, featureOutputFilename, dataOriginName)
print(dataDF)
"""
Audio Model Creation
"""
# Extract the audio features from the dataframe
# Replace calm with neutral
dataDF['emotion'] = dataDF['emotion'].replace("calm", "neutral")
# Merge gender and emotion
# dataDF['emotion'] = dataDF[['gender', 'emotion']].agg('-'.join, axis=1)
print("Class Distribution:")
print(dataDF['emotion'].value_counts())
# Run model for each permutation of the features
# counter = 1
# for perm in itertools.permutations(featureSet, len(featureSet)):
# for val in list(perm):
# if counter == 1:
# dataDF['features'] = dataDF[val]
# else:
# dataDF['features'] = dataDF['features']+dataDF[val]
# Select our feature and convert to the required shape
featureDataFrame = dataDF['mfcc'].values.tolist()
featureDataFrame = np.asarray(featureDataFrame)
# Run our model code
model_creation_audio.run_model_audio(featureDataFrame, dataDF, "emotion", 5, dataOriginName+"_MFCC", 128, 150)
# Load a pre-trained model
# weightFile = "F:\\emo_detect\\results\\EMO_DB_08-30-2021-16-21-25\\EMO_DB-iter-0\\EMO_DB-iter-0.h5"
# modelFile = "F:\\emo_detect\\results\\EMO_DB_08-30-2021-16-21-25\\EMO_DB-iter-0\\EMO_DB-iter-0.json"
# pretrained_model = model_evaluation_audio.loadJsonModel(weightFile, modelFile)
#
# model_creation_audio.run_pretrained_model_audio(featureDataFrame, dataDF, "emotion", dataOriginName, pretrained_model)
# Merge Features into singular vector
dataDF['feature'] = dataDF['mfcc'] + dataDF['melspectrogram']
print(dataDF['feature'])
# Select our feature and convert to the required shape
featureDataFrame = dataDF['feature'].values.tolist()
featureDataFrame = np.asarray(featureDataFrame)
# Run our model code
model_creation_audio.run_model_audio(featureDataFrame, dataDF, "emotion", 5, dataOriginName+"_MFCC-MELSPEC", 128, 150)
experiment_1()