-
Notifications
You must be signed in to change notification settings - Fork 0
/
train.py
81 lines (63 loc) · 2.25 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
import os
from model import UNet
from dataset import DataGenerator
import tensorflow as tf
from labeldata import labels
import matplotlib.pyplot as plt
CWD = os.path.dirname(__file__)
LEARNING_RATE = 1e-3
EPOCHS = 100
BATCH_SIZE = 16
IMAGE_HEIGHT = 128
IMAGE_WIDTH = 128
TRAIN_IMG_DIR = os.path.join(CWD, 'data/train')
VAL_IMG_DIR = os.path.join(CWD, 'data/val')
MODEL_PATH = os.path.join(CWD, 'model/')
N_CLASSES = len(labels)
def plot_model(history):
"""
Plots model accuracy and loss.
"""
plt.plot(history.history['accuracy'])
plt.plot(history.history['val_accuracy'])
plt.title('Model Accuracy')
plt.ylabel('Accuracy')
plt.xlabel('epoch')
plt.legend(['train', 'val'], loc = 'upper left')
plt.savefig(os.path.join(CWD, 'acc.png'))
plt.close()
plt.plot(history.history['loss'])
plt.plot(history.history['val_loss'])
plt.title('Model Loss')
plt.ylabel('Loss')
plt.xlabel('epoch')
plt.legend(['train', 'val'], loc = 'upper left')
plt.savefig(os.path.join(CWD, 'loss.png'))
plt.close()
def train(model, train_data, val_data):
"""
Trains the model, plots model accuracy and loss, and saves model.
Args:
model: model to train
train_data: Training data generator
val_data: Validation data generator
"""
callback = tf.keras.callbacks.EarlyStopping("val_accuracy", patience = 15, min_delta = 0.001, mode="max")
model.compile(optimizer=tf.keras.optimizers.legacy.Adam(learning_rate=LEARNING_RATE),
loss=tf.keras.losses.SparseCategoricalCrossentropy(from_logits=True),
metrics = ["accuracy"])
history = model.fit(train_data,
epochs=EPOCHS,
validation_data=val_data,
callbacks = [callback]
)
plot_model(history)
model.save(MODEL_PATH,save_format='tf')
if __name__ == "__main__":
tf.random.set_seed(42)
train_path = os.path.join(CWD, TRAIN_IMG_DIR)
val_path = os.path.join(CWD, VAL_IMG_DIR)
train_data_generator = DataGenerator(train_path, batch_size = BATCH_SIZE)
val_data_generator = DataGenerator(val_path, shuffle = False, batch_size = BATCH_SIZE)
model = UNet(N_CLASSES)
train(model, train_data_generator, val_data_generator)