-
Notifications
You must be signed in to change notification settings - Fork 1
/
2-run_models.py
254 lines (204 loc) · 8.59 KB
/
2-run_models.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
import os
import shutil
from Models.models import cnn, perceptron
from train_utils import train_pd_models, train_cd_models
file_dir = os.path.abspath(os.path.dirname(__file__))
# set parameters (initialize with None if they are variable)
# as well as data types for SQL data base with model results
params = dict() # map parameter names to parameter values
params_dtypes = dict() # map parameter names to parameters types
# (for storage in SQLITE data base)
params['max_epochs'] = None
params['mini_batch_size'] = None
params['n_hidden_dense'] = None
params['dropout'] = None
params['loss_function'] = 'categorical_crossentropy'
params['random_seed'] = 100
params['initial_learning_rate'] = None
params['early_stopping'] = True
params['patience'] = 10
params['resolution'] = None
params['deafness_type'] = None
# data types of parameters for storage in SQLITE data base
params_dtypes['max_epochs'] = 'INTEGER'
params_dtypes['mini_batch_size'] = 'INTEGER'
params_dtypes['n_hidden_dense'] = 'INTEGER'
params_dtypes['dropout'] = 'REAL'
params_dtypes['loss_function'] = 'TEXT'
params_dtypes['random_seed'] = 'INTEGER'
params_dtypes['initial_learning_rate'] = 'REAL'
params_dtypes['early_stopping'] = 'INTEGER'
params_dtypes['patience'] = 'INTEGER'
params_dtypes['resolution'] = 'TEXT'
params_dtypes['deafness_type'] = 'TEXT'
# helper functions
def remove_file_if_exists(filepath):
if os.path.isfile(filepath):
print('File already exists: %s' % filepath)
print('Will overwrite it.')
os.remove(filepath)
def remove_dir_if_exists(dir):
if os.path.isdir(dir):
print('Directory already exists: %s' % dir)
print('Will delete and re-create it.')
shutil.rmtree(dir)
os.makedirs(dir)
def train_per(pretrain, data_name):
params['data_dir'] = os.path.join(file_dir, 'Featurize', 'featurized',
data_name)
db_dir = os.path.join(file_dir, 'results', 'data_bases')
if not os.path.exists(db_dir):
os.makedirs(db_dir)
params['data_base_path'] = os.path.join(db_dir, '%s.sqlite' % params['id'])
remove_file_if_exists(params['data_base_path'])
params['model_dir'] = os.path.join(file_dir, 'results',
'models', params['id'])
remove_dir_if_exists(params['model_dir'])
if not os.path.exists(params['model_dir']):
os.makedirs(params['model_dir'])
params['n_hidden_dense'] = None
params['dense_dropout'] = None
params['kernel_size1'] = None
params['kernel_size2'] = None
params['number_filters'] = None
params['stride1'] = None
params['stride2'] = None
params['conv_dropout'] = None
params['mini_batch_size'] = 32
params['initial_learning_rate'] = 0.01
if pretrain:
train_pd_models(perceptron, params, params_dtypes)
else:
train_cd_models(perceptron, params, params_dtypes)
def train_cnn(pretrain, data_name):
params['data_dir'] = os.path.join(file_dir, 'Featurize', 'featurized',
data_name)
db_dir = os.path.join(file_dir, 'results', 'data_bases')
if not os.path.exists(db_dir):
os.makedirs(db_dir)
params['data_base_path'] = os.path.join(db_dir, '%s.sqlite' % params['id'])
remove_file_if_exists(params['data_base_path'])
params['model_dir'] = os.path.join(file_dir, 'results',
'models', params['id'])
remove_dir_if_exists(params['model_dir'])
if not os.path.exists(params['model_dir']):
os.makedirs(params['model_dir'])
params['n_hidden_dense'] = 100
params['dense_dropout'] = 0.5
params['number_filters'] = 5
params['kernel_size1'] = 5
params['kernel_size2'] = 5
params['stride1'] = 2
params['stride2'] = 2
params['conv_dropout'] = 0.1
params['mini_batch_size'] = 100
params['initial_learning_rate'] = 0.1
if pretrain:
train_pd_models(cnn, params, params_dtypes)
else:
train_cd_models(cnn, params, params_dtypes)
if __name__ == '__main__':
# 1. CNN
##########################################################################
# gender recognition, congenitally deaf (CD) models
params['id'] = 'gender_cd_cnn'
params['max_epochs'] = 9999999
train_cnn(pretrain=False, data_name='gender')
params['id'] = 'gender_cd_cnn_1ep'
params['max_epochs'] = 1
train_cnn(pretrain=False, data_name='gender')
params['id'] = 'gender_cd_cnn_0ep'
params['max_epochs'] = 0
train_cnn(pretrain=False, data_name='gender')
# gender recognition, postlingually deaf (PD) models
params['id'] = 'gender_pd_cnn'
params['max_epochs'] = 9999999
params['pretrained_dir'] = None
train_cnn(pretrain=True, data_name='gender')
params['id'] = 'gender_pd_cnn_1ep'
params['max_epochs'] = 1
params['pretrained_dir'] = os.path.join(file_dir, 'results', 'models',
'gender_pd_cnn')
train_cnn(pretrain=True, data_name='gender')
params['id'] = 'gender_pd_cnn_0ep'
params['max_epochs'] = 0
params['pretrained_dir'] = os.path.join(file_dir, 'results', 'models',
'gender_pd_cnn')
train_cnn(pretrain=True, data_name='gender')
# word recognition, congenitally deaf (CD) models
params['id'] = 'words_cd_cnn'
params['max_epochs'] = 9999999
train_cnn(pretrain=False, data_name='words')
params['id'] = 'words_cd_cnn_1ep'
params['max_epochs'] = 1
train_cnn(pretrain=False, data_name='words')
params['id'] = 'words_cd_cnn_0ep'
params['max_epochs'] = 0
train_cnn(pretrain=False, data_name='words')
# word recognition, postlingually deaf (PD) models
params['id'] = 'words_pd_cnn'
params['max_epochs'] = 9999999
params['pretrained_dir'] = None
train_cnn(pretrain=True, data_name='words')
params['id'] = 'words_pd_cnn_1ep'
params['max_epochs'] = 1
params['pretrained_dir'] = os.path.join(file_dir, 'results', 'models',
'words_pd_cnn')
train_cnn(pretrain=True, data_name='words')
params['id'] = 'words_pd_cnn_0ep'
params['max_epochs'] = 0
params['pretrained_dir'] = os.path.join(file_dir, 'results', 'models',
'words_pd_cnn')
train_cnn(pretrain=True, data_name='words')
# 1. perceptron
###########################################################################
# gender recognition, congenitally deaf (CD) models
params['id'] = 'gender_cd_per'
params['max_epochs'] = 9999999
train_per(pretrain=False, data_name='gender')
params['id'] = 'gender_cd_per_1ep'
params['max_epochs'] = 1
train_per(pretrain=False, data_name='gender')
params['id'] = 'gender_cd_per_0ep'
params['max_epochs'] = 0
train_per(pretrain=False, data_name='gender')
# gender recognition, postlingually deaf (PD) models
params['id'] = 'gender_pd_per'
params['max_epochs'] = 9999999
params['pretrained_dir'] = None
train_per(pretrain=True, data_name='gender')
params['id'] = 'gender_pd_per_1ep'
params['max_epochs'] = 1
params['pretrained_dir'] = os.path.join(file_dir, 'results', 'models',
'gender_pd_per')
train_per(pretrain=True, data_name='gender')
params['id'] = 'gender_pd_per_0ep'
params['max_epochs'] = 0
params['pretrained_dir'] = os.path.join(file_dir, 'results', 'models',
'gender_pd_per')
train_per(pretrain=True, data_name='gender')
# word recognition, congenitally deaf (CD) models
params['id'] = 'words_cd_per'
params['max_epochs'] = 9999999
train_per(pretrain=False, data_name='words')
params['id'] = 'words_cd_per_1ep'
params['max_epochs'] = 1
train_per(pretrain=False, data_name='words')
params['id'] = 'words_cd_per_0ep'
params['max_epochs'] = 0
train_per(pretrain=False, data_name='words')
# word recognition, postlingually deaf (PD) models
params['id'] = 'words_pd_per'
params['max_epochs'] = 9999999
params['pretrained_dir'] = None
train_per(pretrain=True, data_name='words')
params['id'] = 'words_pd_per_1ep'
params['max_epochs'] = 1
params['pretrained_dir'] = os.path.join(file_dir, 'results', 'models',
'words_pd_per')
train_per(pretrain=True, data_name='words')
params['id'] = 'words_pd_per_0ep'
params['max_epochs'] = 0
params['pretrained_dir'] = os.path.join(file_dir, 'results', 'models',
'words_pd_per')
train_per(pretrain=True, data_name='words')