-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathtrain_utils.py
executable file
·80 lines (70 loc) · 2.26 KB
/
train_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
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
import collections
import datetime
import json
import os
import os
import re
import sys
import time
from os.path import basename
import functools
import numpy as np
class Record_Results(object):
def __init__(self, logfile):
if os.path.isfile(logfile):
ext = logfile.split('.')[-1]
filename = logfile[:-len(ext)-1]
file_suff = filename.split('_')[-1]
filename = filename[:-len(file_suff)-1]
try:
if 'v' in file_suff:
file_suff = file_suff.remove('v')
file_suff = 'v'+str(int(file_suff)+1)
else:
file_suff = file_suff+'_v1'
except:
file_suff += get_date_str()
if filename:
logfile_c = filename+'_'+file_suff+'.'+ext
else: logfile_c = file_suff+'.'+ext
with open(logfile_c, 'w') as f:
f.write(open(logfile, 'r').read())
self.logfile = logfile
print ('logfile:', logfile)
self.f = open(logfile,'w')
self.f.close()
def fprint(self, *stt):
sto = functools.reduce(lambda x,y: str(x)+' '+str(y), list(stt))
print (sto)
try:
sto = str(datetime.datetime.now())+':'+ sto
except: pass
assert os.path.exists(self.logfile)
self.f = open(self.logfile, 'a')
try:
self.f.write('\n'+sto)
except: pass
self.f.close()
def clear(self):
self.f = open(self.logfile, 'w')
self.f.close()
def close(self):
print ('no need to close')
return
def get_date_str():
datetim = str(datetime.datetime.now()).replace('.','').replace('-','').replace(':','').replace(' ','')[2:14]
return datetim
def createDir(direc):
command = "mkdir -p "+direc
#print "create dir for ", direc
if not (os.path.exists(direc) and os.path.isdir(direc)):
os.system(command)
# write to a config file
def write_config(config_filename, config):
with open(config_filename, 'w') as config_file:
json.dump(config, config_file)
# load from config file
def load_config(config_filename):
with open(config_filename) as config_file:
config = json.load(config_file)
return config