-
Notifications
You must be signed in to change notification settings - Fork 3
/
model.py
128 lines (93 loc) · 4.21 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
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
'''
Models for Multi-Task Temporal Shift Attention Networks for On-Device Contactless Vitals Measurement
Author: Xin Liu
'''
import tensorflow as tf
from tensorflow import keras
from tensorflow.python.keras import backend as K
from tensorflow.python.keras.layers import Conv2D, Conv3D, Input, AveragePooling2D, \
multiply, Dense, Dropout, Flatten, AveragePooling3D
from tensorflow.python.keras.models import Model
class Attention_mask(tf.keras.layers.Layer):
def call(self, x):
xsum = K.sum(x, axis=1, keepdims=True)
xsum = K.sum(xsum, axis=2, keepdims=True)
xshape = K.int_shape(x)
return x / xsum * xshape[1] * xshape[2] * 0.5
def get_config(self):
config = super(Attention_mask, self).get_config()
return config
class TSM(tf.keras.layers.Layer):
def call(self, x, n_frame, fold_div=3):
nt, h, w, c = x.shape
x = K.reshape(x, (-1, n_frame, h, w, c))
fold = c // fold_div
last_fold = c - (fold_div - 1) * fold
out1, out2, out3 = tf.split(x, [fold, fold, last_fold], axis=-1)
# Shift left
padding_1 = tf.zeros_like(out1)
padding_1 = padding_1[:, -1, :, :, :]
padding_1 = tf.expand_dims(padding_1, 1)
_, out1 = tf.split(out1, [1, n_frame - 1], axis=1)
out1 = tf.concat([out1, padding_1], axis=1)
# Shift right
padding_2 = tf.zeros_like(out2)
padding_2 = padding_2[:, 0, :, :, :]
padding_2 = tf.expand_dims(padding_2, 1)
out2, _ = tf.split(out2, [n_frame - 1, 1], axis=1)
out2 = tf.concat([padding_2, out2], axis=1)
out = tf.concat([out1, out2, out3], axis=-1)
out = K.reshape(out, (-1, h, w, c))
return out
def get_config(self):
config = super(TSM, self).get_config()
return config
def TSM_Cov2D(x, n_frame, nb_filters=128, kernel_size=(3, 3), activation='tanh', padding='same'):
x = TSM()(x, n_frame)
x = Conv2D(nb_filters, kernel_size, padding=padding, activation=activation)(x)
return x
# %% MTTS-CAN
def MTTS_CAN(n_frame, nb_filters1, nb_filters2, input_shape, kernel_size=(3, 3), dropout_rate1=0.25,
dropout_rate2=0.5, pool_size=(2, 2), nb_dense=128):
diff_input = Input(shape=input_shape)
rawf_input = Input(shape=input_shape)
d1 = TSM_Cov2D(diff_input, n_frame, nb_filters1, kernel_size, padding='same', activation='tanh')
d2 = TSM_Cov2D(d1, n_frame, nb_filters1, kernel_size, padding='valid', activation='tanh')
r1 = Conv2D(nb_filters1, kernel_size, padding='same', activation='tanh')(rawf_input)
r2 = Conv2D(nb_filters1, kernel_size, activation='tanh')(r1)
g1 = Conv2D(1, (1, 1), padding='same', activation='sigmoid')(r2)
g1 = Attention_mask()(g1)
gated1 = multiply([d2, g1])
d3 = AveragePooling2D(pool_size)(gated1)
d4 = Dropout(dropout_rate1)(d3)
r3 = AveragePooling2D(pool_size)(r2)
r4 = Dropout(dropout_rate1)(r3)
d5 = TSM_Cov2D(d4, n_frame, nb_filters2, kernel_size, padding='same', activation='tanh')
d6 = TSM_Cov2D(d5, n_frame, nb_filters2, kernel_size, padding='valid', activation='tanh')
r5 = Conv2D(nb_filters2, kernel_size, padding='same', activation='tanh')(r4)
r6 = Conv2D(nb_filters2, kernel_size, activation='tanh')(r5)
g2 = Conv2D(1, (1, 1), padding='same', activation='sigmoid')(r6)
g2 = Attention_mask()(g2)
gated2 = multiply([d6, g2])
d7 = AveragePooling2D(pool_size)(gated2)
d8 = Dropout(dropout_rate1)(d7)
d9 = Flatten()(d8)
d10_y = Dense(nb_dense, activation='tanh')(d9)
d11_y = Dropout(dropout_rate2)(d10_y)
out_y = Dense(1, name='output_1')(d11_y)
d10_r = Dense(nb_dense, activation='tanh')(d9)
d11_r = Dropout(dropout_rate2)(d10_r)
out_r = Dense(1, name='output_2')(d11_r)
model = Model(inputs=[diff_input, rawf_input], outputs=[out_y, out_r])
return model
# %%
class HeartBeat(keras.callbacks.Callback):
def __init__(self, train_gen, test_gen, args, cv_split, save_dir):
super(HeartBeat, self).__init__()
self.train_gen = train_gen
self.test_gen = test_gen
self.args = args
self.cv_split = cv_split
self.save_dir = save_dir
def on_epoch_end(self, epoch, logs={}):
print('PROGRESS: 0.00%')