-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathmodel_load_snore.py
688 lines (502 loc) · 28.2 KB
/
model_load_snore.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
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
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
from keras.models import Sequential, Model
from keras.layers import LSTM, Dense, Dropout, concatenate, Input, Lambda, Conv2D, Flatten, Permute, Reshape, multiply, Activation, add, dot, Conv2DTranspose, average, maximum, GRU, Conv1D
from keras.layers.pooling import MaxPooling1D, MaxPooling2D
from keras.layers.normalization import BatchNormalization
from keras.layers.noise import GaussianDropout
from keras.applications.vgg16 import VGG16
from keras.applications.vgg16 import preprocess_input
from keras import metrics
from keras import backend as K
import numpy as np
from snore_data_extractor import *
from keras.callbacks import ModelCheckpoint, EarlyStopping, TensorBoard
import tensorflow as tf
from keras import losses
from sklearn.metrics import recall_score, confusion_matrix
from keras.preprocessing.image import ImageDataGenerator
from keras.utils import plot_model
#class for slicing tensors in keras
class Slice:
def __init__(self, dim=0, portion=0):
if dim != 0:
self.dim = dim
if portion != 0:
self.portion = portion
self.start = 0
else:
self.i = 0
def slice_pieces_3D(self, x):
if self.dim == 3:
original_shape = K.int_shape(x)
output = x[:, :, :, self.i]
elif self.dim == 2:
original_shape = K.int_shape(x)
output = x[:, :, self.i, :]
elif self.dim == 1:
original_shape = K.int_shape(x)
output = x[:, self.i, :, :]
self.i += 1
return output
def slice_pieces_2D(self, x):
if self.dim == 2:
original_shape = K.int_shape(x)
output = x[:, :, self.i]
elif self.dim == 1:
original_shape = K.int_shape(x)
output = x[:, self.i, :]
self.i += 1
return output
def slice_portions_3D(self, x):
if self.dim == 3:
original_shape = K.int_shape(x)
output = x[:, :, :, self.start:self.start+int(original_shape[3]/self.portion)]
self.start += int(original_shape[3]/self.portion)
elif self.dim == 2:
original_shape = K.int_shape(x)
output = x[:, :, self.start:self.start+int(original_shape[2]/self.portion), :]
self.start += int(original_shape[2]/self.portion)
elif self.dim == 1:
original_shape = K.int_shape(x)
output = x[:, self.start:self.start+int(original_shape[1]/self.portion), :, :]
self.start += int(original_shape[1]/self.portion)
return output
def slice_portions_2D(self, x):
if self.dim == 2:
original_shape = K.int_shape(x)
output = x[:, :, self.start:self.start+int(original_shape[2]/self.portion)]
self.start += int(original_shape[2]/self.portion)
elif self.dim == 1:
original_shape = K.int_shape(x)
output = x[:, self.start:self.start+int(original_shape[1]/self.portion), :]
self.start += int(original_shape[1]/self.portion)
return output
# DenConvGRU+Channel Slice Model
def image_entry_model_32(time_steps, data_dim):
inputs = Input(shape=(time_steps, data_dim, 3))
x_0 = inputs
x_1_1 = Conv2D(filters=16, kernel_size=(3, 4), strides=(2, 2), padding="same", activation="relu")(x_0)
x_2_1 = MaxPooling2D(pool_size=(2,2), padding='same')(x_1_1)
x_3_1 = MaxPooling2D(pool_size=(4,4), padding='same')(x_1_1)
x_1 = x_1_1
x_2_2 = Conv2D(filters=16, kernel_size=(3, 4), strides=(2, 2), padding="same", activation="relu")(x_1)
x_3_2 = MaxPooling2D(pool_size=(2,2), padding='same')(x_2_2)
x_2 = concatenate([x_2_1, x_2_2])
x_3_3 = Conv2D(filters=16, kernel_size=(3, 4), strides=(2, 2), padding="same", activation="relu")(x_2)
x_3 = concatenate([x_3_1, x_3_2, x_3_3])
x_3 = MaxPooling2D(pool_size=(3,3))(x_3)
x_shape = K.int_shape(x_3)
x_3 = [Lambda(slicer_3D.slice_pieces_3D, output_shape=(x_shape[1],x_shape[2], 1))(x_3) for _ in range(x_shape[3])]
x_3 = [Reshape((1, K.int_shape(Flatten()(each))[1]))(Flatten()(each)) for each in x_3]
x_3 = concatenate(x_3, axis=1)
x_4 = GRU(256, return_sequences=True)(x_3)
x_5 = GRU(128)(x_4)
prediction = Dense(4, activation="softmax")(x_5)
model = Model(inputs=inputs, outputs=prediction)
model.summary()
'''
train = snore_data_extractor(load_folder_path, one_hot=True, data_mode="train", resize=(data_dim, time_steps), timechain=False, duplicate=True)
devel = snore_data_extractor(load_folder_path, one_hot=True, data_mode="devel", resize=(data_dim, time_steps), timechain=False, duplicate=True)
epoch_num = 500
batch_size = 16
loss = tf.reduce_mean(losses.kullback_leibler_divergence(labels, predicts))
train_step = tf.train.RMSPropOptimizer(learning_rate=0.001, momentum=0.01).minimize(loss)
'''
return model
# DenConvGRU+Time Slice Model
def image_entry_model_33(time_steps, data_dim):
inputs = Input(shape=(time_steps, data_dim, 3))
x_0 = inputs
x_1_1 = Conv2D(filters=16, kernel_size=(3, 4), strides=(2, 2), padding="same", activation="relu")(x_0)
x_2_1 = MaxPooling2D(pool_size=(2,2), padding='same')(x_1_1)
x_3_1 = MaxPooling2D(pool_size=(4,4), padding='same')(x_1_1)
x_1 = x_1_1
x_2_2 = Conv2D(filters=16, kernel_size=(3, 4), strides=(2, 2), padding="same", activation="relu")(x_1)
x_3_2 = MaxPooling2D(pool_size=(2,2), padding='same')(x_2_2)
x_2 = concatenate([x_2_1, x_2_2])
x_3_3 = Conv2D(filters=16, kernel_size=(3, 4), strides=(2, 2), padding="same", activation="relu")(x_2)
x_3 = concatenate([x_3_1, x_3_2, x_3_3])
x_3 = MaxPooling2D(pool_size=(3,3))(x_3)
x_3 = Permute((3, 2, 1))(x_3)
x_shape = K.int_shape(x_3)
x_3 = [Lambda(slicer_3D.slice_pieces_3D, output_shape=(x_shape[1],x_shape[2], 1))(x_3) for _ in range(x_shape[3])]
x_3 = [Reshape((1, K.int_shape(Flatten()(each))[1]))(Flatten()(each)) for each in x_3]
x_3 = concatenate(x_3, axis=1)
x_4 = GRU(256, return_sequences=True)(x_3)
x_5 = GRU(128)(x_4)
prediction = Dense(4, activation="softmax")(x_5)
model = Model(inputs=inputs, outputs=prediction)
model.summary()
'''
train = snore_data_extractor(load_folder_path, one_hot=True, data_mode="train", resize=(data_dim, time_steps), timechain=False, duplicate=True)
devel = snore_data_extractor(load_folder_path, one_hot=True, data_mode="devel", resize=(data_dim, time_steps), timechain=False, duplicate=True)
epoch_num = 500
batch_size = 16
loss = tf.reduce_mean(losses.kullback_leibler_divergence(labels, predicts))
train_step = tf.train.RMSPropOptimizer(learning_rate=0.001, momentum=0.01).minimize(loss)
'''
return model
# Fusion-DualConvGRU+L2 Regularizer
def image_entry_model_36(time_steps, data_dim):
inputs = Input(shape=(time_steps, data_dim, 3))
x_0 = inputs
def Conv_filters(x_0):
x_1_1 = Conv2D(filters=16, kernel_size=(3, 4), strides=(2, 2), padding="same", activation="relu")(x_0)
x_1_2 = Conv2D(filters=16, kernel_size=(3, 2), strides=(2, 2), padding="same", activation="relu")(x_0)
x_1 = average([x_1_1, x_1_2])
x_2_1 = Conv2D(filters=16, kernel_size=(3, 4), strides=(2, 2), padding="same", activation="relu")(x_1)
x_2_2 = Conv2D(filters=16, kernel_size=(3, 2), strides=(2, 2), padding="same", activation="relu")(x_1)
x_2 = average([x_2_1, x_2_2])
x_3_1 = Conv2D(filters=16, kernel_size=(3, 4), strides=(2, 2), padding="same", activation="relu")(x_2)
x_3_2 = Conv2D(filters=16, kernel_size=(3, 2), strides=(2, 2), padding="same", activation="relu")(x_2)
x_3 = average([x_3_1, x_3_2])
x_4 = MaxPooling2D(pool_size=(3, 3), padding="same")(x_3)
return x_4
x_4_1 = Conv_filters(x_0)
x_4_1 = Permute((3, 2, 1))(x_4_1)
x_shape = K.int_shape(x_4_1)
x_4_1 = [Lambda(slicer_3D_0.slice_pieces_3D, output_shape=(x_shape[1],x_shape[2], 1))(x_4_1) for _ in range(x_shape[3])]
x_4_1 = [Reshape((1, K.int_shape(Flatten()(each))[1]))(Flatten()(each)) for each in x_4_1]
x_4_1 = concatenate(x_4_1, axis=1)
x_5_1 = GRU(256, return_sequences=True)(x_4_1)
x_6_1 = GRU(128)(x_5_1)
x_4_2 = Conv_filters(x_0)
x_shape = K.int_shape(x_4_2)
x_4_2 = [Lambda(slicer_3D_1.slice_pieces_3D, output_shape=(x_shape[1],x_shape[2], 1))(x_4_2) for _ in range(x_shape[3])]
x_4_2 = [Reshape((1, K.int_shape(Flatten()(each))[1]))(Flatten()(each)) for each in x_4_2]
x_4_2 = concatenate(x_4_2, axis=1)
x_5_2 = GRU(256, return_sequences=True)(x_4_2)
x_6_2 = GRU(128)(x_5_2)
x_6 = concatenate([x_6_1, x_6_2])
x_7 = Dense(128, activation="relu")(x_6)
prediction = Dense(4, activation="softmax")(x_7)
model = Model(inputs=inputs, outputs=prediction)
model.summary()
'''
train = snore_data_extractor(load_folder_path, one_hot=True, data_mode="train", resize=(data_dim, time_steps), timechain=False, duplicate=True)
devel = snore_data_extractor(load_folder_path, one_hot=True, data_mode="devel", resize=(data_dim, time_steps), timechain=False, duplicate=True)
epoch_num = 500
batch_size = 16
loss = tf.reduce_mean(losses.kullback_leibler_divergence(labels, predicts))
train_step = tf.train.RMSPropOptimizer(learning_rate=0.001, momentum=0.01).minimize(loss)
'''
return model
# Fusion-DualConvGRU+Dropout
def image_entry_model_37(time_steps, data_dim):
inputs = Input(shape=(time_steps, data_dim, 3))
x_0 = inputs
def Conv_filters(x_0):
x_1_1 = Conv2D(filters=16, kernel_size=(3, 4), strides=(2, 2), padding="same", activation="relu")(x_0)
x_1_2 = Conv2D(filters=16, kernel_size=(3, 2), strides=(2, 2), padding="same", activation="relu")(x_0)
x_1 = average([x_1_1, x_1_2])
x_2_1 = Conv2D(filters=16, kernel_size=(3, 4), strides=(2, 2), padding="same", activation="relu")(x_1)
x_2_2 = Conv2D(filters=16, kernel_size=(3, 2), strides=(2, 2), padding="same", activation="relu")(x_1)
x_2 = average([x_2_1, x_2_2])
x_3_1 = Conv2D(filters=16, kernel_size=(3, 4), strides=(2, 2), padding="same", activation="relu")(x_2)
x_3_2 = Conv2D(filters=16, kernel_size=(3, 2), strides=(2, 2), padding="same", activation="relu")(x_2)
x_3 = average([x_3_1, x_3_2])
x_4 = MaxPooling2D(pool_size=(3, 3), padding="same")(x_3)
x_4 = Dropout(0.5)(x_4)
return x_4
x_4_1 = Conv_filters(x_0)
x_4_1 = Permute((3, 2, 1))(x_4_1)
x_shape = K.int_shape(x_4_1)
x_4_1 = [Lambda(slicer_3D_0.slice_pieces_3D, output_shape=(x_shape[1],x_shape[2], 1))(x_4_1) for _ in range(x_shape[3])]
x_4_1 = [Reshape((1, K.int_shape(Flatten()(each))[1]))(Flatten()(each)) for each in x_4_1]
x_4_1 = concatenate(x_4_1, axis=1)
x_5_1 = GRU(256, return_sequences=True)(x_4_1)
x_6_1 = GRU(128)(x_5_1)
x_4_2 = Conv_filters(x_0)
x_shape = K.int_shape(x_4_2)
x_4_2 = [Lambda(slicer_3D_1.slice_pieces_3D, output_shape=(x_shape[1],x_shape[2], 1))(x_4_2) for _ in range(x_shape[3])]
x_4_2 = [Reshape((1, K.int_shape(Flatten()(each))[1]))(Flatten()(each)) for each in x_4_2]
x_4_2 = concatenate(x_4_2, axis=1)
x_5_2 = GRU(256, return_sequences=True)(x_4_2)
x_6_2 = GRU(128)(x_5_2)
x_6 = concatenate([x_6_1, x_6_2])
x_7 = Dense(128, activation="relu")(x_6)
prediction = Dense(4, activation="softmax")(x_7)
model = Model(inputs=inputs, outputs=prediction)
model.summary()
'''
train = snore_data_extractor(load_folder_path, one_hot=True, data_mode="train", resize=(data_dim, time_steps), timechain=False, duplicate=True)
devel = snore_data_extractor(load_folder_path, one_hot=True, data_mode="devel", resize=(data_dim, time_steps), timechain=False, duplicate=True)
epoch_num = 500
batch_size = 16
loss = tf.reduce_mean(losses.kullback_leibler_divergence(labels, predicts))
train_step = tf.train.RMSPropOptimizer(learning_rate=0.001, momentum=0.01).minimize(loss)
'''
return model
# 1DConvGRU
def image_entry_model_38(time_steps, data_dim):
inputs = Input(shape=(time_steps, data_dim, 1))
x_0 = Reshape((time_steps, data_dim))(inputs)
x_1 = Conv1D(filters=128, kernel_size=3, strides=2, padding="same", activation="relu")(x_0)
x_2 = Conv1D(filters=128, kernel_size=3, strides=2, padding="same", activation="relu")(x_1)
x_3 = Conv1D(filters=128, kernel_size=3, strides=2, padding="same", activation="relu")(x_2)
x_3 = MaxPooling1D(pool_size=3, padding="same")(x_3)
x_4 = GRU(256, return_sequences=True)(x_3)
x_5 = GRU(128)(x_4)
prediction = Dense(4, activation="softmax")(x_5)
model = Model(inputs=inputs, outputs=prediction)
model.summary()
'''
train = snore_data_extractor(load_folder_path, one_hot=True, data_mode="train", resize=(data_dim, time_steps), timechain=False, duplicate=True)
devel = snore_data_extractor(load_folder_path, one_hot=True, data_mode="devel", resize=(data_dim, time_steps), timechain=False, duplicate=True)
epoch_num = 500
batch_size = 16
loss = tf.reduce_mean(losses.kullback_leibler_divergence(labels, predicts))
train_step = tf.train.RMSPropOptimizer(learning_rate=0.001, momentum=0.0).minimize(loss)
'''
return model
# DualConvGRU+Channel Slice Model
def image_entry_model_41(time_steps, data_dim):
inputs = Input(shape=(time_steps, data_dim, 3))
x_0 = inputs
x_1_1 = Conv2D(filters=16, kernel_size=(3, 4), strides=(2, 2), padding="same", activation="relu")(x_0)
x_1_2 = Conv2D(filters=16, kernel_size=(3, 2), strides=(2, 2), padding="same", activation="relu")(x_0)
x_1 = average([x_1_1, x_1_2])
x_2_1 = Conv2D(filters=16, kernel_size=(3, 4), strides=(2, 2), padding="same", activation="relu")(x_1)
x_2_2 = Conv2D(filters=16, kernel_size=(3, 2), strides=(2, 2), padding="same", activation="relu")(x_1)
x_2 = average([x_2_1, x_2_2])
x_3_1 = Conv2D(filters=16, kernel_size=(3, 4), strides=(2, 2), padding="same", activation="relu")(x_2)
x_3_2 = Conv2D(filters=16, kernel_size=(3, 2), strides=(2, 2), padding="same", activation="relu")(x_2)
x_3 = average([x_3_1, x_3_2])
x_4 = MaxPooling2D(pool_size=(3, 3), padding="same")(x_3)
x_shape = K.int_shape(x_4)
x_4 = [Lambda(slicer_3D.slice_pieces_3D, output_shape=(x_shape[1],x_shape[2], 1))(x_4) for _ in range(x_shape[3])]
x_4 = [Reshape((1, K.int_shape(Flatten()(each))[1]))(Flatten()(each)) for each in x_4]
x_4 = concatenate(x_4, axis=1)
x_5 = GRU(256, return_sequences=True)(x_4)
x_6 = GRU(128)(x_5)
prediction = Dense(4, activation="softmax")(x_6)
model = Model(inputs=inputs, outputs=prediction)
model.summary()
'''
train = snore_data_extractor(load_folder_path, one_hot=True, data_mode="train", resize=(data_dim, time_steps), timechain=False, duplicate=True)
devel = snore_data_extractor(load_folder_path, one_hot=True, data_mode="devel", resize=(data_dim, time_steps), timechain=False, duplicate=True)
epoch_num = 500
batch_size = 16
loss = tf.reduce_mean(losses.kullback_leibler_divergence(labels, predicts))
train_step = tf.train.RMSPropOptimizer(learning_rate=0.001, momentum=0.01).minimize(loss)
'''
return model
# DualConvGRU+Time Slice Model
def image_entry_model_42(time_steps, data_dim):
inputs = Input(shape=(time_steps, data_dim, 3))
x_0 = inputs
x_1_1 = Conv2D(filters=16, kernel_size=(3, 4), strides=(2, 2), padding="same", activation="relu")(x_0)
x_1_2 = Conv2D(filters=16, kernel_size=(3, 2), strides=(2, 2), padding="same", activation="relu")(x_0)
x_1 = average([x_1_1, x_1_2])
x_2_1 = Conv2D(filters=16, kernel_size=(3, 4), strides=(2, 2), padding="same", activation="relu")(x_1)
x_2_2 = Conv2D(filters=16, kernel_size=(3, 2), strides=(2, 2), padding="same", activation="relu")(x_1)
x_2 = average([x_2_1, x_2_2])
x_3_1 = Conv2D(filters=16, kernel_size=(3, 4), strides=(2, 2), padding="same", activation="relu")(x_2)
x_3_2 = Conv2D(filters=16, kernel_size=(3, 2), strides=(2, 2), padding="same", activation="relu")(x_2)
x_3 = average([x_3_1, x_3_2])
x_4 = MaxPooling2D(pool_size=(3, 3), padding="same")(x_3)
x_4 = Permute((3, 2, 1))(x_4)
x_shape = K.int_shape(x_4)
x_4 = [Lambda(slicer_3D.slice_pieces_3D, output_shape=(x_shape[1],x_shape[2], 1))(x_4) for _ in range(x_shape[3])]
x_4 = [Reshape((1, K.int_shape(Flatten()(each))[1]))(Flatten()(each)) for each in x_4]
x_4 = concatenate(x_4, axis=1)
x_5 = GRU(256, return_sequences=True)(x_4)
x_6 = GRU(128)(x_5)
prediction = Dense(4, activation="softmax")(x_6)
model = Model(inputs=inputs, outputs=prediction)
model.summary()
'''
train = snore_data_extractor(load_folder_path, one_hot=True, data_mode="train", resize=(data_dim, time_steps), timechain=False, duplicate=True)
devel = snore_data_extractor(load_folder_path, one_hot=True, data_mode="devel", resize=(data_dim, time_steps), timechain=False, duplicate=True)
epoch_num = 500
batch_size = 16
loss = tf.reduce_mean(losses.kullback_leibler_divergence(labels, predicts))
train_step = tf.train.RMSPropOptimizer(learning_rate=0.001, momentum=0.01).minimize(loss)
'''
return model
# DualConvGRU+Channel Slice Model+Dropout
def image_entry_model_43(time_steps, data_dim):
inputs = Input(shape=(time_steps, data_dim, 3))
x_0 = inputs
x_1_1 = Conv2D(filters=16, kernel_size=(3, 4), strides=(2, 2), padding="same", activation="relu")(x_0)
x_1_2 = Conv2D(filters=16, kernel_size=(3, 2), strides=(2, 2), padding="same", activation="relu")(x_0)
x_1 = average([x_1_1, x_1_2])
x_2_1 = Conv2D(filters=16, kernel_size=(3, 4), strides=(2, 2), padding="same", activation="relu")(x_1)
x_2_2 = Conv2D(filters=16, kernel_size=(3, 2), strides=(2, 2), padding="same", activation="relu")(x_1)
x_2 = average([x_2_1, x_2_2])
x_3_1 = Conv2D(filters=16, kernel_size=(3, 4), strides=(2, 2), padding="same", activation="relu")(x_2)
x_3_2 = Conv2D(filters=16, kernel_size=(3, 2), strides=(2, 2), padding="same", activation="relu")(x_2)
x_3 = average([x_3_1, x_3_2])
x_4 = MaxPooling2D(pool_size=(3, 3), padding="same")(x_3)
x_4 = Dropout(0.5)(x_4)
x_shape = K.int_shape(x_4)
x_4 = [Lambda(slicer_3D.slice_pieces_3D, output_shape=(x_shape[1],x_shape[2], 1))(x_4) for _ in range(x_shape[3])]
x_4 = [Reshape((1, K.int_shape(Flatten()(each))[1]))(Flatten()(each)) for each in x_4]
x_4 = concatenate(x_4, axis=1)
x_5 = GRU(256, return_sequences=True)(x_4)
x_6 = GRU(128)(x_5)
prediction = Dense(4, activation="softmax")(x_6)
model = Model(inputs=inputs, outputs=prediction)
model.summary()
'''
train = snore_data_extractor(load_folder_path, one_hot=True, data_mode="train", resize=(data_dim, time_steps), timechain=False, duplicate=True)
devel = snore_data_extractor(load_folder_path, one_hot=True, data_mode="devel", resize=(data_dim, time_steps), timechain=False, duplicate=True)
epoch_num = 500
batch_size = 16
loss = tf.reduce_mean(losses.kullback_leibler_divergence(labels, predicts))
train_step = tf.train.RMSPropOptimizer(learning_rate=0.001, momentum=0.01).minimize(loss)
'''
return model
# DualConvGRU+Time Slice Model+Dropout
def image_entry_model_44(time_steps, data_dim):
inputs = Input(shape=(time_steps, data_dim, 3))
x_0 = inputs
x_1_1 = Conv2D(filters=16, kernel_size=(3, 4), strides=(2, 2), padding="same", activation="relu")(x_0)
x_1_2 = Conv2D(filters=16, kernel_size=(3, 2), strides=(2, 2), padding="same", activation="relu")(x_0)
x_1 = average([x_1_1, x_1_2])
x_2_1 = Conv2D(filters=16, kernel_size=(3, 4), strides=(2, 2), padding="same", activation="relu")(x_1)
x_2_2 = Conv2D(filters=16, kernel_size=(3, 2), strides=(2, 2), padding="same", activation="relu")(x_1)
x_2 = average([x_2_1, x_2_2])
x_3_1 = Conv2D(filters=16, kernel_size=(3, 4), strides=(2, 2), padding="same", activation="relu")(x_2)
x_3_2 = Conv2D(filters=16, kernel_size=(3, 2), strides=(2, 2), padding="same", activation="relu")(x_2)
x_3 = average([x_3_1, x_3_2])
x_4 = MaxPooling2D(pool_size=(3, 3), padding="same")(x_3)
x_4 = Permute((3, 2, 1))(x_4)
x_4 = Dropout(0.5)(x_4)
x_shape = K.int_shape(x_4)
x_4 = [Lambda(slicer_3D.slice_pieces_3D, output_shape=(x_shape[1],x_shape[2], 1))(x_4) for _ in range(x_shape[3])]
x_4 = [Reshape((1, K.int_shape(Flatten()(each))[1]))(Flatten()(each)) for each in x_4]
x_4 = concatenate(x_4, axis=1)
x_5 = GRU(256, return_sequences=True)(x_4)
x_6 = GRU(128)(x_5)
prediction = Dense(4, activation="softmax")(x_6)
model = Model(inputs=inputs, outputs=prediction)
model.summary()
'''
train = snore_data_extractor(load_folder_path, one_hot=True, data_mode="train", resize=(data_dim, time_steps), timechain=False, duplicate=True)
devel = snore_data_extractor(load_folder_path, one_hot=True, data_mode="devel", resize=(data_dim, time_steps), timechain=False, duplicate=True)
epoch_num = 500
batch_size = 16
loss = tf.reduce_mean(losses.kullback_leibler_divergence(labels, predicts))
train_step = tf.train.RMSPropOptimizer(learning_rate=0.001, momentum=0.01).minimize(loss)
'''
return model
# DualConvGRU+Channel Slice Model+L2 Regularizer
def image_entry_model_45(time_steps, data_dim):
inputs = Input(shape=(time_steps, data_dim, 3))
x_0 = inputs
x_1_1 = Conv2D(filters=16, kernel_size=(3, 4), strides=(2, 2), padding="same", activation="relu")(x_0)
x_1_2 = Conv2D(filters=16, kernel_size=(3, 2), strides=(2, 2), padding="same", activation="relu")(x_0)
x_1 = average([x_1_1, x_1_2])
x_2_1 = Conv2D(filters=16, kernel_size=(3, 4), strides=(2, 2), padding="same", activation="relu")(x_1)
x_2_2 = Conv2D(filters=16, kernel_size=(3, 2), strides=(2, 2), padding="same", activation="relu")(x_1)
x_2 = average([x_2_1, x_2_2])
x_3_1 = Conv2D(filters=16, kernel_size=(3, 4), strides=(2, 2), padding="same", activation="relu")(x_2)
x_3_2 = Conv2D(filters=16, kernel_size=(3, 2), strides=(2, 2), padding="same", activation="relu")(x_2)
x_3 = average([x_3_1, x_3_2])
x_4 = MaxPooling2D(pool_size=(3, 3), padding="same")(x_3)
x_shape = K.int_shape(x_4)
x_4 = [Lambda(slicer_3D.slice_pieces_3D, output_shape=(x_shape[1],x_shape[2], 1))(x_4) for _ in range(x_shape[3])]
x_4 = [Reshape((1, K.int_shape(Flatten()(each))[1]))(Flatten()(each)) for each in x_4]
x_4 = concatenate(x_4, axis=1)
x_5 = GRU(256, return_sequences=True)(x_4)
x_6 = GRU(128)(x_5)
prediction = Dense(4, activation="softmax")(x_6)
model = Model(inputs=inputs, outputs=prediction)
model.summary()
'''
train = snore_data_extractor(load_folder_path, one_hot=True, data_mode="train", resize=(data_dim, time_steps), timechain=False, duplicate=True)
devel = snore_data_extractor(load_folder_path, one_hot=True, data_mode="devel", resize=(data_dim, time_steps), timechain=False, duplicate=True)
epoch_num = 500
batch_size = 16
regularizer = tf.contrib.layers.l2_regularizer(0.01)
loss = tf.reduce_mean(losses.kullback_leibler_divergence(labels, predicts)) + tf.contrib.layers.apply_regularization(regularizer, weights_list=train_var[:-8])
train_step = tf.train.RMSPropOptimizer(learning_rate=0.001).minimize(loss)
'''
return model
# DualConvGRU+Time Slice Model+L2 Regularizer
def image_entry_model_46(time_steps, data_dim):
inputs = Input(shape=(time_steps, data_dim, 3))
x_0 = inputs
x_1_1 = Conv2D(filters=16, kernel_size=(3, 4), strides=(2, 2), padding="same", activation="relu")(x_0)
x_1_2 = Conv2D(filters=16, kernel_size=(3, 2), strides=(2, 2), padding="same", activation="relu")(x_0)
x_1 = average([x_1_1, x_1_2])
x_2_1 = Conv2D(filters=16, kernel_size=(3, 4), strides=(2, 2), padding="same", activation="relu")(x_1)
x_2_2 = Conv2D(filters=16, kernel_size=(3, 2), strides=(2, 2), padding="same", activation="relu")(x_1)
x_2 = average([x_2_1, x_2_2])
x_3_1 = Conv2D(filters=16, kernel_size=(3, 4), strides=(2, 2), padding="same", activation="relu")(x_2)
x_3_2 = Conv2D(filters=16, kernel_size=(3, 2), strides=(2, 2), padding="same", activation="relu")(x_2)
x_3 = average([x_3_1, x_3_2])
x_4 = MaxPooling2D(pool_size=(3, 3), padding="same")(x_3)
x_4 = Permute((3, 2, 1))(x_4)
x_shape = K.int_shape(x_4)
x_4 = [Lambda(slicer_3D.slice_pieces_3D, output_shape=(x_shape[1],x_shape[2], 1))(x_4) for _ in range(x_shape[3])]
x_4 = [Reshape((1, K.int_shape(Flatten()(each))[1]))(Flatten()(each)) for each in x_4]
x_4 = concatenate(x_4, axis=1)
x_5 = GRU(256, return_sequences=True)(x_4)
x_6 = GRU(128)(x_5)
prediction = Dense(4, activation="softmax")(x_6)
model = Model(inputs=inputs, outputs=prediction)
model.summary()
'''
train = snore_data_extractor(load_folder_path, one_hot=True, data_mode="train", resize=(data_dim, time_steps), timechain=False, duplicate=True)
devel = snore_data_extractor(load_folder_path, one_hot=True, data_mode="devel", resize=(data_dim, time_steps), timechain=False, duplicate=True)
epoch_num = 500
batch_size = 16
regularizer = tf.contrib.layers.l2_regularizer(0.01)
loss = tf.reduce_mean(losses.kullback_leibler_divergence(labels, predicts)) + tf.contrib.layers.apply_regularization(regularizer, weights_list=train_var[:-8])
train_step = tf.train.RMSPropOptimizer(learning_rate=0.001, momentum=0.01).minimize(loss)
'''
return model
if __name__ == "__main__":
# some basic setups of model
############################################################################
num_classes = 4
time_steps = 252
data_dim = 176
slicer_3D = Slice(dim=3)
slicer_3D_0 = Slice(dim=3)
slicer_3D_1 = Slice(dim=3)
load_folder_path = "/data/jw11815/snore_spectrogram_5/"
# extract train data with preprocessing
############################################################################
train = snore_data_extractor(load_folder_path, one_hot=True, data_mode="train", resize=(data_dim, time_steps), timechain=False, duplicate=True, colour_mode="RGB")
train_features, train_labels = train.full_data()
train_features = np.array(train_features).astype("float32")
train_features /= 255
x_train = np.rollaxis(train_features, 2, 1)
y_train = np.array(train_labels)
# extract dev data with preprocessing
############################################################################
devel = snore_data_extractor(load_folder_path, one_hot=True, data_mode="devel", resize=(data_dim, time_steps), timechain=False, duplicate=False, colour_mode="RGB")
devel_features, devel_labels = devel.full_data()
devel_features = np.array(devel_features).astype("float32")
devel_features /= 255
x_devel = np.rollaxis(devel_features, 2, 1)
y_devel = np.array(devel_labels)
# extract test data with preprocessing
############################################################################
test = snore_data_extractor(load_folder_path, one_hot=True, data_mode="test", resize=(data_dim, time_steps), timechain=False, duplicate=False, colour_mode="RGB")
test_features, test_labels = test.full_data()
test_features = np.array(test_features).astype("float32")
test_features /= 255
x_test = np.rollaxis(test_features, 2, 1)
y_test = np.array(test_labels)
# restore the model according to the best results of dev data during training
############################################################################
model = image_entry_model_45(time_steps, data_dim)
model.load_weights("weights_spectro_5_45_dev.h5")
predict_train = model.predict(x_train)
train_UAR = recall_score(np.argmax(y_train, axis=1), np.argmax(predict_train, axis=1), average="macro")
print ("This is the current train UAR: ", train_UAR)
print (confusion_matrix(np.argmax(y_train, axis=1), np.argmax(predict_train, axis=1)))
print ("\n")
predict_devel = model.predict(x_devel)
devel_UAR = recall_score(np.argmax(y_devel, axis=1), np.argmax(predict_devel, axis=1), average="macro")
print ("This is the current devel UAR: ", devel_UAR)
print (confusion_matrix(np.argmax(y_devel, axis=1), np.argmax(predict_devel, axis=1)))
print ("\n")
predict_test = model.predict(x_test)
test_UAR = recall_score(np.argmax(y_test, axis=1), np.argmax(predict_test, axis=1), average="macro")
print ("This is the current test UAR: ", test_UAR)
print (confusion_matrix(np.argmax(y_test, axis=1), np.argmax(predict_test, axis=1)))
print ("\n")