forked from ChunML/ssd-tf2
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtrain.py
executable file
·141 lines (112 loc) · 5.36 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
import argparse
import tensorflow as tf
import os
import sys
import time
import yaml
from tensorflow.keras.optimizers.schedules import PiecewiseConstantDecay
from voc_data import create_batch_generator
from anchor import generate_default_boxes
from network import create_ssd
from losses import create_losses
parser = argparse.ArgumentParser()
parser.add_argument('--data-dir', default='../dataset')
parser.add_argument('--data-year', default='2007')
parser.add_argument('--arch', default='ssd300')
parser.add_argument('--batch-size', default=32, type=int)
parser.add_argument('--num-batches', default=-1, type=int)
parser.add_argument('--neg-ratio', default=3, type=int)
parser.add_argument('--initial-lr', default=1e-3, type=float)
parser.add_argument('--momentum', default=0.9, type=float)
parser.add_argument('--weight-decay', default=5e-4, type=float)
parser.add_argument('--num-epochs', default=120, type=int)
parser.add_argument('--checkpoint-dir', default='checkpoints')
parser.add_argument('--pretrained-type', default='base')
parser.add_argument('--gpu-id', default='0')
args = parser.parse_args()
os.environ['CUDA_VISIBLE_DEVICES'] = args.gpu_id
NUM_CLASSES = 21
@tf.function
def train_step(imgs, gt_confs, gt_locs, ssd, criterion, optimizer):
with tf.GradientTape() as tape:
confs, locs = ssd(imgs)
conf_loss, loc_loss = criterion(
confs, locs, gt_confs, gt_locs)
loss = conf_loss + loc_loss
l2_loss = [tf.nn.l2_loss(t) for t in ssd.trainable_variables]
l2_loss = args.weight_decay * tf.math.reduce_sum(l2_loss)
loss += l2_loss
gradients = tape.gradient(loss, ssd.trainable_variables)
optimizer.apply_gradients(zip(gradients, ssd.trainable_variables))
return loss, conf_loss, loc_loss, l2_loss
if __name__ == '__main__':
os.makedirs(args.checkpoint_dir, exist_ok=True)
with open('./config.yml') as f:
cfg = yaml.load(f)
try:
config = cfg[args.arch.upper()]
except AttributeError:
raise ValueError('Unknown architecture: {}'.format(args.arch))
default_boxes = generate_default_boxes(config)
batch_generator, val_generator, info = create_batch_generator(
args.data_dir, args.data_year, default_boxes,
config['image_size'],
args.batch_size, args.num_batches,
mode='train', augmentation=['flip']) # the patching algorithm is currently causing bottleneck sometimes
try:
ssd = create_ssd(NUM_CLASSES, args.arch,
args.pretrained_type,
checkpoint_dir=args.checkpoint_dir)
except Exception as e:
print(e)
print('The program is exiting...')
sys.exit()
criterion = create_losses(args.neg_ratio, NUM_CLASSES)
steps_per_epoch = info['length'] // args.batch_size
lr_fn = PiecewiseConstantDecay(
boundaries=[int(steps_per_epoch * args.num_epochs * 2 / 3),
int(steps_per_epoch * args.num_epochs * 5 / 6)],
values=[args.initial_lr, args.initial_lr * 0.1, args.initial_lr * 0.01])
optimizer = tf.keras.optimizers.SGD(
learning_rate=lr_fn,
momentum=args.momentum)
train_log_dir = 'logs/train'
val_log_dir = 'logs/val'
train_summary_writer = tf.summary.create_file_writer(train_log_dir)
val_summary_writer = tf.summary.create_file_writer(val_log_dir)
for epoch in range(args.num_epochs):
avg_loss = 0.0
avg_conf_loss = 0.0
avg_loc_loss = 0.0
start = time.time()
for i, (_, imgs, gt_confs, gt_locs) in enumerate(batch_generator):
loss, conf_loss, loc_loss, l2_loss = train_step(
imgs, gt_confs, gt_locs, ssd, criterion, optimizer)
avg_loss = (avg_loss * i + loss.numpy()) / (i + 1)
avg_conf_loss = (avg_conf_loss * i + conf_loss.numpy()) / (i + 1)
avg_loc_loss = (avg_loc_loss * i + loc_loss.numpy()) / (i + 1)
if (i + 1) % 50 == 0:
print('Epoch: {} Batch {} Time: {:.2}s | Loss: {:.4f} Conf: {:.4f} Loc: {:.4f}'.format(
epoch + 1, i + 1, time.time() - start, avg_loss, avg_conf_loss, avg_loc_loss))
avg_val_loss = 0.0
avg_val_conf_loss = 0.0
avg_val_loc_loss = 0.0
for i, (_, imgs, gt_confs, gt_locs) in enumerate(val_generator):
val_confs, val_locs = ssd(imgs)
val_conf_loss, val_loc_loss = criterion(
val_confs, val_locs, gt_confs, gt_locs)
val_loss = val_conf_loss + val_loc_loss
avg_val_loss = (avg_val_loss * i + val_loss.numpy()) / (i + 1)
avg_val_conf_loss = (avg_val_conf_loss * i + val_conf_loss.numpy()) / (i + 1)
avg_val_loc_loss = (avg_val_loc_loss * i + val_loc_loss.numpy()) / (i + 1)
with train_summary_writer.as_default():
tf.summary.scalar('loss', avg_loss, step=epoch)
tf.summary.scalar('conf_loss', avg_conf_loss, step=epoch)
tf.summary.scalar('loc_loss', avg_loc_loss, step=epoch)
with val_summary_writer.as_default():
tf.summary.scalar('loss', avg_val_loss, step=epoch)
tf.summary.scalar('conf_loss', avg_val_conf_loss, step=epoch)
tf.summary.scalar('loc_loss', avg_val_loc_loss, step=epoch)
if (epoch + 1) % 10 == 0:
ssd.save_weights(
os.path.join(args.checkpoint_dir, 'ssd_epoch_{}.h5'.format(epoch + 1)))