-
Notifications
You must be signed in to change notification settings - Fork 8
/
parser.py
219 lines (201 loc) · 6.67 KB
/
parser.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
import os
import argparse
from itertools import izip
import time
import datetime
HELP_MSG = '\n\nUse python cnn_train.py -h to see all options and examples of usage'
def get_arguments(arguments):
parser = argparse.ArgumentParser()
parser.add_argument(
'-r','--root',
dest = 'root',
type = str,
nargs = 1,
required = True,
help = """Root containg "train" and "test" folders that
contain train files and test files. Each file
must be named according to the correspondent
class"""
)
parser.add_argument(
'-l', '--layers',
dest = 'number_of_layers',
type = int,
nargs = 1,
default = [8],
required = False,
help = """Number of layers, not including the classification
layer"""
)
parser.add_argument(
'-arc', '--architecture',
dest = 'architecture',
type = str,
nargs = '+',
default = ["conv", "pool", "conv", "pool", "conv", "pool", "fc", "fc"],
required = False,
help = """Architecture structure of the network. Must be
entered as strings separeted by spaces. Each
string defines the types of the layers. "conv"
stands for convolution layers, "pool" stands
for pooling layers and "fc" stands for fully
connected layers. The classification layer must
not be included
Example:
--architecture conv pool conv pool conv pool fc fc"""
)
parser.add_argument(
'-f', '--functions',
dest = 'functions',
type = str,
nargs = '+',
default = ["relu", "avg", "relu", "avg", "relu", "avg", "relu", "relu"],
required = False,
help = """Activation functions and type of pooling layers
ordered according to --architecture parameter's
layers order. Must not include the function of
the classification layer
Example of --architecture:
--architecture conv pool conv pool conv pool fc fc fc
Example of --functions:
--fuctions relu avg relu avg relu avg relu relu"""
)
parser.add_argument(
'-w','--widths',
dest = 'widths',
type = int,
nargs = '+',
default = [30, 20, 30, 20, 30, 10, 1500, 500],
required = False,
help = """Width of each layer ordered according to
--architecture paramater. For "fc" layers, the
number of neurons must be entered. The number of
neurons for the classification layer must not be
entered, since the number of neurons on this
layer is defined according to the amount of
classes in train and test folders.
Example of --architecture:
--architecture conv pool conv pool conv pool fc fc fc
Example of --widths:
--widths 30 20 30 20 30 10 1500 500"""
)
parser.add_argument(
'-s','--strides',
dest = 'strides',
type = int,
nargs = '+',
default = [1, 20, 1, 20, 1, 10],
required = False,
help = """Strides of convolution and pooling layers
ordered according to --architecture parameter.
Example of --architecture:
--architecture conv pool conv pool conv pool fc fc fc
Example of --strides:
--strides 1 20 1 20 1 10
Being 1 1 1 the strides of convolution layers
and 20 20 20 the strides of pooling layers"""
)
parser.add_argument(
'-fm','--feature-maps',
dest = 'feature_maps',
type = int,
nargs = '+',
default = [64, 32, 16],
required = False,
help = """Number of feature maps per convolution layer,
separated by spaces.
Example of --architecture:
--architecture conv pool conv pool conv pool fc fc fc
Example of --feature-maps:
--feature-maps 64 32 16"""
)
parser.add_argument(
'-trb', '--train-batch-size',
dest = 'train_batch_size',
type = int,
nargs = 1,
default = [32],
required = False,
help = """Train batch size"""
)
parser.add_argument(
'-tsb','--test-batch-size',
dest = 'test_batch_size',
type = int,
nargs = 1,
default = [32],
required = False,
help = """Test batch size"""
)
parser.add_argument(
'-e','--epochs',
dest = 'epochs',
type = int,
nargs = 1,
default = [30],
required = False,
help = """Number of epochs"""
)
parser.add_argument(
'-d','--dropout',
dest = 'dropout',
type = float,
nargs = 1,
default = [0.5],
required = False,
help = """Dropout rate"""
)
parser.add_argument(
'-p','--prefix',
dest = 'prefix',
type = str,
nargs = 1,
default = ['RUN_'+datetime.datetime.now().strftime('%Y%m%d_%H%M%S')],
required = False,
help = """Prefix of the file names to be created"""
)
options = parser.parse_args(arguments)
if not os.path.isdir(options.root[0] + '/Train'):
print('ERROR\n\nPath: '+options.root[0]+'/Train does not exist'+HELP_MSG)
exit(1)
if not os.path.isdir(options.root[0] + '/Test'):
print('ERROR\n\nPath: '+options.root[0]+'/Test does not exist'+HELP_MSG)
exit(2)
if not (options.number_of_layers[0] == len(options.architecture)):
print('ERROR\n\nNumber of layers is not equal to the number of layers defined on --architecture parameter'+HELP_MSG)
exit(3)
if not (options.number_of_layers[0] == len(options.functions)):
print('ERROR\n\nNumber of functions is not equal to the number of layers defined on --architecture parameter'+HELP_MSG)
exit(4)
if not (options.number_of_layers[0] == len(options.widths)):
print('ERROR\n\nNumber of widths is not equal to the number of layers defined on --architecture parameter'+HELP_MSG)
exit(5)
if not (len([a for a in options.architecture if a=='conv']) == len(options.feature_maps)):
print('ERROR\n\nNumber of feature maps is not equal to the number of convolution layers defined on --architecture parameter'+HELP_MSG)
exit(6)
if not (len([a for a in options.architecture if a=='conv' or a=='pool']) == len(options.strides)):
print('ERROR\n\nNumber of strides is not equal to the amount of convolution and pooling layers defined on --architecture parameter'+HELP_MSG)
exit(7)
return options
def print_options(options):
print('*' * 79 + '\n**' + ' ' * 33 + ' OPTIONS ' + ' ' * 33 + '**\n' + '*' * 79)
print('%20s %-25s' % ('Root:',options.root[0]))
print('%20s %-25d' % ('Train batch:',options.train_batch_size[0]))
print('%20s %-25d' % ('Test batch:',options.test_batch_size[0]))
print('%20s %-25d' % ('Epochs:',options.epochs[0]))
print('%20s %-25.2f' % ('Dropout:',options.dropout[0]))
print('%20s %-25d' % ('Number of layers:',options.number_of_layers[0]))
print('%20s %-25s' % ('Prefix:',options.prefix[0]))
print('%20s %-50s' % ('Architecture:',''.join('%-7s' % t for t in izip(options.architecture))))
print('%20s %-50s' % ('Functions:',''.join('%-7s' % t for t in izip(options.functions))))
print('%20s %-50s' % ('Widths:',''.join('%-7s' % t for t in izip(options.widths))))
print('%20s %-50s' % ('Strides:',''.join('%-7s' % t for t in izip(options.widths))))
feature_maps_string = '%20s ' % 'Feature maps:'
j = 0
for i in range(len(options.architecture)):
if options.architecture[i] == 'conv':
feature_maps_string += '%-7s' % str(options.feature_maps[j])
j += 1
else:
feature_maps_string += '%-7s' % '-'
print(feature_maps_string)