This repository has been archived by the owner on Oct 3, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 16
/
Copy pathrun_dishes.py
134 lines (98 loc) · 4.54 KB
/
run_dishes.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
#!/usr/bin/env python
# -*- coding: utf-8 -*-
"""The details that matter: Frequency resolution of spectrograms in acoustic scene classification.
Train hybrid model with a single pretrained detector (`dishes`).
"""
import argparse
import os
import sys
if __name__ == '__main__':
# Parse CLI
parser = argparse.ArgumentParser()
parser.add_argument('-D', '--device', help='Theano device used for computations')
parser.add_argument('--all', dest='all', action='store_true',
help='Train on all folds (for final model testing)')
args = parser.parse_args()
# Initialize GPU / Keras
DEVICE = args.device if args.device else 'gpu0'
THEANO_FLAGS = ('device={},'
'floatX=float32,'
'dnn.conv.algo_bwd_filter=deterministic,'
'dnn.conv.algo_bwd_data=deterministic').format(DEVICE)
os.environ['THEANO_FLAGS'] = THEANO_FLAGS
os.environ['KERAS_BACKEND'] = 'theano'
# Import internals
sys.path.append(os.path.abspath(os.path.dirname(__file__)) + '/..')
from arf.core import *
from arf.generics import describe
import arf.monitor
# ----------------------------------------------------------------------
# Train
BANDS = 200
folds = [1, 2, 3, 4] if not args.all else ['all']
for FOLD in folds:
RUN = 'dishes'
if args.all:
RUN += '_all'
np.random.seed(20170713)
task = Task(mel_bands=BANDS)
task.load_dataset(fold=FOLD)
task.generate_features()
# Event detectors
def make_detector(inp, name):
fg = Conv2D(10, kernel_size=(BANDS, 50), kernel_initializer='he_uniform',
name=f'{name}_1')(inp)
fg = BatchNormalization(axis=1, name=f'{name}_2')(fg)
fg = LeakyReLU(name=f'{name}_3')(fg)
fg = Dropout(0.25, name=f'{name}_4')(fg)
fg = Conv2D(10, kernel_size=(1, 1), kernel_initializer='he_uniform',
name=f'{name}_5')(fg)
fg = BatchNormalization(axis=1, name=f'{name}_6')(fg)
fg = LeakyReLU(name=f'{name}_7')(fg)
fg = Dropout(0.25, name=f'{name}_8')(fg)
fg = Conv2D(1, kernel_size=(1, 1), kernel_initializer='he_uniform',
activation='sigmoid', name=f'{name}_9')(fg)
fg = GlobalMaxPooling2D()(fg)
fg = RepeatVector(451)(fg)
return Reshape((1, 1, 451))(fg)
# Background
inputs = Input(shape=(1, BANDS, 500))
bg = Conv2D(100, kernel_size=(BANDS, 50), kernel_initializer='he_uniform')(inputs)
bg = BatchNormalization(axis=1)(bg)
bg = LeakyReLU()(bg)
bg = Dropout(0.25)(bg)
bg = Conv2D(100, kernel_size=(1, 1), kernel_initializer='he_uniform')(bg)
bg = BatchNormalization(axis=1)(bg)
bg = LeakyReLU()(bg)
bg = Dropout(0.25)(bg)
branches = [bg]
for i in range(1):
branches.append(make_detector(inputs, 'dishes'))
bg = concatenate(branches, axis=1)
bg = Conv2D(15, kernel_size=(1, 1), kernel_initializer='he_uniform')(bg)
bg = Lambda(softmax, arguments={'axis': 1}, name='softmax')(bg)
bg = GlobalAveragePooling2D()(bg)
model = Model(inputs=inputs, outputs=bg)
model.compile(loss='categorical_crossentropy', optimizer=keras.optimizers.Adam(lr=0.001),
metrics=['accuracy'])
describe(model)
# Load weights from pretrained detector:
if FOLD == 'all':
pretrained = keras.models.load_model(f'results/d_dishes_2.h5')
else:
pretrained = keras.models.load_model(f'results/d_dishes_{FOLD}.h5')
for layer_idx in [1, 2, 5, 6]:
model.get_layer(f'dishes_{layer_idx}').set_weights(
pretrained.layers[layer_idx].get_weights()
)
batch_size = 32
holdout_batch = next(task.iterbatches(len(task.holdout), task.holdout))
validation_batch = next(task.iterbatches(len(task.validation), task.validation))
monitor = arf.monitor.Monitor(model, holdout_batch, validation_batch, RUN, FOLD)
model.fit_generator(generator=task.iterbatches(batch_size, task.train, augment=True),
steps_per_epoch=len(task.train) // batch_size,
epochs=500,
callbacks=[monitor],
max_queue_size=10)
model.load_weights(f'results/run_{RUN}_{FOLD}.h5')
task.save_predictions(model, run=RUN, fold=FOLD)