-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy paththesisWhaleCross60SFB.py
More file actions
558 lines (410 loc) · 16.1 KB
/
thesisWhaleCross60SFB.py
File metadata and controls
558 lines (410 loc) · 16.1 KB
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
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
# !/usr/bin/env python
# coding: utf-8
#from modSpec import create_mod_spectrogram
import matplotlib.ticker as ticker
import matplotlib.pyplot as plt
import pickle
import tensorflow
from tensorflow import keras
from tensorflow.keras import regularizers
from keras.constraints import *
from tensorflow.keras.models import *
from tensorflow.keras.layers import *
from tensorflow.keras.losses import *
from tensorflow.keras.optimizers import *
from tensorflow.keras.initializers import *
from tensorflow.keras.callbacks import ModelCheckpoint, LearningRateScheduler
from sklearn.model_selection import train_test_split
import librosa
import librosa.display
import numpy as np
import pandas as pd
import random
import sklearn
#from sklearn.metrics import precision_score, recall_score, confusion_matrix, classification_report, accuracy_score, f1_score, roc_curve, auc
from scipy import interp
#from audiomentations import *
#import matplotlib.pyplot as plt
from itertools import cycle
import ast
import time
# get_ipython().run_line_magic('matplotlib', 'inline')
import os
import warnings
#import cv2
import copy
import gc
warnings.filterwarnings('ignore')
from sklearn.model_selection import train_test_split, KFold, StratifiedKFold, RepeatedKFold
from sklearn.datasets import make_multilabel_classification
from multiprocessing import Process, Manager
#from iterstrat.ml_stratifiers import MultilabelStratifiedShuffleSplit
#code found online for reinitializing the weights of a model in a method
def reset_weights(model):
for layer in model.layers:
if isinstance(layer, tensorflow.keras.Model):
reset_weights(layer)
continue
for k, initializer in layer.__dict__.items():
if "initializer" not in k:
continue
# find the corresponding variable
var = getattr(layer, k.replace("_initializer", ""))
var.assign(initializer(var.shape, var.dtype))
init = HeNormal(seed=0)
def convolutional_block18(X, f, filters, stage, block, s=2):
conv_name_base = 'res' + str(stage) + block + '_branch'
bn_name_base = 'bn' + str(stage) + block + '_branch'
F1 = filters
X_shortcut = X
X = Conv2D(filters=F1, kernel_size=(f, f), strides=(s, s), padding='same', name=conv_name_base + '2a', kernel_initializer=glorot_uniform(seed=0))(X)
X = BatchNormalization(axis=3, name=bn_name_base + '2a')(X)
X = Activation('relu')(X)
X = Conv2D(filters=F1, kernel_size=(f, f), strides=(1, 1), padding='same', name=conv_name_base + '2b', kernel_initializer=glorot_uniform(seed=0))(X)
X = BatchNormalization(axis=3, name=bn_name_base + '2b')(X)
X_shortcut = Conv2D(filters=F1, kernel_size=(1, 1), strides=(s, s), padding='same', name=conv_name_base + '1', kernel_initializer=glorot_uniform(seed=0))(X_shortcut)
X_shortcut = BatchNormalization(axis=3, name=bn_name_base + '1')(X_shortcut)
X = Add()([X, X_shortcut])
X = Activation('relu')(X)
return X
def identity_block18(X, f, filters, stage, block):
conv_name_base = 'res' + str(stage) + block + '_branch'
bn_name_base = 'bn' + str(stage) + block + '_branch'
F1 = filters
X_shortcut = X
X = Conv2D(filters=F1, kernel_size=(f, f), strides=(1, 1), padding='same', name=conv_name_base + '2a', kernel_initializer=glorot_uniform(seed=0))(X)
X = BatchNormalization(axis=3, name=bn_name_base + '2a')(X)
X = Activation('relu')(X)
X = Conv2D(filters=F1, kernel_size=(f, f), strides=(1, 1), padding='same', name=conv_name_base + '2b', kernel_initializer=glorot_uniform(seed=0))(X)
X = BatchNormalization(axis=3, name=bn_name_base + '2b')(X)
X = Activation('relu')(X)
X = Add()([X, X_shortcut])# SKIP Connection
X = Activation('relu')(X)
return X
def ResNet18(input_shape=(input)):
X_input = Input(input_shape)
X = GaussianNoise(0.05)(X_input)
X = RandomFlip(mode="horizontal", seed=None)(X)
#X = RandomTranslation(height_factor = 0, width_factor = .1, fill_mode="nearest", interpolation="bilinear", seed=None, fill_value=0.0)(X)
X = Conv2D(64, (7, 7), strides=(2, 2),padding="same", name='conv1', kernel_initializer=glorot_uniform(seed=0))(X)
X = BatchNormalization(axis=3, name='bn_conv1')(X)
X = Activation('relu')(X)
X = MaxPooling2D((3, 3), strides=(2, 2),padding="same")(X)
X = convolutional_block18(X, f=3, filters=64, stage=2, block='a', s=1)
X = identity_block18(X, f=3, filters=64, stage=2, block='b')
X = convolutional_block18(X, f=3, filters=128, stage=3, block='a', s=2)
X = identity_block18(X, f=3, filters=128, stage=3, block='b')
X = convolutional_block18(X, f=3, filters=256, stage=4, block='a', s=2)
X = identity_block18(X, f=3, filters=256, stage=4, block='b')
X = convolutional_block18(X, f=3, filters=512, stage=5, block='a', s=2)
X = identity_block18(X, f=3, filters=512, stage=5, block='b')
X = AveragePooling2D(pool_size=(2, 2), padding='same')(X)
X = Flatten()(X)
model = Model(inputs=X_input, outputs=X, name='ResNet18')
return model
reg = None
def cnn(input_shape=(input)):
X_input = Input(input_shape)
X = GaussianNoise(0.025)(X_input)
X = RandomFlip(mode="horizontal", seed=None)(X)
X = RandomTranslation(height_factor = 0, width_factor = .1, fill_mode="nearest", interpolation="bilinear", seed=None, fill_value=0.0)(X)
X = Conv2D(64, (7, 7), strides=(1, 1), padding="same", kernel_regularizer= reg)(X)
X = BatchNormalization()(X)
X = SpatialDropout2D(0.20, data_format='channels_last', )(X)
X = Activation('relu')(X)
X = Conv2D(64, (5, 5), strides=(1, 1), padding="same", kernel_regularizer= reg)(X)
X = BatchNormalization()(X)
X = SpatialDropout2D(0.20, data_format='channels_last', )(X)
X = MaxPooling2D((2, 4), strides=(2, 4),padding="same",)(X)
X = Activation('relu')(X)
X = Conv2D(128, (3, 3), padding="same", kernel_regularizer= reg)(X)
X = BatchNormalization()(X)
X = Dropout(rate=0.20)(X)
X = MaxPooling2D((2, 2), strides=(2, 2),padding="same")(X)
X = Activation('relu')(X)
X = Conv2D(256, (3, 3), padding="same", kernel_regularizer= reg)(X)
X = BatchNormalization()(X)
X = Dropout(rate=0.20)(X)
X = Activation('sigmoid')(X)
X = Flatten()(X)
model = Model(inputs=X_input, outputs=X, name='crnn')
return model
# Defining a function to create a dense block
def dense_block(x, num_layers, growth_rate):
# Looping over the number of layers
for i in range(num_layers):
# Creating a bottleneck layer
x1 = BatchNormalization()(x)
x1 = Activation("relu")(x1)
x1 = Conv2D(4 * growth_rate, (1, 1), padding="same")(x1)
# Creating a convolution layer
x1 = BatchNormalization()(x1)
x1 = Activation("relu")(x1)
x1 = Conv2D(growth_rate, (3, 3), padding="same")(x1)
# Concatenating the input and the output
x = Concatenate()([x, x1])
# Returning the final output
return x
# Defining a function to create a transition layer
def transition_layer(x, compression_factor):
# Reducing the number of channels
num_channels = int(x.shape[-1] * compression_factor)
# Creating a batch normalization layer
x = BatchNormalization()(x)
# Creating a convolution layer
x = Conv2D(num_channels, (1, 1), padding="same")(x)
# Creating an average pooling layer
x = AveragePooling2D((2, 2), strides=(2, 2))(x)
# Returning the final output
return x
def DenseNet(input_shape=(input)):
num_blocks = 4 # The number of dense blocks
num_layers = [4, 4, 4, 2]
growth_rate = 8 # The growth rate of the network
compression_factor = 0.5
X_input = Input(input_shape)
X = GaussianNoise(0.025)(X_input)
X = RandomFlip(mode="horizontal", seed=None)(X)
X = RandomTranslation(height_factor = 0, width_factor = .1, fill_mode="nearest", interpolation="bilinear", seed=None, fill_value=0.0)(X)
X = Conv2D(64, (7, 7), strides=(1, 1), padding="same", kernel_regularizer= reg)(X)
X = BatchNormalization()(X)
#X = SpatialDropout2D(0.20, data_format='channels_last', )(X)
X = Activation('relu')(X)
for i in range(num_blocks):
# Creating a dense block
X = dense_block(X, num_layers[i], growth_rate)
# Creating a transition layer if it is not the last block
if i != num_blocks - 1:
X = transition_layer(X, compression_factor)
X = BatchNormalization()(X)
X = Activation("sigmoid")(X)
X = Flatten()(X)
model = Model(inputs=X_input, outputs=X, name='DenseNet')
return model
def runTrain(L):
physical_devices = tensorflow.config.list_physical_devices('gpu')
for gpu_instance in physical_devices:
tensorflow.config.experimental.set_memory_growth(gpu_instance, True)
# Get the list of GPUs
gpus = tensorflow.config.list_physical_devices('GPU')
# Set the memory limit for each GPU
for gpu in gpus:
tensorflow.config.set_logical_device_configuration(
gpu,
[tensorflow.config.LogicalDeviceConfiguration(memory_limit=75 * 1024)]
)
resultsloss = L[0]
resultsacc = L[1]
X_train = L[2]
y_train = L[3]
X_test = L[4]
y_test = L[5]
count = L[6]
model = Sequential()
input_shape=(128, 256, 1)
model.add(ResNet18(input_shape=input_shape))
model.add(Dense(256))
model.add(BatchNormalization())
model.add(Dropout(rate=0.20))
model.add(Activation('sigmoid'))
model.add(Dense(2))
model.add(Activation('sigmoid'))
initial_learning_rate = 0.01
lr_schedule = tensorflow.keras.optimizers.schedules.ExponentialDecay(
initial_learning_rate,
decay_steps=10000,
decay_rate=0.5,
staircase=False)
model.compile(optimizer = Adam(learning_rate = lr_schedule), loss = 'binary_crossentropy' , metrics = [tensorflow.keras.metrics.BinaryAccuracy()])
time.sleep(10.00)
#clear out old memory
tensorflow.keras.backend.clear_session()
#re-initialize the models weights
reset_weights(model)
#resetting the new checkpoint for this run through
model_checkpoint = [
ModelCheckpoint(filepath = './checks/thesisWhaleRes60SFB25664' + str(count) + ".hdf5", monitor='val_binary_accuracy',verbose=1, save_best_only=True, mode = 'max'),
#tensorflow.keras.callbacks.EarlyStopping(
# monitor="val_auc",
# min_delta=.0001,
# patience=13,
# verbose=0,
# mode="max",
# baseline=None,
# restore_best_weights=False,
#),
#tensorflow.keras.callbacks.ReduceLROnPlateau(
# monitor="val_auc",
# factor=0.5,
# patience=5,
# verbose=0,
# mode="max",
# min_delta=0.0001,
# cooldown=0,
# min_lr=0
#)
]
#executing the train function
history = model.fit(
x=X_train,
y=y_train,
epochs=50,
batch_size = 64,
validation_data = (X_test, y_test),
callbacks=[model_checkpoint],
class_weight=None,
max_queue_size=10,
shuffle = True)
time.sleep(2.00)
tensorflow.keras.backend.clear_session()
plt.plot(history.history['binary_accuracy'])
plt.plot(history.history['val_binary_accuracy'])
plt.title('Model binary accuracy')
plt.ylabel('Accuracy')
plt.xlabel('Epoch')
plt.legend(['Train', 'Validation'], loc='upper left')
plt.savefig('./results/Accuracy60S12832' + str(count) +'.png')
plt.clf()
plt.plot(history.history['loss'])
plt.plot(history.history['val_loss'])
plt.title('Model Loss')
plt.ylabel('Loss')
plt.xlabel('Epoch')
plt.legend(['Train', 'Validation'], loc='upper left')
plt.savefig('./results/Loss60S12832' + str(count) +'.png')
# loading the best checkpoint for the train/validation
model.load_weights('./checks/thesisWhaleRes60SFB25664' + str(count) + '.hdf5')
#clear out old memory
time.sleep(2.00)
tensorflow.keras.backend.clear_session()
#getting the best validation scores
score = model.evaluate(
x=X_test,
y=y_test,
batch_size = 64)
#getting the total real negatives in the validation data
#y_testN = [i for i in y_val if i < .5]
#adding this interations results
resultsloss.append(score[0])
resultsacc.append(score[1])
count+=1
L[0] = resultsloss
L[1] = resultsacc
L[6] = count
if __name__ == "__main__":
infile = open('./dataframes/allDatasets250_60FBWhale.pkl','rb')
data = pickle.load(infile)
infile.close()
data = data.sample(frac = 1, random_state=10)
dataset = [] # Train Dataset
vdataset =[]
#iterating over valid train/validation data
count = 0
countn = 0
for row in data.itertuples():
if row.dataset == 'casey2017':
continue
print(count)
arrList=[]
#if row.source !='coswara':
# continue
y, label = row.array, np.array(row.label)
if len(y) != 16384:
continue
if 1 not in label:
countn+=1
if countn>77500:
continue
lArr = []
countm = 0
for x in label:
lArr.append(x)
stft = np.abs(librosa.stft(y = y, n_fft = 256, hop_length = 64))**2
ms2 = stft/stft.max()
ms_DB = librosa.power_to_db(S = ms2, ref = 0)
ms_DB = ms_DB - 20
ms_DB = ms_DB/ms_DB.max()
ms_DB = ms_DB[1:,1:]
ms_DB = np.reshape(ms_DB,(128,256,1))
count+=1
dataset.append( (ms_DB, np.array(lArr)) )
data_X, data_y = zip(*dataset)
# Train/Validation Dataset
data_X, data_y = np.array(data_X), np.array(data_y)
print(data_y)
#initializing variables and sets for the train/validation loop
label = []
for x in data_y:
if x[0] == 1:
label.append(0)
elif x[1] == 1:
label.append(1)
label = np.array(label)
count = 0
resultsloss = []
resultsacc = []
train = []
val = []
trainY = []
valY = []
#manager = Manager()
#lst = manager.list()
lst = []
lst.append(resultsloss)
lst.append(resultsacc)
lst.append(train)
lst.append(val)
lst.append(trainY)
lst.append(valY)
lst.append(count)
#begining train/validation loop
skf = RepeatedKFold(n_splits = 5, random_state = 10, n_repeats=2)
#skf.get_n_splits(data_X, data_y)
for trainIdx, testIdx in skf.split(data_X, data_y):
if lst[6] >0 :
break
gc.collect()
#if lst[6] < 2:
# lst[6] += 1
# continue
X_train = data_X[trainIdx]
X_test = data_X[testIdx]
y_train = []
y_test = []
X_train = np.array([x.reshape( (128,256,1 ) ) for x in X_train])
X_test = np.array([x.reshape( (128,256,1 ) ) for x in X_test])
y_train = data_y[trainIdx]
y_test = data_y[testIdx]
print(X_train.shape)
print(y_train.shape)
lst[2] = X_train
lst[3] = y_train
lst[4] = X_test
lst[5] = y_test
#p = Process(target=runTrain, args=[lst])
#p.start()
#p.join()
runTrain(lst)
#just cuz
continue
resultsloss = lst[0]
resultsacc = lst[1]
#convert results of train/validation to a dataframe
df_results = pd.DataFrame(data={"Loss":resultsloss, "Acc":resultsacc})
#df_results.to_csv('./Virufy_Train_Data1.csv')
df_results = df_results[['Loss', 'Acc']]
#adding the average of the results dataframe
resultsloss.append('Avg')
resultsacc.append('Avg')
#resultsauc.append('Avg')
#resultssens.append('Avg')
#resultsspec.append('Avg')
resultsloss.append(df_results['Loss'].mean())
resultsacc.append(df_results['Acc'].mean())
#resultsauc.append(df_results['AUC'].mean())
#resultssens.append(df_results['Sens'].mean())
#resultsspec.append(df_results['Spec'].mean())
#recreating the results dataframe with the average and test data
df_results = pd.DataFrame(data={"Loss":resultsloss, "Acc":resultsacc})
#sending to a CSV
df_results.to_csv('./results/whaleCross.csv')