forked from s17472/VRS
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmodel.py
151 lines (127 loc) · 5.28 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
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
import config
from keras.layers import (Conv3D, Dense, Dropout, Flatten, MaxPooling3D,
Multiply)
from keras.layers.core import Lambda
from keras.models import Input, Model
def get_rgb(input_x):
"""
Extract the rgb
Args:
input_x: image to extract
Returns:
list of rgb
"""
rgb = input_x[..., :3]
return rgb
def get_opt(input_x):
"""
Extract the optical flows
Args:
input_x: image to extract
Returns:
list of optical flows
"""
opt = input_x[..., 3:5]
return opt
def flow_gated_network_model() -> Model:
"""
Build model of FGN
Returns:
Keras model of FGN structure
"""
inputs = Input(shape=(config.FRAMES_NO, config.SIZE, config.SIZE, 5))
print((config.FRAMES_NO, config.SIZE, config.SIZE, 5))
rgb = Lambda(get_rgb, output_shape=None)(inputs)
opt = Lambda(get_opt, output_shape=None)(inputs)
# RGB channel
rgb = Conv3D(
16, kernel_size=(1, 3, 3), strides=(1, 1, 1), kernel_initializer='he_normal', activation='relu',
padding='same')(rgb)
rgb = Conv3D(
16, kernel_size=(3, 1, 1), strides=(1, 1, 1), kernel_initializer='he_normal', activation='relu',
padding='same')(rgb)
rgb = MaxPooling3D(pool_size=(1, 2, 2))(rgb)
rgb = Conv3D(
16, kernel_size=(1, 3, 3), strides=(1, 1, 1), kernel_initializer='he_normal', activation='relu',
padding='same')(rgb)
rgb = Conv3D(
16, kernel_size=(3, 1, 1), strides=(1, 1, 1), kernel_initializer='he_normal', activation='relu',
padding='same')(rgb)
rgb = MaxPooling3D(pool_size=(1, 2, 2))(rgb)
rgb = Conv3D(
32, kernel_size=(1, 3, 3), strides=(1, 1, 1), kernel_initializer='he_normal', activation='relu',
padding='same')(rgb)
rgb = Conv3D(
32, kernel_size=(3, 1, 1), strides=(1, 1, 1), kernel_initializer='he_normal', activation='relu',
padding='same')(rgb)
rgb = MaxPooling3D(pool_size=(1, 2, 2))(rgb)
rgb = Conv3D(
32, kernel_size=(1, 3, 3), strides=(1, 1, 1), kernel_initializer='he_normal', activation='relu',
padding='same')(rgb)
rgb = Conv3D(
32, kernel_size=(3, 1, 1), strides=(1, 1, 1), kernel_initializer='he_normal', activation='relu',
padding='same')(rgb)
rgb = MaxPooling3D(pool_size=(1, 2, 2))(rgb)
# Optical Flow channel
opt = Conv3D(
16, kernel_size=(1, 3, 3), strides=(1, 1, 1), kernel_initializer='he_normal', activation='relu',
padding='same')(opt)
opt = Conv3D(
16, kernel_size=(3, 1, 1), strides=(1, 1, 1), kernel_initializer='he_normal', activation='relu',
padding='same')(opt)
opt = MaxPooling3D(pool_size=(1, 2, 2))(opt)
opt = Conv3D(
16, kernel_size=(1, 3, 3), strides=(1, 1, 1), kernel_initializer='he_normal', activation='relu',
padding='same')(opt)
opt = Conv3D(
16, kernel_size=(3, 1, 1), strides=(1, 1, 1), kernel_initializer='he_normal', activation='relu',
padding='same')(opt)
opt = MaxPooling3D(pool_size=(1, 2, 2))(opt)
opt = Conv3D(
32, kernel_size=(1, 3, 3), strides=(1, 1, 1), kernel_initializer='he_normal', activation='relu',
padding='same')(opt)
opt = Conv3D(
32, kernel_size=(3, 1, 1), strides=(1, 1, 1), kernel_initializer='he_normal', activation='relu',
padding='same')(opt)
opt = MaxPooling3D(pool_size=(1, 2, 2))(opt)
opt = Conv3D(
32, kernel_size=(1, 3, 3), strides=(1, 1, 1), kernel_initializer='he_normal', activation='sigmoid',
padding='same')(opt)
opt = Conv3D(
32, kernel_size=(3, 1, 1), strides=(1, 1, 1), kernel_initializer='he_normal', activation='sigmoid',
padding='same')(opt)
opt = MaxPooling3D(pool_size=(1, 2, 2))(opt)
# Fusion and Pooling
x = Multiply()([rgb, opt])
x = MaxPooling3D(pool_size=(8, 1, 1))(x)
# Merging Block
x = Conv3D(
64, kernel_size=(1, 3, 3), strides=(1, 1, 1), kernel_initializer='he_normal', activation='relu',
padding='same')(x)
x = Conv3D(
64, kernel_size=(3, 1, 1), strides=(1, 1, 1), kernel_initializer='he_normal', activation='relu',
padding='same')(x)
x = MaxPooling3D(pool_size=(2, 2, 2))(x)
x = Conv3D(
64, kernel_size=(1, 3, 3), strides=(1, 1, 1), kernel_initializer='he_normal', activation='relu',
padding='same')(x)
x = Conv3D(
64, kernel_size=(3, 1, 1), strides=(1, 1, 1), kernel_initializer='he_normal', activation='relu',
padding='same')(x)
x = MaxPooling3D(pool_size=(2, 2, 2))(x)
x = Conv3D(
128, kernel_size=(1, 3, 3), strides=(1, 1, 1), kernel_initializer='he_normal', activation='relu',
padding='same')(x)
x = Conv3D(
128, kernel_size=(3, 1, 1), strides=(1, 1, 1), kernel_initializer='he_normal', activation='relu',
padding='same')(x)
x = MaxPooling3D(pool_size=(2, 1, 1))(x)
# FC Layers
x = Flatten()(x)
x = Dense(128, activation='relu')(x)
x = Dropout(0.2)(x)
x = Dense(32, activation='relu')(x)
# build the model
pred = Dense(2, activation='softmax')(x)
model = Model(inputs=inputs, outputs=pred)
return model