-
Notifications
You must be signed in to change notification settings - Fork 4
/
training_cli.py
188 lines (157 loc) · 4.54 KB
/
training_cli.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
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
from mesonet.model import build_model
from mesonet.train import train_model
def execute_train(args):
args = vars(args)
args["train_data_dir"] = args["path"]
args.pop("func", None)
args.pop("path", None)
model = build_model()
return train_model(model=model, **args)
def train_parser(subparser):
parser = subparser.add_parser(
"train",
description="Train the MesoNet architecture as detailed in the paper.",
)
data_group = parser.add_argument_group(
"data directory, augmentations and validation split",
)
data_group.add_argument(
"path",
help="path to the directory containing the training data",
)
data_group.add_argument(
"-nda",
"--no-def-aug",
action="store_false",
help="disable the default augmentations used in the paper",
dest="use_default_augmentation",
)
data_group.add_argument(
"-s",
"--validation-split",
type=float,
default=None,
metavar="SPLIT",
help="size of the validation split as a proportion (default: %(default)s)",
)
epoch_group = parser.add_argument_group("epochs, batch size")
epoch_group.add_argument(
"-e",
"--epochs",
type=int,
default=30,
help="#epochs the model should be trained for (default: %(default)s)",
)
epoch_group.add_argument(
"-bs",
"--batch-size",
type=int,
default=32,
metavar="BS",
help="batch size for the dataset (default: %(default)s)",
)
optimizer_group = parser.add_argument_group(
"compilation, learning rate and learning rate decay",
)
optimizer_group.add_argument(
"-nc",
"--no-compile",
action="store_false",
help="disable model compilation (useful for resuming training)",
dest="compile",
)
optimizer_group.add_argument(
"-lo",
"--loss",
default="binary_crossentropy",
help="loss function (default: %(default)s)",
)
optimizer_group.add_argument(
"-lr",
"--learning-rate",
type=float,
default=1e-3,
metavar="LR",
help="(initial) learning rate of the model (default: %(default)s)",
dest="lr",
)
optimizer_group.add_argument(
"-nld",
"--no-decay",
action="store_false",
help="disable learning rate decay",
dest="lr_decay",
)
optimizer_group.add_argument(
"-dr",
"--decay-rate",
type=float,
default=1e-1,
metavar="RATE",
help="rate at which the learning rate should decay (default: %(default)s)",
)
optimizer_group.add_argument(
"-dl",
"--decay-limit",
type=float,
default=1e-6,
metavar="LIMIT",
help="learning rate value after which decay should stop (default: %(default)s)",
)
stop_early_group = parser.add_argument_group("stop early, monitor, mode, patience")
stop_early_group.add_argument(
"-nse",
"--no-stop-early",
action="store_false",
help="disable EarlyStopping callback",
dest="stop_early",
)
stop_early_group.add_argument(
"-m",
"--monitor",
default="val_accuracy",
help="metric to monitor for the EarlyStopping callback (default: %(default)s)",
)
stop_early_group.add_argument(
"--mode",
default="max",
choices=["max", "min", "auto"],
help=(
"see https://keras.io/api/callbacks/early_stopping/"
"(default: %(default)s)"
),
)
stop_early_group.add_argument(
"-pa",
"--patience",
type=int,
default=20,
help=(
"number of epochs with no improvement after which "
"training will be stopped (default: %(default)s)"
),
)
misc_group = parser.add_argument_group("checkpoint, tensorboard, plot")
misc_group.add_argument(
"-ncp",
"--no-checkpoint",
action="store_false",
help="disabe ModelCheckpoint callback",
dest="checkpoint",
)
misc_group.add_argument(
"-ntb",
"--no-tensorboard",
action="store_false",
help="disable TensorBoard callback",
dest="tensorboard",
)
misc_group.add_argument(
"-np",
"--no-plot",
action="store_false",
help="disable plotting of loss curve",
dest="loss_curve",
)
parser.set_defaults(func=execute_train)
return parser