-
Notifications
You must be signed in to change notification settings - Fork 0
/
densetest.py
277 lines (212 loc) · 9.53 KB
/
densetest.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
import keras
from keras.models import Model
from keras.layers import Input, GlobalAveragePooling2D, Dense, concatenate, AveragePooling2D
from keras.layers.convolutional import Conv2D
from keras.layers.core import Activation, Dropout
from keras.layers.normalization import BatchNormalization
import numpy as np
from keras.optimizers import Adam
from tensorflow.contrib.learn.python.learn.datasets.mnist import extract_images, extract_labels
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/'
def preProcess(dataset):
print('TotalCount: {}'.format(totalRecordCount))
trainDataEndIndex = int(totalRecordCount*0.8)
totalLabel = 50
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)
# 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])
# 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))
return X_train, X_test, y_train, y_test
# 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
return dataSet
class DenseNet:
def __init__(self, input_shape=None, dense_blocks=3, dense_layers=-1, growth_rate=12, nb_classes=None,
dropout_rate=0.5, bottleneck=False, compression=1.0, weight_decay=1e-4, depth=40):
# Checks
if nb_classes == None:
raise Exception(
'Please define number of classes (e.g. num_classes=10). This is required for final softmax.')
if compression <= 0.0 or compression > 1.0:
raise Exception('Compression have to be a value between 0.0 and 1.0.')
if type(dense_layers) is list:
if len(dense_layers) != dense_blocks:
raise AssertionError('Number of dense blocks have to be same length to specified layers')
elif dense_layers == -1:
dense_layers = int((depth - 4) / 3)
if bottleneck:
dense_layers = int(dense_layers / 2)
dense_layers = [dense_layers for _ in range(dense_blocks)]
else:
dense_layers = [dense_layers for _ in range(dense_blocks)]
self.dense_blocks = dense_blocks
self.dense_layers = dense_layers
self.input_shape = input_shape
self.growth_rate = growth_rate
self.weight_decay = weight_decay
self.dropout_rate = dropout_rate
self.bottleneck = bottleneck
self.compression = compression
self.nb_classes = nb_classes
def build_model(self):
img_input = Input(shape=self.input_shape, name='img_input')
nb_channels = self.growth_rate
x = Conv2D(2*self.growth_rate, (3,3),
padding='same', strides = (1,1),
kernel_regularizer=keras.regularizers.l2(self.weight_decay))(img_input)
for block in range(self.dense_blocks-1):
x, nb_channels = self.dense_block(x, self.dense_layers[block], nb_channels, self.growth_rate,
self.dropout_rate, self.bottleneck, self.weight_decay)
x = self.transition_layer(x, nb_channels, self.dropout_rate, self.compression, self.weight_decay)
nb_channels = int(nb_channels*self.compression)
x, nb_channels = self.dense_block(x, self.dense_layers[-1], nb_channels, self.growth_rate, self.dropout_rate, self.weight_decay)
x = BatchNormalization()(x)
x = Activation('relu')(x)
x = GlobalAveragePooling2D()(x)
prediction = Dense(self.nb_classes, activation='softmax')(x)
return Model(inputs=img_input, outputs=prediction, name='densenet')
def dense_block(self, x, nb_layers, nb_channels, growth_rate, dropout_rate=None, bottleneck=False, weight_decay=1e-4):
for i in range(nb_layers):
cb = self.convolution_block(x, growth_rate, dropout_rate, bottleneck)
nb_channels += growth_rate
x = concatenate([cb,x])
return x, nb_channels
def convolution_block(self, x, nb_channels, dropout_rate=None, bottleneck=False, weight_decay=1e-4):
# Bottleneck
if bottleneck:
bottleneckWidth = 4
x = BatchNormalization()(x)
x = Activation('relu')(x)
x = Conv2D(nb_channels * bottleneckWidth, (1, 1),
kernel_regularizer=keras.regularizers.l2(weight_decay))(x)
# Dropout
if dropout_rate:
x = Dropout(dropout_rate)(x)
# Standard (BN-ReLU-Conv)
x = BatchNormalization()(x)
x = Activation('relu')(x)
x = Conv2D(nb_channels, (3, 3), padding='same')(x)
# Dropout
if dropout_rate:
x = Dropout(dropout_rate)(x)
return x
def transition_layer(self, x, nb_channels, dropout_rate=None, compression=1.0, weight_decay=1e-4):
x = BatchNormalization()(x)
x = Activation('relu')(x)
x = Conv2D(int(nb_channels * compression), (1, 1), padding='same',
kernel_regularizer=keras.regularizers.l2(weight_decay))(x)
# Adding dropout
if dropout_rate:
x = Dropout(dropout_rate)(x)
x = AveragePooling2D((2, 2), strides=(2, 2))(x)
return x
def dataset_shuffle(dataset): # Returns separated shuffled data and classes from dataset
np.random.shuffle(dataset)
n, m = dataset.shape
x = data[:, 0:m-1]
y = data[:, m-1]
return x, y # Return shuffled x and y with preserved order
print('creating net')
densenet = DenseNet((128,128,1), nb_classes=50, depth=10)
print('building model')
model = densenet.build_model()
model_optimizer = Adam(lr=1E-3, beta_1=0.9, beta_2=0.999, epsilon=1e-08)
print('compiling model')
model.compile(loss='categorical_crossentropy', optimizer=model_optimizer, metrics=['accuracy'])
'''
with open('my/directory/train-images-idx3-ubyte.gz', 'rb') as f:
train_images = extract_images(f)
with open('my/directory/train-labels-idx1-ubyte.gz', 'rb') as f:
train_labels = extract_labels(f)
idx = np.random.permutation(len(train_images))
x,y = train_images[idx], train_labels[idx]
trainDataEndIndex = int(len(train_images)*0.8)
totalLabel = 10
X_train = x[:trainDataEndIndex]
X_test = x[trainDataEndIndex:]
X_train = np.array([x.reshape( (28, 28, 1) ) for x in X_train])
X_test = np.array([x.reshape( (28, 28, 1 ) ) for x in X_test])
y_train = y[:trainDataEndIndex]
y_test = y[trainDataEndIndex:]
y_train = np.array(keras.utils.to_categorical(y_train, totalLabel))
y_test = np.array(keras.utils.to_categorical(y_test, totalLabel))
print(X_train[0])
print(y_train[0])
'''
dataset= importData()
X_train, X_test, y_train, y_test = preProcess(dataset)
batchSize=128
epochs=100
model.fit(X_train,
y=y_train,
epochs=epochs,
batch_size=batchSize,
validation_data= (X_test, y_test))
model.save('models/SingleModel.4.dense10.h5')