-
Notifications
You must be signed in to change notification settings - Fork 8
/
main2.py
305 lines (235 loc) · 9.03 KB
/
main2.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
# ======================================================================================================================================================
#!/usr/bin/env python
# coding: utf-8
# Angle of Arrival Estimation of Uniform Linear and Cicular Array
# Jianyuan Jet Yu, [email protected]
# v2; 2 dimension
# ===============
import yagmail
from myFunction import myCompress
import time
import os
import math
# ======================== global setting ========================
dim = 1
isSend = 0 # send Email
epochs = 200 # number of learning epochs
batch_size = 400
os.environ["KMP_DUPLICATE_LIB_OK"] = "1" # sometime make "1" for Mac
TEXT = "test"
num_bins = 50
tic = time.time()
import random
os.environ["CUDA_VISIBLE_DEVICES"] = "0"
import tensorflow as tf
import keras.backend.tensorflow_backend as KTF
def get_session(gpu_fraction=1):
num_threads = os.environ.get('OMP_NUM_THREADS')
gpu_options = tf.GPUOptions(per_process_gpu_memory_fraction=gpu_fraction)
if num_threads:
return tf.Session(config=tf.ConfigProto(
gpu_options=gpu_options, intra_op_parallelism_threads=num_threads))
else:
return tf.Session(config=tf.ConfigProto(gpu_options=gpu_options))
KTF.set_session(get_session())
import numpy as np
import hdf5storage
from keras import regularizers
from keras.models import Model, Sequential
from keras.layers import Input, Dense, Lambda, Reshape, Conv1D, Conv2D,\
AveragePooling2D,Flatten, Dropout, SimpleRNN, LSTM, concatenate, Layer
from keras import backend as K
from keras.layers.normalization import BatchNormalization
from keras.callbacks import EarlyStopping
from IPython.core.display import Image, display
import matplotlib.pyplot as plt
from numpy import ma
import scipy.io as sio
from IPython.display import Image
from matplotlib import cm as CM
from nbconvert import HTMLExporter
import keras
keras.__version__
# Visualize training history
from keras import callbacks
tb = callbacks.TensorBoard(log_dir='./logs', histogram_freq=10, batch_size=32,
write_graph=True, write_grads=True, write_images=False,
embeddings_freq=0, embeddings_layer_names=None, embeddings_metadata=None)
# Early stopping
early_stop = EarlyStopping(monitor='val_loss', min_delta=0, patience=5, verbose=1, mode='auto')
#!nvidia-smi
# Distance Functions
def true_dist(y_true, y_pred):
return np.sqrt(np.square(np.abs(y_pred[:,0]-y_true[:,0]))+ np.square(np.abs(y_pred[:,1]-y_true[:,1])) )
def dist(y_true, y_pred):
return tf.reduce_mean((tf.sqrt(tf.square(tf.abs(y_pred[:,0]-y_true[:,0]))+ tf.square(tf.abs(y_pred[:,1]-y_true[:,1])))))
class RBFLayer(Layer):
def __init__(self, units, gamma, **kwargs):
super(RBFLayer, self).__init__(**kwargs)
self.units = units
self.gamma = K.cast_to_floatx(gamma)
def build(self, input_shape):
self.mu = self.add_weight(name='mu',
shape=(int(input_shape[1]), self.units),
initializer='uniform',
trainable=True)
super(RBFLayer, self).build(input_shape)
def call(self, inputs):
diff = K.expand_dims(inputs) - self.mu
l2 = K.sum(K.pow(diff,2), axis=1)
res = K.exp(-1 * self.gamma * l2)
return res
def compute_output_shape(self, input_shape):
return (input_shape[0], self.units)
# ===============
# Example for Measurement Quality
t = hdf5storage.loadmat('./data/testXtrain.mat')
Xtrain = np.transpose(t['Xtrain'])
t = hdf5storage.loadmat('./data/testYtrain.mat')
Ytrain = np.transpose(t['Ytrain'])
t = hdf5storage.loadmat('./data/testXtest.mat')
Xtest = np.transpose(t['Xtest'])
t = hdf5storage.loadmat('./data/testYtest.mat')
Ytest = np.transpose(t['Ytest'])
Xval = Xtrain[0:600,:];
Yval = Ytrain[0:600,:];
Xtrain = Xtrain[601:,:];
Ytrain = Ytrain[601:,:];
# extend dim
Xtrain = Xtrain[:,:,np.newaxis]
Xtest = Xtest[:,:,np.newaxis]
Xval = Xval[:,:,np.newaxis]
nn_input = Input((78,1))
nn_output = Flatten()(nn_input)
# nn_output = LSTM(units=136, dropout_U = 0.2, dropout_W = 0.2)(nn_input)
nn_output = Dense(64,activation='relu', kernel_regularizer=regularizers.l2(0.001))(nn_output)
nn_output = RBFLayer(64, 0.5)(nn_output)
nn_output = RBFLayer(64, 0.5)(nn_output)
#nn_output = Dropout(0.2)(nn_output)
#nn_output = BatchNormalization()(nn_output)
#nn_output = Dense(64,activation='relu', kernel_regularizer=regularizers.l2(0.001))(nn_output)
#nn_output = Dropout(0.2)(nn_output)
#nn_output = BatchNormalization()(nn_output)
#nn_output = Dense(64,activation='relu', kernel_regularizer=regularizers.l2(0.001))(nn_output)
#nn_output = Dropout(0.2)(nn_output)
#nn_output = BatchNormalization()(nn_output)
nn_output = Dense(2,activation='linear')(nn_output)
# directly output 3 para, non classify
nn = Model(inputs=nn_input,outputs=nn_output)
nn.compile(optimizer='Adam', loss='mse',metrics=[dist])
nn.summary()
train_hist = nn.fit(x=Xtrain,y=Ytrain,\
batch_size = batch_size ,epochs = epochs ,\
validation_data=(Xval, Yval), \
shuffle=True,\
callbacks=[tb, early_stop])
# Evaluate Performance
Ypredtrain = nn.predict( Xtrain)
Ypred = nn.predict( Xtest)
Ypredtrain = Ypredtrain/math.pi*180
Ypred = Ypred/math.pi*180
Ytest = Ytest/math.pi*180
Ytrain = Ytrain/math.pi*180
errors_train = true_dist(Ytrain,Ypredtrain)
errors_test = true_dist(Ytest,Ypred)
Mean_Error_Train = np.mean(np.abs(errors_train))
Mean_Error_Test= np.mean(np.abs(errors_test))
print("Mean error on Train area:", Mean_Error_Train)
print("Mean error on Test area:",Mean_Error_Test)
toc = time.time()
timeCost = toc - tic
print( "--- Totally %s seconds ---" %(timeCost))
# Histogramm of errors on test Area
plt.figure(2)
errors = true_dist(Ypred , Ytest)
plt.hist(errors,bins=64)
plt.ylabel('Number of occurence')
plt.xlabel('Estimate error (deg)')
plt.grid(True)
plt.title('histogram of estimation error')
plt.savefig('./Figpy/err_occ.png')
num_bins = 50
plt.figure(3)
counts, bin_edges = np.histogram(errors, bins=num_bins)
cdf = np.cumsum(counts)/np.sum(counts)
plt.plot(bin_edges[1:], cdf)
plt.xlabel('est error (deg)')
plt.ylabel('F(X<x)')
plt.grid(True)
plt.title('Cdfplot of distance error')
plt.savefig('./Figpy/err_cdf.png')
# ===============
# Error Vector over Area in XY
#error_vectors = np.real(Ypred - Ytest)
#plt.figure(4)
#plt.quiver(np.real(Ytest[:,0]),np.real(Ytest[:,1]),error_vectors[:,0],error_vectors[:,1],errors)
#plt.xlabel("x in m")
#plt.ylabel("y in m")
#plt.grid(True)
#plt.title('quiver of distance error')
#plt.savefig('position.png')
plt.figure(5)
plt.plot(train_hist.history['dist'])
plt.plot(train_hist.history['val_dist'])
plt.title('distance')
plt.ylabel('distance')
plt.xlabel('epoch')
plt.grid(True)
plt.legend(['train', 'validate'])
plt.savefig('./Figpy/hist_dist.png')
plt.figure(6)
plt.plot(train_hist.history['loss'])
plt.plot(train_hist.history['val_loss'])
plt.title('model loss')
plt.ylabel('loss')
plt.xlabel('epoch')
plt.grid(True)
plt.legend(['train', 'validate'])
plt.savefig('./Figpy/hist_loss.png')
dim = 0
plt.figure(7)
plt.scatter( Ytrain[:,dim], Ypredtrain[:,dim],facecolors='none',edgecolors='b')
plt.title('dim%d train - est vs ground'%dim)
plt.ylabel('est')
plt.xlabel('ground')
plt.grid(True)
plt.savefig('./Figpy/dim%d_train.png'%dim)
plt.figure(8)
plt.scatter( Ytest[:,dim], Ypred[:,dim],facecolors='none',edgecolors='b')
plt.title('dim%d test - est vs ground'%dim)
plt.ylabel('est')
plt.xlabel('ground')
plt.grid(True)
plt.savefig('./Figpy/dim%d_test.png'%dim)
dim = 1
plt.figure(9)
plt.scatter( Ytrain[:,dim], Ypredtrain[:,dim],facecolors='none',edgecolors='b')
plt.title('dim%d train - est vs ground'%dim)
plt.ylabel('est')
plt.xlabel('ground')
plt.grid(True)
plt.savefig('./Figpy/dim%d_train.png'%dim)
plt.figure(10)
plt.scatter( Ytest[:,dim], Ypred[:,dim],facecolors='none',edgecolors='b')
plt.title('dim%d test - est vs ground'%dim)
plt.ylabel('est')
plt.xlabel('ground')
plt.grid(True)
plt.savefig('./Figpy/dim%d_test.png'%dim)
# =============== save result ===========
with open("outPut.txt", "w") as text_file:
text_file.write( "--- Totally %s seconds --- \n" %(timeCost))
text_file.write("Mean error on Train area: %s \n" %(Mean_Error_Train))
text_file.write("Mean error on Test area: %s \n" %(Mean_Error_Test))
# =============== send email ===========
FROM = '[email protected]'
TO = '[email protected]'
SUBJECT = 'CTW train err %.4f, test err %.4f'%(Mean_Error_Train, Mean_Error_Test)
ATTACHMENTS = ['./Figpy/dim%d_test.png'%dim,'./Figpy/dim%d_train.png'%dim,
'outPut.txt','./Figpy/cdf.png','./Figpy/err_occ.png','./Figpy/err_cdf.png',\
'./Figpy/hist_dist.png','./Figpy/hist_loss.png','main.py'
]
yag = yagmail.SMTP(FROM, 'yu3843526')
if isSend:
yag.send(TO, SUBJECT, TEXT,ATTACHMENTS)
# ======================================================================================================================================================