forked from smart-ad/face-estimation
-
Notifications
You must be signed in to change notification settings - Fork 0
/
train.py
144 lines (124 loc) · 5.51 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
142
143
144
import pandas as pd
import logging
import argparse
from pathlib import Path
import numpy as np
from keras.callbacks import LearningRateScheduler, ModelCheckpoint
from keras.optimizers import SGD, Adam
from keras.utils import np_utils
from wide_resnet import WideResNet
from utils import load_data
from keras.preprocessing.image import ImageDataGenerator
from mixup_generator import MixupGenerator
from random_eraser import get_random_eraser
logging.basicConfig(level=logging.DEBUG)
def get_args():
parser = argparse.ArgumentParser(description="This script trains the CNN model for age and gender estimation.",
formatter_class=argparse.ArgumentDefaultsHelpFormatter)
parser.add_argument("--input", "-i", type=str, required=True,
help="path to input database mat file")
parser.add_argument("--batch_size", type=int, default=12,
help="batch size")
parser.add_argument("--nb_epochs", type=int, default=100,
help="number of epochs")
parser.add_argument("--lr", type=float, default=0.01,
help="initial learning rate")
parser.add_argument("--opt", type=str, default="sgd",
help="optimizer name; 'sgd' or 'adam'")
parser.add_argument("--depth", type=int, default=16,
help="depth of network (should be 10, 16, 22, 28, ...)")
parser.add_argument("--width", type=int, default=8,
help="width of network")
parser.add_argument("--validation_split", type=float, default=0.1,
help="validation split ratio")
parser.add_argument("--aug", action="store_true",
help="use data augmentation if set true")
parser.add_argument("--output_path", type=str, default="checkpoints",
help="checkpoint dir")
args = parser.parse_args()
return args
class Schedule:
def __init__(self, nb_epochs, initial_lr):
self.epochs = nb_epochs
self.initial_lr = initial_lr
def __call__(self, epoch_idx):
if epoch_idx < self.epochs * 0.25:
return self.initial_lr
elif epoch_idx < self.epochs * 0.50:
return self.initial_lr * 0.2
elif epoch_idx < self.epochs * 0.75:
return self.initial_lr * 0.04
return self.initial_lr * 0.008
def get_optimizer(opt_name, lr):
if opt_name == "sgd":
return SGD(lr=lr, momentum=0.9, nesterov=True)
elif opt_name == "adam":
return Adam(lr=lr)
else:
raise ValueError("optimizer name should be 'sgd' or 'adam'")
def main():
args = get_args()
input_path = args.input
batch_size = args.batch_size
nb_epochs = args.nb_epochs
lr = args.lr
opt_name = args.opt
depth = args.depth
k = args.width
validation_split = args.validation_split
use_augmentation = args.aug
output_path = Path(__file__).resolve().parent.joinpath(args.output_path)
output_path.mkdir(parents=True, exist_ok=True)
logging.debug("Loading data...")
image, gender, age, _, image_size, _ = load_data(input_path)
X_data = image
y_data_g = np_utils.to_categorical(gender, 2)
y_data_a = np_utils.to_categorical(age, 101)
model = WideResNet(image_size, depth=depth, k=k)()
opt = get_optimizer(opt_name, lr)
model.compile(optimizer=opt, loss=["categorical_crossentropy", "categorical_crossentropy"],
metrics=['accuracy'])
logging.debug("Model summary...")
model.count_params()
model.summary()
callbacks = [LearningRateScheduler(schedule=Schedule(nb_epochs, lr)),
ModelCheckpoint(str(output_path) + "/weights.{epoch:02d}-{val_loss:.2f}.hdf5",
monitor="val_loss",
verbose=1,
save_best_only=True,
mode="auto")
]
logging.debug("Running training...")
data_num = len(X_data)
indexes = np.arange(data_num)
np.random.shuffle(indexes)
X_data = X_data[indexes]
y_data_g = y_data_g[indexes]
y_data_a = y_data_a[indexes]
train_num = int(data_num * (1 - validation_split))
X_train = X_data[:train_num]
X_test = X_data[train_num:]
y_train_g = y_data_g[:train_num]
y_test_g = y_data_g[train_num:]
y_train_a = y_data_a[:train_num]
y_test_a = y_data_a[train_num:]
if use_augmentation:
datagen = ImageDataGenerator(
width_shift_range=0.1,
height_shift_range=0.1,
horizontal_flip=True,
preprocessing_function=get_random_eraser(v_l=0, v_h=255))
training_generator = MixupGenerator(X_train, [y_train_g, y_train_a], batch_size=batch_size, alpha=0.2,
datagen=datagen)()
hist = model.fit_generator(generator=training_generator,
steps_per_epoch=train_num // batch_size,
validation_data=(X_test, [y_test_g, y_test_a]),
epochs=nb_epochs, verbose=1,
callbacks=callbacks)
else:
hist = model.fit(X_train, [y_train_g, y_train_a], batch_size=batch_size, epochs=nb_epochs, callbacks=callbacks,
validation_data=(X_test, [y_test_g, y_test_a]))
logging.debug("Saving history...")
pd.DataFrame(hist.history).to_hdf(output_path.joinpath("history_{}_{}.h5".format(depth, k)), "history")
if __name__ == '__main__':
main()