-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathTrain_YOLO.py
307 lines (265 loc) · 9.76 KB
/
Train_YOLO.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
"""
MODIFIED FROM keras-yolo3 PACKAGE, https://github.com/qqwweee/keras-yolo3
Retrain the YOLO model for your own dataset.
10-26-20 MODIFIED by bertelschmitt to use new repo name if changed to something else than "TrainYourOwnYOLO"
10-31-20 UPDATED by bertelschmitt to reflect TrainYourOwnYOLO versions as of 10-31-20
"""
import os
import sys
import argparse
import warnings
def get_parent_dir(n=1):
"""returns the n-th parent dicrectory of the current
working directory"""
current_path = os.path.dirname(os.path.abspath(__file__))
for _ in range(n):
current_path = os.path.dirname(current_path)
return current_path
src_path = os.path.join(get_parent_dir(0), "src")
sys.path.append(src_path)
utils_path = os.path.join(get_parent_dir(1), "Utils")
sys.path.append(utils_path)
import numpy as np
import keras.backend as K
from keras.layers import Input, Lambda
from keras.models import Model
from keras.optimizers import Adam
from keras.callbacks import (
TensorBoard,
ModelCheckpoint,
ReduceLROnPlateau,
EarlyStopping,
)
from keras_yolo3.yolo3.model import (
preprocess_true_boxes,
yolo_body,
tiny_yolo_body,
yolo_loss,
)
from keras_yolo3.yolo3.utils import get_random_data
from PIL import Image
from time import time
import tensorflow.compat.v1 as tf
import pickle
from Train_Utils import (
get_classes,
get_anchors,
create_model,
create_tiny_model,
data_generator,
data_generator_wrapper,
ChangeToOtherMachine,
)
keras_path = os.path.join(src_path, "keras_yolo3")
Data_Folder = os.path.join(get_parent_dir(1), "Data")
Image_Folder = os.path.join(Data_Folder, "Source_Images", "Training_Images")
VoTT_Folder = os.path.join(Image_Folder, "vott-csv-export")
YOLO_filename = os.path.join(VoTT_Folder, "data_train.txt")
Model_Folder = os.path.join(Data_Folder, "Model_Weights")
YOLO_classname = os.path.join(Model_Folder, "data_classes.txt")
log_dir = Model_Folder
anchors_path = os.path.join(keras_path, "model_data", "yolo_anchors.txt")
weights_path = os.path.join(keras_path, "yolo.h5")
#10-26-20 get name of current repo, which should be the directory one down from ours
current_repo = get_parent_dir(1).rsplit('/', 1)[1]
FLAGS = None
if __name__ == "__main__":
# Delete all default flags
parser = argparse.ArgumentParser(argument_default=argparse.SUPPRESS)
"""
Command line options
"""
parser.add_argument(
"--annotation_file",
type=str,
default=YOLO_filename,
help="Path to annotation file for Yolo. Default is " + YOLO_filename,
)
parser.add_argument(
"--classes_file",
type=str,
default=YOLO_classname,
help="Path to YOLO classnames. Default is " + YOLO_classname,
)
parser.add_argument(
"--log_dir",
type=str,
default=log_dir,
help="Folder to save training logs and trained weights to. Default is "
+ log_dir,
)
parser.add_argument(
"--anchors_path",
type=str,
default=anchors_path,
help="Path to YOLO anchors. Default is " + anchors_path,
)
parser.add_argument(
"--weights_path",
type=str,
default=weights_path,
help="Path to pre-trained YOLO weights. Default is " + weights_path,
)
parser.add_argument(
"--val_split",
type=float,
default=0.1,
help="Percentage of training set to be used for validation. Default is 10%.",
)
parser.add_argument(
"--is_tiny",
default=False,
action="store_true",
help="Use the tiny Yolo version for better performance and less accuracy. Default is False.",
)
parser.add_argument(
"--random_seed",
type=float,
default=None,
help="Random seed value to make script deterministic. Default is 'None', i.e. non-deterministic.",
)
parser.add_argument(
"--epochs",
type=int,
default=51,
help="Number of epochs for training last layers and number of epochs for fine-tuning layers. Default is 51.",
)
parser.add_argument(
"--warnings",
default=False,
action="store_true",
help="Display warning messages. Default is False.",
)
FLAGS = parser.parse_args()
if not FLAGS.warnings:
tf.logging.set_verbosity(tf.logging.ERROR)
os.environ["TF_CPP_MIN_LOG_LEVEL"] = "3"
warnings.filterwarnings("ignore")
#Backported w/o change 10/31/20 from TrainYourOwnYOLO version as of 10/31/20 by BS
# Get WandB integration if setup
try:
import wandb
from wandb.integration.keras import WandbCallback # type: ignore
wandb.ensure_configured()
if wandb.api.api_key is None:
_has_wandb = False
wandb.termwarn(
"W&B installed but not logged in. Run `wandb login` or set the WANDB_API_KEY env variable."
)
else:
_has_wandb = False if os.getenv("WANDB_DISABLED") else True
except (ImportError, AttributeError):
_has_wandb = False
np.random.seed(FLAGS.random_seed)
log_dir = FLAGS.log_dir
class_names = get_classes(FLAGS.classes_file)
num_classes = len(class_names)
if FLAGS.is_tiny and FLAGS.weights_path == weights_path:
weights_path = os.path.join(os.path.dirname(FLAGS.weights_path), "yolo-tiny.h5")
if FLAGS.is_tiny and FLAGS.anchors_path == anchors_path:
anchors_path = os.path.join(
os.path.dirname(FLAGS.anchors_path), "yolo-tiny_anchors.txt"
)
anchors = get_anchors(anchors_path)
input_shape = (416, 416) # multiple of 32, height, width
epoch1, epoch2 = FLAGS.epochs, FLAGS.epochs
is_tiny_version = len(anchors) == 6 # default setting
if FLAGS.is_tiny:
model = create_tiny_model(
input_shape, anchors, num_classes, freeze_body=2, weights_path=weights_path
)
else:
model = create_model(
input_shape, anchors, num_classes, freeze_body=2, weights_path=weights_path
) # make sure you know what you freeze
log_dir_time = os.path.join(log_dir, "{}".format(int(time())))
logging = TensorBoard(log_dir=log_dir_time)
checkpoint = ModelCheckpoint(
os.path.join(log_dir, "checkpoint.h5"),
monitor="val_loss",
save_weights_only=True,
save_best_only=True,
period=5,
)
reduce_lr = ReduceLROnPlateau(monitor="val_loss", factor=0.1, patience=3, verbose=1)
early_stopping = EarlyStopping(
monitor="val_loss", min_delta=0, patience=10, verbose=1
)
val_split = FLAGS.val_split
with open(FLAGS.annotation_file) as f:
lines = f.readlines()
# This step makes sure that the path names correspond to the local machine
# This is important if annotation and training are done on different machines (e.g. training on AWS)
# 10-26-20 Changed by bertelschmitt to call with current_repo
lines = ChangeToOtherMachine(lines, remote_machine="", repo=current_repo)
np.random.shuffle(lines)
num_val = int(len(lines) * val_split)
num_train = len(lines) - num_val
# From here on down, all backported w/o change 10/31/20 from TrainYourOwnYOLO version as of 10/31/20 by BS
# Train with frozen layers first, to get a stable loss.
# Adjust num epochs to your dataset. This step is enough to obtain a decent model.
frozen_callbacks = [logging, checkpoint]
if _has_wandb:
wandb.init(
project="TrainYourOwnYOLO", config=vars(FLAGS), sync_tensorboard=False
)
wandb_callback = WandbCallback(save_model=False)
frozen_callbacks.append(wandb_callback)
model.compile(
optimizer=Adam(lr=1e-3),
loss={
# use custom yolo_loss Lambda layer.
"yolo_loss": lambda y_true, y_pred: y_pred
},
)
batch_size = 32
print(
"Train on {} samples, val on {} samples, with batch size {}.".format(
num_train, num_val, batch_size
)
)
history = model.fit_generator(
data_generator_wrapper(
lines[:num_train], batch_size, input_shape, anchors, num_classes
),
steps_per_epoch=max(1, num_train // batch_size),
validation_data=data_generator_wrapper(
lines[num_train:], batch_size, input_shape, anchors, num_classes
),
validation_steps=max(1, num_val // batch_size),
epochs=epoch1,
initial_epoch=0,
callbacks=frozen_callbacks,
)
model.save_weights(os.path.join(log_dir, "trained_weights_stage_1.h5"))
# Unfreeze and continue training, to fine-tune.
# Train longer if the result is unsatisfactory.
full_callbacks = [logging, checkpoint, reduce_lr, early_stopping]
if _has_wandb:
full_callbacks.append(wandb_callback)
for i in range(len(model.layers)):
model.layers[i].trainable = True
model.compile(
optimizer=Adam(lr=1e-4), loss={"yolo_loss": lambda y_true, y_pred: y_pred}
) # recompile to apply the change
print("Unfreeze all layers.")
batch_size = 4 # note that more GPU memory is required after unfreezing the body
print(
"Train on {} samples, val on {} samples, with batch size {}.".format(
num_train, num_val, batch_size
)
)
history = model.fit_generator(
data_generator_wrapper(
lines[:num_train], batch_size, input_shape, anchors, num_classes
),
steps_per_epoch=max(1, num_train // batch_size),
validation_data=data_generator_wrapper(
lines[num_train:], batch_size, input_shape, anchors, num_classes
),
validation_steps=max(1, num_val // batch_size),
epochs=epoch1 + epoch2,
initial_epoch=epoch1,
callbacks=full_callbacks,
)
model.save_weights(os.path.join(log_dir, "trained_weights_final.h5"))