-
Notifications
You must be signed in to change notification settings - Fork 2
/
main.py
273 lines (222 loc) · 11.8 KB
/
main.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
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
import os
os.environ["TF_CPP_MIN_LOG_LEVEL"] = "3"
import argparse
from common import set_seed
from common import get_logger
from common import get_session
from common import search_same
from common import create_stamp
from dataloader import set_dataset
from dataloader import dataloader
from dataloader import dataloader_supcon
from model import create_model
from loss import crossentropy
from loss import supervised_contrastive
from callback import OptionalLearningRateSchedule
from callback import create_callbacks
import tensorflow as tf
def main(args):
args, initial_epoch = search_same(args)
if initial_epoch == -1:
# training was already finished!
return
elif initial_epoch == 0:
# first training or training with snapshot
args.stamp = create_stamp()
get_session(args)
logger = get_logger("MyLogger")
for k, v in vars(args).items():
logger.info("{} : {}".format(k, v))
##########################
# Strategy
##########################
# strategy = tf.distribute.MirroredStrategy()
strategy = tf.distribute.experimental.CentralStorageStrategy()
assert args.batch_size % strategy.num_replicas_in_sync == 0
logger.info('{} : {}'.format(strategy.__class__.__name__, strategy.num_replicas_in_sync))
logger.info("GLOBAL BATCH SIZE : {}".format(args.batch_size))
logger.info("BATCH SIZE PER REPLICA : {}".format(args.batch_size // strategy.num_replicas_in_sync))
##########################
# Dataset
##########################
trainset, valset = set_dataset(args)
steps_per_epoch = args.steps or len(trainset) // args.batch_size
validation_steps = len(valset) // args.batch_size
logger.info("TOTAL STEPS OF DATASET FOR TRAINING")
logger.info("========== trainset ==========")
logger.info(" --> {}".format(len(trainset)))
logger.info(" --> {}".format(steps_per_epoch))
logger.info("=========== valset ===========")
logger.info(" --> {}".format(len(valset)))
logger.info(" --> {}".format(validation_steps))
##########################
# Model & Metric & Generator
##########################
# metrics
metrics = {
'loss' : tf.keras.metrics.Mean('loss', dtype=tf.float32),
'val_loss': tf.keras.metrics.Mean('val_loss', dtype=tf.float32),
}
if args.loss == 'crossentropy':
metrics.update({
'acc1' : tf.keras.metrics.TopKCategoricalAccuracy(1, 'acc1', dtype=tf.float32),
'acc5' : tf.keras.metrics.TopKCategoricalAccuracy(5, 'acc5', dtype=tf.float32),
'val_acc1' : tf.keras.metrics.TopKCategoricalAccuracy(1, 'val_acc1', dtype=tf.float32),
'val_acc5' : tf.keras.metrics.TopKCategoricalAccuracy(5, 'val_acc5', dtype=tf.float32)})
with strategy.scope():
model = create_model(args, logger)
if args.summary:
model.summary()
return
# optimizer
lr_scheduler = OptionalLearningRateSchedule(args, steps_per_epoch, initial_epoch)
if args.optimizer == 'sgd':
optimizer = tf.keras.optimizers.SGD(lr_scheduler, momentum=.9, decay=.0001)
elif args.optimizer == 'rmsprop':
optimizer = tf.keras.optimizers.RMSprop(lr_scheduler)
elif args.optimizer == 'adam':
optimizer = tf.keras.optimizers.Adam(lr_scheduler)
# loss & generator
if args.loss == 'supcon':
criterion = supervised_contrastive(args, args.batch_size // strategy.num_replicas_in_sync)
train_generator = dataloader_supcon(args, trainset, 'train', args.batch_size)
val_generator = dataloader_supcon(args, valset, 'train', args.batch_size, shuffle=False)
elif args.loss == 'crossentropy':
criterion = crossentropy(args)
train_generator = dataloader(args, trainset, 'train', args.batch_size)
val_generator = dataloader(args, valset, 'val', args.batch_size, shuffle=False)
else:
raise ValueError()
train_generator = strategy.experimental_distribute_dataset(train_generator)
val_generator = strategy.experimental_distribute_dataset(val_generator)
csvlogger, train_writer, val_writer = create_callbacks(args, metrics)
logger.info("Build Model & Metrics")
##########################
# READY Train
##########################
train_iterator = iter(train_generator)
val_iterator = iter(val_generator)
# @tf.function
def do_step(iterator, mode):
def get_loss(inputs, labels, training=True):
logits = tf.cast(model(inputs, training=training), tf.float32)
loss = criterion(labels, logits)
loss_mean = tf.nn.compute_average_loss(loss, global_batch_size=args.batch_size)
return logits, loss, loss_mean
def step_fn(from_iterator):
if args.loss == 'supcon':
(img1, img2), labels = from_iterator
inputs = tf.concat([img1, img2], axis=0)
else:
inputs, labels = from_iterator
if mode == 'train':
with tf.GradientTape() as tape:
logits, loss, loss_mean = get_loss(inputs, labels)
grads = tape.gradient(loss_mean, model.trainable_variables)
optimizer.apply_gradients(list(zip(grads, model.trainable_variables)))
else:
logits, loss, loss_mean = get_loss(inputs, labels, training=False)
if args.loss == 'crossentropy':
metrics['acc' if mode == 'train' else 'val_acc'].update_state(labels, logits)
return loss
loss_per_replica = strategy.run(step_fn, args=(next(iterator),))
loss_mean = strategy.reduce(tf.distribute.ReduceOp.MEAN, loss_per_replica, axis=0)
metrics['loss' if mode == 'train' else 'val_loss'].update_state(loss_mean)
##########################
# Train
##########################
for epoch in range(initial_epoch, args.epochs):
print('\nEpoch {}/{}'.format(epoch+1, args.epochs))
print('Learning Rate : {}'.format(optimizer.learning_rate(optimizer.iterations)))
# train
print('Train')
progBar_train = tf.keras.utils.Progbar(steps_per_epoch, stateful_metrics=metrics.keys())
for step in range(steps_per_epoch):
do_step(train_iterator, 'train')
progBar_train.update(step, values=[(k, v.result()) for k, v in metrics.items() if not 'val' in k])
if args.tensorboard and args.tb_interval > 0:
if (epoch*steps_per_epoch+step) % args.tb_interval == 0:
with train_writer.as_default():
for k, v in metrics.items():
if not 'val' in k:
tf.summary.scalar(k, v.result(), step=epoch*steps_per_epoch+step)
if args.tensorboard and args.tb_interval == 0:
with train_writer.as_default():
for k, v in metrics.items():
if not 'val' in k:
tf.summary.scalar(k, v.result(), step=epoch)
# val
print('\n\nValidation')
progBar_val = tf.keras.utils.Progbar(validation_steps, stateful_metrics=metrics.keys())
for step in range(validation_steps):
do_step(val_iterator, 'val')
progBar_val.update(step, values=[(k, v.result()) for k, v in metrics.items() if 'val' in k])
# logs
logs = {k: v.result().numpy() for k, v in metrics.items()}
logs['epoch'] = epoch + 1
if args.checkpoint:
if args.loss == 'supcon':
ckpt_path = '{:04d}_{:.4f}.h5'.format(epoch+1, logs['val_loss'])
else:
ckpt_path = '{:04d}_{:.4f}_{:.4f}.h5'.format(epoch+1, logs['val_acc'], logs['val_loss'])
model.save_weights(
os.path.join(
args.result_path,
'{}/{}/checkpoint'.format(args.dataset, args.stamp),
ckpt_path))
print('\nSaved at {}'.format(
os.path.join(
args.result_path,
'{}/{}/checkpoint'.format(args.dataset, args.stamp),
ckpt_path)))
if args.history:
csvlogger = csvlogger.append(logs, ignore_index=True)
csvlogger.to_csv(os.path.join(args.result_path, '{}/{}/history/epoch.csv'.format(args.dataset, args.stamp)), index=False)
if args.tensorboard:
with train_writer.as_default():
tf.summary.scalar('loss', metrics['loss'].result(), step=epoch)
if args.loss == 'crossentropy':
tf.summary.scalar('acc', metrics['acc'].result(), step=epoch)
with val_writer.as_default():
tf.summary.scalar('val_loss', metrics['val_loss'].result(), step=epoch)
if args.loss == 'crossentropy':
tf.summary.scalar('val_acc', metrics['val_acc'].result(), step=epoch)
for k, v in metrics.items():
v.reset_states()
if __name__ == "__main__":
def check_arguments(args):
assert args.src_path is not None, 'src_path must be entered.'
assert args.data_path is not None, 'data_path must be entered.'
assert args.result_path is not None, 'result_path must be entered.'
return args
parser = argparse.ArgumentParser()
parser.add_argument("--backbone", type=str, default='resnet50')
parser.add_argument("--batch_size", type=int, default=32,
help="batch size per replica")
parser.add_argument("--classes", type=int, default=200)
parser.add_argument("--dataset", type=str, default='imagenet')
parser.add_argument("--img_size", type=int, default=224)
parser.add_argument("--steps", type=int, default=0)
parser.add_argument("--epochs", type=int, default=100)
parser.add_argument("--optimizer", type=str, default='sgd')
parser.add_argument("--lr", type=float, default=.001)
parser.add_argument("--loss", type=str, default='crossentropy', choices=['crossentropy', 'supcon'])
parser.add_argument("--temperature", type=float, default=0.007)
parser.add_argument("--augment", type=str, default='sim')
parser.add_argument("--randaug_layer", type=int, default=2)
parser.add_argument("--checkpoint", action='store_true')
parser.add_argument("--history", action='store_true')
parser.add_argument("--tensorboard", action='store_true')
parser.add_argument("--tb_interval", type=int, default=0)
parser.add_argument("--lr_mode", type=str, default='constant', choices=['constant', 'exponential', 'cosine'])
parser.add_argument("--lr_value", type=float, default=.1)
parser.add_argument("--lr_interval", type=str, default='20,50,80')
parser.add_argument("--lr_warmup", type=int, default=0)
parser.add_argument('--src_path', type=str, default='.')
parser.add_argument('--data_path', type=str, default=None)
parser.add_argument('--result_path', type=str, default='./result')
parser.add_argument('--snapshot', type=str, default=None)
parser.add_argument("--gpus", type=str, default=-1)
parser.add_argument("--summary", action='store_true')
parser.add_argument("--ignore_search", type=str, default='')
main(check_arguments(parser.parse_args()))