-
Notifications
You must be signed in to change notification settings - Fork 3
/
train.py
252 lines (207 loc) · 9.14 KB
/
train.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
import math
import os
os.environ['CUDA_VISIBLE_DEVICES'] = '0'
os.environ['TF_CPP_MIN_LOG_LEVEL'] = '2'
from datetime import datetime
import click
import tensorflow as tf
from hourglass104 import StackedHourglassNetwork
from preprocess import Preprocessor
IMAGE_SHAPE = (256, 256, 3)
HEATMAP_SIZE = (64, 64)
def automatic_gpu_usage() :
gpus = tf.config.experimental.list_physical_devices('GPU')
if gpus:
try:
# Currently, memory growth needs to be the same across GPUs
for gpu in gpus:
tf.config.experimental.set_memory_growth(gpu, True)
logical_gpus = tf.config.experimental.list_logical_devices('GPU')
print(len(gpus), "Physical GPUs,", len(logical_gpus), "Logical GPUs")
except RuntimeError as e:
# Memory growth must be set before GPUs have been initialized
print(e)
class Trainer(object):
def __init__(self,
model,
epochs,
global_batch_size,
strategy,
initial_learning_rate,
version='0.0.1',
start_epoch=1,
tensorboard_dir='./logs'):
self.start_epoch = start_epoch
self.model = model
self.epochs = epochs
self.strategy = strategy
self.global_batch_size = global_batch_size
self.loss_object = tf.keras.losses.MeanSquaredError(
reduction=tf.keras.losses.Reduction.NONE)
# "we use rmsprop with a learning rate of 2.5e-4.""
self.optimizer = tf.keras.optimizers.Adam(
learning_rate=initial_learning_rate)
self.model = model
self.current_learning_rate = initial_learning_rate
self.last_val_loss = math.inf
self.lowest_val_loss = math.inf
self.patience_count = 0
self.max_patience = 10
self.tensorboard_dir = tensorboard_dir
self.best_model = None
self.version = version
def lr_decay(self):
if self.patience_count >= self.max_patience:
self.current_learning_rate /= 10.0
self.patience_count = 0
elif self.last_val_loss == self.lowest_val_loss:
self.patience_count = 0
self.patience_count += 1
self.optimizer.learning_rate = self.current_learning_rate
def lr_decay_step(self, epoch):
if epoch == 25 or epoch == 50 or epoch == 75:
self.current_learning_rate /= 10.0
self.optimizer.learning_rate = self.current_learning_rate
def compute_loss(self, labels, outputs):
loss = 0
for output in outputs:
weights = tf.cast(labels > 0, dtype=tf.float32) * 81 + 1
loss += tf.math.reduce_mean(
tf.math.square(labels - output) * weights) * (
1. / self.global_batch_size)
return loss
def train_step(self, inputs):
images, labels = inputs
with tf.GradientTape() as tape:
outputs = self.model(images, training=True)
loss = self.compute_loss(labels, outputs)
grads = tape.gradient(
target=loss, sources=self.model.trainable_variables)
self.optimizer.apply_gradients(
zip(grads, self.model.trainable_variables))
return loss
def val_step(self, inputs):
images, labels = inputs
outputs = self.model(images, training=False)
loss = self.compute_loss(labels, outputs)
return loss
def run(self, train_dist_dataset, val_dist_dataset):
@tf.function
def distributed_train_epoch(dataset):
tf.print('Start distributed traininng...')
total_loss = 0.0
num_train_batches = 0.0
for one_batch in dataset:
per_replica_loss = self.strategy.experimental_run_v2(
self.train_step, args=(one_batch, ))
batch_loss = self.strategy.reduce(
tf.distribute.ReduceOp.SUM, per_replica_loss, axis=None)
total_loss += batch_loss
num_train_batches += 1
tf.print('Trained batch', num_train_batches, 'batch loss',
batch_loss, 'epoch total loss', total_loss / num_train_batches)
return total_loss, num_train_batches
@tf.function
def distributed_val_epoch(dataset):
total_loss = 0.0
num_val_batches = 0.0
for one_batch in dataset:
per_replica_loss = self.strategy.experimental_run_v2(
self.val_step, args=(one_batch, ))
num_val_batches += 1
batch_loss = self.strategy.reduce(
tf.distribute.ReduceOp.SUM, per_replica_loss, axis=None)
tf.print('Validated batch', num_val_batches, 'batch loss',
batch_loss)
if not tf.math.is_nan(batch_loss):
# TODO: Find out why the last validation batch loss become NaN
total_loss += batch_loss
else:
num_val_batches -= 1
return total_loss, num_val_batches
summary_writer = tf.summary.create_file_writer(self.tensorboard_dir)
summary_writer.set_as_default()
for epoch in range(self.start_epoch, self.epochs + 1):
tf.summary.experimental.set_step(epoch)
self.lr_decay()
tf.summary.scalar('epoch learning rate',
self.current_learning_rate)
print('Start epoch {} with learning rate {}'.format(
epoch, self.current_learning_rate))
train_total_loss, num_train_batches = distributed_train_epoch(
train_dist_dataset)
train_loss = train_total_loss / num_train_batches
print('Epoch {} train loss {}'.format(epoch, train_loss))
tf.summary.scalar('epoch train loss', train_loss)
val_total_loss, num_val_batches = distributed_val_epoch(
val_dist_dataset)
val_loss = val_total_loss / num_val_batches
print('Epoch {} val loss {}'.format(epoch, val_loss))
tf.summary.scalar('epoch val loss', val_loss)
# save model when reach a new lowest validation loss
if val_loss < self.lowest_val_loss:
self.save_model(epoch, val_loss)
self.lowest_val_loss = val_loss
self.last_val_loss = val_loss
return self.best_model
def save_model(self, epoch, loss):
model_name = './models/model-v{}-epoch-{}-loss-{:.4f}.h5'.format(
self.version, epoch, loss)
self.model.save_weights(model_name)
self.best_model = model_name
print("Model {} saved.".format(model_name))
def create_dataset(tfrecords, batch_size, num_heatmap, is_train):
preprocess = Preprocessor(
IMAGE_SHAPE, (HEATMAP_SIZE[0], HEATMAP_SIZE[1], num_heatmap), is_train)
dataset = tf.data.Dataset.list_files(tfrecords)
dataset = tf.data.TFRecordDataset(dataset)
dataset = dataset.map(
preprocess, num_parallel_calls=tf.data.experimental.AUTOTUNE)
if is_train:
dataset = dataset.shuffle(batch_size)
dataset = dataset.batch(batch_size)
dataset = dataset.prefetch(buffer_size=tf.data.experimental.AUTOTUNE)
return dataset
def train(epochs, start_epoch, learning_rate, tensorboard_dir, checkpoint,
num_heatmap, batch_size, train_tfrecords, val_tfrecords, version):
strategy = tf.distribute.MirroredStrategy()
global_batch_size = strategy.num_replicas_in_sync * batch_size
train_dataset = create_dataset(
train_tfrecords, global_batch_size, num_heatmap, is_train=True)
val_dataset = create_dataset(
val_tfrecords, global_batch_size, num_heatmap, is_train=False)
if not os.path.exists(os.path.join('./models')):
os.makedirs(os.path.join('./models/'))
with strategy.scope():
train_dist_dataset = strategy.experimental_distribute_dataset(
train_dataset)
val_dist_dataset = strategy.experimental_distribute_dataset(
val_dataset)
model = StackedHourglassNetwork(IMAGE_SHAPE, 4, 1, num_heatmap)
if checkpoint and os.path.exists(checkpoint):
model.load_weights(checkpoint)
trainer = Trainer(
model,
epochs,
global_batch_size,
strategy,
initial_learning_rate=learning_rate,
start_epoch=start_epoch,
version=version,
tensorboard_dir=tensorboard_dir)
print('Start training...')
return trainer.run(train_dist_dataset, val_dist_dataset)
if __name__ == "__main__":
tfrecords_dir = './tfrecords_mpii/'
train_tfrecords = os.path.join(tfrecords_dir, 'train*')
val_tfrecords = os.path.join(tfrecords_dir, 'val*')
epochs = 10
batch_size = 8
num_heatmap = 16
tensorboard_dir = './logs/'
learning_rate = 0.0007
start_epoch = 1
automatic_gpu_usage()
pretrained_path = None # './models_old/model-v0.0.2-epoch-15-loss-1.1013.h5'
train(epochs, start_epoch, learning_rate, tensorboard_dir, pretrained_path,
num_heatmap, batch_size, train_tfrecords, val_tfrecords, '0.0.1')