File size: 3,973 Bytes
4a5ddda a53ae2a 4a5ddda a53ae2a 4a5ddda |
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 |
import tensorflow as tf
from typing import Tuple
def _inception_module(
input_tensor,
stride=1,
activation="linear",
use_bottleneck=True,
kernel_size=40,
bottleneck_size=32,
nb_filters=32,
):
if use_bottleneck and int(input_tensor.shape[-1]) > 1:
input_inception = tf.keras.layers.Conv1D(
filters=bottleneck_size,
kernel_size=1,
padding="same",
activation=activation,
use_bias=False,
)(input_tensor)
else:
input_inception = input_tensor
# kernel_size_s = [3, 5, 8, 11, 17]
kernel_size_s = [kernel_size // (2**i) for i in range(3)]
conv_list = []
for i in range(len(kernel_size_s)):
conv_list.append(
tf.keras.layers.Conv1D(
filters=nb_filters,
kernel_size=kernel_size_s[i],
strides=stride,
padding="same",
activation=activation,
use_bias=False,
)(input_inception)
)
max_pool_1 = tf.keras.layers.MaxPool1D(pool_size=3, strides=stride, padding="same")(
input_tensor
)
conv_6 = tf.keras.layers.Conv1D(
filters=nb_filters,
kernel_size=1,
padding="same",
activation=activation,
use_bias=False,
)(max_pool_1)
conv_list.append(conv_6)
x = tf.keras.layers.Concatenate(axis=2)(conv_list)
x = tf.keras.layers.BatchNormalization()(x)
x = tf.keras.layers.Activation(activation="relu")(x)
return x
def _shortcut_layer(input_tensor, out_tensor):
shortcut_y = tf.keras.layers.Conv1D(
filters=int(out_tensor.shape[-1]), kernel_size=1, padding="same", use_bias=False
)(input_tensor)
shortcut_y = tf.keras.layers.BatchNormalization()(shortcut_y)
x = tf.keras.layers.Add()([shortcut_y, out_tensor])
x = tf.keras.layers.Activation("relu")(x)
return x
def build_age_model(
input_shape: Tuple[int, int],
nb_classes: int,
depth: int = 6,
use_residual: bool = True,
)-> tf.keras.models.Model:
"""
Model proposed by HI Fawas et al 2019 "Finding AlexNet for Time Series Classification - InceptionTime"
"""
input_layer = tf.keras.layers.Input(input_shape)
x = input_layer
input_res = input_layer
for d in range(depth):
x = _inception_module(x)
if use_residual and d % 3 == 2:
x = _shortcut_layer(input_res, x)
input_res = x
gap_layer = tf.keras.layers.GlobalAveragePooling1D()(x)
output_layer = tf.keras.layers.Dense(units=nb_classes, activation="linear")(
gap_layer
)
model = tf.keras.models.Model(inputs=input_layer, outputs=output_layer)
model.compile(
loss=tf.keras.losses.MeanAbsoluteError(),
optimizer=tf.keras.optimizers.Adam(learning_rate=0.001),
metrics=[tf.keras.metrics.MeanSquaredError()],
)
return model
def build_gender_model(
input_shape: Tuple[int, int],
nb_classes: int,
depth: int = 6,
use_residual: bool = True,
)-> tf.keras.models.Model:
"""
Model proposed by HI Fawas et al 2019 "Finding AlexNet for Time Series Classification - InceptionTime"
"""
input_layer = tf.keras.layers.Input(input_shape)
x = input_layer
input_res = input_layer
for d in range(depth):
x = _inception_module(x)
if use_residual and d % 3 == 2:
x = _shortcut_layer(input_res, x)
input_res = x
gap_layer = tf.keras.layers.GlobalAveragePooling1D()(x)
output_layer = tf.keras.layers.Dense(units=nb_classes, activation="sigmoid")(
gap_layer
)
model = tf.keras.models.Model(inputs=input_layer, outputs=output_layer)
model.compile(
loss=tf.keras.losses.BinaryCrossentropy(),
optimizer=tf.keras.optimizers.Adam(learning_rate=0.001),
metrics=[tf.keras.metrics.AUC(curve='ROC',name="AUROC")],
)
return model |