-
Notifications
You must be signed in to change notification settings - Fork 0
/
train.py
323 lines (276 loc) · 14.6 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
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
from __future__ import print_function #
import argparse
import datetime
import numpy as np
import os
from keras import backend as K
from keras.callbacks import EarlyStopping, ReduceLROnPlateau, TensorBoard
from keras.preprocessing.image import ImageDataGenerator
from progressbar import Bar, Percentage, ProgressBar
from six.moves.urllib.request import urlretrieve
from zipfile import ZipFile
""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""
def parseArguments():
""" Parses the command line arguments.
Args: None
Returns: The ArgumentParser object containing values of all the arguments.
"""
parser = argparse.ArgumentParser()
parser.add_argument("--download_data",
help="Set if app needs to download tiny imagenet dataset from web.",
action="store_true")
parser.add_argument("--data_dir", type=str,
default="data/tiny-imagenet-200/tiny-imagenet-200",
help="Directory in which the input data is stored.")
parser.add_argument("--reconfigure_val",
help="Set if val dataset should be reconfigured.",
action="store_true")
parser.add_argument("--model",
type=str,
default="ext-resnet50",
help="Name of the architecture to train and validate the data. One of [vgg16/resnet50/ext-resnet50/ext-resnet41/ext-resnet62].")
parser.add_argument("--pretrained",
help="Use imagenet pretrained model if set. Not applicable if [model] is [ext-resnet50].",
action="store_true")
parser.add_argument("--name",
type=str,
default="default",
help="Name of this training run. Will store results in {output/log/weights}/[model]/[name].")
parser.add_argument("--init",
type=str,
default="random-uniform",
help="""Kernel initializer of the hidden layers. [random-uniform/glorot-uniform/he-uniform].
Used only when [model] is [vgg16/ext-resnet50].""")
parser.add_argument("--activation",
type=str,
default="relu",
help="""The activation function of the network. One of [relu/softsign/tanh].
Used only when [model] is [ext-resnet50].""")
parser.add_argument("--loss",
type=str,
default="categorical_crossentropy",
help="""The loss function of the network. One of [categorical_crossentropy/mean_squared_error/categorical_hinge].
Used only when [model] is [ext-resnet50].""")
parser.add_argument("--do",
type=float,
default=0.0,
help="""The dropout at the output of each block.
Used only when [model] is [ext-resnet50].""")
parser.add_argument("--lr",
type=float,
default=0.1,
help="""The learning rate of the network.
Used only when [model] is [ext-resnet50].""")
parser.add_argument("--kernel_size",
type=str,
default="same",
help="""The kernel size for the network. One of [same/halved].
Used only when [model] is [ext-resnet50].""")
parser.add_argument("--kernel_number",
type=str,
default="same",
help="""The number of kernels at each layer of the network. One of [same/doubled].
Used only when [model] is [ext-resnet50].""")
parser.add_argument("--data_aug",
type=str,
default="basic",
help="Data augmentation to perform. One of [basic/no/yes].")
return parser.parse_known_args()
""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""
""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""
def reconfigureValSet(data_dir):
""" Reconfigures the validation dataset such that each class' data is under a directory with class' name.
Args:
data_dir: The root directory of the images.
Returns: None
"""
# Open val_annotations.txt to find the labels for the validation images.
with open(os.path.join(data_dir, "val", "val_annotations.txt"), "r") as text_file:
content = text_file.readlines()
content = [x.strip() for x in content]
# Build dict to map image name to label.
label_dict = {}
for line in content:
line_split = line.split("\t")
label_dict[line_split[0]] = line_split[1]
flag = False
# Travel through the val folder.
for subdir, dirs, files in os.walk(os.path.join(data_dir, "val")):
for file in files:
if file.endswith(".JPEG"):
flag = True
if not os.path.exists(os.path.join(data_dir, "val", label_dict[file])):
os.makedirs(os.path.join(data_dir, "val", label_dict[file]))
os.rename(os.path.join(data_dir, "val", "images", file), os.path.join(data_dir, "val", label_dict[file], file))
if flag:
os.rmdir(os.path.join(data_dir, "val", "images"))
break
""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""
""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""
def main(arguments):
""" Main method of the file.
Args:
arguments: ArgumentParser object which contains user-configurable parameters.
For more info, look into parseArguments() method.
Returns: None
"""
# Download data, if asked by user.
if arguments.download_data:
# Create data folder, if it doesn't exist.
if not os.path.exists(os.path.join("data")):
os.makedirs(os.path.join("data"))
# Define URL to get data from,
# and the zip folder path where the data will be stored.
url = "http://cs231n.stanford.edu/tiny-imagenet-200.zip"
zipFolder = os.path.join("data", "tiny-imagenet-200.zip")
""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""
# Define a method which will unzip the downloaded zip folder.
def unzip():
print("Unzipping data...")
zipFile = ZipFile(zipFolder, "r")
uncompressedSize = sum(file.file_size for file in zipFile.infolist())
extractedSize = 0
pbar = ProgressBar(widgets=[Percentage(), Bar()], maxval=100).start()
start = datetime.datetime.now()
for file in zipFile.infolist():
extractedSize += file.file_size
percent = extractedSize * 100 / uncompressedSize
pbar.update(percent)
zipFile.extract(file, path=zipFolder.rsplit(".", 1)[0])
print("Unzipped in {} s.".format((datetime.datetime.now() - start).seconds))
zipFile.close()
""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""
# Proceed to download only if the tiny imagenet folder does not already exist.
if (not os.path.exists(os.path.join("data", "tiny-imagenet-200"))):
# Proceed to download only if the downloaded zip folder does not exist,
# else directly unzip the previously downloaded zip file.
if (not os.path.isfile(zipFolder)):
print("Retrieving dataset from web...")
pbar = ProgressBar(widgets=[Percentage(), Bar()], maxval=100).start()
def dlProgress(count, blockSize, totalSize):
percent = int(count * blockSize * 100 / totalSize)
pbar.update(percent)
start = datetime.datetime.now()
urlretrieve(url, zipFolder, reporthook=dlProgress)
print("Downloaded in {} s.".format((datetime.datetime.now() - start).seconds))
unzip()
reconfigureValSet(arguments.data_dir)
else: print("Dataset folder already exists.")
# Define image parameters.
imgWidth = 64 # width of image
imgHeight = 64 # height of image
imgChannels = 3 # channels of image, RGB
lenTrainData = 100000 # total number of training data
lenValData = 10000 # total number of validation data
classes = 200 # number of classes
# Define image shape.
if (K.image_data_format() == "channels_first"):
imgShape = (imgChannels, imgWidth, imgHeight)
else:
imgShape = (imgWidth, imgHeight, imgChannels)
# Create Model.'
network = arguments.model
if (network == "vgg16"):
batchSize = 256
numEpochs = 2000 # 74 epochs used in the original paper
learningRate = 0.01
from models import vgg16
model = vgg16.createNetwork(imgShape, classes, learningRate, arguments.pretrained, arguments.init)
elif (network == "resnet50"):
batchSize = 256
numEpochs = 2000 # 60e4 iterations in the original paper
learningRate = 0.1
from models import resnet50
model = resnet50.createNetwork(imgShape, classes, learningRate)
elif (network == "ext-resnet41"):
batchSize = 256
numEpochs = 2000
learningRate = 0.1
from models import ext_resnet41
model = ext_resnet41.createNetwork(imgShape, classes, learningRate)
elif (network == "ext-resnet50"):
batchSize = 256
numEpochs = 2000
learningRate = arguments.lr
from models import ext_resnet50
model = ext_resnet50.createNetwork(imgShape, classes, learningRate, arguments.activation, arguments.init, arguments.loss, arguments.do, arguments.kernel_size, arguments.kernel_number)
elif (network == "ext-resnet62"):
batchSize = 256
numEpochs = 2000
learningRate = 0.1
from models import ext_resnet62
model = ext_resnet62.createNetwork(imgShape, classes, learningRate)
# Set featurewise mean of dataset.
if K.image_data_format() == "channels_first":
featurewiseMean = np.array([0.485, 0.456, 0.406], dtype=np.float32).reshape(3, 1, 1)
featurewiseStd = np.array([0.229, 0.224, 0.225], dtype=np.float32).reshape(3, 1, 1)
else:
featurewiseMean = np.array([0.485, 0.456, 0.406], dtype=np.float32).reshape(1, 1, 3)
featurewiseStd = np.array([0.229, 0.224, 0.225], dtype=np.float32).reshape(1, 1, 3)
# Create training data generator.
if (arguments.data_aug == "no"):
trainDataGen = ImageDataGenerator(featurewise_center=True)
elif (arguments.data_aug == "yes"):
trainDataGen = ImageDataGenerator(featurewise_center=True,
horizontal_flip=True,
vertical_flip=True,
rotation_range=20,
width_shift_range=0.2,
height_shift_range=0.2)
trainDataGen.std = featurewiseStd
else:
trainDataGen = ImageDataGenerator(featurewise_center=True,
horizontal_flip=True,
vertical_flip=True)
trainDataGen.mean = featurewiseMean
trainGen = trainDataGen.flow_from_directory(os.path.join(arguments.data_dir, "train"),
target_size=(imgWidth, imgHeight),
batch_size=batchSize)
# Create validation data generator.
if arguments.reconfigure_val: reconfigureValSet(arguments.data_dir)
valDataGen = ImageDataGenerator(featurewise_center=True)
valDataGen.mean = featurewiseMean
if (arguments.data_aug == "yes"): valDataGen.std = featurewiseStd
valGen = valDataGen.flow_from_directory(os.path.join(arguments.data_dir, "val"),
target_size=(imgWidth, imgHeight),
batch_size=batchSize)
# Create directories to store output.
if arguments.pretrained: network = network + "-imagenet"
if not os.path.exists(os.path.join("output", network)):
os.makedirs(os.path.join("output", network))
name = arguments.name.replace(" ", "_")
if not os.path.exists(os.path.join("output", network, name)):
os.makedirs(os.path.join("output", network, name))
outputTime = str(datetime.datetime.now()).replace(" ", "_").replace(":", ".")
if not os.path.exists(os.path.join("output", network, name, outputTime)):
os.makedirs(os.path.join("output", network, name, outputTime))
if not os.path.exists(os.path.join("weights", network, name)):
os.makedirs(os.path.join("weights", network, name))
# Create callbacks.
tbCallback = TensorBoard(log_dir="./logs/{}/{}/{}".format(network, name, outputTime), histogram_freq=0, write_grads=True, write_graph=True)
lrCallback = ReduceLROnPlateau(monitor="val_categorical_accuracy", patience=5, factor=0.1, verbose=1, min_lr=0.00001)
esCallback = EarlyStopping(monitor="val_categorical_accuracy", patience=10, verbose=1, min_delta=0.0001)
# Fit model to the dataset...
model.fit_generator(trainGen,
steps_per_epoch=lenTrainData // batchSize,
epochs=numEpochs,
validation_data=valGen,
validation_steps=lenValData // batchSize,
callbacks=[tbCallback, lrCallback, esCallback])
# Save the model...
model.save_weights(os.path.join("weights", network, name, outputTime + ".h5"))
""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""
""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""
if __name__ == "__main__":
""" Start of program execution. """
# Parse arguments from command line.
args, unparsed = parseArguments()
# Create folders, if they don't exist, to store results.
if not os.path.exists("logs"):
os.makedirs("logs")
if not os.path.exists("output"):
os.makedirs("output")
if not os.path.exists("weights"):
os.makedirs("weights")
main(args)
""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""