forked from zacjiang/GMA
-
Notifications
You must be signed in to change notification settings - Fork 0
/
evaluate_single.py
92 lines (66 loc) · 2.7 KB
/
evaluate_single.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
import sys
sys.path.append('core')
import argparse
import os
import cv2
import glob
import numpy as np
import torch
from PIL import Image
import imageio
import matplotlib.pyplot as plt
from network import RAFTGMA
from utils import flow_viz
from utils.utils import InputPadder
import os
DEVICE = 'cuda'
def load_image(imfile):
img = np.array(Image.open(imfile)).astype(np.uint8)
img = torch.from_numpy(img).permute(2, 0, 1).float()
return img[None].to(DEVICE)
def viz(img, flo, flow_dir):
img = img[0].permute(1, 2, 0).cpu().numpy()
flo = flo[0].permute(1, 2, 0).cpu().numpy()
# map flow to rgb image
flo = flow_viz.flow_to_image(flo)
imageio.imwrite(os.path.join(flow_dir, 'flo.png'), flo)
print(f"Saving optical flow visualisation at {os.path.join(flow_dir, 'flo.png')}")
def normalize(x):
return x / (x.max() - x.min())
def demo(args):
model = torch.nn.DataParallel(RAFTGMA(args))
model.load_state_dict(torch.load(args.model))
print(f"Loaded checkpoint at {args.model}")
model = model.module
model.to(DEVICE)
model.eval()
flow_dir = os.path.join(args.path, args.model_name)
if not os.path.exists(flow_dir):
os.makedirs(flow_dir)
with torch.no_grad():
images = glob.glob(os.path.join(args.path, '*.png')) + \
glob.glob(os.path.join(args.path, '*.jpg'))
images = sorted(images)
for imfile1, imfile2 in zip(images[:-1], images[1:]):
image1 = load_image(imfile1)
image2 = load_image(imfile2)
print(f"Reading in images at {imfile1} and {imfile2}")
padder = InputPadder(image1.shape)
image1, image2 = padder.pad(image1, image2)
flow_low, flow_up = model(image1, image2, iters=12, test_mode=True)
print(f"Estimating optical flow...")
viz(image1, flow_up, flow_dir)
if __name__ == '__main__':
parser = argparse.ArgumentParser()
parser.add_argument('--model', help="restore checkpoint")
parser.add_argument('--model_name', help="define model name", default="GMA")
parser.add_argument('--path', help="dataset for evaluation")
parser.add_argument('--num_heads', default=1, type=int,
help='number of heads in attention and aggregation')
parser.add_argument('--position_only', default=False, action='store_true',
help='only use position-wise attention')
parser.add_argument('--position_and_content', default=False, action='store_true',
help='use position and content-wise attention')
parser.add_argument('--mixed_precision', action='store_true', help='use mixed precision')
args = parser.parse_args()
demo(args)