-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathutil.py
83 lines (69 loc) · 2.59 KB
/
util.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
import os
import numpy as np
import shutil
import torch
from glob import glob
from scipy.interpolate import interp2d
from skimage import io
def save_checkpoint(state, is_best, save_path, filename='checkpoint.pth.tar'):
torch.save(state, os.path.join(save_path,filename))
if is_best:
shutil.copyfile(os.path.join(save_path,filename), os.path.join(save_path,'model_best.pth.tar'))
class AverageMeter(object):
"""Computes and stores the average and current value"""
def __init__(self):
self.reset()
def reset(self):
self.val = 0
self.avg = 0
self.sum = 0
self.count = 0
def update(self, val, n=1):
self.val = val
self.sum += val * n
self.count += n
self.avg = self.sum / self.count
def __repr__(self):
return '{:.3f} ({:.3f})'.format(self.val, self.avg)
def flow2rgb(flow_map, max_value):
flow_map_np = flow_map.detach().cpu().numpy()
_, h, w = flow_map_np.shape
flow_map_np[:,(flow_map_np[0] == 0) & (flow_map_np[1] == 0)] = float('nan')
rgb_map = np.ones((3,h,w)).astype(np.float32)
if max_value is not None:
normalized_flow_map = flow_map_np / max_value
else:
normalized_flow_map = flow_map_np / (np.abs(flow_map_np).max())
rgb_map[0] += normalized_flow_map[0]
rgb_map[1] -= 0.5*(normalized_flow_map[0] + normalized_flow_map[1])
rgb_map[2] += normalized_flow_map[1]
return rgb_map.clip(0,1)
def create_pairs(img_path, type_object = 'swan', ext='bmp'):
"""
The objective is to create sequential pairs (1-2, 2-3, ...) from a set of images in a folder
output = [1-2,2-3,...]
"""
files = sorted(glob(os.path.join(img_path,type_object + '*.' + ext)))
img_pairs_final = []
for i in range(len(files)-1):
img1 = files[i]
img2 = files[i+1]
img_pairs = [img1, img2]
img_pairs_final.append(img_pairs)
return img_pairs_final
def concatenation(unary_flow, to_ref_flow):
flow = np.zeros((unary_flow.shape[0],unary_flow.shape[1],2), dtype=np.float32)
x0 = np.arange(0, unary_flow.shape[0])
y0 = np.arange(0, unary_flow.shape[1])
xx, yy = np.meshgrid(x0, y0)
z = to_ref_flow[xx,yy,1]
fx = interp2d(x0,y0,z,kind='cubic')
z = to_ref_flow[xx,yy,0]
fy = interp2d(x0,y0,z,kind='cubic')
for x in range(unary_flow.shape[0]):
for y in range(unary_flow.shape[1]):
flow_x = fx(x+unary_flow[x,y,1], y+unary_flow[x,y,0])
flow_y = fy(x+unary_flow[x,y,1], y+unary_flow[x,y,0])
flow[x,y,1] = unary_flow[x,y,1] + flow_x
flow[x,y,0] = unary_flow[x,y,0] + flow_y
return flow