@@ -71,28 +71,33 @@ def compute_flow(model, image1, image2, weights=None):
71
71
72
72
image_size = image1 .shape [1 :]
73
73
74
+ image1 , image2 = image1 [None ].cuda (), image2 [None ].cuda ()
75
+
74
76
hws = compute_grid_indices (image_size )
75
- if weights is None :
76
- weights = compute_weight (hws , image_size , sigma = 0.05 )
77
+ if weights is None : # no tile
78
+ padder = InputPadder (image1 .shape )
79
+ image1 , image2 = padder .pad (image1 , image2 )
77
80
78
- image1 , image2 = image1 [ None ]. cuda () , image2 [ None ]. cuda ( )
81
+ flow_pre , _ = model ( image1 , image2 )
79
82
80
- flows = 0
81
- flow_count = 0
83
+ flow_pre = padder .unpad (flow_pre )
84
+ flow = flow_pre [0 ].permute (1 , 2 , 0 ).cpu ().numpy ()
85
+ else : # tile
86
+ flows = 0
87
+ flow_count = 0
82
88
83
- for idx , (h , w ) in enumerate (hws ):
84
- image1_tile = image1 [:, :, h :h + TRAIN_SIZE [0 ], w :w + TRAIN_SIZE [1 ]]
85
- image2_tile = image2 [:, :, h :h + TRAIN_SIZE [0 ], w :w + TRAIN_SIZE [1 ]]
86
- flow_pre , _ = model (image1_tile , image2_tile )
87
- padding = (w , image_size [1 ]- w - TRAIN_SIZE [1 ], h , image_size [0 ]- h - TRAIN_SIZE [0 ], 0 , 0 )
88
- flows += F .pad (flow_pre * weights [idx ], padding )
89
- # flow_count += F.pad(weights, padding)
90
- flow_count += F .pad (weights [idx ], padding )
89
+ for idx , (h , w ) in enumerate (hws ):
90
+ image1_tile = image1 [:, :, h :h + TRAIN_SIZE [0 ], w :w + TRAIN_SIZE [1 ]]
91
+ image2_tile = image2 [:, :, h :h + TRAIN_SIZE [0 ], w :w + TRAIN_SIZE [1 ]]
92
+ flow_pre , _ = model (image1_tile , image2_tile )
93
+ padding = (w , image_size [1 ]- w - TRAIN_SIZE [1 ], h , image_size [0 ]- h - TRAIN_SIZE [0 ], 0 , 0 )
94
+ flows += F .pad (flow_pre * weights [idx ], padding )
95
+ flow_count += F .pad (weights [idx ], padding )
91
96
92
- flow_pre = flows / flow_count
93
- flow = flow_pre [0 ].permute (1 , 2 , 0 ).cpu ().numpy ()
97
+ flow_pre = flows / flow_count
98
+ flow = flow_pre [0 ].permute (1 , 2 , 0 ).cpu ().numpy ()
94
99
95
- return flow , weights
100
+ return flow
96
101
97
102
def compute_adaptive_image_size (image_size ):
98
103
target_size = TRAIN_SIZE
@@ -153,7 +158,7 @@ def visualize_flow(root_dir, viz_root_dir, model, img_pairs, keep_size):
153
158
print (f"processing { fn1 } , { fn2 } ..." )
154
159
155
160
image1 , image2 , viz_fn = prepare_image (root_dir , viz_root_dir , fn1 , fn2 , keep_size )
156
- flow , weights = compute_flow (model , image1 , image2 , weights )
161
+ flow = compute_flow (model , image1 , image2 , weights )
157
162
flow_img = flow_viz .flow_to_image (flow )
158
163
cv2 .imwrite (viz_fn , flow_img [:, :, [2 ,1 ,0 ]])
159
164
0 commit comments