-
Notifications
You must be signed in to change notification settings - Fork 205
/
util.py
50 lines (40 loc) · 1.4 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
import os
import numpy as np
import shutil
import torch
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)