-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathtrain.py
221 lines (186 loc) · 6.32 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
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
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
import os
import argparse
import tensorflow as tf
from autoencoder.autoencoder import AutoEncoder
from processing.preprocessing import Preprocessor
import inspection
import logging
logging.basicConfig(level=logging.INFO)
logger = logging.getLogger(__name__)
"""
Valid combinations for input arguments for architecture, color_mode and loss:
+----------------+----------------+
| Model Architecture |
+----------------+----------------+
| mvtecCAE | ResnetCAE |
| baselineCAE | |
| inceptionCAE | |
========================+================+================+
|| | | |
|| grayscale | ssim, l2 | ssim, l2 |
Color || | | |
Mode ----------------+----------------+----------------+
|| | | |
|| RGB | mssim, l2 | mssim, l2 |
|| | | |
--------+---------------+----------------+----------------+
"""
def main(args):
# get parsed arguments from user
input_dir = args.input_dir
architecture = args.architecture
color_mode = args.color
loss = args.loss
batch_size = args.batch
epochs = args.epochs
lr_estimate = args.lr_estimate
policy = args.policy
# check arguments
check_arguments(architecture, color_mode, loss)
# get autoencoder
autoencoder = AutoEncoder(input_dir, architecture, color_mode, loss, batch_size)
# load data as generators that yield batches of preprocessed images
preprocessor = Preprocessor(
input_directory=input_dir,
rescale=autoencoder.rescale,
shape=autoencoder.shape,
color_mode=autoencoder.color_mode,
)
train_generator = preprocessor.get_train_generator(
batch_size=autoencoder.batch_size, shuffle=True
)
validation_generator = preprocessor.get_val_generator(
batch_size=autoencoder.batch_size, shuffle=False, purpose="val",
)
# find best learning rates for training
lr_opt = autoencoder.find_lr_opt(train_generator, validation_generator, lr_estimate)
# train with optimal learning rate
autoencoder.fit(lr_opt=lr_opt, epochs=epochs, policy=policy)
# save model and configuration
autoencoder.save()
# inspect validation and test images for visual assessement
if args.inspect:
inspection.inspect_images(model_path=autoencoder.save_path)
logger.info("done.")
return
def check_arguments(architecture, color_mode, loss):
if loss == "mssim" and color_mode == "grayscale":
raise ValueError("MSSIM works only with rgb images")
if loss == "ssim" and color_mode == "rgb":
raise ValueError("SSIM works only with grayscale images")
return
def print_info():
if tf.test.is_gpu_available():
print("GPU was detected...")
else:
print("No GPU was detected. CNNs can be very slow without a GPU...")
print("Tensorflow version: {} ...".format(tf.__version__))
print("Keras version: {} ...".format(tf.keras.__version__))
return
def create_parser():
parser = argparse.ArgumentParser(
description="Train an AutoEncoder on an image dataset.",
epilog="Example usage: python3 train.py -d mvtec/capsule -a mvtec2 -b 8 -l ssim -c grayscale",
)
parser.add_argument(
"-d",
"--input-dir",
type=str,
required=True,
metavar="",
help="directory containing training images",
)
parser.add_argument(
"-a",
"--architecture",
type=str,
required=False,
metavar="",
choices=[
"anoCAE",
"mvtecCAE",
"baselineCAE",
"inceptionCAE",
"resnetCAE",
"skipCAE",
],
default="mvtec2",
help="architecture of the model to use for training: 'mvtecCAE', 'baselineCAE', 'inceptionCAE', 'resnetCAE' or 'skipCAE'",
)
parser.add_argument(
"-c",
"--color",
type=str,
required=False,
metavar="",
choices=["rgb", "grayscale"],
default="grayscale",
help="color mode for preprocessing images before training: 'rgb' or 'grayscale'",
)
parser.add_argument(
"-l",
"--loss",
type=str,
required=False,
metavar="",
choices=["mssim", "ssim", "l2"],
default="ssim",
help="loss function to use for training: 'mssim', 'ssim' or 'l2'",
)
parser.add_argument(
"-b",
"--batch",
type=int,
required=False,
metavar="",
default=8,
help="batch size to use for training",
)
parser.add_argument(
"-e",
"--epochs",
type=int,
required=False,
metavar="",
default=None,
help="Number of epochs to train",
)
parser.add_argument(
"-r",
"--lr-estimate",
type=str,
required=False,
metavar="",
choices=["custom", "ktrain"],
default="custom",
help="method to find optimal learning rate",
)
parser.add_argument(
"-p",
"--policy",
type=str,
required=False,
metavar="",
choices=["cyclic", "1cycle"],
default="cyclic",
help="learning rate policy to train with",
)
parser.add_argument(
"-i",
"--inspect",
action="store_true",
help="generate inspection plots after training",
)
return parser
if __name__ == "__main__":
parser = create_parser() # added
args = parser.parse_args()
print_info()
# run main function
main(args)
# Examples of commands to initiate training with mvtec architecture LEGO_cb
# python3 train.py -d LEGO_cb -a anoCAE -b 8 -l mssim -c rgb -r custom --inspect
# python3 train.py -d LEGO_cb -a baselineCAE -b 8 -l mssim -c rgb -r custom --inspect
# python3 train.py -d LEGO_cb -a inceptionCAE -b 8 -l mssim -c rgb -r custom --inspect
# python3 train.py -d LEGO_cb -a mvtecCAE -b 8 -l mssim -c rgb -r custom --inspect
# python3 train.py -d LEGO_cb -a resnetCAE -b 8 -l mssim -c rgb -r custom --inspect