forked from princeton-vl/SEA-RAFT
-
Notifications
You must be signed in to change notification settings - Fork 0
/
demo.py
224 lines (197 loc) · 8.43 KB
/
demo.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
import sys
sys.path.append('core')
import argparse
import os
import cv2
import math
import numpy as np
import torch
import torch.nn as nn
import torch.nn.functional as F
import torch.utils.data as data
from config.parser import parse_args
import datasets
from raft import RAFT
from utils.flow_viz import flow_to_image
from utils.utils import load_ckpt
def create_color_bar(height, width, color_map):
"""
Create a color bar image using a specified color map.
:param height: The height of the color bar.
:param width: The width of the color bar.
:param color_map: The OpenCV colormap to use.
:return: A color bar image.
"""
# Generate a linear gradient
gradient = np.linspace(0, 255, width, dtype=np.uint8)
gradient = np.repeat(gradient[np.newaxis, :], height, axis=0)
# Apply the colormap
color_bar = cv2.applyColorMap(gradient, color_map)
return color_bar
def add_color_bar_to_image(image, color_bar, orientation='vertical'):
"""
Add a color bar to an image.
:param image: The original image.
:param color_bar: The color bar to add.
:param orientation: 'vertical' or 'horizontal'.
:return: Combined image with the color bar.
"""
if orientation == 'vertical':
return cv2.vconcat([image, color_bar])
else:
return cv2.hconcat([image, color_bar])
def vis_heatmap(name, image, heatmap):
# theta = 0.01
# print(heatmap.max(), heatmap.min(), heatmap.mean())
heatmap = heatmap[:, :, 0]
heatmap = (heatmap - heatmap.min()) / (heatmap.max() - heatmap.min())
# heatmap = heatmap > 0.01
heatmap = (heatmap * 255).astype(np.uint8)
colored_heatmap = cv2.applyColorMap(heatmap, cv2.COLORMAP_JET)
overlay = image * 0.3 + colored_heatmap * 0.7
# Create a color bar
height, width = image.shape[:2]
color_bar = create_color_bar(50, width, cv2.COLORMAP_JET) # Adjust the height and colormap as needed
# Add the color bar to the image
overlay = overlay.astype(np.uint8)
combined_image = add_color_bar_to_image(overlay, color_bar, 'vertical')
cv2.imwrite(name, cv2.cvtColor(combined_image, cv2.COLOR_RGB2BGR))
def get_heatmap(info, args):
raw_b = info[:, 2:]
log_b = torch.zeros_like(raw_b)
weight = info[:, :2].softmax(dim=1)
log_b[:, 0] = torch.clamp(raw_b[:, 0], min=0, max=args.var_max)
log_b[:, 1] = torch.clamp(raw_b[:, 1], min=args.var_min, max=0)
heatmap = (log_b * weight).sum(dim=1, keepdim=True)
return heatmap
def forward_flow(args, model, image1, image2):
output = model(image1, image2, iters=args.iters, test_mode=True)
flow_final = output['flow'][-1]
info_final = output['info'][-1]
return flow_final, info_final
def calc_flow(args, model, image1, image2):
img1 = F.interpolate(image1, scale_factor=2 ** args.scale, mode='bilinear', align_corners=False)
img2 = F.interpolate(image2, scale_factor=2 ** args.scale, mode='bilinear', align_corners=False)
H, W = img1.shape[2:]
flow, info = forward_flow(args, model, img1, img2)
flow_down = F.interpolate(flow, scale_factor=0.5 ** args.scale, mode='bilinear', align_corners=False) * (0.5 ** args.scale)
info_down = F.interpolate(info, scale_factor=0.5 ** args.scale, mode='area')
return flow_down, info_down
@torch.no_grad()
def demo_data(name, args, model, image1, image2, flow_gt):
path = f"demo/{name}/"
os.system(f"mkdir -p {path}")
H, W = image1.shape[2:]
cv2.imwrite(f"{path}image1.jpg", cv2.cvtColor(image1[0].permute(1, 2, 0).cpu().numpy(), cv2.COLOR_RGB2BGR))
cv2.imwrite(f"{path}image2.jpg", cv2.cvtColor(image2[0].permute(1, 2, 0).cpu().numpy(), cv2.COLOR_RGB2BGR))
flow_gt_vis = flow_to_image(flow_gt[0].permute(1, 2, 0).cpu().numpy(), convert_to_bgr=True)
cv2.imwrite(f"{path}gt.jpg", flow_gt_vis)
flow, info = calc_flow(args, model, image1, image2)
flow_vis = flow_to_image(flow[0].permute(1, 2, 0).cpu().numpy(), convert_to_bgr=True)
cv2.imwrite(f"{path}flow_final.jpg", flow_vis)
diff = flow_gt - flow
diff_vis = flow_to_image(diff[0].permute(1, 2, 0).cpu().numpy(), convert_to_bgr=True)
cv2.imwrite(f"{path}error_final.jpg", diff_vis)
heatmap = get_heatmap(info, args)
vis_heatmap(f"{path}heatmap_final.jpg", image1[0].permute(1, 2, 0).cpu().numpy(), heatmap[0].permute(1, 2, 0).cpu().numpy())
epe = torch.sum((flow - flow_gt)**2, dim=1).sqrt()
print(f"EPE: {epe.mean().cpu().item()}")
@torch.no_grad()
def demo_chairs(model, args, device=torch.device('cuda')):
dataset = datasets.FlyingChairs(split='training')
image1, image2, flow_gt, _ = dataset[1345]
image1 = image1[None].to(device)
image2 = image2[None].to(device)
flow_gt = flow_gt[None].to(device)
demo_data('chairs', args, model, image1, image2, flow_gt)
def demo_sintel(model, args, device=torch.device('cuda')):
dstype = 'final'
dataset = datasets.MpiSintel(split='training', dstype=dstype)
image1, image2, flow_gt, _ = dataset[100]
image1 = image1[None].to(device)
image2 = image2[None].to(device)
flow_gt = flow_gt[None].to(device)
demo_data('sintel', args, model, image1, image2, flow_gt)
@torch.no_grad()
def demo_spring(model, args, device=torch.device('cuda'), split='train'):
dataset = datasets.SpringFlowDataset(split=split)
idx = 19198
if split == 'train' or split == 'val':
image1, image2, flow_gt, _ = dataset[idx]
else:
image1, image2, _ = dataset[idx]
h, w = image1.shape[1:]
flow_gt = torch.zeros((2, h, w))
image1 = image1[None].to(device)
image2 = image2[None].to(device)
flow_gt = flow_gt[None].to(device)
demo_data('spring', args, model, image1, image2, flow_gt)
@torch.no_grad()
def demo_tartanair(model, args, device=torch.device('cuda')):
dataset = datasets.TartanAir()
image1, image2, flow_gt, _ = dataset[1070]
image1 = image1[None].to(device)
image2 = image2[None].to(device)
flow_gt = flow_gt[None].to(device)
demo_data('tartanair', args, model, image1, image2, flow_gt)
@torch.no_grad()
def demo_infinigen(model, args, device=torch.device('cuda')):
dataset = datasets.Infinigen()
image1, image2, flow_gt, _ = dataset[1000]
image1 = image1[None].to(device)
image2 = image2[None].to(device)
flow_gt = flow_gt[None].to(device)
demo_data('infinigen', args, model, image1, image2, flow_gt)
@torch.no_grad()
def demo_hd1k(model, args, device=torch.device('cuda')):
dataset = datasets.HD1K()
print(len(dataset))
image1, image2, flow_gt, _ = dataset[0]
image1 = image1[None].to(device)
image2 = image2[None].to(device)
flow_gt = flow_gt[None].to(device)
demo_data('hd1k', args, model, image1, image2, flow_gt)
@torch.no_grad()
def demo_middlebury(model, args, device=torch.device('cuda')):
dataset = datasets.Middlebury()
image1, image2, flow_gt, _ = dataset[3]
image1 = image1[None].to(device)
image2 = image2[None].to(device)
flow_gt = flow_gt[None].to(device)
demo_data('middlebury', args, model, image1, image2, flow_gt)
@torch.no_grad()
def demo_custom(model, args, device=torch.device('cuda')):
image1 = cv2.imread('../custom_images/0011.png')
image1 = cv2.cvtColor(image1, cv2.COLOR_BGR2RGB)
image2 = cv2.imread('../custom_images/0012.png')
image2 = cv2.cvtColor(image2, cv2.COLOR_BGR2RGB)
image1 = torch.tensor(image1, dtype=torch.float32).permute(2, 0, 1)
image2 = torch.tensor(image2, dtype=torch.float32).permute(2, 0, 1)
H, W = image1.shape[1:]
flow_gt = torch.zeros([2, H, W], device=device)
image1 = image1[None].to(device)
image2 = image2[None].to(device)
flow_gt = flow_gt[None].to(device)
demo_data('custom_downsample', args, model, image1, image2, flow_gt)
def main():
parser = argparse.ArgumentParser()
parser.add_argument('--cfg', help='experiment configure file name', required=True, type=str)
parser.add_argument('--model', help='checkpoint path', required=True, type=str)
args = parse_args(parser)
model = RAFT(args)
load_ckpt(model, args.model)
model = model.cuda()
model.eval()
# demo_custom(model, args)
if args.dataset == 'chairs':
demo_chairs(model, args)
elif args.dataset == 'things' or args.dataset == 'sintel':
demo_sintel(model, args)
elif args.dataset == 'spring':
demo_spring(model, args, split='train')
elif args.dataset == 'hd1k':
demo_hd1k(model, args)
elif args.dataset == 'middlebury':
demo_middlebury(model, args)
if __name__ == '__main__':
main()