-
Notifications
You must be signed in to change notification settings - Fork 0
/
train.py
368 lines (307 loc) · 16.2 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
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
"""
EfficientPose (c) by Steinbeis GmbH & Co. KG für Technologietransfer
Haus der Wirtschaft, Willi-Bleicher-Straße 19, 70174 Stuttgart, Germany
Yannick Bukschat: yannick.bukschat@stw.de
Marcus Vetter: marcus.vetter@stw.de
EfficientPose is licensed under a
Creative Commons Attribution-NonCommercial 4.0 International License.
The license can be found in the LICENSE file in the root directory of this source tree
or at http://creativecommons.org/licenses/by-nc/4.0/.
---------------------------------------------------------------------------------------------------------------------------------
---------------------------------------------------------------------------------------------------------------------------------
Based on:
Keras EfficientDet implementation (https://github.com/xuannianz/EfficientDet) licensed under the Apache License, Version 2.0
---------------------------------------------------------------------------------------------------------------------------------
The official EfficientDet implementation (https://github.com/google/automl) licensed under the Apache License, Version 2.0
---------------------------------------------------------------------------------------------------------------------------------
EfficientNet Keras implementation (https://github.com/qubvel/efficientnet) licensed under the Apache License, Version 2.0
---------------------------------------------------------------------------------------------------------------------------------
Keras RetinaNet implementation (https://github.com/fizyr/keras-retinanet) licensed under
Copyright 2017-2018 Fizyr (https://fizyr.com)
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
"""
import argparse
import time
import os
import sys
import tensorflow as tf
from tensorflow import keras
from tensorflow.keras.optimizers import Adam
from model import build_EfficientPose
from losses import smooth_l1, focal, transformation_loss
from efficientnet import BASE_WEIGHTS_PATH, WEIGHTS_HASHES
from custom_load_weights import custom_load_weights
def parse_args(args):
"""
Parse the arguments.
"""
date_and_time = time.strftime("%d_%m_%Y_%H_%M_%S")
parser = argparse.ArgumentParser(description = 'Simple EfficientPose training script.')
subparsers = parser.add_subparsers(help = 'Arguments for specific dataset types.', dest = 'dataset_type')
subparsers.required = True
linemod_parser = subparsers.add_parser('linemod')
linemod_parser.add_argument('linemod_path', help = 'Path to dataset directory (ie. /Datasets/Linemod_preprocessed).')
linemod_parser.add_argument('--object-id', help = 'ID of the Linemod Object to train on', type = int, default = 8)
occlusion_parser = subparsers.add_parser('occlusion')
occlusion_parser.add_argument('occlusion_path', help = 'Path to dataset directory (ie. /Datasets/Linemod_preprocessed/).')
parser.add_argument('--rotation-representation', help = 'Which representation of the rotation should be used. Choose from "axis_angle", "rotation_matrix" and "quaternion"', default = 'axis_angle')
parser.add_argument('--weights', help = 'File containing weights to init the model parameter')
parser.add_argument('--freeze-backbone', help = 'Freeze training of backbone layers.', action = 'store_true')
parser.add_argument('--no-freeze-bn', help = 'Do not freeze training of BatchNormalization layers.', action = 'store_true')
parser.add_argument('--batch-size', help = 'Size of the batches.', default = 1, type = int)
parser.add_argument('--lr', help = 'Learning rate', default = 1e-4, type = float)
parser.add_argument('--no-color-augmentation', help = 'Do not use colorspace augmentation', action = 'store_true')
parser.add_argument('--no-6dof-augmentation', help = 'Do not use 6DoF augmentation', action = 'store_true')
parser.add_argument('--phi', help = 'Hyper parameter phi', default = 0, type = int, choices = (0, 1, 2, 3, 4, 5, 6))
parser.add_argument('--gpu', help = 'Id of the GPU to use (as reported by nvidia-smi).')
parser.add_argument('--epochs', help = 'Number of epochs to train.', type = int, default = 500)
parser.add_argument('--steps', help = 'Number of steps per epoch.', type = int, default = int(179 * 10))
parser.add_argument('--snapshot-path', help = 'Path to store snapshots of models during training', default = os.path.join("checkpoints", date_and_time))
parser.add_argument('--tensorboard-dir', help = 'Log directory for Tensorboard output', default = os.path.join("logs", date_and_time))
parser.add_argument('--no-snapshots', help = 'Disable saving snapshots.', dest = 'snapshots', action = 'store_false')
parser.add_argument('--no-evaluation', help = 'Disable per epoch evaluation.', dest = 'evaluation', action = 'store_false')
parser.add_argument('--compute-val-loss', help = 'Compute validation loss during training', dest = 'compute_val_loss', action = 'store_true')
parser.add_argument('--score-threshold', help = 'score threshold for non max suppresion', type = float, default = 0.5)
parser.add_argument('--validation-image-save-path', help = 'path where to save the predicted validation images after each epoch', default = None)
# Fit generator arguments
parser.add_argument('--multiprocessing', help = 'Use multiprocessing in fit_generator.', action = 'store_true')
parser.add_argument('--workers', help = 'Number of generator workers.', type = int, default = 4)
parser.add_argument('--max-queue-size', help = 'Queue length for multiprocessing workers in fit_generator.', type = int, default = 10)
print(vars(parser.parse_args(args)))
return parser.parse_args(args)
def main(args = None):
"""
Train an EfficientPose model.
Args:
args: parseargs object containing configuration for the training procedure.
"""
allow_gpu_growth_memory()
# parse arguments
if args is None:
args = sys.argv[1:]
args = parse_args(args)
# create the generators
print("\nCreating the Generators...")
train_generator, validation_generator = create_generators(args)
print("Done!")
num_rotation_parameters = train_generator.get_num_rotation_parameters()
num_classes = train_generator.num_classes()
num_anchors = train_generator.num_anchors
# optionally choose specific GPU
if args.gpu:
os.environ['CUDA_VISIBLE_DEVICES'] = args.gpu
print("\nBuilding the Model...")
model, prediction_model, all_layers = build_EfficientPose(args.phi,
num_classes = num_classes,
num_anchors = num_anchors,
freeze_bn = not args.no_freeze_bn,
score_threshold = args.score_threshold,
num_rotation_parameters = num_rotation_parameters)
print("Done!")
# load pretrained weights
if args.weights:
if args.weights == 'imagenet':
model_name = 'efficientnet-b{}'.format(args.phi)
file_name = '{}_weights_tf_dim_ordering_tf_kernels_autoaugment_notop.h5'.format(model_name)
file_hash = WEIGHTS_HASHES[model_name][1]
weights_path = keras.utils.get_file(file_name,
BASE_WEIGHTS_PATH + file_name,
cache_subdir='models',
file_hash=file_hash)
model.load_weights(weights_path, by_name=True)
else:
print('Loading model, this may take a second...')
custom_load_weights(filepath = args.weights, layers = all_layers, skip_mismatch = True)
print("\nDone!")
# freeze backbone layers
if args.freeze_backbone:
# 227, 329, 329, 374, 464, 566, 656
for i in range(1, [227, 329, 329, 374, 464, 566, 656][args.phi]):
model.layers[i].trainable = False
# compile model
model.compile(optimizer=Adam(lr = args.lr, clipnorm = 0.001),
loss={'regression': smooth_l1(),
'classification': focal(),
'transformation': transformation_loss(model_3d_points_np = train_generator.get_all_3d_model_points_array_for_loss(),
num_rotation_parameter = num_rotation_parameters)},
loss_weights = {'regression' : 1.0,
'classification': 1.0,
'transformation': 0.02})
# create the callbacks
callbacks = create_callbacks(
model,
prediction_model,
validation_generator,
args,
)
if not args.compute_val_loss:
validation_generator = None
elif args.compute_val_loss and validation_generator is None:
raise ValueError('When you have no validation data, you should not specify --compute-val-loss.')
# start training
return model.fit_generator(
generator = train_generator,
steps_per_epoch = args.steps,
initial_epoch = 0,
epochs = args.epochs,
verbose = 1,
callbacks = callbacks,
workers = args.workers,
use_multiprocessing = args.multiprocessing,
max_queue_size = args.max_queue_size,
validation_data = validation_generator
)
def allow_gpu_growth_memory():
"""
Set allow growth GPU memory to true
"""
config = tf.ConfigProto()
config.gpu_options.allow_growth = True
_ = tf.Session(config = config)
def create_callbacks(training_model, prediction_model, validation_generator, args):
"""
Creates the callbacks to use during training.
Args:
training_model: The model that is used for training.
prediction_model: The model that should be used for validation.
validation_generator: The generator for creating validation data.
args: parseargs args object.
Returns:
A list of callbacks used for training.
"""
callbacks = []
tensorboard_callback = None
if args.dataset_type == "linemod":
snapshot_path = os.path.join(args.snapshot_path, "object_" + str(args.object_id))
if args.validation_image_save_path:
save_path = os.path.join(args.validation_image_save_path, "object_" + str(args.object_id))
else:
save_path = args.validation_image_save_path
if args.tensorboard_dir:
tensorboard_dir = os.path.join(args.tensorboard_dir, "object_" + str(args.object_id))
if validation_generator.is_symmetric_object(args.object_id):
metric_to_monitor = "ADD-S"
mode = "max"
else:
metric_to_monitor = "ADD"
mode = "max"
elif args.dataset_type == "occlusion":
snapshot_path = os.path.join(args.snapshot_path, "occlusion")
if args.validation_image_save_path:
save_path = os.path.join(args.validation_image_save_path, "occlusion")
else:
save_path = args.validation_image_save_path
if args.tensorboard_dir:
tensorboard_dir = os.path.join(args.tensorboard_dir, "occlusion")
metric_to_monitor = "ADD(-S)"
mode = "max"
else:
snapshot_path = args.snapshot_path
save_path = args.validation_image_save_path
tensorboard_dir = args.tensorboard_dir
if save_path:
os.makedirs(save_path, exist_ok = True)
if tensorboard_dir:
tensorboard_callback = keras.callbacks.TensorBoard(
log_dir = tensorboard_dir,
histogram_freq = 0,
batch_size = args.batch_size,
write_graph = True,
write_grads = False,
write_images = False,
embeddings_freq = 0,
embeddings_layer_names = None,
embeddings_metadata = None
)
callbacks.append(tensorboard_callback)
if args.evaluation and validation_generator:
from eval.eval_callback import Evaluate
evaluation = Evaluate(validation_generator, prediction_model, tensorboard = tensorboard_callback, save_path = save_path)
callbacks.append(evaluation)
# save the model
if args.snapshots:
# ensure directory created first; otherwise h5py will error after epoch.
os.makedirs(snapshot_path, exist_ok = True)
checkpoint = keras.callbacks.ModelCheckpoint(os.path.join(snapshot_path, 'phi_{phi}_{dataset_type}_best_{metric}.h5'.format(phi = str(args.phi), metric = metric_to_monitor, dataset_type = args.dataset_type)),
verbose = 1,
#save_weights_only = True,
save_best_only = True,
monitor = metric_to_monitor,
mode = mode)
callbacks.append(checkpoint)
callbacks.append(keras.callbacks.ReduceLROnPlateau(
monitor = 'MixedAveragePointDistanceMean_in_mm',
factor = 0.5,
patience = 25,
verbose = 1,
mode = 'min',
min_delta = 0.0001,
cooldown = 0,
min_lr = 1e-7
))
return callbacks
def create_generators(args):
"""
Create generators for training and validation.
Args:
args: parseargs object containing configuration for generators.
Returns:
The training and validation generators.
"""
common_args = {
'batch_size': args.batch_size,
'phi': args.phi,
}
if args.dataset_type == 'linemod':
from generators.linemod import LineModGenerator
train_generator = LineModGenerator(
args.linemod_path,
args.object_id,
rotation_representation = args.rotation_representation,
use_colorspace_augmentation = not args.no_color_augmentation,
use_6DoF_augmentation = not args.no_6dof_augmentation,
**common_args
)
validation_generator = LineModGenerator(
args.linemod_path,
args.object_id,
train = False,
shuffle_dataset = False,
shuffle_groups = False,
rotation_representation = args.rotation_representation,
use_colorspace_augmentation = False,
use_6DoF_augmentation = False,
**common_args
)
elif args.dataset_type == 'occlusion':
from generators.occlusion import OcclusionGenerator
train_generator = OcclusionGenerator(
args.occlusion_path,
rotation_representation = args.rotation_representation,
use_colorspace_augmentation = not args.no_color_augmentation,
use_6DoF_augmentation = not args.no_6dof_augmentation,
**common_args
)
validation_generator = OcclusionGenerator(
args.occlusion_path,
train = False,
shuffle_dataset = False,
shuffle_groups = False,
rotation_representation = args.rotation_representation,
use_colorspace_augmentation = False,
use_6DoF_augmentation = False,
**common_args
)
else:
raise ValueError('Invalid data type received: {}'.format(args.dataset_type))
return train_generator, validation_generator
if __name__ == '__main__':
main()