-
Notifications
You must be signed in to change notification settings - Fork 1
/
cab_cnn.py
61 lines (47 loc) · 2.59 KB
/
cab_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
#!/usr/bin/env python3
import numpy as np
import tensorflow as tf
from tensorflow import keras
from tensorflow.keras import layers
MAX_POOLING_SIZE = 5
SR = int(22050/MAX_POOLING_SIZE)
SNIPPET_LENGTH = 30
input_size = SNIPPET_LENGTH * SR
NUM_CLASSIFIERS = 40
def get_model():
inputs = keras.Input(shape=(input_size, 1))
x = keras.layers.Conv1D(filters=16, kernel_size=4, padding='same', activation=keras.activations.relu)(inputs)
x = keras.layers.MaxPool1D(pool_size=4, strides=2, padding='same')(x)
x = keras.layers.Dropout(0.15)(x)
x = keras.layers.Conv1D(filters=32, kernel_size=4, padding='same', activation=keras.activations.relu)(x)
x = keras.layers.MaxPool1D(pool_size=4, strides=2, padding='same')(x)
x = keras.layers.Dropout(0.15)(x)
x = keras.layers.Conv1D(filters=32, kernel_size=10, padding='same', activation=keras.activations.relu)(x)
x = keras.layers.MaxPool1D(pool_size=10, strides=5, padding='same')(x)
x = keras.layers.Dropout(0.1)(x)
x = keras.layers.Conv1D(filters=128, kernel_size=10, padding='same', activation=keras.activations.relu)(x)
x = keras.layers.MaxPool1D(pool_size=10, strides=5, padding='same')(x)
classifiers = []
for i in range(NUM_CLASSIFIERS):
c_inputs = keras.Input(shape=(1323, 128))
y = keras.layers.Dense(8, activation=keras.activations.relu)(c_inputs)
y = keras.layers.Dense(4, activation=keras.activations.relu)(y)
y = keras.layers.Dense(1, activation=keras.activations.sigmoid)(y)
classifier = keras.Model(inputs=c_inputs, outputs=y, name=f'Classifier_{i+1}')
classifiers.append(classifier)
classifier_outputs = []
for i in range(NUM_CLASSIFIERS):
classifier_outputs.append(classifiers[i](x))
classifier_outputs = keras.layers.Concatenate()(classifier_outputs)
acb_inputs = keras.Input(shape=(1323, 128))
y = keras.layers.Dense(160, activation=keras.activations.relu)(acb_inputs)
y = keras.layers.Dense(80, activation=keras.activations.relu)(y)
y = keras.layers.Dense(40, activation=keras.activations.softmax)(y)
attention_block = keras.Model(inputs=acb_inputs, outputs=y, name=f'Attention_Block')
acb_outputs = attention_block(x)
outputs = keras.layers.Multiply()([classifier_outputs, acb_outputs])
outputs = tf.reduce_sum(outputs, axis=-1)
outputs = tf.reduce_mean(outputs, axis=-1)
model = keras.Model(inputs=inputs, outputs=outputs)
model.compile(optimizer='Adam', loss=keras.losses.binary_crossentropy, metrics=[keras.metrics.binary_accuracy])
return model