-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathutils.py
63 lines (49 loc) · 1.68 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
from __future__ import unicode_literals, print_function, division
from io import open
import os
import time
import math
import numpy as np
import torch.nn as nn
class MyDataParallel(nn.DataParallel):
"""
Wrapper class for nn.DataParallel
This class provides direct accesses to attributes for the original module
"""
def __init__(self, model, **kargs):
super(MyDataParallel, self).__init__(model, **kargs)
def __getattr__(self, name):
try:
return super().__getattr__(name)
except AttributeError:
return getattr(self.module, name) # Direct access to attributes belonging to the original module
class TimeChecker():
def __init__(self):
self.start_time_list = dict()
self.time_accum = dict()
self.start_time_list['default'] = 0
self.time_accum['default'] = 0
#self.startTime = 0
self.epoch = 0
def reset(self, k = 'default'):
self.start_time_list[k] = 0
self.time_accum[k] = 0
def start(self, k = 'default'):
self.start_time_list[k] = time.time()
def elapsed(self, k = 'default'):
interval_time = time.time() - self.start_time_list[k]
self.time_accum[k] = self.time_accum.get(k, 0) + interval_time
return time_format(interval_time)
def get_time(self, k = 'default'):
return time_format( self.time_accum.get(k, 0) )
def epoch_count(self):
self.epoch += 1
def time_format(s):
h = math.floor(s / 3600)
m = math.floor((s-3600*h) / 60)
s = s - h*3600 - m*60
return '%dh %dm %ds' % (h, m, s)
def timeSince(since):
now = time.time()
s = now - since
return '%s' % (time_format(s))