-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmodel_compilators.py
59 lines (46 loc) · 2.38 KB
/
model_compilators.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
from keras.layers import Dense, Input, GRU, Bidirectional, TimeDistributed
from keras.models import Model
from hierarchical_attention_network import HanAttentionLayer
from keras.optimizers import Adam
def compile_model(n_videos, max_poses, max_frames, lr, print_summary=False):
print(n_videos)
sentence_input = Input(shape=(max_poses,51))
linear_rep = Dense(64, activation='relu')(sentence_input)
pose_annotation = Bidirectional(GRU(64, return_sequences=True))(linear_rep)
print(f'pose_annotation: {pose_annotation.shape}')
attn_pose = HanAttentionLayer(64)(pose_annotation)
print(f'attn_pose: {attn_pose.shape}')
frameEncoder = Model(sentence_input, attn_pose)
video_input = Input(shape=(max_frames, max_poses, 51))
print(f'video_input: {video_input.shape}')
video_encoder = TimeDistributed(frameEncoder)(video_input)
print(f'video_encoder: {video_encoder.shape}')
linear_rep_frame = Dense(64, activation='relu')(video_encoder)
frame_annotation = Bidirectional(GRU(64, return_sequences=True))(linear_rep_frame)
attn_frame = HanAttentionLayer(64)(frame_annotation)
preds = Dense(2, activation='softmax')(attn_frame)
model = Model(video_input, preds)
opt = Adam(learning_rate=lr)
model.compile(loss='binary_crossentropy', optimizer=opt, metrics=['acc'])
if print_summary:
model.summary()
return model
def get_model_parts(dense_dim: int):
MAX_FRAMES = 151
MAX_POSES = 20
sentence_input = Input(shape=(MAX_POSES,51))
linear_rep = Dense(dense_dim, activation='relu')(sentence_input) # Dense 64 dim
pose_annotation = Bidirectional(GRU(64, return_sequences=True))(linear_rep)
print(f'pose_annotation: {pose_annotation.shape}')
attn_pose = HanAttentionLayer(64)(pose_annotation)
print(f'attn_pose: {attn_pose.shape}')
frameEncoder = Model(sentence_input, attn_pose)
video_input = Input(shape=(MAX_FRAMES, MAX_POSES, 51))
print(f'video_input: {video_input.shape}')
video_encoder = TimeDistributed(frameEncoder)(video_input)
print(f'video_encoder: {video_encoder.shape}')
linear_rep_frame = Dense(dense_dim, activation='relu')(video_encoder) # Dense 64 dim
frame_annotation = Bidirectional(GRU(64, return_sequences=True))(linear_rep_frame)
attn_frame = HanAttentionLayer(100)(frame_annotation)
preds = Dense(2, activation='softmax')(attn_frame)
return video_input, preds