forked from Wi-Pose/Wi-Pose
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtrain_resize_index.py
317 lines (246 loc) · 13.6 KB
/
train_resize_index.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
306
307
308
309
310
311
import tensorflow as tf
import numpy as np
import pickle
import normalization
import cv2
import math
import os
os.environ['CUDA_VISIBLE_DEVICES']='2,3'
class autoencoder():
def __init__(
self,
train_data = None,
batch_size = 16,
learning_rate = 0.001,
training_epochs = 20,
time_scale = 20,
param_file = False,
is_train = True
):
self.train = train_data
self.batch_size = batch_size
self.lr = learning_rate
self.learning_rate=learning_rate
self.is_train = is_train
self.training_epochs = training_epochs
self.time_scale = time_scale
self.build()
print "Neural networks build!"
self.saver = tf.train.Saver()
self.sess = tf.Session()
init = tf.global_variables_initializer()
self.sess.run(init)
if is_train is True:
if param_file is True:
self.saver.restore(self.sess, "./params/train.ckpt")
print("loading neural-network params...")
self.learn()
else:
print "learning initialization!"
self.learn()
else:
self.saver.restore(self.sess, "./params/train.ckpt")
self.show()
def build(self):
self.input = tf.placeholder(tf.float32, shape = [None, 30, self.time_scale, 4], name='csi_input')
self.tag = tf.placeholder(tf.float32, shape = [None, 120, 160, 1], name ='image_origin')
self.output= tf.placeholder(tf.float32, shape = [None, 120, 160,1], name='image_output')
with tf.variable_scope('CNN'):
alpha = 0.01
w_initializer = tf.random_normal_initializer(0.,0.1)
b_initializer = tf.constant_initializer(0.1)
self.W_e_conv1 = tf.get_variable('w1', [3, 3, 4, 8], initializer=w_initializer)
b_e_conv1 = tf.get_variable('b1', [8, ], initializer=b_initializer)
self.conv1 = tf.nn.relu(
tf.add(tf.nn.conv2d(self.input, self.W_e_conv1, strides=[1, 2, 2, 1], padding='SAME'), b_e_conv1))
print self.conv1.shape
self.W_e_conv2 = tf.get_variable('w2', [1, 1, 8, 8], initializer=w_initializer)
b_e_conv2 = tf.get_variable('b2', [8, ], initializer=b_initializer)
self.conv2 = tf.nn.relu(
tf.add(tf.nn.conv2d(self.conv1, self.W_e_conv2, strides=[1, 1, 1, 1], padding='SAME'), b_e_conv2))
print self.conv2.shape
self.W_e_conv3 = tf.get_variable('w3', [3, 3, 8, 32], initializer=w_initializer)
b_e_conv3 = tf.get_variable('b3', [32, ], initializer=b_initializer)
self.conv3 = tf.nn.relu(
tf.add(tf.nn.conv2d(self.conv2, self.W_e_conv3, strides=[1, 2, 2, 1], padding='SAME'), b_e_conv3))
print self.conv3.shape
self.W_e_conv4 = tf.get_variable('w4', [1, 1, 32, 32], initializer=w_initializer)
b_e_conv4 = tf.get_variable('b4', [32, ], initializer=b_initializer)
self.conv4 = tf.nn.relu(
tf.add(tf.nn.conv2d(self.conv3, self.W_e_conv4, strides=[1, 1, 1, 1], padding='SAME'), b_e_conv4))
print self.conv4.shape
self.W_e_conv5 = tf.get_variable('w5', [3, 3, 32, 128], initializer=w_initializer)
b_e_conv5 = tf.get_variable('b5', [128, ], initializer=b_initializer)
self.conv5 = tf.nn.relu(
tf.add(tf.nn.conv2d(self.conv4, self.W_e_conv5, strides=[1, 2, 2, 1], padding='SAME'), b_e_conv5))
print self.conv5.shape
self.W_e_conv6 = tf.get_variable('w6', [1, 1, 128, 128], initializer=w_initializer)
b_e_conv6 = tf.get_variable('b6', [128, ], initializer=b_initializer)
self.conv6 = tf.nn.relu(
tf.add(tf.nn.conv2d(self.conv5, self.W_e_conv6, strides=[1, 1, 1, 1], padding='SAME'), b_e_conv6))
print self.conv6.shape
weight_features = self.SENET(self.conv6, 1)
weight_features = tf.reshape(weight_features, [-1, 4 * 3 * 128])
self.w_code = tf.get_variable('w_code', [4 * 3 * 128, 8 * 10 * 128], initializer=w_initializer, )
self.b_code = tf.get_variable('b_code', [8 * 10 * 128, ], initializer=b_initializer, )
encoder = tf.nn.relu(tf.matmul(weight_features, self.w_code) + self.b_code)
encoder = tf.reshape(encoder, [-1, 8, 10, 128])
decoder_1 = tf.image.resize_images(encoder, size=(15, 20),
method=tf.image.ResizeMethod.NEAREST_NEIGHBOR)
self.W_d_conv1 = tf.get_variable('w_d_1', [1, 1, 128, 64], initializer=w_initializer)
decoder_1 = tf.nn.conv2d(decoder_1, self.W_d_conv1, strides=[1, 1, 1, 1], padding='SAME', )
decoder_1 = tf.maximum(alpha * decoder_1, decoder_1)
print decoder_1.shape
self.W_d_conv2 = tf.get_variable('w_d_2', [1, 1, 64, 64], initializer=w_initializer)
decoder_2 = tf.nn.conv2d(decoder_1, self.W_d_conv2, strides=[1, 1, 1, 1], padding='SAME', )
decoder_2 = tf.maximum(alpha * decoder_2, decoder_2)
print decoder_2.shape
decoder_3 = tf.image.resize_images(decoder_2, size=(30, 40),
method=tf.image.ResizeMethod.NEAREST_NEIGHBOR)
self.W_d_conv3 = tf.get_variable('w_d_3', [3, 3, 64, 32], initializer=w_initializer)
decoder_3 = tf.nn.conv2d(decoder_3, self.W_d_conv3, strides=[1, 1, 1, 1], padding='SAME', )
decoder_3 = tf.maximum(alpha * decoder_3, decoder_3)
print decoder_3.shape
self.W_d_conv4 = tf.get_variable('w_d_4', [3, 3, 32, 32], initializer=w_initializer)
decoder_4 = tf.nn.conv2d(decoder_3, self.W_d_conv4, strides=[1, 1, 1, 1], padding='SAME', )
decoder_4 = tf.maximum(alpha * decoder_4, decoder_4)
print decoder_4.shape
decoder_5 = tf.image.resize_images(decoder_4, size=(60, 80),
method=tf.image.ResizeMethod.NEAREST_NEIGHBOR)
self.W_d_conv5 = tf.get_variable('w_d_5', [3, 3, 32, 8], initializer=w_initializer)
decoder_5 = tf.nn.conv2d(decoder_5, self.W_d_conv5, strides=[1, 1, 1, 1], padding='SAME', )
decoder_5 = tf.maximum(alpha * decoder_5, decoder_5)
print decoder_5.shape
self.W_d_conv6 = tf.get_variable('w_d_6', [3, 3, 8, 8], initializer=w_initializer)
decoder_6 = tf.nn.conv2d(decoder_5, self.W_d_conv6, strides=[1, 1, 1, 1], padding='SAME', )
decoder_6 = tf.maximum(alpha * decoder_6, decoder_6)
print decoder_6.shape
decoder_7 = tf.image.resize_images(decoder_6, size=(120, 160),
method=tf.image.ResizeMethod.NEAREST_NEIGHBOR)
self.W_d_conv7 = tf.get_variable('w_d_7', [3, 3, 8, 1], initializer=w_initializer)
decoder_7 = tf.nn.conv2d(decoder_7, self.W_d_conv7, strides=[1, 1, 1, 1], padding='SAME', )
decoder_7 = tf.maximum(alpha * decoder_7, decoder_7)
print decoder_7.shape
self.output = tf.reshape(decoder_7, [-1, 120, 160, 1])
max = tf.reduce_max(self.output)
min = tf.reduce_min(self.output)
self.output = (self.output - min) / (max - min)
self.output = tf.clip_by_value(self.output, 1e-7, 0.9999999)
with tf.variable_scope('loss'):
alpha, beta, rho = 5e-6, 7.5e-6, 0.08
Wset = [self.W_e_conv1, self.W_e_conv2, self.W_e_conv3, self.w2, self.W_d_conv1,self.W_d_conv2,self.W_d_conv3]
self.loss =tf.reduce_mean(-tf.reduce_sum(self.tag*tf.log(self.output)+(1.0-self.tag)*tf.log(1.0-self.output)))
with tf.variable_scope('train'):
self.optimizer = tf.train.AdamOptimizer(self.lr).minimize(self.loss)
def batch_Convert(self, csidata, image, csi_index_list):
csidata_batch, image_batch = None, None
for index in range(len(image)):
xs = csidata[:,csi_index_list[index]-self.time_scale+1:csi_index_list[index]+1 ,:]
ys = image[index]
if (index)%4==0:
csidata_batch = np.array([xs]) if csidata_batch is None else np.append(csidata_batch, [xs], axis=0)
image_batch = np.array([ys]) if image_batch is None else np.append(image_batch, [ys], axis= 0)
return csidata_batch, image_batch
def learn(self):
stop_flag=0
for j in range(self.training_epochs):
for train_data in self.train:
print train_data[2].shape
xs = train_data[0].astype(np.float32)
xs = np.nan_to_num(xs)
batch_xs, batch_ys = self.batch_Convert(xs, train_data[1], train_data[2])
print batch_xs.shape
batch_xs = np.reshape(batch_xs, [-1, 30, self.time_scale, 4])
batch_ys = batch_ys.astype(np.float32)
batch_ys = np.nan_to_num(batch_ys)
batch_ys = np.reshape(batch_ys, [-1, 120, 160, 1])
batch_ys = np.clip(batch_ys, 1e-7, 0.9999999)
for i in range(2000):
loss = 0
_, c ,output,tag= self.sess.run([self.optimizer, self.loss,self.output,self.tag], feed_dict={self.input: batch_xs, self.tag: batch_ys})
if i==10:
pass
loss += c
if math.isnan(loss) is True:
stop_flag=1
break
if np.any(np.isnan(batch_xs)):
print "Input Nan Type Error!! "
if np.any(np.isnan(batch_ys)):
print "Tag Nan Type Error!! "
if i % 5 == 0:
print("Total Epoch:", '%d' % (j), "Pic Rpoch:",'%d' % (i), "total cost=", "{:.9f}".format(loss))
if stop_flag==1:
break
if stop_flag==1:
break
print("Optimization Finished!")
self.saver.save(self.sess, "./params/train.ckpt")
def show(self):
"""
display the performance of autoencoder
:return: a autoencoder model using unsupervised learning
"""
count = 0
for train_data in self.train:
xs = train_data[0].astype(np.float32)
xs = np.nan_to_num(xs)
batch_xs, batch_ys = self.batch_Convert(xs, train_data[1], train_data[2])
batch_xs = np.reshape(batch_xs, [-1, 30, self.time_scale, 4])
output = self.sess.run(self.output, feed_dict={self.input: batch_xs})
output = np.reshape(output, (-1, 120, 160, 1))
for i in range(len(output)):
output[i] = output[i] * 255
output1 = output[i].astype(np.uint8)
# cv2.imshow("Image", output1)
# cv2.waitKey(0)
# cv2.imwrite('generator/'+str(count)+'.jpg',output1)
#
#
batch_ys[i] = batch_ys[i] * 255
target = batch_ys[i].astype(np.uint8)
cv2.imwrite('target/' + str(count) + '.jpg', target)
count += 1
def conv2d(self, x, W):
return tf.nn.conv2d(x, W, strides=[1,2,2,1], padding='SAME')
def deconv2d(self, x,W, output_shape):
return tf.nn.conv2d_transpose(x, W, output_shape, strides=[1,2,2,1], padding = 'SAME')
def kldlv(rho, rho_hat):
invrho = tf.subtract(tf.constant(1.), rho)
invrhohat = tf.subtract(tf.constant(1.), rho_hat)
logrho = tf.add(logfunc(rho, rho_hat), logfunc(invrho, invrhohat))
return logrho
def logfunc(x, x2):
return tf.multiply(x, tf.log(tf.div(x, x2)))
def batchNormalization(data):
for each_item in range(len(data)):
data[each_item] = normalization.MINMAXNormalization(data[each_item])
def package(train_data):
csi_rx1,csi_rx2, image ,index= train_data[0], train_data[1],train_data[2],train_data[3]
tn_data = np.append(csi_rx1, csi_rx2, axis=0)
tn_data = np.transpose(tn_data, [1,2,0])
return [tn_data, image,index]
if __name__ =="__main__":
np.set_printoptions(threshold=np.inf)
train_data=[]
for i in range( ):#data range
index=i+1
if index == :#abnormal data
pass
elif index == :#test data
pass
# with open('../data_523/data_index/training_data_' + str(index) + '.pkl', 'rb') as handle:
# data_temp = pickle.load(handle)
# batchNormalization(data_temp[0])
# #batchNormalization(data_temp[1])
# data_nor = package(data_temp)
# train_data.append(data_nor)
else:
with open('../data_523/data_index_dwt/training_data_' + str(index) + '.pkl', 'rb') as handle:
data_temp = pickle.load(handle)
batchNormalization(data_temp[0])
#batchNormalization(data_temp[1])
data_nor = package(data_temp)
train_data.append(data_nor)
print len(train_data)
autoencoder(train_data=train_data)