-
Notifications
You must be signed in to change notification settings - Fork 0
/
model.py
183 lines (131 loc) · 6.22 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
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
"""
This file contains the blocks used to build the U-Net models and the VGG16 feature extractor.
"""
from keras.models import Model
from keras.layers import Input, Convolution2D, LeakyReLU, AveragePooling2D, UpSampling2D, Concatenate, \
ReLU, MaxPooling2D
import tensorflow as tf
def _down_sampling_block(x: tf.Tensor, filters: int, size: int, trainable: bool = True) -> tf.Tensor:
"""
U-Net down-sampling block.
:param x: the input tensor
:param filters: the number of filters of the output layer
:param size: the size of the convolutional kernel
:param trainable: whether the block is trainable
:return: the output tensor
"""
x = AveragePooling2D(pool_size=2, padding="valid")(x)
x = Convolution2D(filters=filters, kernel_size=(size, size), strides=1, padding="same", trainable=trainable)(x)
x = LeakyReLU(alpha=0.1)(x)
x = Convolution2D(filters=filters, kernel_size=(size, size), strides=1, padding="same", trainable=trainable)(x)
x = LeakyReLU(alpha=0.1)(x)
return x
def _up_sampling_block(x: tf.Tensor, y: tf.Tensor, filters: int, trainable: bool = True) -> tf.Tensor:
"""
U-Net up-sampling block.
:param x: the input tensor
:param y: the residual skip connection tensor
:param filters: the number of filters of the output layer
:param trainable: whether the block is trainable
:return: the output tensor
"""
x = UpSampling2D(interpolation="bilinear")(x)
x = Convolution2D(filters=filters, kernel_size=(3, 3), strides=1, padding="same", trainable=trainable)(x)
x = LeakyReLU(alpha=0.1)(x)
x = Concatenate(axis=-1)([x, y])
x = Convolution2D(filters=filters, kernel_size=(3, 3), strides=1, padding="same", trainable=trainable)(x)
x = LeakyReLU(alpha=0.1)(x)
return x
def _u_net_block(x: tf.Tensor, filters: int, trainable: bool = True) -> tf.Tensor:
"""
U-Net block.
:param x: the input tensor
:param filters: the number of filters of the output layer
:param trainable: whether the block is trainable
:return: the output tensor
"""
x = Convolution2D(filters=32, kernel_size=(7, 7), strides=1, padding="same", trainable=trainable)(x)
x = LeakyReLU(alpha=0.1)(x)
x = Convolution2D(filters=32, kernel_size=(7, 7), strides=1, padding="same", trainable=trainable)(x)
x_0 = LeakyReLU(alpha=0.1)(x)
x_1 = _down_sampling_block(x_0, 64, 5, trainable=trainable)
x_2 = _down_sampling_block(x_1, 128, 3, trainable=trainable)
x_3 = _down_sampling_block(x_2, 256, 3, trainable=trainable)
x_4 = _down_sampling_block(x_3, 512, 3, trainable=trainable)
x = _down_sampling_block(x_4, 512, 3, trainable=trainable)
x = _up_sampling_block(x, x_4, 512, trainable=trainable)
x = _up_sampling_block(x, x_3, 256, trainable=trainable)
x = _up_sampling_block(x, x_2, 128, trainable=trainable)
x = _up_sampling_block(x, x_1, 64, trainable=trainable)
x = _up_sampling_block(x, x_0, 32, trainable=trainable)
x = Convolution2D(filters=filters, kernel_size=(3, 3), strides=1, padding="same", trainable=trainable)(x)
x = LeakyReLU(alpha=0.1)(x)
return x
def build_model_base_flows(trainable: bool = True) -> Model:
"""
Builds the U-Net model used to compute the base flows.
:param trainable: whether the model is trainable
:return: the model
"""
input_frame_0 = Input(shape=(None, None, 3))
input_frame_1 = Input(shape=(None, None, 3))
x = Concatenate(axis=-1)([input_frame_0, input_frame_1])
model = Model([input_frame_0, input_frame_1], _u_net_block(x, 4, trainable=trainable))
return model
def build_model_offset_flows(trainable: bool = True) -> Model:
"""
Builds the U-Net model used to compute the offset flows.
:param trainable: whether the model is trainable
:return: the model
"""
input_frame_0 = Input(shape=(None, None, 3))
input_frame_1 = Input(shape=(None, None, 3))
input_base_flow_01 = Input(shape=(None, None, 2))
input_base_flow_10 = Input(shape=(None, None, 2))
input_base_flow_t1 = Input(shape=(None, None, 2))
input_base_flow_t0 = Input(shape=(None, None, 2))
input_frame_1_inter = Input(shape=(None, None, 3))
input_frame_0_inter = Input(shape=(None, None, 3))
x = Concatenate(axis=-1)([
input_frame_0, input_frame_1, input_base_flow_01, input_base_flow_10, input_base_flow_t1, input_base_flow_t0,
input_frame_1_inter, input_frame_0_inter
])
model = Model([
input_frame_0, input_frame_1, input_base_flow_01, input_base_flow_10, input_base_flow_t1, input_base_flow_t0,
input_frame_1_inter, input_frame_0_inter
], _u_net_block(x, 5, trainable=trainable))
return model
def _convolution_block(
x: tf.Tensor, length: int, filters: int, size: int, pooling: bool = True, trainable: bool = False
) -> tf.Tensor:
"""
VGG16 convolution block.
:param x: the input tensor
:param length: the number of convolution layers within the block
:param filters: the number of filters of the output layer
:param size: the size of the convolution kernel
:param pooling: whether to apply max-pooling
:param trainable: whether the block is trainable
:return: the output tensor
"""
x = Convolution2D(filters=filters, kernel_size=size, strides=1, padding="same", trainable=trainable)(x)
for _ in range(length - 1):
x = ReLU()(x)
x = Convolution2D(filters=filters, kernel_size=size, strides=1, padding="same", trainable=trainable)(x)
if pooling:
x = ReLU()(x)
x = MaxPooling2D(pool_size=2)(x)
return x
def build_feature_extractor(trainable: bool = False) -> Model:
"""
Builds the VGG16 feature extractor used for the perceptual loss.
:param trainable: whether the model is trainable (should be False if used as a loss)
:return: the model
"""
input_frame = Input(shape=(None, None, 3))
x = _convolution_block(input_frame, 2, 64, 3, pooling=True, trainable=trainable)
x = _convolution_block(x, 2, 128, 3, pooling=True, trainable=trainable)
x = _convolution_block(x, 3, 256, 3, pooling=True, trainable=trainable)
x = _convolution_block(x, 3, 512, 3, pooling=False, trainable=trainable)
model = Model(input_frame, x, trainable=trainable)
return model