-
Notifications
You must be signed in to change notification settings - Fork 2
/
solver.py
207 lines (166 loc) · 7.81 KB
/
solver.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
import os
import time
import logging
import numpy as np
import tensorflow as tf
from datetime import datetime
# noinspection PyPep8Naming
import tensorflow_utils as tf_utils
from dataset import Dataset
from pix2pix import Pix2Pix
logger = logging.getLogger(__name__) # logger
logger.setLevel(logging.INFO)
class Solver(object):
def __init__(self, flags):
run_config = tf.ConfigProto()
run_config.gpu_options.allow_growth = True
self.sess = tf.Session(config=run_config)
self.flags = flags
self.iter_time = 0
self._make_folders()
self._init_logger()
self.dataset = Dataset(self.flags.dataset, self.flags, log_path=self.log_out_dir)
self.model = Pix2Pix(self.sess, self.flags, self.dataset.image_size, self.dataset(self.flags.is_train),
log_path=self.log_out_dir)
self.saver = tf.train.Saver()
self.sess.run(tf.global_variables_initializer())
tf_utils.show_all_variables()
def _make_folders(self):
if self.flags.is_train: # train stage
if self.flags.load_model is None:
cur_time = datetime.now().strftime("%Y%m%d-%H%M%S")
self.model_out_dir = "{}/model/{}".format(self.flags.dataset, cur_time)
if not os.path.isdir(self.model_out_dir):
os.makedirs(self.model_out_dir)
else:
cur_time = self.flags.load_model
self.model_out_dir = "{}/model/{}".format(self.flags.dataset, self.flags.load_model)
self.sample_out_dir = "{}/sample/{}".format(self.flags.dataset, cur_time)
if not os.path.isdir(self.sample_out_dir):
os.makedirs(self.sample_out_dir)
self.log_out_dir = "{}/logs/{}".format(self.flags.dataset, cur_time)
self.train_writer = tf.summary.FileWriter("{}/logs/{}".format(self.flags.dataset, cur_time),
graph_def=self.sess.graph_def)
elif not self.flags.is_train: # test stage
self.model_out_dir = "{}/model/{}".format(self.flags.dataset, self.flags.load_model)
self.test_out_dir = "{}/test/{}".format(self.flags.dataset, self.flags.load_model)
self.eval_out_dir = "../eval/pix2pix"
self.gt_out_dir = "../eval/gt"
self.ct_out_dir = "../eval/ct"
self.log_out_dir = "{}/logs/{}".format(self.flags.dataset, self.flags.load_model)
if not os.path.isdir(self.test_out_dir):
os.makedirs(self.test_out_dir)
if not os.path.isdir(self.eval_out_dir):
os.makedirs(self.eval_out_dir)
if not os.path.isdir(self.gt_out_dir):
os.makedirs(self.gt_out_dir)
if not os.path.isdir(self.ct_out_dir):
os.makedirs(self.ct_out_dir)
def _init_logger(self):
formatter = logging.Formatter('%(asctime)s:%(name)s:%(message)s')
# file handler
file_handler = logging.FileHandler(os.path.join(self.log_out_dir, 'solver.log'))
file_handler.setFormatter(formatter)
file_handler.setLevel(logging.INFO)
# stream handler
stream_handler = logging.StreamHandler()
stream_handler.setFormatter(formatter)
# add handlers
logger.addHandler(file_handler)
logger.addHandler(stream_handler)
if self.flags.is_train:
logger.info('gpu_index: {}'.format(self.flags.gpu_index))
logger.info('batch_size: {}'.format(self.flags.batch_size))
logger.info('dataset: {}'.format(self.flags.dataset))
logger.info('is_train: {}'.format(self.flags.is_train))
logger.info('learning_rate: {}'.format(self.flags.learning_rate))
logger.info('beta1: {}'.format(self.flags.beta1))
logger.info('iters: {}'.format(self.flags.iters))
logger.info('print_freq: {}'.format(self.flags.print_freq))
logger.info('save_freq: {}'.format(self.flags.save_freq))
logger.info('sample_freq: {}'.format(self.flags.sample_freq))
logger.info('load_model: {}'.format(self.flags.load_model))
def train(self):
# load initialized checkpoint that provided
if self.flags.load_model is not None:
if self.load_model():
print(' [*] Load SUCCESS!\n')
else:
print(' [!] Load Failed...\n')
# threads for tfrecord
coord = tf.compat.v1.train.Coordinator()
threads = tf.compat.v1.train.start_queue_runners(sess=self.sess, coord=coord)
try:
while self.iter_time < self.flags.iters:
# samppling images and save them
self.sample(self.iter_time)
# train_step
loss, summary = self.model.train_step()
self.model.print_info(loss, self.iter_time)
self.train_writer.add_summary(summary, self.iter_time)
self.train_writer.flush()
# save model
self.save_model(self.iter_time)
self.iter_time += 1
self.save_model(self.flags.iters)
except KeyboardInterrupt:
coord.request_stop()
except Exception as e:
coord.request_stop(e)
finally:
# when done, ask the threads to stop
coord.request_stop()
coord.join(threads)
def test(self, sum):
if self.load_model():
logger.info(' [*] Load SUCCESS!')
else:
logger.info(' [!] Load Failed...')
# threads for tfrecord
coord = tf.train.Coordinator()
threads = tf.train.start_queue_runners(sess=self.sess, coord=coord)
iter_time = 0
total_time = 0.
try:
while iter_time < sum:
print(iter_time)
tic = time.time()
imgs, img_names = self.model.test_step()
total_time += time.time() - tic
self.model.plots_test(imgs, img_names, self.test_out_dir, self.eval_out_dir, self.gt_out_dir,
self.ct_out_dir)
iter_time += 1
logger.info('Avg. PT: {:.2f} msec.'.format(total_time / sum * 1000.))
except KeyboardInterrupt:
coord.request_stop()
except Exception as e:
coord.request_stop(e)
except tf.errors.OutOfRangeError:
coord.request_stop()
# finally:
# # when done, ask the threads to stop
# # uncomment for one file and comment for run mutiple files
# coord.request_stop()
# coord.join(threads)
# # index =1
def sample(self, iter_time):
if np.mod(iter_time, self.flags.sample_freq) == 0:
imgs = self.model.sample_imgs()
self.model.plots(imgs, self.iter_time, self.dataset.image_size, self.sample_out_dir)
def save_model(self, iter_time):
if np.mod(iter_time + 1, self.flags.save_freq) == 0:
model_name = 'model'
self.saver.save(self.sess, os.path.join(self.model_out_dir, model_name), global_step=self.iter_time)
logger.info(' [*] Model saved! Iter: {}'.format(iter_time))
def load_model(self):
logger.info(' [*] Reading checkpoint...')
ckpt = tf.train.get_checkpoint_state(self.model_out_dir)
if ckpt and ckpt.model_checkpoint_path:
ckpt_name = os.path.basename(ckpt.model_checkpoint_path)
self.saver.restore(self.sess, os.path.join(self.model_out_dir, ckpt_name))
meta_graph_path = ckpt.model_checkpoint_path + '.meta'
self.iter_time = int(meta_graph_path.split('-')[-1].split('.')[0])
logger.info(' [*] Load iter_time: {}'.format(self.iter_time))
return True
else:
return False