/
train_cnn.py
344 lines (262 loc) · 12.2 KB
/
train_cnn.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
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
import matplotlib
matplotlib.use('Agg')
import matplotlib.pyplot as plt
import matplotlib.ticker as ticker
import tensorflow as tf
import keras
from keras.models import Model
from keras.layers import Conv2D, MaxPooling2D, Activation, Dropout
from keras.layers import Flatten, Input, Dense, BatchNormalization
from keras import backend as K
from keras.utils import plot_model
from sklearn.model_selection import train_test_split
import os, sys, errno
os.environ['HDF5_USE_FILE_LOCKING'] = 'FALSE'
import pickle
import glob
import numpy as np
import pandas as pd
import seaborn as sns
import time
import argparse
# Global values
PATH = '/datax/scratch/bbrzycki/data/nb-localization'
TCHANS, FCHANS = 32, 1024
def mkdir(d):
try:
os.makedirs(d)
except OSError as e:
if e.errno != errno.EEXIST:
raise
class DataGenerator(keras.utils.Sequence):
'Generates data for Keras'
def __init__(self, list_IDs, labels, batch_size=32, dim=(TCHANS, FCHANS), n_channels=1, n_classes=1, shuffle=True):
'Initialization'
self.dim = dim
self.batch_size = batch_size
self.labels = labels
self.list_IDs = list_IDs
self.n_channels = n_channels
self.n_classes = n_classes
self.shuffle = shuffle
self.on_epoch_end()
def __len__(self):
'Denotes the number of batches per epoch'
return int(np.floor(len(self.list_IDs) / self.batch_size))
def __getitem__(self, index):
'Generate one batch of data'
# Generate indexes of the batch
indexes = self.indexes[index*self.batch_size:(index+1)*self.batch_size]
# Find list of IDs
list_IDs_temp = [self.list_IDs[k] for k in indexes]
# Generate data
X, y = self.__data_generation(list_IDs_temp)
return X, y
def on_epoch_end(self):
'Updates indexes after each epoch'
self.indexes = np.arange(len(self.list_IDs))
if self.shuffle == True:
np.random.shuffle(self.indexes)
def __data_generation(self, list_IDs_temp):
'Generates data containing batch_size samples'
#
X = np.reshape(np.array([np.load(fn)[:TCHANS, :FCHANS] for fn in list_IDs_temp]),
(self.batch_size, *self.dim, 1))
if self.n_channels == 2:
X2 = np.copy(X)
# Normalize over entire frame
X -= np.mean(X, axis=(1, 2, 3), keepdims=True)
X /= np.std(X, axis=(1, 2, 3), keepdims=True)
if self.n_channels == 2:
# Normalize over frequency
X2 -= np.mean(X2, axis=(1, 3), keepdims=True)
X2 /= np.std(X2, axis=(1, 3), keepdims=True)
X = np.concatenate([X, X2], axis=3)
# filenames in csv are only the filename, whereas glob dives the rest of the path
y = np.array([self.labels[os.path.split(fn)[1]] for fn in list_IDs_temp]).reshape((self.batch_size, self.n_classes))
return X, y
def index_diff(y_true, y_pred):
return K.mean(K.abs(y_true - y_pred)**2)**0.5 * FCHANS
def BaselineModel(n_classes):
def Conv_Pool(x, num_filters=32):
x = Conv2D(num_filters, (3, 3), activation='relu')(x)
x = MaxPooling2D((2, 2))(x)
return x
inputs = Input(shape=(32, 1024, 1))
conv1 = Conv2D(32, (3, 3), activation='relu')(inputs)
convpool1 = Conv_Pool(conv1, 32)
convpool2 = Conv_Pool(convpool1, 32)
convpool3 = Conv_Pool(convpool2, 64)
flat = Flatten()(convpool3)
dense1 = Dense(64, activation='relu')(flat)
dense2 = Dense(64, activation='relu')(dense1)
dropout = Dropout(0.5)(dense2)
outputs = Dense(n_classes, activation='linear')(dropout)
model = Model(inputs=inputs,
outputs=outputs)
model.compile(loss='mean_squared_error',
optimizer='adam',
metrics=[index_diff])
return model
def FinalModel(n_classes, dense1_num=1024, dense2_num=1024):
def Residual(x, layers=32):
conv = Conv2D(layers, (3, 3), padding='same')(x)
residual = keras.layers.add([x, conv])
act = Activation('relu')(residual)
normed = BatchNormalization()(act)
return normed
inputs = Input(shape=(TCHANS, FCHANS, 2))
strided1 = Conv2D(32, (3, 3), strides=2, activation='relu')(inputs)
residual1 = Residual(strided1, 32)
strided2 = Conv2D(32, (3, 3), strides=2, activation='relu')(residual1)
residual2 = Residual(strided2, 32)
strided3 = Conv2D(64, (3, 3), strides=2, activation='relu')(residual2)
flat = Flatten()(strided3)
dense1 = Dense(dense1_num, activation='relu')(flat)
dense2 = Dense(dense2_num, activation='relu')(dense1)
dropout = Dropout(0.5)(dense2)
outputs = Dense(n_classes, activation='linear')(dropout)
model = Model(inputs=inputs,
outputs=outputs)
model.compile(loss='mean_squared_error',
optimizer='adam',
metrics=[index_diff])
return model
def choose_data(rfi_num=0, use_bright=False, split_name='train'):
xsig = '{:d}sig'.format(1 + rfi_num)
# Load dataset paths and labels
filenames = glob.glob('{}/{}/{}/*.npy'.format(PATH, xsig, split_name))
# For bright case, exclude 0, 5 dB signals
if use_bright and split_name == 'train':
dim_filenames = glob.glob('{}/{}/{}/0*.npy'.format(PATH, xsig, split_name))
filenames = list(set(filenames) - set(dim_filenames))
training_size = len(filenames)
labels_df = pd.read_csv('{}/{}/{}/labels.csv'.format(PATH, xsig, split_name))
if rfi_num == 0:
labels = {row['filename']: [row['start_index']/FCHANS,
row['end_index']/FCHANS]
for index, row in labels_df.iterrows()}
else:
labels = {row['filename']: [row['start_index']/FCHANS,
row['end_index']/FCHANS,
row['rfi_start_index']/FCHANS,
row['rfi_end_index']/FCHANS]
for index, row in labels_df.iterrows()}
return filenames, labels
def run_training(model_name='baseline',
rfi_num=0,
use_bright=False,
batch_size=32,
epochs=100,
seed=42,
gpu_id='0',
exp_name='',
dense1_num=1024,
dense2_num=1024):
# Set which gpu to use per experiment
os.environ['CUDA_VISIBLE_DEVICES'] = gpu_id
np.random.seed(seed)
tf.random.set_seed(seed)
start_time = time.time()
xsig = '{:d}sig'.format(1 + rfi_num)
n_classes = (1 + rfi_num) * 2
# Create directory for models and output files
model_dir_path = '{}/training/{}_{}_{:d}bs'.format(PATH,
model_name,
xsig,
batch_size)
if use_bright:
model_dir_path = '{}_bright'.format(model_dir_path)
if exp_name != '':
model_dir_path = '{}_{}'.format(model_dir_path, exp_name)
mkdir(model_dir_path)
# Set number of channels used in data preprocessing depending on the model
if model_name == 'baseline':
n_channels = 1
model = BaselineModel(n_classes)
elif model_name == 'final':
n_channels = 2
model = FinalModel(n_classes, dense1_num, dense2_num)
else:
sys.exit('Invalid model name')
plot_model(model, to_file='{}/architecture.png'.format(model_dir_path), show_shapes=True)
# Load dataset paths and labels
filenames, labels = choose_data(rfi_num=rfi_num,
use_bright=use_bright,
split_name='train')
# Create train/validation split
X_train, X_validation = train_test_split(filenames, test_size=0.2, random_state=seed)
# Make dataset generators
train_params = {'dim': (TCHANS, FCHANS),
'batch_size': batch_size,
'n_channels': n_channels,
'n_classes': n_classes,
'shuffle': True}
training_generator = DataGenerator(X_train, labels, **train_params)
validation_generator = DataGenerator(X_validation, labels, **train_params)
# Set up model training using custom generators, with callbacks for early stopping
model_fn = '{}/model.h5'.format(model_dir_path)
history_fn = '{}/history'.format(model_dir_path)
history = model.fit(x=training_generator,
steps_per_epoch=len(X_train) // batch_size,
epochs=epochs,
validation_data=validation_generator,
validation_steps=len(X_validation) // batch_size,
use_multiprocessing=True,
workers=8,
callbacks=[keras.callbacks.ModelCheckpoint(model_fn,
monitor='val_loss',
verbose=0,
save_best_only=True,
mode='auto'),
keras.callbacks.ReduceLROnPlateau(monitor='val_loss',
factor=0.1,
patience=5,
min_lr=1e-6),
keras.callbacks.EarlyStopping(monitor='val_loss',
patience=10,
verbose=0,
mode='auto')])
# Save models and history
model.save_weights(model_fn)
time_elapsed = time.time() - start_time
history.history['time_elapsed'] = time_elapsed
history.history['gpu_id'] = gpu_id
with open(history_fn, 'wb') as f:
pickle.dump(history.history, f)
print('Training time: {:.2f} min'.format(time_elapsed/60))
# Make predictions on test data
test_filenames, test_labels = choose_data(rfi_num=rfi_num,
use_bright=use_bright,
split_name='test')
test_params = {'dim': (TCHANS, FCHANS),
'batch_size': 32, # ensure # test divisible by batch size
'n_channels': n_channels,
'n_classes': n_classes,
'shuffle': False}
test_generator = DataGenerator(test_filenames, test_labels, **test_params)
predictions = model.predict(x=test_generator)
true_labels = [test_labels[os.path.split(fn)[1]] for fn in test_filenames]
pred_dict = {os.path.split(fn)[1]: np.stack([tr, pr])
for fn, tr, pr in zip(test_filenames, true_labels, predictions)}
np.save('{}/test_predictions.npy'.format(model_dir_path), pred_dict)
def main():
training_path = '{}/training'.format(PATH)
mkdir(training_path)
parser = argparse.ArgumentParser()
parser.add_argument('--model_name', '-m', type=str, default='baseline', required=True)
parser.add_argument('--rfi_num', '-r', type=int, default=0) # 0 or 1
parser.add_argument('--use_bright', action='store_true')
parser.add_argument('--batch_size', '-b', type=int, default=32)
parser.add_argument('--epochs', '-e', type=int, default=100)
parser.add_argument('--seed', type=int, default=42)
parser.add_argument('--gpu_id', type=str, default='0')
parser.add_argument('--exp_name', type=str, default='')
parser.add_argument('--dense1_num', type=int, default=1024)
parser.add_argument('--dense2_num', type=int, default=1024)
args = parser.parse_args()
# Convert args to dictionary
params = vars(args)
run_training(**params)
if __name__ == "__main__":
main()