-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCRN_Video_Network.py
376 lines (346 loc) · 14.6 KB
/
CRN_Video_Network.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
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
from typing import Tuple, List, Optional, Union
import torch
import torch.nn as nn
from math import log2
from CRN.Refinement_Module import RefinementModule
from support_scripts.components import FlowNetWrapper
class CRNVideo(torch.nn.Module):
def __init__(
self,
use_tanh: bool,
input_tensor_size: Tuple[int, int],
final_image_size: Tuple[int, int],
num_classes: int,
num_inner_channels: int,
use_feature_encoder: bool,
layer_norm_type: str,
use_resnet_rms: bool,
num_resnet_processing_rms: int,
num_prior_frames: int,
use_optical_flow: bool,
use_edge_map: bool,
use_twin_network: bool,
num_output_images: int,
normalised_prior_frames: bool,
use_simple_warped_image_merging: bool,
):
super(CRNVideo, self).__init__()
self.use_tanh: bool = use_tanh
self.input_tensor_size: Tuple[int, int] = input_tensor_size
self.final_image_size: Tuple[int, int] = final_image_size
self.num_classes: int = num_classes
self.num_inner_channels: int = num_inner_channels
self.use_feature_encoder: bool = use_feature_encoder
self.layer_norm_type: str = layer_norm_type
self.use_resnet_rms: bool = use_resnet_rms
self.num_resnet_processing_rms: int = num_resnet_processing_rms
self.num_prior_frames: int = num_prior_frames
self.use_optical_flow: bool = use_optical_flow
self.use_edge_map: bool = use_edge_map
self.use_twin_network: bool = use_twin_network
self.num_output_images: int = num_output_images
self.normalised_prior_frames: bool = normalised_prior_frames
self.use_simple_warped_image_merging: bool = use_simple_warped_image_merging
self.num_output_image_channels: int = 3
# Checking settings are valid
if self.num_output_images > 1:
assert (
self.num_prior_frames == 0 and self.use_optical_flow is False
), "num_prior_frames > 0 required if use_optical_flow == True"
if self.use_optical_flow:
assert (
self.num_prior_frames > 0
), "num_prior_frames > 0 required if use_optical_flow == True"
# To manage memory usage, number of conv filters in each RM decreases over time
base_rms_conv_channel_settings: list = [
1024,
1024,
1024,
1024,
1024,
512,
512,
128,
32,
]
# Modify settings to match input value for max number of conv filters
self.rms_conv_channel_settings: list = [
min(self.num_inner_channels, x) for x in base_rms_conv_channel_settings
]
# Calculate number of RMs based on output image size
self.num_rms: int = int(log2(final_image_size[0])) - 1
# Create and pupulate lists with RMs
self.rms_list: nn.ModuleList = nn.ModuleList(
[
RefinementModule(
semantic_input_channel_count=self.num_classes,
feature_encoder_input_channel_count=(self.use_feature_encoder * 3),
edge_map_input_channel_count=(self.use_edge_map * 1),
base_conv_channel_count=self.rms_conv_channel_settings[0],
prior_conv_channel_count=0,
final_conv_output_channel_count=0,
is_final_module=False,
input_height_width=self.input_tensor_size,
norm_type=self.layer_norm_type,
num_prior_frames=self.num_prior_frames,
num_resnet_processing_rms=0,
resnet_mode=self.use_resnet_rms,
resnet_no_add=False,
use_semantic_input=True,
use_image_input=(
self.num_prior_frames > 0 and not self.use_twin_network
),
is_flow_output=False,
is_twin_model=self.use_twin_network,
output_flow_mask=False,
)
]
)
self.rms_list.extend(
[
RefinementModule(
semantic_input_channel_count=self.num_classes,
feature_encoder_input_channel_count=(self.use_feature_encoder * 3),
edge_map_input_channel_count=(self.use_edge_map * 1),
base_conv_channel_count=self.rms_conv_channel_settings[i],
prior_conv_channel_count=self.rms_conv_channel_settings[i - 1],
final_conv_output_channel_count=0,
is_final_module=False,
input_height_width=(2 ** (i + 2), 2 ** (i + 3)),
norm_type=self.layer_norm_type,
num_prior_frames=self.num_prior_frames,
num_resnet_processing_rms=0,
resnet_mode=self.use_resnet_rms,
resnet_no_add=False,
use_semantic_input=True,
use_image_input=(
self.num_prior_frames > 0 and not self.use_twin_network
),
is_twin_model=self.use_twin_network,
is_flow_output=False,
output_flow_mask=False,
)
for i in range(1, self.num_rms - 1)
]
)
self.rms_list.append(
RefinementModule(
semantic_input_channel_count=self.num_classes,
feature_encoder_input_channel_count=(self.use_feature_encoder * 3),
edge_map_input_channel_count=(self.use_edge_map * 1),
base_conv_channel_count=self.rms_conv_channel_settings[
self.num_rms - 1
],
prior_conv_channel_count=self.rms_conv_channel_settings[
self.num_rms - 2
],
final_conv_output_channel_count=self.num_output_image_channels
* self.num_output_images,
is_final_module=True,
input_height_width=final_image_size,
norm_type=self.layer_norm_type,
num_prior_frames=self.num_prior_frames,
num_resnet_processing_rms=self.num_resnet_processing_rms,
resnet_mode=self.use_resnet_rms,
resnet_no_add=False,
use_semantic_input=True,
use_image_input=(
self.num_prior_frames > 0 and not self.use_twin_network
),
is_flow_output=self.use_optical_flow and not self.use_twin_network,
is_twin_model=self.use_twin_network,
output_flow_mask=False,
)
)
# Flow network
self.rms_list_twin: nn.ModuleList = nn.ModuleList(
[
RefinementModule(
semantic_input_channel_count=0,
feature_encoder_input_channel_count=0,
edge_map_input_channel_count=0,
base_conv_channel_count=self.rms_conv_channel_settings[0],
prior_conv_channel_count=0,
final_conv_output_channel_count=0,
is_final_module=False,
input_height_width=self.input_tensor_size,
norm_type=self.layer_norm_type,
num_prior_frames=self.num_prior_frames,
num_resnet_processing_rms=0,
resnet_mode=self.use_resnet_rms,
resnet_no_add=False,
use_semantic_input=False,
use_image_input=self.num_prior_frames > 0,
is_twin_model=True,
is_flow_output=False,
output_flow_mask=False,
)
if self.use_twin_network and self.num_prior_frames > 0
else nn.Identity()
]
)
self.rms_list_twin.extend(
[
RefinementModule(
semantic_input_channel_count=0,
feature_encoder_input_channel_count=0,
edge_map_input_channel_count=0,
base_conv_channel_count=self.rms_conv_channel_settings[i],
prior_conv_channel_count=self.rms_conv_channel_settings[i - 1],
final_conv_output_channel_count=0,
is_final_module=False,
input_height_width=(2 ** (i + 2), 2 ** (i + 3)),
norm_type=self.layer_norm_type,
num_prior_frames=self.num_prior_frames,
num_resnet_processing_rms=0,
resnet_mode=self.use_resnet_rms,
resnet_no_add=False,
use_semantic_input=False,
use_image_input=self.num_prior_frames > 0,
is_flow_output=False,
is_twin_model=True,
output_flow_mask=False,
)
if self.use_twin_network
else nn.Identity()
for i in range(1, self.num_rms - 1)
]
)
self.rms_list_twin.append(
RefinementModule(
semantic_input_channel_count=0,
feature_encoder_input_channel_count=0,
edge_map_input_channel_count=0,
base_conv_channel_count=self.rms_conv_channel_settings[
self.num_rms - 1
],
prior_conv_channel_count=self.rms_conv_channel_settings[
self.num_rms - 2
],
final_conv_output_channel_count=0,
is_final_module=True,
input_height_width=final_image_size,
norm_type=self.layer_norm_type,
num_prior_frames=self.num_prior_frames,
num_resnet_processing_rms=self.num_resnet_processing_rms,
resnet_mode=self.use_resnet_rms,
resnet_no_add=False,
use_semantic_input=False,
use_image_input=self.num_prior_frames > 0,
is_flow_output=True,
is_twin_model=True,
output_flow_mask=not self.use_simple_warped_image_merging,
)
if self.use_twin_network and self.use_optical_flow
else nn.Identity()
)
if self.use_tanh:
self.tan_h = nn.Tanh()
# Grid for warping
if self.use_optical_flow:
self.grid: torch.Tensor = FlowNetWrapper.get_grid(
1, self.final_image_size, torch.device("cuda:0")
)
def forward(
self,
msk: torch.Tensor,
feature_encoding: torch.Tensor,
edge_map: torch.Tensor,
prev_images: torch.Tensor = None,
prev_masks: torch.Tensor = None,
) -> Tuple[torch.Tensor, torch.Tensor, torch.Tensor, torch.Tensor, torch.Tensor]:
# List of output, both flow and gen nets sum and are stored here
output_list: list = []
output_1 = self.rms_list[0](
msk, None, feature_encoding, edge_map, prev_images, prev_masks
)["x"]
if self.use_twin_network and self.num_prior_frames > 0:
output_1 = (
output_1
+ self.rms_list_twin[0](
msk, None, feature_encoding, edge_map, prev_images, prev_masks
)["x"]
)
output_list.append(output_1)
for i in range(1, len(self.rms_list) - 1):
output_i = self.rms_list[i](
msk,
output_list[-1],
feature_encoding,
edge_map,
prev_images,
prev_masks,
)["x"]
if self.use_twin_network: # Preparation for Siamese network test
output_i = output_i + self.rms_list_twin[i](
msk,
output_list[-1],
feature_encoding,
edge_map,
prev_images,
prev_masks,
)["x"]
output_list.append(output_i)
# Generated image, use final gen RM
output_final_rms_list: dict = self.rms_list[-1](
msk,
output_list[-1],
feature_encoding,
edge_map,
prev_images,
prev_masks,
)
output_gen = (output_final_rms_list["out_img"] + 1.0) / 2.0
output_flow = None
output_mask = None
output_warped = None
# Optical flow and merge mask, use final flow RM
if self.use_optical_flow:
if not self.use_twin_network:
output_flow: Optional[torch.Tensor] = output_final_rms_list["out_flow"]
if not self.use_simple_warped_image_merging:
output_mask: Optional[torch.Tensor] = output_final_rms_list[
"out_mask"
]
else:
output_flow_and_mask = self.rms_list_twin[-1](
msk,
output_list[-1],
feature_encoding,
edge_map,
prev_images,
prev_masks,
)
output_flow: Optional[torch.Tensor] = output_flow_and_mask["out_flow"]
if not self.use_simple_warped_image_merging:
output_mask: Optional[torch.Tensor] = output_flow_and_mask[
"out_mask"
]
# Warp prior frame with flow
output_warped: Optional[torch.Tensor] = FlowNetWrapper.resample(
prev_images[:, 0:3] + (self.normalised_prior_frames * 0.5),
output_flow,
self.grid,
)
if not self.use_simple_warped_image_merging:
output: torch.Tensor = (output_mask * output_gen) + (
(torch.ones_like(output_mask) - output_mask) * output_warped
)
else:
output: torch.Tensor = (output_gen + output_warped) / 2
else:
output = output_gen
output_gen = None
# For multiple output images, split final output and put back together as separate images.
if self.num_output_images > 1:
a, b, c = torch.chunk(output.unsqueeze(2), 3, 1)
output = torch.cat((a, b, c), 2)
else:
output = output.unsqueeze(1)
return (
output,
output_gen,
output_warped,
output_flow,
output_mask,
)