-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathtrain.py
More file actions
102 lines (86 loc) · 4.07 KB
/
train.py
File metadata and controls
102 lines (86 loc) · 4.07 KB
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
import tensorflow as tf
import time
import numpy as np
from configuration import IMAGE_HEIGHT, IMAGE_WIDTH, CHANNELS, EPOCHS, NUM_CLASSES, BATCH_SIZE, save_model_dir, \
load_weights_before_training, load_weights_from_epoch, save_frequency, test_images_during_training, \
test_images_dir_list, print_frequency
from core.ground_truth import ReadDataset, MakeGT
from core.loss import SSDLoss
from core.make_dataset import TFDataset
from core.ssd import SSD, ssd_prediction
from utils.visualize import visualize_training_results
from core.models.resnet import ResNet50
def print_model_summary(network):
#network.build(input_shape=(None, IMAGE_HEIGHT, IMAGE_WIDTH, CHANNELS))
#network.summary()
img = np.random.random((1, 224, 224, 3)).astype('float32')
# extracted_features = features_extraction_model(img)
#print(network(img))
network(img)
network.summary()
if __name__ == '__main__':
# tests
#resnet = ResNet50()
#print_model_summary(network=resnet)
ssd = SSD()
print_model_summary(network=ssd)
#print(ssd.features_list)
#def toto():
# GPU settings
gpus = tf.config.list_physical_devices("GPU")
#if gpus:
# for gpu in gpus:
# tf.config.experimental.set_memory_growth(gpu, True)
dataset = TFDataset()
train_data, train_count = dataset.generate_datatset()
ssd = SSD()
print_model_summary(network=ssd)
if load_weights_before_training:
ssd.load_weights(filepath=save_model_dir+"epoch2-{}".format(load_weights_from_epoch))
print("Successfully load weights!")
else:
load_weights_from_epoch = -1
# loss
loss = SSDLoss()
# optimizer
optimizer = tf.optimizers.Adam(learning_rate=0.001)
# metrics
loss_metric = tf.metrics.Mean()
cls_loss_metric = tf.metrics.Mean()
reg_loss_metric = tf.metrics.Mean()
def train_step(batch_images, batch_labels):
with tf.GradientTape() as tape:
pred = ssd(batch_images, training=False)
output = ssd_prediction(feature_maps=pred, num_classes=NUM_CLASSES + 1)
gt = MakeGT(batch_labels, pred)
gt_boxes = gt.generate_gt_boxes()
loss_value, cls_loss, reg_loss = loss(y_true=gt_boxes, y_pred=output)
gradients = tape.gradient(loss_value, ssd.trainable_variables)
optimizer.apply_gradients(grads_and_vars=zip(gradients, ssd.trainable_variables))
loss_metric.update_state(values=loss_value)
cls_loss_metric.update_state(values=cls_loss)
reg_loss_metric.update_state(values=reg_loss)
for epoch in range(load_weights_from_epoch + 1, EPOCHS):
for step, batch_data in enumerate(train_data):
start_time = time.time()
images, labels = ReadDataset().read(batch_data)
train_step(batch_images=images, batch_labels=labels)
spent_time = time.time() - start_time
if step % print_frequency == 0:
print("Epoch: {}/{}, step: {}/{}, time spent: {:.2f}s, loss: {:.5f}, "
"cls loss: {:.5f}, reg loss: {:.5f}".format(epoch,
EPOCHS,
step,
tf.math.ceil(train_count / BATCH_SIZE),
spent_time,
loss_metric.result(),
cls_loss_metric.result(),
reg_loss_metric.result()))
loss_metric.reset_states()
cls_loss_metric.reset_states()
reg_loss_metric.reset_states()
if epoch % save_frequency == 0:
ssd.save_weights(filepath=save_model_dir+"epoch2-{}".format(epoch), save_format="tf")
if test_images_during_training:
visualize_training_results(pictures=test_images_dir_list, model=ssd, epoch=epoch)
ssd.save_weights(filepath=save_model_dir+"saved_model", save_format="tf")