-
Notifications
You must be signed in to change notification settings - Fork 0
/
all_datasets_training.py
301 lines (241 loc) · 10.2 KB
/
all_datasets_training.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
import os
from keras import backend as K
from keras.layers import Conv1D, BatchNormalization, GlobalAveragePooling1D, Permute, Dropout, Flatten
from keras.layers import Input, Dense, LSTM, CuDNNLSTM, concatenate, Activation, GRU, SimpleRNN
from keras.models import Model
from utils.constants import MAX_SEQUENCE_LENGTH_LIST, NB_CLASSES_LIST
from utils.keras_utils import train_model, evaluate_model
from utils.layer_utils import AttentionLSTM
def generate_lstmfcn(MAX_SEQUENCE_LENGTH, NB_CLASS, NUM_CELLS=8):
ip = Input(shape=(1, MAX_SEQUENCE_LENGTH))
x = LSTM(NUM_CELLS)(ip)
x = Dropout(0.8)(x)
y = Permute((2, 1))(ip)
y = Conv1D(128, 8, padding='same', kernel_initializer='he_uniform')(y)
y = BatchNormalization()(y)
y = Activation('relu')(y)
y = Conv1D(256, 5, padding='same', kernel_initializer='he_uniform')(y)
y = BatchNormalization()(y)
y = Activation('relu')(y)
y = Conv1D(128, 3, padding='same', kernel_initializer='he_uniform')(y)
y = BatchNormalization()(y)
y = Activation('relu')(y)
y = GlobalAveragePooling1D()(y)
x = concatenate([x, y])
out = Dense(NB_CLASS, activation='softmax')(x)
model = Model(ip, out)
model.summary()
# add load model code here to fine-tune
return model
def generate_alstmfcn(MAX_SEQUENCE_LENGTH, NB_CLASS, NUM_CELLS=8):
ip = Input(shape=(1, MAX_SEQUENCE_LENGTH))
x = AttentionLSTM(NUM_CELLS)(ip)
x = Dropout(0.8)(x)
y = Permute((2, 1))(ip)
y = Conv1D(128, 8, padding='same', kernel_initializer='he_uniform')(y)
y = BatchNormalization()(y)
y = Activation('relu')(y)
y = Conv1D(256, 5, padding='same', kernel_initializer='he_uniform')(y)
y = BatchNormalization()(y)
y = Activation('relu')(y)
y = Conv1D(128, 3, padding='same', kernel_initializer='he_uniform')(y)
y = BatchNormalization()(y)
y = Activation('relu')(y)
y = GlobalAveragePooling1D()(y)
x = concatenate([x, y])
out = Dense(NB_CLASS, activation='softmax')(x)
model = Model(ip, out)
model.summary()
# add load model code here to fine-tune
return model
if __name__ == "__main__":
dataset_map = [('Adiac', 0),
('ArrowHead', 1),
('ChlorineConcentration', 2),
('InsectWingbeatSound', 3),
('Lighting7', 4),
('Wine', 5),
('WordsSynonyms', 6),
('50words', 7),
('Beef', 8),
('DistalPhalanxOutlineAgeGroup', 9),
('DistalPhalanxOutlineCorrect', 10),
('DistalPhalanxTW', 11),
('ECG200', 12),
('ECGFiveDays', 13),
('BeetleFly', 14),
('BirdChicken', 15),
('ItalyPowerDemand', 16),
('SonyAIBORobotSurface', 17),
('SonyAIBORobotSurfaceII', 18),
('MiddlePhalanxOutlineAgeGroup', 19),
('MiddlePhalanxOutlineCorrect', 20),
('MiddlePhalanxTW', 21),
('ProximalPhalanxOutlineAgeGroup', 22),
('ProximalPhalanxOutlineCorrect', 23),
('ProximalPhalanxTW', 24),
('MoteStrain', 25),
('MedicalImages', 26),
('Strawberry', 27),
('ToeSegmentation1', 28),
('Coffee', 29),
('Cricket_X', 30),
('Cricket_Y', 31),
('Cricket_Z', 32),
('uWaveGestureLibrary_X', 33),
('uWaveGestureLibrary_Y', 34),
('uWaveGestureLibrary_Z', 35),
('ToeSegmentation2', 36),
('DiatomSizeReduction', 37),
('car', 38),
('CBF', 39),
('CinC_ECG_torso', 40),
('Computers', 41),
('Earthquakes', 42),
('ECG5000', 43),
('ElectricDevices', 44),
('FaceAll', 45),
('FaceFour', 46),
('FacesUCR', 47),
('Fish', 48),
('FordA', 49),
('FordB', 50),
('Gun_Point', 51),
('Ham', 52),
('HandOutlines', 53),
('Haptics', 54),
('Herring', 55),
('InlineSkate', 56),
('LargeKitchenAppliances', 57),
('Lighting2', 58),
('MALLAT', 59),
('Meat', 60),
('NonInvasiveFatalECG_Thorax1', 61),
('NonInvasiveFatalECG_Thorax2', 62),
('OliveOil', 63),
('OSULeaf', 64),
('PhalangesOutlinesCorrect', 65),
('Phoneme', 66),
('plane', 67),
('RefrigerationDevices', 68),
('ScreenType', 69),
('ShapeletSim', 70),
('ShapesAll', 71),
('SmallKitchenAppliances', 72),
('StarlightCurves', 73),
('SwedishLeaf', 74),
('Symbols', 75),
('synthetic_control', 76),
('Trace', 77),
('Patterns', 78),
('TwoLeadECG', 79),
('UWaveGestureLibraryAll', 80),
('wafer', 81),
('Worms', 82),
('WormsTwoClass', 83),
('yoga', 84),
('ACSF1', 85),
('AllGestureWiimoteX', 86),
('AllGestureWiimoteY', 87),
('AllGestureWiimoteZ', 88),
('BME', 89),
('Chinatown', 90),
('Crop', 91),
('DodgerLoopDay', 92),
('DodgerLoopGame', 93),
('DodgerLoopWeekend', 94),
('EOGHorizontalSignal', 95),
('EOGVerticalSignal', 96),
('EthanolLevel', 97),
('FreezerRegularTrain', 98),
('FreezerSmallTrain', 99),
('Fungi', 100),
('GestureMidAirD1', 101),
('GestureMidAirD2', 102),
('GestureMidAirD3', 103),
('GesturePebbleZ1', 104),
('GesturePebbleZ2', 105),
('GunPointAgeSpan', 106),
('GunPointMaleVersusFemale', 107),
('GunPointOldVersusYoung', 108),
('HouseTwenty', 109),
('InsectEPGRegularTrain', 110),
('InsectEPGSmallTrain', 111),
('MelbournePedestrian', 112),
('MixedShapesRegularTrain', 113),
('MixedShapesSmallTrain', 114),
('PickupGestureWiimoteZ', 115),
('PigAirwayPressure', 116),
('PigArtPressure', 117),
('PigCVP', 118),
('PLAID', 119),
('PowerCons', 120),
('Rock', 121),
('SemgHandGenderCh2', 122),
('SemgHandMovementCh2', 123),
('SemgHandSubjectCh2', 124),
('ShakeGestureWiimoteZ', 125),
('SmoothSubspace', 126),
('UMD', 127)
]
print("Num datasets : ", len(dataset_map))
print()
base_log_name = '%s_%d_cells_new_datasets.csv'
base_weights_dir = '%s_%d_cells_weights/'
MODELS = [
('lstmfcn', generate_lstmfcn),
('alstmfcn', generate_alstmfcn),
]
# Number of cells
CELLS = [8, 64, 128]
# Normalization scheme
# Normalize = False means no normalization will be done
# Normalize = True / 1 means sample wise z-normalization
# Normalize = 2 means dataset normalization.
normalize_dataset = True
for model_id, (MODEL_NAME, model_fn) in enumerate(MODELS):
for cell in CELLS:
successes = []
failures = []
if not os.path.exists(base_log_name % (MODEL_NAME, cell)):
file = open(base_log_name % (MODEL_NAME, cell), 'w')
file.write('%s,%s,%s,%s\n' % ('dataset_id', 'dataset_name', 'dataset_name_', 'test_accuracy'))
file.close()
for dname, did in dataset_map[0:2]:
MAX_SEQUENCE_LENGTH = MAX_SEQUENCE_LENGTH_LIST[did]
NB_CLASS = NB_CLASSES_LIST[did]
# release GPU Memory
K.clear_session() # 释放内存
file = open(base_log_name % (MODEL_NAME, cell), 'a+')
weights_dir = base_weights_dir % (MODEL_NAME, cell)
if not os.path.exists('weights/' + weights_dir):
os.makedirs('weights/' + weights_dir)
dataset_name_ = weights_dir + dname
# try:
model = model_fn(MAX_SEQUENCE_LENGTH, NB_CLASS, cell)
print('*' * 20, "Training model for dataset %s" % (dname), '*' * 20)
# comment out the training code to only evaluate !
train_model(model, did, dataset_name_, epochs=2, batch_size=128, normalize_timeseries=normalize_dataset)
acc = evaluate_model(model, did, dataset_name_, batch_size=128, normalize_timeseries=normalize_dataset)
s = "%d,%s,%s,%0.6f\n" % (did, dname, dataset_name_, acc)
file.write(s)
file.flush()
successes.append(s)
# except Exception as e:
# traceback.print_exc()
#
# s = "%d,%s,%s,%s\n" % (did, dname, dataset_name_, 0.0)
# failures.append(s)
#
# print()
file.close()
print('\n\n')
print('*' * 20, "Successes", '*' * 20)
print()
for line in successes:
print(line)
print('\n\n')
print('*' * 20, "Failures", '*' * 20)
print()
for line in failures:
print(line)