-
Notifications
You must be signed in to change notification settings - Fork 4
/
logger.py
142 lines (112 loc) · 4.94 KB
/
logger.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
from datetime import datetime
from pytz import timezone
import os
import shutil
import torch
import time
from itertools import cycle
from shutil import get_terminal_size
from threading import Thread
from time import sleep
class Loader:
def __init__(self, desc="Loading...", end="OK!", timeout=0.1):
self.desc = desc
self.end = end
self.timeout = timeout
self._thread = Thread(target=self._animate, daemon=True)
self.steps = ["⢿", "⣻", "⣽", "⣾", "⣷", "⣯", "⣟", "⡿"]
self.done = False
def start(self):
self._thread.start()
return self
def _animate(self):
for c in cycle(self.steps):
if self.done:
break
print(f"\r{self.desc} {c}", flush=True, end="")
sleep(self.timeout)
def __enter__(self):
self.start()
def stop(self):
self.done = True
cols = get_terminal_size((80, 20)).columns
print("\r" + " " * cols, end="", flush=True)
text = self.desc + " " + self.end
print(f"\r{text}", flush=True)
def __exit__(self, exc_type, exc_value, tb):
self.stop()
class pttm():
def __init__(self):
self.start_time = time.time()
self.previos_time = self.start_time
def print_status(self, epoch, idx, trainloader):
_len = len(trainloader)
current_time = time.time()
time_step = current_time - self.previos_time
self.previos_time = current_time
remain_time = time.strftime('%H:%M:%S', time.gmtime(int((len(trainloader) - (idx + 1)) * time_step)))
progress_time = time.strftime('%H:%M:%S', time.gmtime(int(current_time - self.start_time)))
print("Epoch : {} [{}/{} ({}%)] [{}/{}] {}{}".format(
epoch,
idx + 1,
_len,
int((idx + 1)/len(trainloader) * 100),
progress_time,
remain_time,
"\033[101m" + " " * int((idx + 1)/len(trainloader) * 30) + "\033[0m", "\033[43m" + " " * (30 - int((idx + 1)/len(trainloader) * 30)) + "\033[0m"),
end="\r"
)
def get_current_time():
return datetime.now(timezone('Asia/Seoul')).strftime('%Y-%m-%d %H:%M:%S')
def make_new_work_space():
current_time = get_current_time()
new_root_dir = os.path.join("./log", current_time)
if not os.path.exists(new_root_dir):
os.makedirs(new_root_dir)
return new_root_dir
def save_model(root_dir, epoch, model, name):
model_dir = os.path.join(root_dir, "model")
if not os.path.exists(model_dir):
os.makedirs(model_dir)
torch.save({
"epoch": epoch,
"model_state_dict": model.state_dict()
}, model_dir + "/{}_best_model.pth".format(name))
def save_config_file(root_dir):
config_file = "./config.py"
log_dir = os.path.join(root_dir, "train")
if not os.path.exists(log_dir):
os.makedirs(log_dir)
if os.path.isfile(log_dir + "/config.py"):
pass
else:
shutil.copy(config_file, log_dir)
def save_testing_log(root_dir, msg):
log_dir = os.path.join(root_dir, "test")
if not os.path.exists(log_dir):
os.makedirs(log_dir)
f = open(log_dir + "/test_log.txt", 'a')
f.write(msg + "\n")
f.close()
def copy_result(work_dir, valid_name):
for (path, _, files) in os.walk(os.path.join(work_dir, "buffer", "total", valid_name)):
for filename in files:
ext = os.path.splitext(filename)[-1]
if ext == '.png':
if not os.path.exists(path.replace("buffer", "result")):
os.makedirs(path.replace("buffer", "result"))
shutil.copy("%s/%s" % (path, filename), "%s/%s" % (path.replace("buffer", "result"), filename))
for (path, _, files) in os.walk(os.path.join(work_dir, "buffer", "pred", valid_name)):
for filename in files:
ext = os.path.splitext(filename)[-1]
if ext == '.png':
if not os.path.exists(path.replace("buffer", "result")):
os.makedirs(path.replace("buffer", "result"))
shutil.copy("%s/%s" % (path, filename), "%s/%s" % (path.replace("buffer", "result"), filename))
for (path, _, files) in os.walk(os.path.join(work_dir, "buffer", "gt", valid_name)):
for filename in files:
ext = os.path.splitext(filename)[-1]
if ext == '.png':
if not os.path.exists(path.replace("buffer", "result")):
os.makedirs(path.replace("buffer", "result"))
shutil.copy("%s/%s" % (path, filename), "%s/%s" % (path.replace("buffer", "result"), filename))