-
Notifications
You must be signed in to change notification settings - Fork 739
/
tools.py
executable file
·147 lines (116 loc) · 5.32 KB
/
tools.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
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
# freda (todo) :
import os, time, sys, math
import subprocess, shutil
from os.path import *
import numpy as np
from inspect import isclass
from pytz import timezone
from datetime import datetime
import inspect
import torch
def datestr():
pacific = timezone('US/Pacific')
now = datetime.now(pacific)
return '{}{:02}{:02}_{:02}{:02}'.format(now.year, now.month, now.day, now.hour, now.minute)
def module_to_dict(module, exclude=[]):
return dict([(x, getattr(module, x)) for x in dir(module)
if isclass(getattr(module, x))
and x not in exclude
and getattr(module, x) not in exclude])
class TimerBlock:
def __init__(self, title):
print("{}".format(title))
def __enter__(self):
self.start = time.clock()
return self
def __exit__(self, exc_type, exc_value, traceback):
self.end = time.clock()
self.interval = self.end - self.start
if exc_type is not None:
self.log("Operation failed\n")
else:
self.log("Operation finished\n")
def log(self, string):
duration = time.clock() - self.start
units = 's'
if duration > 60:
duration = duration / 60.
units = 'm'
print(" [{:.3f}{}] {}".format(duration, units, string))
def log2file(self, fid, string):
fid = open(fid, 'a')
fid.write("%s\n"%(string))
fid.close()
# duration = time.clock() - self.start
# units = 's'
# if duration > 60:
# duration = duration / 60.
# units = 'm'
# print(" [{:.3f}{}] {}".format(duration, units, string))
def add_arguments_for_module(parser, module, argument_for_class, default, skip_params=[], parameter_defaults={}):
argument_group = parser.add_argument_group(argument_for_class.capitalize())
module_dict = module_to_dict(module)
argument_group.add_argument('--' + argument_for_class, type=str, default=default, choices=module_dict.keys())
args, unknown_args = parser.parse_known_args()
class_obj = module_dict[vars(args)[argument_for_class]]
argspec = inspect.getargspec(class_obj.__init__)
defaults = argspec.defaults[::-1] if argspec.defaults else None
args = argspec.args[::-1]
for i, arg in enumerate(args):
cmd_arg = '{}_{}'.format(argument_for_class, arg)
if arg not in skip_params + ['self', 'args']:
if arg in parameter_defaults.keys():
argument_group.add_argument('--{}'.format(cmd_arg), type=type(parameter_defaults[arg]), default=parameter_defaults[arg])
elif (defaults is not None and i < len(defaults)):
argument_group.add_argument('--{}'.format(cmd_arg), type=type(defaults[i]), default=defaults[i])
else:
print("[Warning]: non-default argument '{}' detected on class '{}'. This argument cannot be modified via the command line"
.format(arg, module.__class__.__name__))
# We don't have a good way of dealing with inferring the type of the argument
# TODO: try creating a custom action and using ast's infer type?
# else:
# argument_group.add_argument('--{}'.format(cmd_arg), required=True)
def kwargs_from_args(args, argument_for_class):
argument_for_class = argument_for_class + '_'
return {key[len(argument_for_class):]: value for key, value in vars(args).items() if argument_for_class in key and key != argument_for_class + 'class'}
def format_dictionary_of_losses(labels, values):
try:
string = ', '.join([('{}: {:' + ('.3f' if value >= 0.001 else '.1e') +'}').format(name, value) for name, value in zip(labels, values)])
except (TypeError, ValueError) as e:
print(zip(labels, values))
string = '[Log Error] ' + str(e)
return string
class IteratorTimer():
def __init__(self, iterable):
self.iterable = iterable
self.iterator = self.iterable.__iter__()
def __iter__(self):
return self
def __len__(self):
return len(self.iterable)
def __next__(self):
start = time.time()
n = self.iterator.next()
self.last_duration = (time.time() - start)
return n
next = __next__
def gpumemusage():
gpu_mem = subprocess.check_output("nvidia-smi | grep MiB | cut -f 3 -d '|'", shell=True).replace(' ', '').replace('\n', '').replace('i', '')
curr, tot = [float(a[:-2]) for a in gpu_mem.split('/')]
util = "%1.2f"%(100*curr/tot)+'%'
cmem = str(int(math.ceil(curr/1024.)))+'GB'
gmem = str(int(math.ceil(tot/1024.)))+'GB'
gpu_mem = util + '--' + join(cmem, gmem)
return gpu_mem
def update_hyperparameter_schedule(args, epoch, global_iteration, optimizer):
if args.schedule_lr_frequency > 0:
for param_group in optimizer.param_groups:
if (global_iteration + 1) % args.schedule_lr_frequency == 0:
param_group['lr'] /= float(args.schedule_lr_fraction)
param_group['lr'] = float(np.maximum(param_group['lr'], 0.000001))
def save_checkpoint(state, is_best, path, prefix, filename='checkpoint.pth.tar'):
prefix_save = os.path.join(path, prefix)
name = prefix_save + '_' + filename
torch.save(state, name)
if is_best:
shutil.copyfile(name, prefix_save + '_model_best.pth.tar')