-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.py
315 lines (287 loc) · 10 KB
/
main.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
import h5py
import argparse
import tensorflow as tf
from pipeline_lstm import SimpleLstmPipeline
from pipeline_crf import SimpleCrfPipeline
from pipeline_scrf import SimpleScrfPipeline
from pipeline_lstmcrf import SimpleLstmcrfPipeline
from pipeline_lstmscrf import SimpleLstmScrfPipeline
import os
# os.environ["TF_CPP_MIN_LOG_LEVEL"]="0"
if __name__ == '__main__':
parser = argparse.ArgumentParser(description='Perform labelling of sequences using a LSTMCRF model.')
# -----------------------------------------------
# General parameters
# -----------------------------------------------
parser.add_argument(
'-i',
'--input-dir',
type=str,
dest='input_dir',
default='/data/hupba/Datasets/breakfast/hdf5/pooled-20-0c/',
help=
'Dataset in hdf5 format (default: %(default)s)')
parser.add_argument(
'-b',
'--batch-size',
type=int,
dest='batch_size',
default=64,
help=
'Batch size (default: %(default)s)')
parser.add_argument(
'-lr',
'--learning-rate',
type=float,
dest='learn_rate',
default=1e-3,
help=
'Starting learning rate. It decays after 100 and 1000 epochs by a factor specified by --decay-rate argument (default: %(default)s)')
parser.add_argument(
'-dr',
'--decay-rate',
type=float,
dest='decay_rate',
default=2,
help=
'Decay rate for inverse time decay (default: %(default)s)')
parser.add_argument(
'-e',
'--num_epochs',
type=int,
dest='num_epochs',
default=150,
help=
'Num epochs (default: %(default)s)')
parser.add_argument(
'-ot',
'--optimizer-type',
type=str,
dest='optimizer_type',
default='adam',
help=
'Optimizer type (sgd or adam) (default: %(default)s)')
parser.add_argument(
'-c',
'--clip-norm',
type=float,
dest='clip_norm',
default=5.0,
help=
'Clipping gradients by norm above clip_norm (default: %(default)s)')
parser.add_argument(
'-T',
'--test-subset',
type=str,
dest='test_subset',
default='s1',
help=
'Test data subset identifier (default: %(default)s)')
parser.add_argument(
'-m',
'--model-type',
type=str,
dest='model_type',
default='lstmcrf',
help=
'Model type (crf, lstm or lstmcrf) (default: %(default)s)')
# -----------------------------------------------
# (LSTM-only parameters)
# -----------------------------------------------
parser.add_argument(
'-s',
'--hidden-size',
type=int,
dest='hidden_size',
default=128,
help=
'Hidden size (default: %(default)s)')
parser.add_argument(
'-p',
'--drop-prob',
type=float,
dest='drop_prob',
default=0.2,
help=
'Dropout probability (default: %(default)s)')
# -----------------------------------------------
parser.add_argument(
'-G',
'--gpu-memory',
type=float,
dest='gpu_memory',
default=0.95,
help=
'GPU memory to reserve (default: %(default)s)')
parser.add_argument(
'-D',
'--cuda-devices',
type=str,
dest='cuda_devices',
default="3",
help=
'GPU devices (default: %(default)s)')
parser.add_argument(
'-L',
'--logging-path',
type=str,
dest='logging_path',
default='/data/hupba/Datasets/breakfast/log/',
help=
'Tensorboard\'s logging path (default: %(default)s)')
parser.add_argument(
'-M',
'--models-path',
type=str,
dest='models_path',
default='/data/hupba/Datasets/breakfast/models/',
help=
'Tensorflow\'s models path (default: %(default)s)')
args = parser.parse_args()
print args
# Read breakfast from hdf5 file
f_dataset = h5py.File(os.path.join(args.input_dir, 'dataset.h5'), 'r')
# f_training = h5py.File(os.path.join(args.input_dir, 'training.h5'), 'r')
# f_validation = h5py.File(os.path.join(args.input_dir, 'testing.h5'), 'r')
# f_testing = h5py.File(os.path.join(args.input_dir, 'testing.h5'), 'r')
# # Read breakfast from hdf5 file
# f_dataset = h5py.File(args.input_file, 'r')
# print('Dataset (%s) attributes:' % (args.input_file))
# for key in f_dataset.attrs.keys():
# print('%s : %s' % (key, str(f_dataset.attrs[key])))
if args.cuda_devices:
os.environ['CUDA_VISIBLE_DEVICES'] = args.cuda_devices
# Create a model (choosen via argument passing)
if args.model_type == 'lstmcrf':
m = SimpleLstmcrfPipeline(
f_dataset,
args.test_subset,
os.path.join(args.logging_path, 'lstmcrf', args.test_subset),
os.path.join(args.models_path, 'lstmcrf', args.test_subset),
batch_size=args.batch_size,
learn_rate=args.learn_rate,
decay_rate=args.decay_rate,
num_epochs=args.num_epochs,
hidden_size=args.hidden_size,
drop_prob=args.drop_prob,
optimizer_type=args.optimizer_type,
clip_norm=args.clip_norm
)
elif args.model_type == 'lstm':
m = SimpleLstmPipeline(
f_dataset,
args.test_subset,
os.path.join(args.logging_path, 'lstm', args.test_subset),
os.path.join(args.models_path, 'lstm', args.test_subset),
batch_size=args.batch_size,
learn_rate=args.learn_rate,
decay_rate=args.decay_rate,
num_epochs=args.num_epochs,
hidden_size=args.hidden_size,
drop_prob=args.drop_prob,
optimizer_type=args.optimizer_type,
clip_norm=args.clip_norm
)
elif args.model_type == 'crf':
m = SimpleCrfPipeline(
f_dataset,
args.test_subset,
os.path.join(args.logging_path, 'crf', args.test_subset),
os.path.join(args.models_path, 'crf', args.test_subset),
batch_size=args.batch_size,
learn_rate=args.learn_rate,
decay_rate=args.decay_rate,
num_epochs=args.num_epochs,
hidden_size=args.hidden_size,
drop_prob=args.drop_prob,
optimizer_type=args.optimizer_type,
clip_norm=args.clip_norm
)
elif args.model_type == 'scrf':
m = SimpleScrfPipeline(
f_dataset,
args.test_subset,
os.path.join(args.logging_path, 'scrf', args.test_subset),
os.path.join(args.models_path, 'scrf', args.test_subset),
batch_size=args.batch_size,
learn_rate=args.learn_rate,
decay_rate=args.decay_rate,
num_epochs=args.num_epochs,
hidden_size=args.hidden_size,
drop_prob=args.drop_prob,
optimizer_type=args.optimizer_type,
clip_norm=args.clip_norm
)
elif args.model_type == 'lstmscrf':
m = SimpleLstmScrfPipeline(
f_dataset,
args.test_subset,
os.path.join(args.logging_path, 'lstmscrf', args.test_subset),
os.path.join(args.models_path, 'lstmscrf', args.test_subset),
batch_size=args.batch_size,
learn_rate=args.learn_rate,
decay_rate=args.decay_rate,
num_epochs=args.num_epochs,
hidden_size=args.hidden_size,
drop_prob=args.drop_prob,
optimizer_type=args.optimizer_type,
clip_norm=args.clip_norm
)
# elif args.model_type == 'lstm':
# m = SimpleLstmPipeline(
# f_training,
# f_testing,
# f_testing,
# args.class_weights_file,
# batch_size=args.batch_size,
# learn_rate=args.learn_rate,
# decay_rate=args.decay_rate,
# num_epochs=args.num_epochs,
# hidden_size=args.hidden_size,
# drop_prob=args.drop_prob,
# optimizer_type=args.optimizer_type,
# clip_norm=args.clip_norm
# )
# elif args.model_type == 'crf':
# m = SimpleCrfPipeline(
# f_dataset['training'],
# f_dataset['validation'] if 'validation' in f_dataset else f_dataset['testing'],
# f_dataset['testing'],
# f_dataset.attrs['no_classes'],
# f_dataset['training']['class_weights'][:],
# batch_size=args.batch_size,
# learn_rate=args.learn_rate,
# decay_rate=args.decay_rate,
# num_epochs=args.num_epochs,
# hidden_size=args.hidden_size,
# drop_prob=args.drop_prob,
# optimizer_type=args.optimizer_type,
# clip_norm=args.clip_norm
# )
# elif args.model_type == 'cnncrf':
# m = SimpleCnnCrfPipeline(
# f_dataset['training'],
# f_dataset['validation'] if 'validation' in f_dataset else f_dataset['testing'],
# f_dataset['testing'],
# f_dataset.attrs['no_classes'],
# f_dataset['training']['class_weights'][:],
# batch_size=args.batch_size,
# learn_rate=args.learn_rate,
# decay_rate=args.decay_rate,
# num_epochs=args.num_epochs,
# hidden_size=args.hidden_size,
# drop_prob=args.drop_prob,
# optimizer_type=args.optimizer_type,
# clip_norm=args.clip_norm
# )
else:
raise NotImplementedError('Please specify a valid model (-M <model_type>).')
# -----------------------------------------------
# RUN
# -----------------------------------------------
gpu_options = tf.GPUOptions(per_process_gpu_memory_fraction=args.gpu_memory)
m.run(gpu_options)
# -----------------------------------------------
# f_training.close()
# f_validation.close()
# f_testing.close()