-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtrain.py
93 lines (74 loc) · 2.16 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
import datetime
from pathlib import Path
import tensorflow as tf
from callbacks import TensorboardReportImages
from dataset import load_dataset, prepare_dataset
from metrics import IOU
from model import build_model
DATASET_ROOT = Path("../dataset/")
OUT_ROOT = Path("../out/")
TENSORBOARD_LOG = Path("../logs/fit", datetime.datetime.now().strftime("%Y%m%d-%H%M%S"))
def make_dataset_split(split_root: Path, dataset_config: dict) -> tf.data.Dataset:
dataset = load_dataset(split_root)
dataset = prepare_dataset(dataset, batch_size=dataset_config["batch_size"])
return dataset
def make_dataset(
dataset_root: Path, dataset_config: dict
) -> dict[str, tf.data.Dataset]:
return {
split: make_dataset_split(dataset_root / split, dataset_config)
for split in ["train", "val", "test"]
}
def main():
dataset_config = {
"batch_size": 32,
}
train_config = {
"epochs": 300,
"learning_rate": 1e-3,
}
dataset = make_dataset(DATASET_ROOT, dataset_config)
model = build_model()
print(model.summary())
model.compile(
optimizer=tf.keras.optimizers.Adam(
learning_rate=tf.keras.optimizers.schedules.ExponentialDecay(
initial_learning_rate=train_config["learning_rate"],
decay_steps=5600,
decay_rate=0.1,
)
),
loss={
"bbox": "mse",
},
metrics=[
IOU(),
],
)
model.fit(
dataset["train"],
validation_data=dataset["val"],
epochs=train_config["epochs"],
callbacks=[
tf.keras.callbacks.TensorBoard(
TENSORBOARD_LOG,
),
TensorboardReportImages(
TENSORBOARD_LOG,
dataset["val"].take(1),
),
],
)
model.evaluate(
dataset["test"],
batch_size=64,
callbacks=[
TensorboardReportImages(
TENSORBOARD_LOG,
dataset["test"],
)
],
)
tf.keras.models.save_model(model, str(OUT_ROOT / "saved_models" / "model"))
if __name__ == "__main__":
main()