-
Notifications
You must be signed in to change notification settings - Fork 11
/
train.py
63 lines (54 loc) · 1.82 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
import os
os.environ["TF_CPP_MIN_LOG_LEVEL"] = "2"
import numpy as np
import tensorflow as tf
from model import build_unet
from data import load_dataset, tf_dataset
from tensorflow.keras.callbacks import ModelCheckpoint, ReduceLROnPlateau, CSVLogger, EarlyStopping
if __name__ == "__main__":
""" Hyperparamaters """
dataset_path = "people_segmentation"
input_shape = (256, 256, 3)
batch_size = 12
epochs = 100
lr = 1e-4
model_path = "unet.h5"
csv_path = "data.csv"
""" Load the dataset """
(train_x, train_y), (test_x, test_y) = load_dataset(dataset_path)
print(f"Train: {len(train_x)} - {len(train_y)}")
print(f"Test: {len(test_x)} - {len(test_y)}")
train_dataset = tf_dataset(train_x, train_y, batch=batch_size)
test_dataset = tf_dataset(test_x, test_y, batch=batch_size)
""" Model """
model = build_unet(input_shape)
model.compile(
loss="binary_crossentropy",
optimizer=tf.keras.optimizers.Adam(lr),
metrics=[
tf.keras.metrics.MeanIoU(num_classes=2),
tf.keras.metrics.Recall(),
tf.keras.metrics.Precision()
]
)
# model.summary()
callbacks = [
ModelCheckpoint(model_path, monitor="val_loss", verbose=1),
ReduceLROnPlateau(monitor="val_loss", patience=5, factor=0.1, verbose=1),
CSVLogger(csv_path),
EarlyStopping(monitor="val_loss", patience=10)
]
train_steps = len(train_x)//batch_size
if len(train_x) % batch_size != 0:
train_steps += 1
test_steps = len(test_x)//batch_size
if len(test_x) % batch_size != 0:
test_steps += 1
model.fit(
train_dataset,
validation_data=test_dataset,
epochs=epochs,
steps_per_epoch=train_steps,
validation_steps=test_steps,
callbacks=callbacks
)