forked from AlexeyAB/darknet
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathexperiment_models.py
209 lines (176 loc) · 7.54 KB
/
experiment_models.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
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
import glob
import os
import re
import sys
import logging
from datetime import datetime
from shutil import copyfile
from log import DarknetLogParser
def try_mkdir(path):
try:
os.mkdir(path)
except OSError:
print("Creation of the directory %s failed" % path)
else:
print("Successfully created the directory %s " % path)
class DataFile(object):
def __init__(self, n_classes=None, f_train=None, f_valid=None, f_test=None, f_names=None, dir_backup=None):
self.n_classes = n_classes
self.f_train = f_train
self.f_test = f_test
self.f_valid = f_valid
self.f_names = f_names
self.dir_backup = dir_backup
def load(self, file):
with open(file) as f:
vars = {}
for line in f:
name, var = line.split('=')
vars[name.strip()] = var.strip()
self.n_classes = int(vars['classes'])
self.f_train = vars['train']
self.f_test = vars['test']
self.f_valid = vars['valid']
self.f_names = vars['names']
self.dir_backup = vars['backup']
def migrate_file(self, new_dir, file, name):
new_file = os.path.join(new_dir, name)
copyfile(file, new_file)
return new_file
def get_names(self):
names = []
with open(self.f_names) as f:
for line in f:
names.append(line.strip())
return names
def get_train_images(self):
return open(self.f_train).read().split('\n')
def get_test_images(self):
return open(self.f_test).read().split('\n')
def migrate(self, new_dir):
test_valid_same = (self.f_test == self.f_valid)
self.f_train = self.migrate_file(new_dir, self.f_train, 'train.txt')
self.f_test = self.migrate_file(new_dir, self.f_test, 'test.txt')
if test_valid_same:
self.f_valid = self.f_test
else:
self.f_valid = self.migrate_file(new_dir, self.f_valid, 'valid.txt')
self.f_names = self.migrate_file(new_dir, self.f_names, 'names.txt')
self.dir_backup = os.path.join(new_dir, os.path.split(self.dir_backup)[1])
try_mkdir(self.dir_backup)
def save(self, file):
try:
with open(file, "w") as f:
f.write('classes = %d\n' % self.n_classes)
f.write('train = %s\n' % self.f_train)
f.write('test = %s\n' % self.f_test)
f.write('valid = %s\n' % self.f_valid)
f.write('names = %s\n' % self.f_names)
f.write('backup = %s' % self.dir_backup)
except:
return False
return True
class TrainSession(object):
def __init__(self, name=None, exp_dir=None, data=None, cfg=None, pretrained_weights=None, copy_pretrained_weights=None):
self.file_logger = logging.getLogger('TrainLog')
self.file_logger.setLevel(logging.INFO)
self.console_logger = logging.getLogger('ConsoleLog')
self.console_logger.setLevel(logging.INFO)
self.log_parser = DarknetLogParser()
self.name = name
self.pretrained_weights_out = self.pretrained_weights_in = None
if not all(v is None for v in [name,exp_dir,data,cfg,pretrained_weights,copy_pretrained_weights]):
self.name = name
self.dir = os.path.join(exp_dir, self.name)
try_mkdir(self.dir)
self.output_file = os.path.join(self.dir, 'output.txt')
self.data_in = data
self.data_out = os.path.join(self.dir, 'yolo.data')
self.df = DataFile()
self.df.load(self.data_in) # load the given .data file
self.df.migrate(self.dir) # migrate all the training files to here so we can know what was used
self.df.save(self.data_out) # save the new training file to the train session directory
self.cfg_in = os.path.join(os.getcwd(), cfg)
self.cfg_out = os.path.join(self.dir, os.path.split(self.cfg_in)[1])
self.pretrained_weights_in = pretrained_weights
self.pretrained_weights_out = os.path.join(self.df.dir_backup, os.path.split(self.pretrained_weights_in)[1])
if copy_pretrained_weights:
copyfile(self.pretrained_weights_in, self.pretrained_weights_out)
else:
self.pretrained_weights_out = self.pretrained_weights_in
copyfile(self.cfg_in, self.cfg_out)
def get_datafile(self):
return self.df
def run(self, calc_map=True, gpus=[1], pretrained_weights = None, clear=False):
if self.pretrained_weights_out is None:
self.pretrained_weights_out = self.pretrained_weights_in = pretrained_weights
self.start_logging(self.output_file)
params = [self.data_out, self.cfg_out, self.pretrained_weights_out]
arg_arr = ['./darknet'] + ["detector", "train"] + params
if not gpus is None and len(gpus) > 0:
arg_arr = arg_arr + ["-gpus"]
gpus = [str(gpu) for gpu in gpus]
arg_arr = arg_arr + [','.join(gpus)]
if calc_map: arg_arr = arg_arr + ['-map']
if clear: arg_arr = arg_arr + ['-clear']
arg_arr = arg_arr + ['-mAP_epochs'] + ['2']
cmd = " ".join(arg_arr)
from subprocess import Popen, PIPE, STDOUT
self.log("Beginning training session:")
self.log(cmd)
sys.stderr = open('out.log', 'w')
with Popen(arg_arr, stdout=PIPE, stderr=STDOUT,
universal_newlines=True, bufsize=0) as p:
for line in iter(p.stdout.readline, ''):
self.log(line.strip())
# for line in p.stdout:
# self.log(line.strip())
rc = p.returncode
self.logger.disabled = True
def load(self, dir):
self.name = os.path.basename(os.path.normpath(dir))
self.data_in = self.data_out = os.path.join(dir, 'yolo.data')
self.output_file = os.path.join(dir, 'output.txt')
self.cfg_in = self.cfg_out = glob.glob(os.path.join(dir,'*.cfg'))[0]
self.df = DataFile()
self.df.load(os.path.join(self.data_in))
def start_logging(self, file):
self.fh = logging.FileHandler(file)
self.fh.setLevel(logging.INFO)
self.file_logger.addHandler(self.fh)
self.ch = logging.StreamHandler()
self.ch.setLevel(logging.INFO)
self.console_logger.addHandler(self.ch)
def init_log(self):
pass
def log(self, s):
self.console_logger.info(s)
p = self.log_parser.parse(s)
if not p is None:
self.file_logger.info(p)
class Experiment(object):
def __init__(self, dir, name):
self.dir = os.path.join(dir, name)
self.name = name
self.dataset = None
self.pretrained_weights = None
self.config = None
self.sessions = {}
if os.path.exists(self.dir):
self.load()
else:
# make experiment directory
try_mkdir(self.dir)
def load(self):
subdirs = os.listdir(self.dir)
for session_name in subdirs:
ts = TrainSession()
ts.load(os.path.join(self.dir, session_name))
self.sessions[session_name] = ts
def new_session(self, data, cfg, pretrained_weights, copy_pretrained_weights=False, name=None):
now = datetime.now()
if name is None:
name = now.strftime("%Y-%m-%d_%H-%M-%S")
session = TrainSession(name, self.dir, data, cfg, pretrained_weights, copy_pretrained_weights)
self.sessions[name] = session
return session