-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlstmrep.py
304 lines (248 loc) · 12.3 KB
/
lstmrep.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
from keras.layers import Lambda, Input, Dense
from keras.models import Model
from keras.datasets import mnist
from keras.losses import mse, binary_crossentropy
from keras.utils import plot_model
from keras import backend as K
#from tensorflow.python.keras.callbacks import TensorBoard
#from tensorflow.keras.callbacks import LearningRateScheduler,EarlyStopping
from keras.utils import to_categorical
from keras.models import Sequential, Model
from keras.layers import Activation, Dense, Dropout, Conv1D,Conv2D, GlobalAveragePooling2D, InputLayer, \
Flatten, MaxPooling2D,MaxPooling1D, LSTM, ConvLSTM2D, Reshape, Concatenate, Input
from keras.layers.normalization import BatchNormalization
import tensorflow as tf
import sys, os
import settings
import utils
sys.path.insert(1, os.path.join(sys.path[0], '..'))
from time import time
import numpy as np
#import matplotlib.pyplot as plt
import os
from config import *
import datetime
import random
import keras.optimizers
import librosa
import librosa.display
import pandas as pd
import warnings
from keras import backend as K
from utils import importData, preprocess, recall_m, precision_m, f1_m, TimingCallback, encPredict, mergeSets
totalLabel =0
filepath = 'lstmrepCheck-{epoch:02d}-{val_loss:.2f}-{val_acc:.2f}-{val_f1_m:.2f}-{val_precision_m:.2f}-{val_recall_m:.2f}-{acc:.2f}-.hdf5'
checkpoint = keras.callbacks.ModelCheckpoint(filepath, monitor='val_acc', verbose=0, save_best_only=False, save_weights_only=False, mode='auto', period=1)
cb = TimingCallback()
def fitCombined(xTrain50,xTrain20,xTest50,xTest20, y_train, y_test,combModel,fixedLayers):
if len(xTrain50.shape)==5:
xTrain50 = np.array([x.reshape( int(xTrain50.shape[2]), int(xTrain50.shape[3]), int(xTrain50.shape[4]) ) for x in xTrain50])
xTest50 = np.array([x.reshape( int(xTest50.shape[2]), int(xTest50.shape[3]), int(xTest50.shape[4])) for x in xTest50])
xTrain20 = np.array([x.reshape( int(xTrain20.shape[2]), int(xTrain20.shape[3]), int(xTrain20.shape[4]) ) for x in xTrain20])
xTest20 = np.array([x.reshape( int(xTest20.shape[2]), int(xTest20.shape[3]), int(xTest20.shape[4])) for x in xTest20])
else:
if len(xTrain50.shape)==4:
xTrain50 = np.array([x.reshape( int(xTrain50.shape[2]),int(xTrain50.shape[3]) ) for x in xTrain50])
xTest50 = np.array([x.reshape( (int(xTest50.shape[2]), int(xTest50.shape[3]))) for x in xTest50])
xTrain20 = np.array([x.reshape( int(xTrain20.shape[2]),int(xTrain20.shape[3]) ) for x in xTrain20])
xTest20 = np.array([x.reshape( (int(xTest20.shape[2]), int(xTest20.shape[3]))) for x in xTest20])
else:
xTrain50 = np.array([x.reshape( int(xTrain50.shape[2]),1) for x in xTrain50])
xTest50 = np.array([x.reshape( int(xTest50.shape[2]),1) for x in xTest50])
xTrain20 = np.array([x.reshape( int(xTrain20.shape[2]),1) for x in xTrain20])
xTest20 = np.array([x.reshape( int(xTest20.shape[2]),1) for x in xTest20])
combModel.compile(optimizer='adam', loss='categorical_crossentropy', metrics=['acc',f1_m,precision_m, recall_m])
initial_learning_rate = 0.01
#epochs = 100
drop = 0.75
epochs_drop = 10.0
decay = initial_learning_rate / epochs
def lr_time_based_decay(epoch, lr):
if epoch < 50:
return initial_learning_rate
else:
lrate = initial_learning_rate * math.pow(drop,
math.floor((1+epoch)/epochs_drop))
return lrate
indata = [xTrain50,xTrain20]
combModel.fit(indata,
y=y_train,
epochs=epochs,
batch_size=batchSize,
validation_data= ([xTest50,xTest20], y_test),#,
callbacks=[checkpoint, cb]
#callbacks=[LearningRateScheduler(lr_time_based_decay, verbose=1)],
)
print('finished fit at ', datetime.datetime.now())
loss, acc, valacc,f1,precision, recall = evaluateCheckPoints("lstmrepCheck")
print('Loss for best accuracy:', loss)
print('Best validation accuracy:', valacc)
print('Best training accuracy:', acc)
sumtime, avgtime, max_itertime,min_itertime = getAggregates(cb.logs)
print('sumtime, avgtime, max_itertime,min_itertime :', sumtime, avgtime, max_itertime,min_itertime )
outfile=open("lstmrep.perf.txt","a")
outfile.write(str(fixedLayers)+","+ str(loss)+","+ str(acc)+","+ str(valacc) +","+str(f1)+","+str(precision)+","+str(recall)+","+str(sumtime)+","+str(avgtime)+","+str(max_itertime)+","+str(min_itertime)+"\n" )
outfile.close()
print('Model exported and finished')
def mergeModels(mod50p, mod20p,startLayer):
input_shape_a=mod50p.layers[startLayer-1].get_output_at(0).shape#(128, 128,1)
nextLyr=mod50p.layers[startLayer-1].get_output_at(0)
inLyr50p=None
inLyr20p=None
if len(input_shape_a)==4:
inLyr50p = Input(shape=(int(input_shape_a[1]),int(input_shape_a[2]),int(input_shape_a[3])))#(128,128,1))#modelbase.layers[startLayer].get_output_at(0))#Input(shape=input_shape_a)
inLyr20p = Input(shape=(int(input_shape_a[1]),int(input_shape_a[2]),int(input_shape_a[3])))#(128,128,1))#modelbase.layers[startLayer].get_output_at(0))#Input(shape=input_shape_a)
else:#
if len(input_shape_a)==3:
inLyr50p = Input(shape=(int(input_shape_a[1]),int(input_shape_a[2])))#(128,128,1))#modelbase.layers[startLayer].get_output_at(0))#Input(shape=input_shape_a)
inLyr20p = Input(shape=(int(input_shape_a[1]),int(input_shape_a[2])))#(128,128,1))#modelbase.layers[startLayer].get_output_at(0))#Input(shape=input_shape_a)
else:
oldshape=K.int_shape(nextLyr)
inLyr50p = Input(shape=(oldshape[1],1))
inLyr20p = Input(shape=(oldshape[1],1))
nextLyr50p=inLyr50p
nextLyr20p=inLyr20p
nextLyr = Concatenate(name='inconCat',axis=1) ([nextLyr50p,nextLyr20p])
if len(input_shape_a)==4:
nextLyr = MaxPooling2D(pool_size=(2,1))(nextLyr)
else:
nextLyr = MaxPooling1D(pool_size=2)(nextLyr)
numlyrs =len(mod50p.layers)
currlyr=0
for layer in mod50p.layers:
if (currlyr >= startLayer):
if not isinstance(nextLyr, keras.layers.Reshape):
print(nextLyr.shape, '==>',str(layer.name), layer.get_output_at(0).shape)
if currlyr <(numlyrs-2):
if isinstance(layer, keras.layers.InputLayer):
nextLyr = layer.get_output_at(0)
if isinstance(layer, keras.layers.Conv2D):
nextLyr = Conv2D(layer.filters,layer.kernel_size)(nextLyr)#, layer.get_output_at(0).shape
if isinstance(layer, keras.layers.MaxPooling2D):
nextLyr = MaxPooling2D(pool_size=layer.pool_size)(nextLyr)
if isinstance(layer, keras.layers.Activation):
nextLyr = Activation('relu')(nextLyr)
if isinstance(layer, keras.layers.Dropout):
nextLyr = Dropout(rate=0.5)(nextLyr)
if isinstance(layer, keras.layers.Flatten):
nextLyr = Flatten()(nextLyr)
if isinstance(layer, keras.layers.Dense):
nextLyr = Dense(units=layer.units)(nextLyr)
if isinstance(layer, keras.layers.LSTM):
nextLyr = LSTM(units= layer.units,return_sequences=layer.return_sequences,unit_forget_bias=layer.unit_forget_bias,dropout=layer.dropout)(nextLyr)
if isinstance(layer, keras.layers.Reshape):
nextLyr = Reshape(layer.target_shape)(nextLyr)
currlyr+=1
numdims=1
for i in range(len(K.int_shape(nextLyr))):
if K.int_shape(nextLyr)[i]!=None:
numdims=numdims*K.int_shape(nextLyr)[i]
nextLyr =Reshape((numdims,))(nextLyr)
lastDense = Dense(totalLabel)(nextLyr)
print('lastDense shape is ', lastDense.shape)
out = Activation('softmax')(lastDense)
newModel = Model(inputs= [inLyr50p, inLyr20p], outputs=out)
newModel.summary()
return newModel
def replicateListToMatch(inList, reqSize):
outList =[]
dx=0
while (len(outList)<reqSize):
print('dx=',dx)
if (dx >=len(inList)):
outList.append(inList[dx %len(inList)])
else:
outList.append(inList[dx])
dx+=1
return np.array(outList)
if __name__ == '__main__':
#tensorboard = TensorBoard(log_dir = "logs/{}".format(time()))
dataset = importData('base')#(train, test) =
totalRecordCount=len(dataset)
#print('total recs =', totalRecordCount, '; Total Labels=', totalLabel, lblmap)
nextdataset = importData('next')#(nextTrain,nextTest) =
#print('total recs =', totalRecordCount, '; Total Labels=', totalLabel, lblmap)
#random.shuffle(dataset)
availCount= int(totalRecordCount*0.5)
availset = dataset[:availCount]
#availset = replicateListToMatch(availset,len(nextdataset)/2)
availset = mergeSets(availset, nextdataset)#train, test, nextTrain, nextTest)
availCount+=len(nextdataset)
print('AvailCount: {}'.format(availCount))
trainDataEndIndex = int(availCount*0.8)
random.shuffle(availset)
train = availset[:trainDataEndIndex]
test = availset[trainDataEndIndex:]
x, y = zip(*availset)
x_train = x[:trainDataEndIndex]
x_test = x[trainDataEndIndex:]
print(y)
print(type(y))
ycat = to_categorical(y)
y_traincat = ycat[:trainDataEndIndex]
y_testcat = ycat[trainDataEndIndex:]
x_train = np.array(x_train)
x_test = np.array(x_test)
x_train = np.expand_dims(x_train,-1)
x_test = np.expand_dims(x_test,-1)
x_train = x_train.astype('float32') / 255
x_test = x_test.astype('float32') / 255
random.shuffle(nextdataset)
nextRecordCount= len(nextdataset)
print('nextRecordCount: {}'.format(nextRecordCount))
nxttrainDataEndIndex = int(nextRecordCount*0.8)
nx, ny = zip(*nextdataset)
nx_train = nx[:nxttrainDataEndIndex]
nx_test = nx[nxttrainDataEndIndex:]
nycat = to_categorical(ny)
ny_traincat = nycat[:nxttrainDataEndIndex]
ny_testcat = nycat[nxttrainDataEndIndex:]
nx_train = np.array(nx_train)
nx_test = np.array(nx_test)
nx_train = np.expand_dims(nx_train,-1)
nx_test = np.expand_dims(nx_test,-1)
nx_train = nx_train.astype('float32') / 255
nx_test = nx_test.astype('float32') / 255
image_size = nx_train[0].shape
#encfine.built=True
#enccoarse.built=True
#print(os.getcwd())
mod50p =keras.models.load_model('50p/Model.1.hdf5',custom_objects={'tf': tf, 'f1_m':f1_m, 'precision_m':precision_m, 'recall_m':recall_m})#, custom_objects={'sampling': sampling}, compile =False)
mod20p =keras.models.load_model('20p/Model.1.hdf5',custom_objects={'tf': tf, 'f1_m':f1_m, 'precision_m':precision_m, 'recall_m':recall_m})#, custom_objects={'sampling': sampling}, compile =False)
#modelbase.summary()
#################################################################################
for layer in mod50p.layers:
layer.name = layer.name + str("_1")
for layer in mod20p.layers:
layer.name = layer.name + str("_2")
orig50_in = mod50p.layers[0].get_output_at(0)
orig20_in = mod20p.layers[0].get_output_at(0)
for i in range(len(mod50p.layers[:-2]),0,-1):
encPreModel = keras.models.clone_model(mod50p)
encPreModel.build(orig50_in)
for j in range( len(mod50p.layers)-i):
encPreModel._layers.pop()
encin = encPreModel.layers[0].get_output_at(0)
encout = encPreModel.layers[-1].get_output_at(0)
encModel=Model(inputs=encin,outputs=encout)
enc50p = keras.models.clone_model(encModel)
X_train50p_encoded = encPredict(encModel,x_train)
X_test50p_encoded = encPredict(encModel,x_test)
###########20p#############################
encPreModel = keras.models.clone_model(mod20p)
encPreModel.build(orig20_in)
#encPreModel.summary()
print('PRESUMMARY')
for j in range( len(mod20p.layers)-i):
encPreModel._layers.pop()
encin = encPreModel.layers[0].get_output_at(0)
encout = encPreModel.layers[-1].get_output_at(0)
encModel=Model(inputs=encin,outputs=encout)
#encModel.summary()
enc20p = keras.models.clone_model(encModel)
X_train20p_encoded = encPredict(encModel,x_train)
X_test20p_encoded = encPredict(encModel,x_test)
startLyr =i
combModel = mergeModels(enc20p, enc50p, startLyr)
if True:#
fitCombined(X_train50p_encoded, X_train20p_encoded, X_test50p_encoded, X_test20p_encoded, y_traincat, y_testcat, combModel, startLyr)