-
Notifications
You must be signed in to change notification settings - Fork 0
/
ESC-Dense.py
227 lines (168 loc) · 8.11 KB
/
ESC-Dense.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
from keras.layers import Lambda, Input, Dense
from keras.models import Model
from keras.datasets import mnist
from keras.losses import mse, binary_crossentropy
from keras.utils import plot_model
from keras import backend as K
from keras.utils import to_categorical
from keras.models import Sequential, Model
from keras.layers import Activation, Dense, Dropout, Conv1D,Conv2D, GlobalAveragePooling2D, InputLayer, \
Flatten, MaxPooling2D,MaxPooling1D, LSTM, ConvLSTM2D, Reshape, Concatenate,concatenate, Input, AveragePooling2D
from keras.layers.normalization import BatchNormalization
from keras.optimizers import Adam
import tensorflow as tf
import sys, os
from time import time
import numpy as np
import os
from config import *
import datetime
import random
import keras.optimizers
import librosa
import librosa.display
import pandas as pd
import warnings
from keras import backend as K
import utils
from utils import importData, preprocess, recall_m, precision_m, f1_m
from timeit import default_timer as timer
# model parameters for training
totalLabel = 50
batchSize = 128
epochs = 100
latent_dim=8
dataSize=128
class DenseNet:
def __init__(self, input_shape=None, dense_blocks=3, dense_layers=-1, growth_rate=12, nb_classes=None,
dropout_rate=None, 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
latent_dim=4
############################
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, (latent_dim//2,latent_dim//2), strides=((latent_dim//2,latent_dim//2)), 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,latent_dim), strides=(latent_dim,latent_dim))(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_4a)
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)
# 27 x 58 x 48
conv_11a = Conv2D(48, (latent_dim//2,latent_dim//2), padding="valid")(act_10a)
# 6 x 29 x 48
pool_12a=MaxPooling2D((latent_dim//2,latent_dim//2), strides=(latent_dim//2,latent_dim//2))(conv_11a)
act_13a = Activation('relu')(pool_12a)
print('inshape a', act_13a.shape)
x = Conv2D(2*self.growth_rate, (3,3),
padding='same', strides = (1,1),
kernel_regularizer=keras.regularizers.l2(self.weight_decay))(act_13a)
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)
model_c= Model(inputs=model_a_in, outputs=act_13a, name='comp')
return Model(inputs=model_a_in, outputs=prediction, name='densenet'), model_c
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
print('creating net')
densenet = DenseNet((128,128,1), nb_classes=50, depth=25)
print('building model')
model, model_comp = 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'])
model.compile(optimizer='adam', loss='categorical_crossentropy', metrics=['acc',f1_m,precision_m, recall_m])
(train_data,train_labels), (test_data, test_labels) = importData()#.load_data()
y_train = np.array(keras.utils.to_categorical(train_labels, totalLabel))
y_test = np.array(keras.utils.to_categorical(test_labels, totalLabel))
X_train = np.array([x.reshape( (128, 128, 1) ) for x in train_data])
X_test = np.array([x.reshape( (128, 128, 1 ) ) for x in test_data])
model.fit(X_train,
y=y_train,
epochs=epochs,
batch_size=batchSize,
validation_data= (X_test, y_test)
)
model.save('ESCDense.FULL.hdf5')