forked from yihuacheng/GazeTR
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ctools.py
61 lines (41 loc) · 1.34 KB
/
ctools.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
import numpy as np
import sys
import time
import os
import json
from easydict import EasyDict as edict
class TimeCounter:
# Create an time counter.
# To count the rest time.
# Input the total times.
def __init__(self, total):
self.total = total
self.cur = 0
self.begin = time.time()
def step(self):
end = time.time()
self.cur += 1
used = (end - self.begin)/self.cur
rest = self.total - self.cur
return np.max(rest * used, 0)
def readfolder(data, specific=None, reverse=False):
""""
Traverse the folder 'data.label' and read data from all files in the folder.
Specific is a list, specify the num of extracted file.
When reverse is True, read the files which num is not in specific.
"""
folders = os.listdir(data.label)
folders.sort()
folder = folders
if specific is not None:
if reverse:
num = np.arange(len(folders))
specific = list(filter(lambda x: x not in specific, num))
folder = [folders[i] for i in specific]
data.label = [os.path.join(data.label, j) for j in folder]
return data, folders
def DictDumps(content):
return json.dumps(content, ensure_ascii=False, indent=4)
def GetLR(optimizer):
LR = optimizer.state_dict()['param_groups'][0]['lr']
return LR