-
Notifications
You must be signed in to change notification settings - Fork 0
/
train.py
230 lines (184 loc) · 8 KB
/
train.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
import os
import math
import keras
import numpy as np
import matplotlib.pyplot as plt
from keras.preprocessing.image import ImageDataGenerator
from keras.layers import Input, Dense, Flatten, Lambda, Dropout, Activation, LSTM, GRU, \
TimeDistributed, Convolution1D, MaxPooling1D, Convolution2D, MaxPooling2D, \
BatchNormalization, GlobalAveragePooling1D, GlobalMaxPooling1D, concatenate, \
ZeroPadding2D, Reshape, merge, GlobalAveragePooling2D, GlobalMaxPooling2D, AveragePooling2D
from keras.layers.local import LocallyConnected1D
from keras.layers.advanced_activations import ELU
from keras.optimizers import Adam, RMSprop
from keras.callbacks import ReduceLROnPlateau, ModelCheckpoint, EarlyStopping, CSVLogger, TensorBoard
from keras import backend as K
from keras.models import Model
from keras.models import load_model
from sklearn.model_selection import train_test_split
import tensorflow.compat.v1 as tf
tf.disable_v2_behavior()
import tensorflow.compat.v1.keras.backend as KTF
config = tf.ConfigProto()
config.gpu_options.allow_growth=True
sess = tf.Session(config=config)
KTF.set_session(sess)
import librosa as librosa
import os
import ast
import librosa.display
import numpy as np
import pandas as pd
from tqdm import tqdm
from subprocess import Popen, PIPE, STDOUT
from libs import utils, dataset
sampling_rate = 44100
path_to_training_labels = "preprocessing/train_labels.csv"
labels = pd.read_csv(path_to_training_labels)
print(labels.head())
genres = utils.get_genre()
print(genres)
dataset_path = 'PATH_TO_DATASET'
#GENERATE SPECTROGRAMS USING SOX
utils.get_all_music(dataset_path, labels, 20, 200)
# SLICING THE SPECTROGRAMS
utils.slice_spectrograms("spectrograms", 128)
train_x, train_y, validation_x, \
validation_y, test_x, test_y = dataset.create_dataset_from_slices(3000, genres, 128, 0.1, 0.1)
# DEFINE BASE CONV BLOCK
def base_conv_block(num_conv_filters, kernel_size):
def f(input_):
x = BatchNormalization()(input_)
x = Activation('relu')(x)
out = Convolution2D(num_conv_filters, kernel_size, padding='same')(x)
return out
return f
# DEFINE MULTI SCALE BLOCK
def multi_scale_block(num_conv_filters):
def f(input_):
branchpool = MaxPooling2D(pool_size=(3,3), strides=(1,1), padding='same')(input_)
branch3x3 = base_conv_block(num_conv_filters, 1)(input_)
branch3x3 = base_conv_block(num_conv_filters, 3)(branch3x3)
branch3x3_2 = base_conv_block(num_conv_filters, 1)(input_)
branch3x3_2 = base_conv_block(num_conv_filters, (1, 7))(branch3x3_2)
branch3x3_2 = base_conv_block(num_conv_filters, (7, 1))(branch3x3_2)
branch3x3_2 = base_conv_block(num_conv_filters, 3)(branch3x3_2)
out = concatenate([branchpool,branch3x3,branch3x3_2], axis=-1)
return out
return f
# DEFINE DENSE BLOCK
def dense_block(num_dense_blocks, num_conv_filters):
def f(input_):
x = input_
for _ in range(num_dense_blocks):
out = multi_scale_block(num_conv_filters)(x)
x = concatenate([x, out], axis=-1)
return x
return f
# DEFINE TRANSITION BLOCK
def transition_block(num_conv_filters):
def f(input_):
x = BatchNormalization()(input_)
x = Activation('relu')(x)
x = Convolution2D(num_conv_filters, 1)(x)
out = AveragePooling2D(pool_size=(2, 2), strides=(2, 2))(x)
return out
return f
# DEFINE MULTI SCALE LEVEL CNN
def multi_scale_level_cnn(input_shape, num_dense_blocks, num_conv_filters, num_classes):
model_input = Input(shape=input_shape)
x = Convolution2D(num_conv_filters, 3, padding='same')(model_input)
x = BatchNormalization()(x)
x = Activation('relu')(x)
x = MaxPooling2D(pool_size=(4, 1))(x)
x = dense_block(num_dense_blocks, num_conv_filters)(x)
x = transition_block(num_conv_filters)(x)
x = BatchNormalization()(x)
x = Activation('relu')(x)
x = GlobalAveragePooling2D()(x)
model_output = Dense(num_classes, activation='softmax')(x)
model = Model(inputs=model_input, outputs=model_output)
return model
# DEFINE PROCESS DATA FOR CONV2D
def process_data_for_conv2D(X, resize_shape=None):
X_conv2D = []
for sample in X:
sample = np.reshape(sample, newshape=(sample.shape[0], sample.shape[1], 1))
if resize_shape:
sample = resize(sample, output_shape=resize_shape)
X_conv2D.append(sample)
return np.array(X_conv2D, dtype=np.float32)
def data_iter(X, y, batch_size):
num_samples = X.shape[0]
idx = list(range(num_samples))
while True:
for i in range(0, num_samples, batch_size):
j = idx[i:min(i+batch_size, num_samples)]
yield X[j, :], y[j, :]
#check the architecture of the net
model = multi_scale_level_cnn(input_shape=(128, 128, 3),
num_dense_blocks=6, num_conv_filters=32, num_classes=8)
model.summary()
# STARTING THE TRAINING
print("starting the training")
#without data argumatent
k_fold = 10
num_classes = 8
train_size = 0.8
val_size = 0.1
test_size = 0.1
epochs = 100
batch_size = 8
lr = 0.01
file_name0 = 'results/fma_model_inceptionv4_B.hdf5'
path = 'results/log/'
csv_name0 = 'fma_results.csv'
train_loss_record = []
train_acc_record = []
val_loss_record = []
val_acc_record = []
test_loss_record = []
test_acc_record = []
for i in range(k_fold):
print('Start %d fold training' % (i+1))
#X_train, y_train, X_val, y_val, X_test, y_test = train_val_test_split(X_melspec, y, train_size=train_size,
# val_size=val_size, test_size=test_size)
file_name = 'results/model/'+str(i)+'_fold_'+file_name0
# log_path = path+str(i)+'_fold_'+'tensorboard_log'
csv_path = path+str(i)+'_fold_'+ csv_name0
lr_change = ReduceLROnPlateau(monitor="loss", factor=0.5, patience=3, min_lr=0.000)
model_checkpoint = ModelCheckpoint(file_name, monitor='val_acc', save_best_only=True, mode='max')
early_stopping = EarlyStopping(monitor='loss', min_delta=0.01, patience=10, mode='min')
csv_logger = CSVLogger(csv_path)
# tb_cb = TensorBoard(log_dir=log_path, write_images=1, histogram_freq=1)
callbacks =[lr_change, model_checkpoint, early_stopping,csv_logger]
opt = Adam(lr=lr)
model = multi_scale_level_cnn(input_shape=(128, 128, 3),
num_dense_blocks=6, num_conv_filters=32, num_classes=num_classes)
model.compile(
loss='categorical_crossentropy',
metrics=['accuracy'],
optimizer=opt)
model.fit(train_x, train_y, batch_size=batch_size, epochs=epochs,
validation_data=(validation_x, validation_y), verbose=1,
callbacks=callbacks)
model_best = load_model(file_name)
train_loss, train_acc = model_best.evaluate(train_x, train_y, batch_size=batch_size, verbose=0)
val_loss, val_acc = model_best.evaluate(validation_x, validation_y, batch_size=batch_size, verbose=0)
test_loss, test_acc = model_best.evaluate(test_x, test_y, batch_size=batch_size, verbose=0)
train_loss_record.append(train_loss)
train_acc_record.append(train_acc)
val_loss_record.append(val_loss)
val_acc_record.append(val_acc)
test_loss_record.append(test_loss)
test_acc_record.append(test_acc)
print('\n\n%d fold train loss %.4f train acc %.4f, val loss %.4f val acc %.4f, test loss %.4f test acc %.4f\n\n' %
(i+1, train_loss, train_acc, val_loss, val_acc, test_loss, test_acc))
train_loss_avg = np.mean(np.array(train_loss_record))
train_acc_avg = np.mean(np.array(train_acc_record))
val_loss_avg = np.mean(np.array(val_loss_record))
val_acc_avg = np.mean(np.array(val_acc_record))
test_loss_avg = np.mean(np.array(test_loss_record))
test_acc_avg = np.mean(np.array(test_acc_record))
print('\n\n%d fold train loss avg %.4f train acc avg %.4f, val loss avg %.4f val acc avg %.4f, test loss avg %.4f test acc avg %.4f' %
(k_fold, train_loss_avg, train_acc_avg, val_loss_avg, val_acc_avg, test_loss_avg, test_acc_avg))