-
Notifications
You must be signed in to change notification settings - Fork 0
/
Dense_MAnifoldLearning_2.py
135 lines (107 loc) · 4.82 KB
/
Dense_MAnifoldLearning_2.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
#------------------------------------------------------------------------------
from keras.layers import Activation as activation
from keras.layers.core import Dense, Flatten , Dropout
from keras.layers.convolutional import Convolution2D
from keras.layers.normalization import BatchNormalization
from keras.layers.pooling import MaxPooling2D
from keras.models import Model
from keras import layers
from keras import Input
import numpy as np
from keras.utils.np_utils import to_categorical
from keras.optimizers import *
from keras import callbacks
from keras.callbacks import TensorBoard, CSVLogger
import os
import scipy.io
import h5py
import mat73
from keras import optimizers
#------------------------------------------------------------------------------
#------------------------------------------------------------------------------
#import data
path='D:\Shapar\ShaghayeghUni\AfterPropozal\Step1-EventLandmark\Programs\MyPrograms\EventExtraction\Keras'
#train:
CbTotal = mat73.loadmat(path+'\CbTotal.mat')
x_train=np.transpose(CbTotal['CbTotal'])
CbOutTotal = mat73.loadmat(path+'\CbOutTotal.mat')
CbOut=np.transpose(CbOutTotal['CbOutTotal'])
LblTotal = scipy.io.loadmat(path+'\LblTotal.mat')
y_train=LblTotal['LblTotal']
#val:
CB_total_val= scipy.io.loadmat(path+'\CB_total_val.mat')
x_val=CB_total_val['CB_total_val']
LBL_total_val = scipy.io.loadmat(path+'\LBL_total_val.mat')
y_val=LBL_total_val['LBL_total_val']
ValOut = scipy.io.loadmat(path+'\ValOut.mat')
ValOut=ValOut['ValOut']
#-----------------------------------------------------------------------------
#------------------------------------------------------------------------------
L=len(x_train[0])
input_tensor = Input(shape=(L,))
x=input_tensor
x = Dense(500, activation="relu")(x)
x = Dense(400, activation="relu")(x)
x2= Dense(300, activation="relu")(x)
x2= Dense(270, activation="linear", name='out1')(x2)
outDae=x2;
x = Dense(300, activation="relu")(x)
x = Dense(300, activation="relu")(x)
x = Dense(500, activation="relu")(x)
x = Dense(300, activation="relu")(x)
x = Dense(103, activation="linear",name='out2')(x)
output = x
model=Model(input_tensor, [outDae, output])
model.summary()
#------------------------------------------------------------------------------
#------------------------------------------------------------------------------
L=len(x_train[0])
input_tensor = Input(shape=(L,))
x=input_tensor
x = Dense(1000, activation="relu")(x)
x = Dense(1000, activation="relu")(x)
x = Dense(1000, activation="relu")(x)
x2= Dense(1000, activation="relu")(x)
x2= Dense(1000, activation="relu")(x)
x2= Dense(270, activation="linear", name='out1')(x2)
outDae=x2;
x = Dense(1000, activation="relu")(x)
x = Dense(1000, activation="relu")(x)
x = Dense(1000, activation="relu")(x)
x = Dense(103, activation="linear",name='out2')(x)
output = x
model=Model(input_tensor, [outDae, output])
model.summary()
#-------------------
#------------------------------------------------------------------------------
opt1 = optimizers.Adam(lr=0.0002, beta_1=0.9, beta_2=0.999, epsilon=1e-08, decay=0.0)
model.compile(loss={'out1': 'mean_squared_error', 'out2': 'mean_squared_error'},
loss_weights={'out1': 0.5, 'out2': 0.5},
optimizer=opt1)
reduce_LR = callbacks.ReduceLROnPlateau(monitor='mean_squared_error', factor=0.1, patience=5, verbose=1, mode='max', min_delta=0.0001, cooldown=0, min_lr=0)
tensorboard1 = TensorBoard(path+'/log_dir')
logger = CSVLogger(path+'/training.log')
folder_model="./model"
path_model = os.path.join(folder_model, 'model_epoch_{epoch:02d}.hdf5')
model_checkpoint = callbacks.ModelCheckpoint(filepath= path_model,monitor="mean_squared_error", mode="max", verbose=0, save_best_only=False, save_weights_only=False)
Early_stop=callbacks.EarlyStopping(monitor='mean_squared_error', min_delta=0.0001, patience=10, verbose=0, mode='auto', baseline=None)
# train model
history = model.fit(x_train, [CbOut,y_train], batch_size=128, epochs=500, shuffle=True, verbose=1,
validation_data=(x_val, [ValOut,y_val]),
callbacks= [model_checkpoint, tensorboard1, logger, reduce_LR, Early_stop]
)
model.save('ModelManifoldLearningnew.h5')
#------------------------------------------------------------------------------
#------------------------------------------------------------------------------
PathTest=path+'\TestMat'
PathOut=path+'\TestOut'
import glob
D=glob.glob(PathTest+"/*.mat")
NumTest=len(D)
for i in range(NumTest):
CB= scipy.io.loadmat(D[i])
CB_value=CB['CB_context']
y_predict = model.predict(CB_value)[1]
D2=D[i].replace(PathTest, PathOut)
scipy.io.savemat(D2, {'lbl': y_predict})
#------------------------------------------------------------------------------