-
Notifications
You must be signed in to change notification settings - Fork 13
/
Copy pathtrain.py
executable file
·209 lines (177 loc) · 4.75 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
'''
Command line script for training the speechVGG network
'''
import os
import numpy as np
from argparse import ArgumentParser
from glob import glob
from keras.optimizers import Adam
from libs.data_generator import DataGenerator
from libs.speech_vgg import speechVGG
from keras.callbacks import TensorBoard, ModelCheckpoint
from keras_tqdm import TQDMCallback
from tensorflow.python.client import device_lib
def parse_args():
parser = ArgumentParser(description='Training script for speechVGG')
parser.add_argument(
'--name',
type=str,
default='',
help='Dataset name'
)
parser.add_argument(
'--train',
type=str,
help='Folder with training images'
)
parser.add_argument(
'--test',
type=str,
help='Folder with testing images'
)
parser.add_argument(
'--batch_size',
type=int,
default=32,
help='What batch-size should we use'
)
parser.add_argument(
'--weight_path',
type=str,
default='./data/weights/',
help='Where to output weights during training'
)
parser.add_argument(
'--height',
type=int,
default=128,
help='height of spectrogram'
)
parser.add_argument(
'--width',
type=int, default=128,
help='width of spectrogram'
)
parser.add_argument(
'--channels',
type=int,
default=1,
help='number of channels of spectrogram'
)
parser.add_argument(
'--classes',
type=int,
default=8,
help='number of classes (or words in our case)'
)
parser.add_argument(
'--lr',
type=float,
default=0.00005,
help='learning rate'
)
parser.add_argument(
'--log_path',
type=str,
default='./data/logs/',
help='Where to output tensorboard logs during training'
)
parser.add_argument(
'--epochs',
type=str,
default='50',
help='Number of training epochs'
)
parser.add_argument(
'--augment',
type=str,
default='yes',
help='Augment training data? yes/no'
)
parser.add_argument(
'--weights',
type=str,
help='weights from where to start training'
)
parser.add_argument(
'--transfer_learning',
type=str,
default='no',
help='Transfer learning? yes/no'
)
parser.add_argument(
'--model',
default='speechVGG',
type=str
)
return parser.parse_args()
# Run script
if __name__ == '__main__':
# Parse command-line arguments
args = parse_args()
# create dictionary of file names
partition = {'train': glob(args.train + '/*.h5'),
'test': glob(args.test + '/*.h5')}
if args.augment == 'yes':
train_augment = True
print('Applying data augmentation in training...')
else:
train_augment = False
print('Training WITHOUT augmentation (?)')
if args.transfer_learning == 'yes':
transfer_learning = True
else:
transfer_learning = False
# Create training generator
train_generator = DataGenerator(
list_IDs=partition['train'],
batch_size=args.batch_size,
dim=(args.width, args.height, args.channels),
classes=args.classes,
data_augmentation=train_augment,
shuffle=True
)
# Create test generator
test_generator = DataGenerator(
list_IDs=partition['test'],
batch_size=args.batch_size,
dim=(args.width, args.height, args.channels),
classes=args.classes,
data_augmentation=False,
shuffle=False
)
if args.model=='speechVGG':
model = speechVGG(
include_top=True,
input_shape=(args.width, args.height, args.channels),
classes=args.classes,
pooling=None,
weights=args.weights,
transfer_learning=transfer_learning
)
# Compile model
model.compile(
optimizer=Adam(args.lr),
loss='categorical_crossentropy',
metrics=['acc']
)
# Train
model.fit_generator(
train_generator,
validation_data=test_generator,
epochs=np.int(args.epochs),
verbose=0,
callbacks=[
TensorBoard(
log_dir=os.path.join(args.log_path, args.name),
write_graph=False
),
ModelCheckpoint(
os.path.join(args.weight_path, args.name, 'weights.{epoch:02d}-{loss:.2f}.h5'),
monitor='val_loss',
save_best_only=True,
save_weights_only=True
),
TQDMCallback()
]
)