-
Notifications
You must be signed in to change notification settings - Fork 51
/
Copy pathtemplate_model_seq_multi_gpu.py
257 lines (182 loc) · 9.01 KB
/
template_model_seq_multi_gpu.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
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
from utils import *
from ops_sequential import *
import time
from tensorflow.python.data.experimental import prefetch_to_device, shuffle_and_repeat, map_and_batch, AUTOTUNE
from glob import glob
from tqdm import tqdm
automatic_gpu_usage() # for efficient gpu use
class SubNetwork(tf.keras.Model):
def __init__(self, channels, name):
super(SubNetwork, self).__init__(name=name)
self.channels = channels
self.model = self.architecture()
def architecture(self):
# Implement sub network architecture
# example
model = []
model += [Conv(self.channels, kernel=7, stride=1, pad=3, pad_type='reflect', use_bias=False, sn=True, name='conv')]
model += [BatchNorm(name='batch_norm')]
model += [Relu()]
model += [Global_Avg_Pooling()]
model += [FullyConnected(units=10, sn=False, name='fc')]
model = Sequential(model, name='d_encoder')
return model
def call(self, x_init, training=None, mask=None):
x = self.model(x_init, training=training)
return x
def build_summary(self, input_shape, detail_summary=False):
x_init = tf.keras.layers.Input(input_shape, name='sub_network_input')
x = self.model(x_init)
if detail_summary:
self.model.summary()
self.build_model = tf.keras.Model(x_init, x, name=self.name)
self.build_model.summary()
def count_parameter(self):
x = self.model.count_params()
return x
class Network():
def __init__(self, args):
super(Network, self).__init__()
self.model_name = 'Network'
self.phase = args.phase
self.checkpoint_dir = args.checkpoint_dir
self.result_dir = args.result_dir
self.log_dir = args.log_dir
self.sample_dir = args.sample_dir
self.save_freq = args.save_freq
# Set parameter
self.dataset_name = args.dataset
self.augment_flag = args.augment_flag
self.batch_size = args.batch_size
self.iteration = args.iteration
self.img_height = args.img_height
self.img_width = args.img_width
self.img_ch = args.img_ch
self.init_lr = args.lr
self.sample_dir = os.path.join(args.sample_dir, self.model_dir)
check_folder(self.sample_dir)
self.checkpoint_dir = os.path.join(args.checkpoint_dir, self.model_dir)
check_folder(self.checkpoint_dir)
self.log_dir = os.path.join(args.log_dir, self.model_dir)
check_folder(self.log_dir)
self.dataset_path = os.path.join('./dataset', self.dataset_name)
##################################################################################
# Model
##################################################################################
def build_model(self):
if self.phase == 'train':
self.strategy = tf.distribute.MirroredStrategy()
self.NUM_GPUS = self.strategy.num_replicas_in_sync
self.iteration = self.iteration // self.NUM_GPUS
self.decay_iter = self.decay_iter // self.NUM_GPUS
with self.strategy.scope() :
""" Input Image"""
img_class = Image_data(self.img_height, self.img_width, self.img_ch, self.dataset_path, self.augment_flag)
img_class.preprocess()
dataset_num = len(img_class.dataset)
print("Dataset number : ", dataset_num)
img_slice = tf.data.Dataset.from_tensor_slices(img_class.dataset)
gpu_device = '/gpu:0'
img_slice = img_slice. \
apply(shuffle_and_repeat(dataset_num)). \
apply(map_and_batch(img_class.image_processing, self.batch_size, num_parallel_batches=AUTOTUNE,
drop_remainder=True)). \
apply(prefetch_to_device(gpu_device, AUTOTUNE))
self.dataset_iter = iter(img_slice)
""" Network """
self.classifier = SubNetwork(channels=64, name='classifier')
""" Optimizer """
self.optimizer = tf.keras.optimizers.Adam(learning_rate=self.init_lr, beta_1=0.5, beta_2=0.999,
epsilon=1e-08)
""" Summary """
# mean metric
self.loss_metric = tf.keras.metrics.Mean('loss',
dtype=tf.float32) # In tensorboard, make a loss to smooth graph
# print summary
input_shape = [self.img_height, self.img_width, self.img_ch]
self.classifier.build_summary(input_shape)
""" Count parameters """
params = self.classifier.count_parameter()
print("Total network parameters : ", format(params, ','))
print("Total gpu numbers : ", self.NUM_GPUS)
""" Checkpoint """
self.ckpt = tf.train.Checkpoint(classifier=self.classifier, optimizer=self.optimizer)
self.manager = tf.train.CheckpointManager(self.ckpt, self.checkpoint_dir, max_to_keep=2)
self.start_iteration = 0
if self.manager.latest_checkpoint:
self.ckpt.restore(self.manager.latest_checkpoint)
self.start_iteration = int(self.manager.latest_checkpoint.split('-')[-1])
print('Latest checkpoint restored!!')
print('start iteration : ', self.start_iteration)
else:
print('Not restoring from saved checkpoint')
else:
""" Test """
""" Network """
self.classifier = SubNetwork(channels=64, name='classifier')
""" Summary """
input_shape = [self.img_height, self.img_width, self.img_ch]
self.classifier.build_summary(input_shape)
""" Count parameters """
params = self.classifier.count_parameter()
print("Total network parameters : ", format(params, ','))
""" Checkpoint """
self.ckpt = tf.train.Checkpoint(classifier=self.classifier)
self.manager = tf.train.CheckpointManager(self.ckpt, self.checkpoint_dir, max_to_keep=2)
if self.manager.latest_checkpoint:
self.ckpt.restore(self.manager.latest_checkpoint).expect_partial()
print('Latest checkpoint restored!!')
else:
print('Not restoring from saved checkpoint')
def train_step(self, real_img):
with tf.GradientTape() as tape:
logit = self.classifier(real_img)
# calculate loss
"""
if classification
loss = cross_entroy_loss(logit, label)
"""
your_loss = 0
loss = your_loss + regularization_loss(self.classifier)
train_variable = self.classifier.trainable_variables
gradient = tape.gradient(loss, train_variable)
self.optimizer.apply_gradients(zip(gradient, train_variable))
# self.loss_metric(self.loss)
return loss
def distribute_train_step(self, real_img):
with self.strategy.scope():
loss = self.strategy.experimental_run_v2(self.train_step, args=(real_img))
self.loss = self.strategy.reduce(tf.distribute.ReduceOp.MEAN, loss, axis=None)
self.loss_metric(self.loss)
return self.loss
def train(self):
start_time = time.time()
# setup tensorboards
train_summary_writer = tf.summary.create_file_writer(self.log_dir)
for idx in range(self.start_iteration, self.iteration):
real_img = next(self.dataset_iter)
# update network
loss = self.distribute_train_step(real_img)
# save to tensorboard
with train_summary_writer.as_default():
tf.summary.scalar('loss', self.loss, step=idx)
tf.summary.scalar('loss_mean', self.loss_metric.result(), step=idx)
# save every self.save_freq
if np.mod(idx + 1, self.save_freq) == 0:
self.manager.save(checkpoint_number=idx + 1)
print("iter: [%6d/%6d] time: %4.4f loss: %.8f" % (
idx, self.iteration, time.time() - start_time, loss))
# save model for final step
self.manager.save(checkpoint_number=self.iteration)
@property
def model_dir(self):
return "{}_{}".format(self.model_name, self.dataset_name)
def test(self):
test_dataset = glob('./dataset/{}/{}/*.jpg'.format(self.dataset_name, 'test_img_folder')) + glob(
'./dataset/{}/{}/*.png'.format(self.dataset_name, 'test_img_folder'))
self.result_dir = os.path.join(self.result_dir, self.model_dir)
check_folder(self.result_dir)
for sample_file in tqdm(test_dataset):
sample_image = load_test_image(sample_file, self.img_width, self.img_height, self.img_ch)
logit = self.classifier(sample_image, training=False)
### ...