-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmodel.py
69 lines (62 loc) · 2.65 KB
/
model.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
import tensorflow as tf
def residual_block(inputs, **kwargs):
supported_kwargs = [
"filters",
"kernel_size",
"strides",
"padding",
"activation",
"kernel_regularizer",
"kernel_initializer",
"bias_regularizer",
"bias_initializer",
]
params = {
k: kwargs[k] for k in kwargs if k in supported_kwargs
}
x = tf.keras.layers.Conv1D(**params)(inputs)
x = tf.keras.layers.Activation(tf.keras.activations.relu)(x)
x = tf.keras.layers.BatchNormalization()(x)
x = tf.keras.layers.Conv1D(**params)(x)
x = tf.keras.layers.BatchNormalization()(x)
return tf.keras.layers.Add()([x, inputs])
def build_model(
input_shape=(256, 1), # (256, 1)
num_residual_blocks=32, # 32
scaling_factor=4, # 4
filters=256, # 256
kernel_size=3, # 3
strides=1, # 1
padding="same", # "same"
kernel_regularizer=None,
kernel_initializer=None,
bias_regularizer=None,
bias_initializer=None,
):
inputs = tf.keras.Input(shape=input_shape, dtype="float32")
# Multiple residual blocks for feature extraction
x = tf.keras.layers.Conv1D(
filters=filters, kernel_size=kernel_size, strides=strides, padding=padding, activation=None,
kernel_regularizer=kernel_regularizer, kernel_initializer=kernel_initializer,
bias_regularizer=bias_regularizer, bias_initializer=bias_initializer,
)(inputs)
for i in range(num_residual_blocks):
x = residual_block(
inputs=x, filters=filters, kernel_size=kernel_size, strides=strides, padding=padding, activation=None,
kernel_regularizer=kernel_regularizer, kernel_initializer=kernel_initializer,
bias_regularizer=bias_regularizer, bias_initializer=bias_initializer,
)
# Up-sampling block
x = tf.keras.layers.Conv1D(
filters=filters, kernel_size=kernel_size, strides=strides, padding="same", activation=None,
kernel_regularizer=kernel_regularizer, kernel_initializer=kernel_initializer,
bias_regularizer=bias_regularizer, bias_initializer=bias_initializer,
)(x)
x = tf.keras.layers.Conv1D(
filters=scaling_factor, kernel_size=kernel_size, strides=strides, padding="same", activation=None,
kernel_regularizer=kernel_regularizer, kernel_initializer=kernel_initializer,
bias_regularizer=bias_regularizer, bias_initializer=bias_initializer
)(x)
x = tf.reshape(x, (-1, x.shape[1] * x.shape[2], 1)) # point shuffle.
x = tf.keras.layers.Activation(tf.keras.activations.relu)(x)
return tf.keras.Model(inputs=inputs, outputs=x, name="FDRN")