-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathlog.py
More file actions
65 lines (58 loc) · 3.07 KB
/
Copy pathlog.py
File metadata and controls
65 lines (58 loc) · 3.07 KB
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
import termcolor
from easydict import EasyDict as edict
import time
def red(message,**kwargs): return termcolor.colored(str(message),color="red",attrs=[k for k,v in kwargs.items() if v is True])
def green(message,**kwargs): return termcolor.colored(str(message),color="green",attrs=[k for k,v in kwargs.items() if v is True])
def blue(message,**kwargs): return termcolor.colored(str(message),color="blue",attrs=[k for k,v in kwargs.items() if v is True])
def cyan(message,**kwargs): return termcolor.colored(str(message),color="cyan",attrs=[k for k,v in kwargs.items() if v is True])
def yellow(message,**kwargs): return termcolor.colored(str(message),color="yellow",attrs=[k for k,v in kwargs.items() if v is True])
def magenta(message,**kwargs): return termcolor.colored(str(message),color="magenta",attrs=[k for k,v in kwargs.items() if v is True])
def grey(message,**kwargs): return termcolor.colored(str(message),color="grey",attrs=[k for k,v in kwargs.items() if v is True])
def get_time(sec):
d = int(sec//(24*60*60))
h = int(sec//(60*60)%24)
m = int((sec//60)%60)
s = int(sec%60)
return d,h,m,s
class Log:
def __init__(self): pass
def process(self,pid):
print(grey("Process ID: {}".format(pid),bold=True))
def title(self,message,opt):
print(yellow(message,bold=True,underline=True))
if opt.isTrain:
with open("{0}/log.txt".format(opt.output_path), "a") as log_file:
now = time.strftime("%c")
log_file.write('================ Training Loss (%s) ================\n' % now)
def info(self,message):
print(magenta(message,bold=True))
def options(self,opt,level=0):
for key,value in sorted(opt.items()):
if isinstance(value,(dict,edict)):
print(" "*level+cyan("* ")+green(key)+":")
self.options(value,level+1)
else:
print(" "*level+cyan("* ")+green(key)+":",yellow(value))
# def loss_train(self,opt,ep,lr,loss,timer):
# message = grey("[train] ",bold=True)
# message += "epoch {}/{}".format(cyan(ep,bold=True),opt.max_epoch)
# message += ", lr:{}".format(yellow("{:.2e}".format(lr),bold=True))
# message += ", loss:{}".format(red("{:.3e}".format(loss),bold=True))
# message += ", time:{}".format(blue("{0}-{1:02d}:{2:02d}:{3:02d}".format(*get_time(timer.elapsed)),bold=True))
# message += " (ETA:{})".format(blue("{0}-{1:02d}:{2:02d}:{3:02d}".format(*get_time(timer.arrival))))
# print(message)
def loss_val(self,opt,loss):
message = grey("[val] ",bold=True)
message += "loss:{}".format(red("{:.3e}".format(loss),bold=True))
print(message)
def loss_train(self,opt,it,loss):
with open("{0}/log.txt".format(opt.output_path),'a+')as f:
message="iter:{} ".format(it)
for k, v in loss.items():
# print(v)
# if v != 0:
v = v.mean().float()
message += '%s: %.3f ' % (k, v)
print(message)
f.write('%s\n' % message)
log = Log()