-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathuflow_utils.py
More file actions
1343 lines (1130 loc) · 49.7 KB
/
uflow_utils.py
File metadata and controls
1343 lines (1130 loc) · 49.7 KB
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
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
# coding=utf-8
# Copyright 2020 The Google Research Authors.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
"""UFlow utils.
This library contains the various util functions used in UFlow.
"""
import time
import tensorflow as tf
from uflow import uflow_plotting
from uflow.uflow_resampler import resampler
def flow_to_warp(flow):
"""Compute the warp from the flow field.
Args:
flow: tf.tensor representing optical flow.
Returns:
The warp, i.e. the endpoints of the estimated flow.
"""
# Construct a grid of the image coordinates.
height, width = flow.shape.as_list()[-3:-1]
i_grid, j_grid = tf.meshgrid(#给定一列输入,输出一个网格
tf.linspace(0.0, height - 1.0, int(height)),
tf.linspace(0.0, width - 1.0, int(width)),
indexing='ij')#均分计算指令,在间隔中生成值.
grid = tf.stack([i_grid, j_grid], axis=2)#将秩为 R 的张量列表堆叠成一个秩为 (R+1) 的张量.
# Potentially add batch dimension to match the shape of flow.
if len(flow.shape) == 4:
grid = grid[None]
# Add the flow field to the image grid.
if flow.dtype != grid.dtype:
grid = tf.cast(grid, flow.dtype)
warp = grid + flow
return warp
def mask_invalid(coords):
"""Mask coordinates outside of the image.
Valid = 1, invalid = 0.
Args:
coords: a 4D float tensor of image coordinates.
Returns:
The mask showing which coordinates are valid.
"""
coords_rank = len(coords.shape)
if coords_rank != 4:
raise NotImplementedError()
max_height = float(coords.shape[-3] - 1)
max_width = float(coords.shape[-2] - 1)
mask = tf.logical_and(
tf.logical_and(coords[:, :, :, 0] >= 0.0,
coords[:, :, :, 0] <= max_height),
tf.logical_and(coords[:, :, :, 1] >= 0.0,
coords[:, :, :, 1] <= max_width))
mask = tf.cast(mask, dtype=tf.float32)[:, :, :, None]
return mask
def resample(source, coords):
"""Resample the source image at the passed coordinates.
Args:
source: tf.tensor, batch of images to be resampled.
coords: tf.tensor, batch of coordinates in the image.
Returns:
The resampled image.
Coordinates should be between 0 and size-1. Coordinates outside of this range
are handled by interpolating with a background image filled with zeros in the
same way that SAME size convolution works.
"""
# Wrap this function because it uses a different order of height/width dims.
orig_source_dtype = source.dtype
if source.dtype != tf.float32:
source = tf.cast(source, tf.float32)
if coords.dtype != tf.float32:
coords = tf.cast(coords, tf.float32)
coords_rank = len(coords.shape)
if coords_rank == 4:
output = resampler(source, coords[:, :, :, ::-1])
if orig_source_dtype != source.dtype:
return tf.cast(output, orig_source_dtype)
return output
else:
raise NotImplementedError()
def compute_range_map(flow,
downsampling_factor=1,
reduce_downsampling_bias=True,
resize_output=True):
"""Count how often each coordinate is sampled.
Counts are assigned to the integer coordinates around the sampled coordinates
using weights from bilinear interpolation.
Args:
flow: A float tensor of shape (batch size x height x width x 2) that
represents a dense flow field.
downsampling_factor: An integer, by which factor to downsample the output
resolution relative to the input resolution. Downsampling increases the
bin size but decreases the resolution of the output. The output is
normalized such that zero flow input will produce a constant ones output.
reduce_downsampling_bias: A boolean, whether to reduce the downsampling bias
near the image boundaries by padding the flow field.
resize_output: A boolean, whether to resize the output at the input
resolution.
Returns:
A float tensor of shape [batch_size, height, width, 1] that denotes how
often each pixel is sampled.
"""
# Get input shape.
input_shape = flow.shape.as_list()
if len(input_shape) != 4:
raise NotImplementedError()
batch_size, input_height, input_width, _ = input_shape
flow_height = input_height
flow_width = input_width
# Apply downsampling (and move the coordinate frame appropriately).
output_height = input_height // downsampling_factor
output_width = input_width // downsampling_factor
if downsampling_factor > 1:
# Reduce the bias that comes from downsampling, where pixels at the edge
# will get lower counts that pixels in the middle of the image, by padding
# the flow field.
if reduce_downsampling_bias:
p = downsampling_factor // 2
flow_height += 2 * p
flow_width += 2 * p
# Apply padding in multiple steps to padd with the values on the edge.
for _ in range(p):
flow = tf.pad(
tensor=flow,
paddings=[[0, 0], [1, 1], [1, 1], [0, 0]],
mode='SYMMETRIC')
coords = flow_to_warp(flow) - p
# Update the coordinate frame to the downsampled one.
coords = (coords + (1 - downsampling_factor) * 0.5) / downsampling_factor
elif downsampling_factor == 1:
coords = flow_to_warp(flow)
else:
raise ValueError('downsampling_factor must be an integer >= 1.')
# Split coordinates into an integer part and a float offset for interpolation.
coords_floor = tf.floor(coords)
coords_offset = coords - coords_floor
coords_floor = tf.cast(coords_floor, 'int32')
# Define a batch offset for flattened indexes into all pixels.
batch_range = tf.reshape(tf.range(batch_size), [batch_size, 1, 1])
idx_batch_offset = tf.tile(
batch_range, [1, flow_height, flow_width]) * output_height * output_width
# Flatten everything.
coords_floor_flattened = tf.reshape(coords_floor, [-1, 2])
coords_offset_flattened = tf.reshape(coords_offset, [-1, 2])
idx_batch_offset_flattened = tf.reshape(idx_batch_offset, [-1])
# Initialize results.
idxs_list = []
weights_list = []
# Loop over differences di and dj to the four neighboring pixels.
for di in range(2):
for dj in range(2):
# Compute the neighboring pixel coordinates.
idxs_i = coords_floor_flattened[:, 0] + di
idxs_j = coords_floor_flattened[:, 1] + dj
# Compute the flat index into all pixels.
idxs = idx_batch_offset_flattened + idxs_i * output_width + idxs_j
# Only count valid pixels.
mask = tf.reshape(
tf.compat.v1.where(
tf.logical_and(
tf.logical_and(idxs_i >= 0, idxs_i < output_height),
tf.logical_and(idxs_j >= 0, idxs_j < output_width))), [-1])
valid_idxs = tf.gather(idxs, mask)
valid_offsets = tf.gather(coords_offset_flattened, mask)
# Compute weights according to bilinear interpolation.
weights_i = (1. - di) - (-1)**di * valid_offsets[:, 0]
weights_j = (1. - dj) - (-1)**dj * valid_offsets[:, 1]
weights = weights_i * weights_j
# Append indices and weights to the corresponding list.
idxs_list.append(valid_idxs)
weights_list.append(weights)
# Concatenate everything.
idxs = tf.concat(idxs_list, axis=0)
weights = tf.concat(weights_list, axis=0)
# Sum up weights for each pixel and reshape the result.
counts = tf.math.unsorted_segment_sum(
weights, idxs, batch_size * output_height * output_width)
count_image = tf.reshape(counts, [batch_size, output_height, output_width, 1])
if downsampling_factor > 1:
# Normalize the count image so that downsampling does not affect the counts.
count_image /= downsampling_factor**2
if resize_output:
count_image = resize(
count_image, input_height, input_width, is_flow=False)
return count_image
def compute_warps_and_occlusion(flows,
occlusion_estimation,
occ_weights=None,
occ_thresholds=None,
occ_clip_max=None,
occlusions_are_zeros=True,
occ_active=None):
"""Compute warps, valid warp masks, advection maps, and occlusion masks."""
if occ_clip_max is not None:
for key in occ_clip_max:
if key not in ['forward_collision', 'fb_abs']:
raise ValueError('occ_clip_max for this key is not supported')
warps = dict()
range_maps_high_res = dict()
range_maps_low_res = dict()
occlusion_logits = dict()
occlusion_scores = dict()
occlusion_masks = dict()
valid_warp_masks = dict()
fb_sq_diff = dict()
fb_sum_sq = dict()
for key in flows:
i, j, t = key
rev_key = (j, i, t)
warps[key] = []
range_maps_high_res[key] = []
range_maps_low_res[rev_key] = []
occlusion_masks[key] = []
valid_warp_masks[key] = []
fb_sq_diff[key] = []
fb_sum_sq[key] = []
for level in range(min(3, len(flows[key]))):
flow_ij = flows[key][level]
flow_ji = flows[rev_key][level]
# Compute warps (coordinates) and a mask for which coordinates are valid.
warps[key].append(flow_to_warp(flow_ij))
valid_warp_masks[key].append(mask_invalid(warps[key][level]))
# Compare forward and backward flow.
flow_ji_in_i = resample(flow_ji, warps[key][level])
fb_sq_diff[key].append(
tf.reduce_sum(
input_tensor=(flow_ij + flow_ji_in_i)**2, axis=-1, keepdims=True))
fb_sum_sq[key].append(
tf.reduce_sum(
input_tensor=(flow_ij**2 + flow_ji_in_i**2),
axis=-1,
keepdims=True))
if level != 0:
continue
# This initializations avoids problems in tensorflow (likely AutoGraph)
occlusion_mask = tf.zeros_like(flow_ij[Ellipsis, :1], dtype=tf.float32)
occlusion_scores['forward_collision'] = tf.zeros_like(
flow_ij[Ellipsis, :1], dtype=tf.float32)
occlusion_scores['backward_zero'] = tf.zeros_like(
flow_ij[Ellipsis, :1], dtype=tf.float32)
occlusion_scores['fb_abs'] = tf.zeros_like(
flow_ij[Ellipsis, :1], dtype=tf.float32)
if occlusion_estimation == 'none' or (
occ_active is not None and not occ_active[occlusion_estimation]):
occlusion_mask = tf.zeros_like(flow_ij[Ellipsis, :1], dtype=tf.float32)
elif occlusion_estimation == 'brox':
occlusion_mask = tf.cast(
fb_sq_diff[key][level] > 0.01 * fb_sum_sq[key][level] + 0.5,
tf.float32)
elif occlusion_estimation == 'fb_abs':
occlusion_mask = tf.cast(fb_sq_diff[key][level]**0.5 > 1.5, tf.float32)
elif occlusion_estimation == 'wang':
range_maps_low_res[rev_key].append(
compute_range_map(
flow_ji,
downsampling_factor=1,
reduce_downsampling_bias=False,
resize_output=False))
# Invert so that low values correspond to probable occlusions,
# range [0, 1].
occlusion_mask = (
1. - tf.clip_by_value(range_maps_low_res[rev_key][level], 0., 1.))
elif occlusion_estimation == 'wang4':
range_maps_low_res[rev_key].append(
compute_range_map(
flow_ji,
downsampling_factor=4,
reduce_downsampling_bias=True,
resize_output=True))
# Invert so that low values correspond to probable occlusions,
# range [0, 1].
occlusion_mask = (
1. - tf.clip_by_value(range_maps_low_res[rev_key][level], 0., 1.))
elif occlusion_estimation == 'wangthres':
range_maps_low_res[rev_key].append(
compute_range_map(
flow_ji,
downsampling_factor=1,
reduce_downsampling_bias=True,
resize_output=True))
# Invert so that low values correspond to probable occlusions,
# range [0, 1].
occlusion_mask = tf.cast(range_maps_low_res[rev_key][level] < 0.75,
tf.float32)
elif occlusion_estimation == 'wang4thres':
range_maps_low_res[rev_key].append(
compute_range_map(
flow_ji,
downsampling_factor=4,
reduce_downsampling_bias=True,
resize_output=True))
# Invert so that low values correspond to probable occlusions,
# range [0, 1].
occlusion_mask = tf.cast(range_maps_low_res[rev_key][level] < 0.75,
tf.float32)
elif occlusion_estimation == 'uflow':
# Compute occlusion from the range map of the forward flow, projected
# back into the frame of image i. The idea is if many flow vectors point
# to the same pixel, those are likely occluded.
if 'forward_collision' in occ_weights and (
occ_active is None or occ_active['forward_collision']):
range_maps_high_res[key].append(
compute_range_map(
flow_ij,
downsampling_factor=1,
reduce_downsampling_bias=True,
resize_output=True))
fwd_range_map_in_i = resample(range_maps_high_res[key][level],
warps[key][level])
# Rescale to [0, max-1].
occlusion_scores['forward_collision'] = tf.clip_by_value(
fwd_range_map_in_i, 1., occ_clip_max['forward_collision']) - 1.0
# Compute occlusion from the range map of the backward flow, which is
# already computed in frame i. Pixels that no flow vector points to are
# likely occluded.
if 'backward_zero' in occ_weights and (occ_active is None or
occ_active['backward_zero']):
range_maps_low_res[rev_key].append(
compute_range_map(
flow_ji,
downsampling_factor=4,
reduce_downsampling_bias=True,
resize_output=True))
# Invert so that low values correspond to probable occlusions,
# range [0, 1].
occlusion_scores['backward_zero'] = (
1. - tf.clip_by_value(range_maps_low_res[rev_key][level], 0., 1.))
# Compute occlusion from forward-backward consistency. If the flow
# vectors are inconsistent, this means that they are either wrong or
# occluded.
if 'fb_abs' in occ_weights and (occ_active is None or
occ_active['fb_abs']):
# Clip to [0, max].
occlusion_scores['fb_abs'] = tf.clip_by_value(
fb_sq_diff[key][level]**0.5, 0.0, occ_clip_max['fb_abs'])
occlusion_logits = tf.zeros_like(flow_ij[Ellipsis, :1], dtype=tf.float32)
for k, v in occlusion_scores.items():
occlusion_logits += (v - occ_thresholds[k]) * occ_weights[k]
occlusion_mask = tf.sigmoid(occlusion_logits)
else:
raise ValueError('Unknown value for occlusion_estimation:',
occlusion_estimation)
occlusion_masks[key].append(
1. - occlusion_mask if occlusions_are_zeros else occlusion_mask)
return warps, valid_warp_masks, range_maps_low_res, occlusion_masks, fb_sq_diff, fb_sum_sq
def apply_warps_stop_grad(sources, warps, level):
"""Apply all warps on the correct sources."""
warped = dict()
for (i, j, t) in warps:
# Only propagate gradient through the warp, not through the source.
warped[(i, j, t)] = resample(
tf.stop_gradient(sources[j]), warps[(i, j, t)][level])
return warped
def upsample(img, is_flow):
"""Double resolution of an image or flow field.
Args:
img: tf.tensor, image or flow field to be resized
is_flow: bool, flag for scaling flow accordingly
Returns:
Resized and potentially scaled image or flow field.
"""
_, height, width, _ = img.shape.as_list()
orig_dtype = img.dtype
if orig_dtype != tf.float32:
img = tf.cast(img, tf.float32)
img_resized = tf.compat.v2.image.resize(img,
(int(height * 2), int(width * 2)))
if is_flow:
# Scale flow values to be consistent with the new image size.
img_resized *= 2
if img_resized.dtype != orig_dtype:
return tf.cast(img_resized, orig_dtype)
return img_resized
def downsample(img, is_flow):
"""Halve the resolution of an image or flow field.
Args:
img: tf.tensor, image or flow field to be resized
is_flow: bool, flag for scaling flow accordingly
Returns:
Resized and potentially scaled image or flow field.
"""
_, height, width, _ = img.shape.as_list()
img_resized = tf.compat.v2.image.resize(img,
(int(height / 2), int(width / 2)))
if is_flow:
# Scale flow values to be consistent with the new image size.
img_resized /= 2
return img_resized
@tf.function
def resize(img, height, width, is_flow, mask=None):
"""Resize an image or flow field to a new resolution.
In case a mask (per pixel {0,1} flag) is passed a weighted resizing is
performed to account for missing flow entries in the sparse flow field. The
weighting is based on the resized mask, which determines the 'amount of valid
flow vectors' that contributed to each individual resized flow vector. Hence,
multiplying by the reciprocal cancels out the effect of considering non valid
flow vectors.
Args:
img: tf.tensor, image or flow field to be resized of shape [b, h, w, c]
height: int, heigh of new resolution
width: int, width of new resolution
is_flow: bool, flag for scaling flow accordingly
mask: tf.tensor, mask (optional) per pixel {0,1} flag
Returns:
Resized and potentially scaled image or flow field (and mask).
"""
def _resize(img, mask=None):
# _, orig_height, orig_width, _ = img.shape.as_list()
orig_height = tf.shape(input=img)[1]
orig_width = tf.shape(input=img)[2]
if orig_height == height and orig_width == width:
# early return if no resizing is required
if mask is not None:
return img, mask
else:
return img
if mask is not None:
# multiply with mask, to ensure non-valid locations are zero
img = tf.math.multiply(img, mask)
# resize image
img_resized = tf.compat.v2.image.resize(
img, (int(height), int(width)), antialias=True)
# resize mask (will serve as normalization weights)
mask_resized = tf.compat.v2.image.resize(
mask, (int(height), int(width)), antialias=True)
# normalize sparse flow field and mask
img_resized = tf.math.multiply(img_resized,
tf.math.reciprocal_no_nan(mask_resized))
mask_resized = tf.math.multiply(mask_resized,
tf.math.reciprocal_no_nan(mask_resized))
else:
# normal resize without anti-alaising
img_resized = tf.compat.v2.image.resize(img, (int(height), int(width)))
if is_flow:
# If image is a flow image, scale flow values to be consistent with the
# new image size.
scaling = tf.reshape([
float(height) / tf.cast(orig_height, tf.float32),
float(width) / tf.cast(orig_width, tf.float32)
], [1, 1, 1, 2])
img_resized *= scaling
if mask is not None:
return img_resized, mask_resized
return img_resized
# Apply resizing at the right shape.
shape = img.shape.as_list()
if len(shape) == 3:
if mask is not None:
img_resized, mask_resized = _resize(img[None], mask[None])
return img_resized[0], mask_resized[0]
else:
return _resize(img[None])[0]
elif len(shape) == 4:
# Input at the right shape.
return _resize(img, mask)
elif len(shape) > 4:
# Reshape input to [b, h, w, c], resize and reshape back.
img_flattened = tf.reshape(img, [-1] + shape[-3:])
if mask is not None:
mask_flattened = tf.reshape(mask, [-1] + shape[-3:])
img_resized, mask_resized = _resize(img_flattened, mask_flattened)
else:
img_resized = _resize(img_flattened)
# There appears to be some bug in tf2 tf.function
# that fails to capture the value of height / width inside the closure,
# leading the height / width undefined here. Call set_shape to make it
# defined again.
img_resized.set_shape(
(img_resized.shape[0], height, width, img_resized.shape[3]))
result_img = tf.reshape(img_resized, shape[:-3] + img_resized.shape[-3:])
if mask is not None:
mask_resized.set_shape(
(mask_resized.shape[0], height, width, mask_resized.shape[3]))
result_mask = tf.reshape(mask_resized,
shape[:-3] + mask_resized.shape[-3:])
return result_img, result_mask
return result_img
else:
raise ValueError('Cannot resize an image of shape', shape)
def random_subseq(sequence, subseq_len):
"""Select a random subsequence of a given length."""
seq_len = tf.shape(input=sequence)[0]
start_index = tf.random.uniform([],
minval=0,
maxval=seq_len - subseq_len + 1,
dtype=tf.int32)
subseq = sequence[start_index:start_index + subseq_len]
return subseq
def normalize_for_feature_metric_loss(features):
"""Normalize features for the feature-metric loss."""
normalized_features = dict()
for key, feature_map in features.items():
# Normalize feature channels to have the same absolute activations.
norm_feature_map = feature_map / (
tf.reduce_sum(
input_tensor=abs(feature_map), axis=[0, 1, 2], keepdims=True) +
1e-16)
# Normalize every pixel feature across all channels to have mean 1.
norm_feature_map /= (
tf.reduce_sum(
input_tensor=abs(norm_feature_map), axis=[-1], keepdims=True) +
1e-16)
normalized_features[key] = norm_feature_map
return normalized_features
def l1(x):
return tf.abs(x + 1e-6)
def robust_l1(x):
"""Robust L1 metric."""
return (x**2 + 0.001**2)**0.5
def abs_robust_loss(diff, eps=0.01, q=0.4):
"""The so-called robust loss used by DDFlow."""
return tf.pow((tf.abs(diff) + eps), q)
def image_grads(image_batch, stride=1):
image_batch_gh = image_batch[:, stride:] - image_batch[:, :-stride]
image_batch_gw = image_batch[:, :, stride:] - image_batch[:, :, :-stride]
return image_batch_gh, image_batch_gw
def image_averages(image_batch):
image_batch_ah = (image_batch[:, 1:] + image_batch[:, :-1]) / 2.
image_batch_aw = (image_batch[:, :, 1:] + image_batch[:, :, :-1]) / 2
return image_batch_ah, image_batch_aw
def get_distance_metric_fns(distance_metrics):
"""Returns a dictionary of distance metrics."""
output = {}
for key, distance_metric in distance_metrics.items():
if distance_metric == 'l1':
output[key] = l1
elif distance_metric == 'robust_l1':
output[key] = robust_l1
elif distance_metric == 'ddflow':
output[key] = abs_robust_loss
else:
raise ValueError('Unknown loss function')
return output
def compute_loss(
weights,
images,
flows,
warps,
valid_warp_masks,
not_occluded_masks,
fb_sq_diff,
fb_sum_sq,
warped_images,
only_forward=False,
selfsup_transform_fns=None,
fb_sigma_teacher=0.003,
fb_sigma_student=0.03,
plot_dir=None,
distance_metrics=None,
smoothness_edge_weighting='gaussian',
stop_gradient_mask=True,
selfsup_mask='gaussian',
ground_truth_occlusions=None,
smoothness_at_level=2
):
"""Compute UFlow losses."""
if distance_metrics is None:
distance_metrics = {
'photo': 'robust_l1',
'census': 'ddflow',
}
distance_metric_fns = get_distance_metric_fns(distance_metrics)
losses = dict()
for key in weights:
if key not in ['edge_constant']:
losses[key] = 0.0
compute_loss_for_these_flows = ['augmented-student']
# Count number of non self-sup pairs, for which we will apply the losses.
num_pairs = sum(
[1.0 for (i, j, c) in warps if c in compute_loss_for_these_flows])
# Iterate over image pairs.
for key in warps:
i, j, c = key
if c not in compute_loss_for_these_flows or (only_forward and i > j):
continue
if ground_truth_occlusions is None:
if stop_gradient_mask:
mask_level0 = tf.stop_gradient(not_occluded_masks[key][0] *
valid_warp_masks[key][0])
else:
mask_level0 = not_occluded_masks[key][0] * valid_warp_masks[key][0]
else:
# For using ground truth mask
if i > j:
continue
ground_truth_occlusions = 1.0 - tf.cast(ground_truth_occlusions,
tf.float32)
mask_level0 = tf.stop_gradient(ground_truth_occlusions *
valid_warp_masks[key][0])
height, width = valid_warp_masks[key][1].get_shape().as_list()[-3:-1]
if 'photo' in weights:
error = distance_metric_fns['photo'](images[i] - warped_images[key])
losses['photo'] += (
weights['photo'] * tf.reduce_sum(input_tensor=mask_level0 * error) /
(tf.reduce_sum(input_tensor=mask_level0) + 1e-16) / num_pairs)
if 'smooth2' in weights or 'smooth1' in weights:
edge_constant = 0.0
if 'edge_constant' in weights:
edge_constant = weights['edge_constant']
abs_fn = None
if smoothness_edge_weighting == 'gaussian':
abs_fn = lambda x: x**2
elif smoothness_edge_weighting == 'exponential':
abs_fn = abs
# Compute image gradients and sum them up to match the receptive field
# of the flow gradients, which are computed at 1/4 resolution.
images_level0 = images[i]
height, width = images_level0.shape.as_list()[-3:-1]
# Resize two times for a smoother result.
images_level1 = resize(
images_level0, int(height) // 2, int(width) // 2, is_flow=False)
images_level2 = resize(
images_level1, int(height) // 4, int(width) // 4, is_flow=False)
images_at_level = [images_level0, images_level1, images_level2]
if 'smooth1' in weights:
img_gx, img_gy = image_grads(images_at_level[smoothness_at_level])
weights_x = tf.exp(-tf.reduce_mean(
input_tensor=(abs_fn(edge_constant * img_gx)),
axis=-1,
keepdims=True))
weights_y = tf.exp(-tf.reduce_mean(
input_tensor=(abs_fn(edge_constant * img_gy)),
axis=-1,
keepdims=True))
# Compute second derivatives of the predicted smoothness.
flow_gx, flow_gy = image_grads(flows[key][smoothness_at_level])
# Compute weighted smoothness
losses['smooth1'] += (
weights['smooth1'] *
(tf.reduce_mean(input_tensor=weights_x * robust_l1(flow_gx)) +
tf.reduce_mean(input_tensor=weights_y * robust_l1(flow_gy))) / 2. /
num_pairs)
if plot_dir is not None:
uflow_plotting.plot_smoothness(key, images, weights_x, weights_y,
robust_l1(flow_gx), robust_l1(flow_gy),
flows, plot_dir)
if 'smooth2' in weights:
img_gx, img_gy = image_grads(
images_at_level[smoothness_at_level], stride=2)
weights_xx = tf.exp(-tf.reduce_mean(
input_tensor=(abs_fn(edge_constant * img_gx)),
axis=-1,
keepdims=True))
weights_yy = tf.exp(-tf.reduce_mean(
input_tensor=(abs_fn(edge_constant * img_gy)),
axis=-1,
keepdims=True))
# Compute second derivatives of the predicted smoothness.
flow_gx, flow_gy = image_grads(flows[key][smoothness_at_level])
flow_gxx, unused_flow_gxy = image_grads(flow_gx)
unused_flow_gyx, flow_gyy = image_grads(flow_gy)
# Compute weighted smoothness
losses['smooth2'] += (
weights['smooth2'] *
(tf.reduce_mean(input_tensor=weights_xx * robust_l1(flow_gxx)) +
tf.reduce_mean(input_tensor=weights_yy * robust_l1(flow_gyy))) /
2. / num_pairs)
if plot_dir is not None:
uflow_plotting.plot_smoothness(key, images, weights_xx, weights_yy,
robust_l1(flow_gxx),
robust_l1(flow_gyy), flows, plot_dir)
if 'ssim' in weights:
ssim_error, avg_weight = weighted_ssim(warped_images[key], images[i],
tf.squeeze(mask_level0, axis=-1))
losses['ssim'] += weights['ssim'] * (
tf.reduce_sum(input_tensor=ssim_error * avg_weight) /
(tf.reduce_sum(input_tensor=avg_weight) + 1e-16) / num_pairs)
if 'census' in weights:
losses['census'] += weights['census'] * census_loss(
images[i],
warped_images[key],
mask_level0,
distance_metric_fn=distance_metric_fns['census']) / num_pairs
if 'selfsup' in weights:
assert selfsup_transform_fns is not None
_, h, w, _ = flows[key][2].shape.as_list()
teacher_flow = flows[(i, j, 'original-teacher')][2]
student_flow = flows[(i, j, 'transformed-student')][2]
teacher_flow = selfsup_transform_fns[2](
teacher_flow, i_or_ij=(i, j), is_flow=True)
if selfsup_mask == 'gaussian':
student_fb_consistency = tf.exp(
-fb_sq_diff[(i, j, 'transformed-student')][2] /
(fb_sigma_student**2 * (h**2 + w**2)))
teacher_fb_consistency = tf.exp(
-fb_sq_diff[(i, j, 'original-teacher')][2] / (fb_sigma_teacher**2 *
(h**2 + w**2)))
elif selfsup_mask == 'advection':
student_fb_consistency = not_occluded_masks[(i, j,
'transformed-student')][2]
teacher_fb_consistency = not_occluded_masks[(i, j,
'original-teacher')][2]
elif selfsup_mask == 'ddflow':
threshold_student = 0.01 * (fb_sum_sq[
(i, j, 'transformed-student')][2]) + 0.5
threshold_teacher = 0.01 * (fb_sum_sq[
(i, j, 'original-teacher')][2]) + 0.5
student_fb_consistency = tf.cast(
fb_sq_diff[(i, j, 'transformed-student')][2] < threshold_student,
tf.float32)
teacher_fb_consistency = tf.cast(
fb_sq_diff[(i, j, 'original-teacher')][2] < threshold_teacher,
tf.float32)
else:
raise ValueError('Unknown selfsup_mask', selfsup_mask)
student_mask = 1. - (
student_fb_consistency *
valid_warp_masks[(i, j, 'transformed-student')][2])
teacher_mask = (
teacher_fb_consistency *
valid_warp_masks[(i, j, 'original-teacher')][2])
teacher_mask = selfsup_transform_fns[2](
teacher_mask, i_or_ij=(i, j), is_flow=False)
error = robust_l1(tf.stop_gradient(teacher_flow) - student_flow)
mask = tf.stop_gradient(teacher_mask * student_mask)
losses['selfsup'] += (
weights['selfsup'] * tf.reduce_sum(input_tensor=mask * error) /
(tf.reduce_sum(input_tensor=tf.ones_like(mask)) + 1e-16) / num_pairs)
if plot_dir is not None:
uflow_plotting.plot_selfsup(key, images, flows, teacher_flow,
student_flow, error, teacher_mask,
student_mask, mask, selfsup_transform_fns,
plot_dir)
losses['total'] = sum(losses.values())
return losses
def supervised_loss(weights, ground_truth_flow, ground_truth_valid,
predicted_flows):
"""Returns a supervised l1 loss when ground-truth flow is provided."""
losses = {}
# ground truth flow is given from image 0 to image 1
predicted_flow = predicted_flows[(0, 1, 'augmented')][0]
# resize flow to match ground truth (only changes resolution if ground truth
# flow was not resized during loading (resize_gt_flow=False)
_, height, width, _ = ground_truth_flow.get_shape().as_list()
predicted_flow = resize(predicted_flow, height, width, is_flow=True)
# compute error/loss metric
error = robust_l1(ground_truth_flow - predicted_flow)
if ground_truth_valid is None:
b, h, w, _ = ground_truth_flow.shape.as_list()
ground_truth_valid = tf.ones((b, h, w, 1), tf.float32)
losses['supervision'] = (
weights['supervision'] *
tf.reduce_sum(input_tensor=ground_truth_valid * error) /
(tf.reduce_sum(input_tensor=ground_truth_valid) + 1e-16))
losses['total'] = losses['supervision']
return losses
def compute_features_and_flow(
feature_model,
flow_model,
batch,
batch_without_aug,
training,
build_selfsup_transformations=None,
teacher_feature_model=None,
teacher_flow_model=None,
teacher_image_version='original',
):
"""Compute features and flow for an image batch.
Args:
feature_model: A model to compute features for flow.
flow_model: A model to compute flow.
batch: A tf.tensor of shape [b, seq, h, w, c] holding a batch of triplets.
batch_without_aug: Batch without photometric augmentation
training: bool that tells the model to use training or inference code.
build_selfsup_transformations: A function which, when called with images
and flows, populates the images and flows dictionary with student images
and modified teacher flows corresponding to the student images.
teacher_feature_model: None or instance of of feature model. If None, will
not compute features and images for teacher distillation.
teacher_flow_model: None or instance of flow model. If None, will not
compute features and images for teacher distillation.
teacher_image_version: str, either 'original' or 'augmented'
Returns:
A tuple consisting of the images, the extracted features, the estimated
flows, and the upsampled refined flows.
"""
images = dict()
flows = dict()
features = dict()
seq_len = int(batch.shape[1])
perform_selfsup = (
training and teacher_feature_model is not None and
teacher_flow_model is not None and
build_selfsup_transformations is not None)
if perform_selfsup:
selfsup_transform_fns = build_selfsup_transformations()
else:
selfsup_transform_fns = None
for i in range(seq_len):
# Populate teacher images with native, unmodified images.
images[(i, 'original')] = batch_without_aug[:, i]
images[(i, 'augmented')] = batch[:, i]
if perform_selfsup:
images[(i, 'transformed')] = selfsup_transform_fns[0](
images[(i, 'augmented')], i_or_ij=i, is_flow=False)
for key, image in images.items():
i, image_version = key
# if perform_selfsup and image_version == 'original':
if perform_selfsup and image_version == teacher_image_version:
features[(i, 'original-teacher')] = teacher_feature_model(
image, split_features_by_sample=False, training=False)
features[(i, image_version + '-student')] = feature_model(
image, split_features_by_sample=False, training=training)
# Only use original images and features computed on those for computing
# photometric losses down the road.
images = {i: images[(i, 'original')] for i in range(seq_len)}
# Compute flow for all pairs of consecutive images that have the same (or no)
# transformation applied to them, i.e. that have the same t.
# pylint:disable=dict-iter-missing-items
for (i, ti) in features:
for (j, tj) in features:
if (i + 1 == j or i - 1 == j) and ti == tj:
t = ti
key = (i, j, t)
# No need to compute the flow for student applied to the original
# image. We just need the features from that for the photometric loss.
if t in ['augmented-student', 'transformed-student']:
# Compute flow from i to j, defined in image i.
flow = flow_model(
features[(i, t)], features[(j, t)], training=training)
elif t in ['original-teacher']:
flow = teacher_flow_model(
features[(i, t)], features[(j, t)], training=False)
else:
continue
# Keep flows at levels 0-2.
flow_level2 = flow[0]
flow_level1 = upsample(flow_level2, is_flow=True)
flow_level0 = upsample(flow_level1, is_flow=True)
flows[key] = [flow_level0, flow_level1, flow_level2]
return flows, selfsup_transform_fns
def compute_flow_for_supervised_loss(feature_model, flow_model, batch,
training):
"""Compute features and flow for an image batch.
Args: