-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathutils.py
80 lines (59 loc) · 1.87 KB
/
utils.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
# ====================================================
# @Author : Xiao Junbin
# @Email : junbin@comp.nus.edu.sg
# @File : utils.py
# ====================================================
import json
import os
import os.path as osp
import shutil
import numpy as np
import pickle as pkl
def clip_gradient(optimizer, grad_clip):
"""
Clips gradients computed during backpropagation to avoid explosion of gradients.
:param optimizer: optimizer with the gradients to be clipped
:param grad_clip: clip value
"""
for group in optimizer.param_groups:
for param in group['params']:
if param.grad is not None:
param.grad.data.clamp_(-grad_clip, grad_clip)
def save_results(save_name, data):
print('Save to {}'.format(save_name))
path = osp.dirname(save_name)
if not osp.exists(path):
os.makedirs(path)
with open(save_name, 'w') as fp:
json.dump(data, fp)
def delete(vname):
if vname != '':
frame_dir = '../ground_data/vidor/frames/'
print('Clean up {}'.format(vname))
shutil.rmtree(osp.join(frame_dir, vname))
def sort_bbox(bboxes, width, height):
"""
sort bbox according to the top-left to bottom-right order
:param bboxes:
:return:
"""
x_c = (bboxes[:, 2] - bboxes[:, 0]) / 2
y_c = (bboxes[:, 3] - bboxes[:, 1]) / 2
points = []
for x, y in zip(x_c, y_c):
points.append((y-1)*width+x)
index = np.argsort(points)
return index
def pkload(file):
data = None
if osp.exists(file) and osp.getsize(file) > 0:
with open(file, 'rb') as fp:
data = pkl.load(fp)
# print('{} does not exist'.format(file))
return data
def pkdump(data, file):
dirname = osp.dirname(file)
if not osp.exists(dirname):
os.makedirs(dirname)
with open(file, 'wb') as fp:
pkl.dump(data, fp)