-
Notifications
You must be signed in to change notification settings - Fork 0
/
model.py
168 lines (121 loc) · 3.92 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
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
import tensorflow as tf
import utils
def time_distributed(layer, op):
layer = tf.keras.layers.TimeDistributed(
op
)(layer)
return layer
def td_conv_bn(layer, filters, kernel_size, strides, padding='same'):
layer = time_distributed(
layer,
tf.keras.layers.Conv2D(
filters=filters,
kernel_size=kernel_size,
strides=strides,
padding=padding,
use_bias=False
))
layer = time_distributed(
layer,
tf.keras.layers.BatchNormalization())
return layer
def conv_bn(layer, filters, kernel_size, strides, padding='same'):
layer = tf.keras.layers.Conv2D(
filters=filters,
kernel_size=kernel_size,
strides=strides,
padding=padding,
use_bias=False
)(layer)
layer = tf.keras.layers.BatchNormalization()(layer)
return layer
def td_res_block(layer, filters, down_sample=False):
shortcut = layer
strides = 2 if down_sample else 1
layer = td_conv_bn(layer, filters, 1, 1)
layer = time_distributed(
layer,
tf.keras.layers.LeakyReLU())
layer = td_conv_bn(layer, filters, 3, strides)
layer = time_distributed(
layer,
tf.keras.layers.LeakyReLU())
layer = td_conv_bn(layer, filters, 1, 1)
if down_sample:
shortcut = td_conv_bn(shortcut, filters, 1, strides)
layer = tf.keras.layers.Add()([shortcut, layer])
layer = time_distributed(
layer,
tf.keras.layers.LeakyReLU())
return layer
def build_image_cnn(avg=True):
inputs = tf.keras.layers.Input(shape=[None, None, None, 3])
layer = td_conv_bn(inputs, 24, 5, 2)
layer = time_distributed(
layer,
tf.keras.layers.LeakyReLU()
)
layer = td_conv_bn(layer, 36, 3, 2)
layer = time_distributed(
layer,
tf.keras.layers.LeakyReLU()
)
layer = td_conv_bn(layer, 48, 3, 2)
layer = time_distributed(
layer,
tf.keras.layers.LeakyReLU()
)
layer = td_conv_bn(layer, 64, 3, 2)
layer = time_distributed(
layer,
tf.keras.layers.LeakyReLU()
)
layer = td_conv_bn(layer, 64, 3, 2)
layer = time_distributed(
layer,
tf.keras.layers.LeakyReLU()
)
features = time_distributed(
layer,
tf.keras.layers.Dropout(rate=0.2)
)
if avg:
features = time_distributed(
features,
tf.keras.layers.GlobalAveragePooling2D()
)
return inputs, features
def build_image_lstm(features):
lstm = tf.keras.layers.LSTM(50)(features)
return lstm
def build_map_cnn(avg=True):
inputs = tf.keras.layers.Input(shape=[None, None, 3])
layer = conv_bn(inputs, 24, 5, 2)
layer = conv_bn(layer, 36, 3, 2)
layer = conv_bn(layer, 36, 3, 2)
layer = conv_bn(layer, 50, 3, 2)
output = tf.keras.layers.Dropout(rate=0.2)(layer)
if avg:
output = tf.keras.layers.GlobalAveragePooling2D()(output)
return inputs, output
def tail_layer(image_last_layer, map_last_layer):
combined_layer = tf.keras.layers.Concatenate()([image_last_layer, map_last_layer])
output = tf.keras.layers.Dense(utils.n_classes)(combined_layer)
return output
def create_model(summary=False):
image_inputs, features = build_image_cnn()
image_last_layer = build_image_lstm(features)
image_output = tf.keras.layers.Dense(utils.n_classes)(image_last_layer)
map_inputs, map_last_layer = build_map_cnn()
combined_outputs = tail_layer(image_last_layer, map_last_layer)
model = tf.keras.models.Model(inputs=[image_inputs, map_inputs], outputs=[image_output, combined_outputs])
if summary:
model.summary()
return model
if __name__ == '__main__':
model = create_model(True)
try:
from tensorflow.keras.utils import plot_model
plot_model(model, show_shapes=True, to_file='model.png')
except:
pass