-
Notifications
You must be signed in to change notification settings - Fork 0
/
trainESC50b3.single.8.lsp.py
346 lines (272 loc) · 11.3 KB
/
trainESC50b3.single.8.lsp.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
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
import tensorflow
from tensorflow import keras
from tensorflow.keras.layers import Activation, Dense, Dropout, Conv2D, \
Flatten, MaxPooling2D, LSTM, ConvLSTM2D, Reshape, Concatenate, Input
from tensorflow.keras.models import Sequential, Model
from attention import Attention
from tensorflow.keras.callbacks import LearningRateScheduler,EarlyStopping
#from keras.callbacks import TensorBoard
#import keras.optimizers
import librosa
import librosa.display
import numpy as np
import pandas as pd
import random
import time
import warnings
import os
import time
import math
warnings.filterwarnings('ignore')
# Your data source for wav files
dataSourceBase = '/home/paul/Downloads/ava_vidprep_supportingModels/ESC-50-aug/'
#dataSourceBase = '/home/paul/Downloads/ava_vidprep_supportingModels/ESC-50-clone/'
#dataSourceBase = '/home/paul/Downloads/ESC-50-tst2/'
# Total wav records for training the model, will be updated by the program
totalRecordCount = 0
# Total classification class for your model (e.g. if you plan to classify 10 different sounds, then the value is 10)
totalLabel = 50
# model parameters for training
batchSize = 128
epochs = 100
latent_dim=8
dataSize=128
timesteps = 128 # Length of your sequences
input_dim = 128
def preprocess(array, labels):
"""
Normalizes the supplied array and reshapes it into the appropriate format.
"""
lookback = 1#latent_dim
array=np.array(array)
maxi=0
#for i in range(array.shape[0]):
# if (maxi<np.max(array[i]):
# maxi= np.max(array[i])
print("arrshape1:", array.shape)
#print("labshape:", labels)
#array, labels = temporalize(array, labels, lookback)
print("arrshape2:", array.shape)
array = np.array(array).astype("float32") / np.max(array)
array = np.reshape(array, (lookback*len(array), dataSize, dataSize,1))
return array, labels
def temporalize(X, y, lookback):
'''
Inputs
X A 3D numpy array ordered by time of shape:
(n_observations x steps_per_ob x n_features)
y A 1D numpy array with indexes aligned with
X, i.e. y[i] should correspond to X[i].
Shape: n_observations.
lookback The window size to look back in the past
records. Shape: a scalar.
Output
output_X A 4D numpy array of shape:
((n_observations-lookback-1) x steps_per_ob x lookback x
n_features)
output_y A 1D array of shape:
(n_observations-lookback-1), aligned with X.
'''
output_X = []
output_y = []
for i in range(len(X) - lookback - 1):
print('look', i, len(output_X), len(output_y))
t=[]
for j in range(1, lookback + 1):
# Gather the past records upto the lookback period
t.append(X[[(i + j + 1)], :])
output_y.append(y[i + lookback + 1])
output_X.append(t)
#return np.array(output_X), np.array(output_y)
return np.squeeze(np.array(output_X)), np.array(output_y)
# This function will import wav files by given data source path.
# And will extract wav file features using librosa.feature.melspectrogram.
# Class label will be extracted from the file name
# File name pattern: {WavFileName}-{ClassLabel}
# e.g. 0001-0 (0001 is the name for the wav and 0 is the class label)
# The program only interested in the class label and doesn't care the wav file name
def importData():
dataSet = []
lblmap ={}
lblid=0
totalCount = 0
progressThreashold = 100
dirlist = os.listdir(dataSourceBase)
for dr in dirlist:
dataSource = os.path.join(dataSourceBase,dr)
for root, _, files in os.walk(dataSource):
for file in files:
fileName, fileExtension = os.path.splitext(file)
if fileExtension != '.wav': continue
if totalCount % progressThreashold == 0:
print('Importing data count:{}'.format(totalCount))
wavFilePath = os.path.join(root, file)
y, sr = librosa.load(wavFilePath, duration=2.97)
ps = librosa.feature.melspectrogram(y=y, sr=sr)
if ps.shape != (128, 128): continue
# extract the class label from the FileName
label0 = dr.split('-')[1]
if label0 not in lblmap:
lblmap[label0] =lblid
lblid+=1
label=lblmap[label0]
#label = dr#fileName.split('-')[1]
print(fileName, label0, label)
dataSet.append( (ps, label) )
totalCount += 1
f = open('dict50.csv','w')
f.write("classID,class")
for lb in lblmap:
f.write(str(lblmap[lb])+','+lb)
f.close()
global totalRecordCount
totalRecordCount = totalCount
print('TotalCount: {}'.format(totalRecordCount))
trainDataEndIndex = int(totalRecordCount*0.8)
random.shuffle(dataSet)
train = dataSet[:trainDataEndIndex]
test = dataSet[trainDataEndIndex:]
print('Total training data:{}'.format(len(train)))
print('Total test data:{}'.format(len(test)))
# Get the data (128, 128) and label from tuple
print("train 0 shape is ",train[0][0].shape)
X_train, y_train = zip(*train)
X_test, y_test = zip(*test)
return (X_train, y_train), (X_test, y_test)#dataSet
# This is the default import function for UrbanSound8K
# https://urbansounddataset.weebly.com/urbansound8k.html
# Please download the URBANSOUND8K and not URBANSOUND
#def buildModel(dataset):
def buildModel(X_train, X_test, y_train, y_test):
'''
# Get the data (128, 128) and label from tuple
print("train 0 shape is ",train[0][0].shape)
X_train, y_train = zip(*train)
X_test, y_test = zip(*test)
# Reshape for CNN input
#X_train = np.array([x.reshape( (128, 128, 1) ) for x in X_train])
#X_test = np.array([x.reshape( (128, 128, 1) ) for x in X_test])
X_train = np.array([x.reshape( (128, 128, 1) ) for x in X_train])
X_test = np.array([x.reshape( (128, 128, 1 ) ) for x in X_test])
'''
Xb_train = X_train.copy()#np.array([x.reshape( (128, 128, 1) ) for x in X_train])
Xb_test = X_test.copy()#np.array([x.reshape( (128, 128, 1 ) ) for x in X_test])
# One-Hot encoding for classes
y_train = np.array(keras.utils.to_categorical(y_train, totalLabel))
y_test = np.array(keras.utils.to_categorical(y_test, totalLabel))
model_a = Sequential()
# Model Input
l_input_shape_a=(128, 128,1,1)
input_shape_a=(128, 128,1)
model_a_in = Input(shape=input_shape_a)
conv_1a = Conv2D(24, (latent_dim//2,latent_dim//2), strides=(1, 1), input_shape=input_shape_a)(model_a_in)
# Using CNN to build model
# 24 depths 128 - 5 + 1 = 124 x 124 x 24
#conv_2a = Conv2D(24, (4,4), strides=(1, 1), input_shape=input_shape_a)(conv_1a)
# 31 x 62 x 24
pool_3a = MaxPooling2D((latent_dim//2,latent_dim//2), strides=(latent_dim//2,latent_dim//2))(conv_1a)
act_4a =Activation('relu')(pool_3a)
# 27 x 58 x 48
conv_5a = Conv2D(48, (latent_dim//2,latent_dim//2), padding="valid")(act_4a)
# 6 x 29 x 48
pool_6a=MaxPooling2D((latent_dim//4,latent_dim//4), strides=(latent_dim//4,latent_dim//4))(conv_5a)
act_7a = Activation('relu')(pool_6a)
# 2 x 25 x 48
conv_8a = Conv2D(48, (latent_dim//2,latent_dim//2), padding="valid")(act_7a)
act_9a = Activation('relu')(conv_8a) # 2 x 25 x 48
conv_9a = Conv2D(48, (latent_dim//2,latent_dim//2), padding="valid")(act_9a)
act_10a = Activation('relu')(conv_9a)
print('inshape a', act_10a.shape)
re_10a = Reshape(target_shape=(latent_dim*latent_dim, 48),input_shape=(latent_dim,latent_dim ,48))(act_10a)
ls11a= LSTM(latent_dim*latent_dim,return_sequences=True,unit_forget_bias=1.0,dropout=0.2)(re_10a)
print('ls11 a shape is ', ls11a.shape)
re11a = Reshape(target_shape=(latent_dim*latent_dim ,latent_dim*latent_dim ))(ls11a)
#merge
model_b = Sequential()
# Model Input
l_input_shape_b=(128, 128,1,1)
input_shape_b=(128, 128,1)
model_b_in = Input(shape=input_shape_b)
print(model_b_in.shape)
conv_1b = Conv2D(24, (latent_dim,latent_dim), strides=(1, 1), input_shape=input_shape_a)(model_b_in)
print(conv_1b.shape)
# Using CNN to build model
# 24 depths 128 - 5 + 1 = 124 x 124 x 24
# 98x98x24
pool_2b = MaxPooling2D((latent_dim,latent_dim), strides=(latent_dim,latent_dim))(conv_1b)
print(pool_2b.shape)
conv_3b = Conv2D(48, (latent_dim,latent_dim), strides=(1, 1), input_shape=input_shape_a)(pool_2b)
print(conv_3b.shape)
act_3b =Activation('relu')(conv_3b)
print(act_3b.shape)
print('inshape b', act_3b.shape)
re_4b = Reshape(target_shape=(latent_dim*latent_dim,48),input_shape=(latent_dim,latent_dim,48))(act_3b)
ls_5b= LSTM(latent_dim*latent_dim,return_sequences=True,unit_forget_bias=1.0,dropout=0.2)(re_4b)
print('ls11 a shape is ', ls_5b.shape)
#at15 = Attention(latent_dim)(ls_5b)
#print('at15 shape is ', at15.shape)
flat12 = Flatten()(ls_5b)
drop13 = Dropout(rate=0.5)(flat12)
dense14 = Dense(64)(drop13)
act15 = Activation('relu')(dense14)
drop16=Dropout(rate=0.5)(act15)
dense17=Dense(totalLabel)(drop16)
out = Activation('softmax')(dense17)
model = Model(inputs=[model_a_in, model_b_in], outputs=out)
model.summary()
model.compile(optimizer="Adam",loss="categorical_crossentropy", metrics=['accuracy'])
initial_learning_rate = 0.01
#epochs = 100
drop = 0.75
epochs_drop = 10.0
decay = initial_learning_rate / epochs
def lr_time_based_decay(epoch, lr):
if epoch < 50:
return initial_learning_rate
else:
lrate = initial_learning_rate * math.pow(drop,
math.floor((1+epoch)/epochs_drop))
return lrate
#opt = keras.optimizers.Adam(learning_rate=0.01)
#model.compile(optimizer=opt,loss="categorical_crossentropy", metrics=['accuracy'])
#print(model.summary())
indata = [X_train,Xb_train]
print ('xtrain shape is ',X_train.shape)
print ('xbtrain shape is ',Xb_train.shape)
print ('indata[0] shape is ',indata[0].shape, '1', indata[1].shape,)
print ('ytrain shape is ',y_train.shape)
'''
early_stopping_monitor = EarlyStopping(
monitor='val_loss',
min_delta=0,
patience=50,
verbose=0,
mode='auto',
baseline=None,
restore_best_weights=True
)
'''
model.fit(indata,
y=y_train,
epochs=epochs,
batch_size=batchSize,
validation_data= ([X_test,Xb_test], y_test),#,
#callbacks=[early_stopping_monitor]
#callbacks=[LearningRateScheduler(lr_time_based_decay, verbose=1)],
)
score = model.evaluate([X_test,Xb_test],
y=y_test)
print('Test loss:', score[0])
print('Test accuracy:', score[1])
timestr = time.strftime('%Y%m%d-%H%M%S')
modelName = 'esc50-sound-classification-{}.h5'.format(timestr)
model.save('models/{}'.format(modelName))
print('Model exported and finished')
if __name__ == '__main__':
(train_data,train_labels), (test_data, test_labels) = importData()#.load_data()
# Normalize and reshape the data
train_data, train_labels = preprocess(train_data,train_labels)
print(train_data.shape)
test_data, test_labels = preprocess(test_data,test_labels)
print(test_data.shape)
buildModel(train_data, test_data, train_labels, test_labels)