-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbin_classfic.py
executable file
·223 lines (176 loc) · 7.53 KB
/
bin_classfic.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
# Sound Source locate
#
# @Time : 2019-10-25 17:37
# @Author : xyzhao
# @File : bin_classfic.py
# @Description: used as a binary classification to judge indoor or outdoor
import tensorflow as tf
import numpy as np
import math
import os
import random
# x = np.array([[-29, 22, 332, -4, 225, 63],
# [-9, 222, 32, -432, 5, 33]])
# y = np.array([[0, 1], [1, 0]])
os.environ["CUDA_VISIBLE_DEVICES"] = "0"
gpu_options = tf.GPUOptions(allow_growth=True)
class BinSupervisor:
"""
init parameters, layers
"""
def __init__(self, n_features, n_actions, lr=0.001):
self.n_features = n_features
self.n_actions = n_actions
self.lr = lr
self.max_epochs = 100
# fixme, use rl as dataset
self.train_data_size = 1445
self.test_data_size = 300
self.batch_size = 16
self.s = tf.placeholder(tf.float32, [None, self.n_features], name='gcc')
self.a = tf.placeholder(tf.float32, [None, self.n_actions], name='label')
with tf.variable_scope('BinSupervised'):
l1 = tf.layers.dense(
inputs=self.s,
# units=10,
units=int(math.sqrt(self.n_features * self.n_actions)),
activation=tf.nn.leaky_relu,
kernel_initializer=tf.random_normal_initializer(mean=0, stddev=0.01),
bias_initializer=tf.constant_initializer(0.1),
name='l1'
)
self.acts_prob = tf.layers.dense(
inputs=l1,
units=self.n_actions,
activation=tf.nn.softmax,
kernel_initializer=tf.random_normal_initializer(mean=0, stddev=0.01),
bias_initializer=tf.constant_initializer(0.1),
name='acts_prob'
)
with tf.variable_scope('bin_loss'):
acts_clip = tf.clip_by_value(self.acts_prob, 1e-10, 0.9999999)
self.loss = -tf.reduce_mean(
tf.reduce_sum(self.a * tf.log(acts_clip) + (1 - self.a) * tf.log(1 - acts_clip), axis=1))
# self.loss = tf.reduce_mean(
# -tf.reduce_sum(self.a * tf.log(self.acts_prob + 0.0000001), reduction_indices=[1]))
with tf.variable_scope('bin_optimizer'):
self.train_op = tf.train.AdamOptimizer(self.lr).minimize(self.loss)
self.init = tf.global_variables_initializer()
self.sess = tf.Session(config=tf.ConfigProto(gpu_options=gpu_options))
self.sess.run(self.init)
# fixme, save all variables above (vars: layers, adam. not op and placeholder)
self.saver = tf.train.Saver()
"""
read from file into self. x, self. y;
vector_train=false, read vector_test data
note: tail line need a blank line
"""
def load_data(self, dir, file, train=True):
if train is True:
data_size = self.train_data_size
else:
data_size = self.test_data_size
self.x = [None] * data_size
self.y = [None] * data_size
with open(os.path.join(dir, file), 'r') as f:
lines = f.readlines()
for i in range(data_size):
# for i-th line
self.x[i] = []
self.y[i] = []
line = lines[i][2:len(lines[i]) - 3]
contents = line.split(',')
# scan each content
for p in range(self.n_features):
if p == self.n_features - 1:
content = contents[p][:len(contents[p]) - 1]
else:
content = contents[p]
self.x[i].append(float(content))
for p in range(self.n_features, len(contents)):
if p == self.n_features:
content = contents[p][2:]
else:
content = contents[p]
self.y[i].append(int(content))
"""
load vector_train data "vector_train", can batch learning
save save every 100 epoch
"""
def train_step(self):
# todo, build dataset
self.load_data("./gccdata/multiple/binary", "vector_bin", train=True)
records_acu = []
records_los = []
if self.train_data_size % self.batch_size == 0:
round = int(self.train_data_size / self.batch_size)
else:
round = int(self.train_data_size / self.batch_size) + 1
indexs = [i for i in range(self.train_data_size)]
"""
begin time epoch
"""
for epoch in range(self.max_epochs + 1):
print("========== Epoch %d ======" % epoch)
correct = 0.0
sum_loss = 0
random.shuffle(indexs)
for i in range(round):
# if not batch, need add lim
# s = x[np.newaxis, :]
# a = y[np.newaxis, :]
if i == (round - 1):
temp = [k for k in indexs[i * self.batch_size:]]
x = [self.x[p] for p in temp]
y = [self.y[p] for p in temp]
else:
temp = [k for k in indexs[i * self.batch_size:(i + 1) * self.batch_size]]
x = [self.x[p] for p in temp]
y = [self.y[p] for p in temp]
# may bug ,x y only single
feed_dict = {self.s: np.array(x), self.a: np.array(y)}
_, loss, acts = self.sess.run([self.train_op, self.loss, self.acts_prob], feed_dict=feed_dict)
sum_loss += loss
for p in range(len(y)):
if np.argmax(acts[p]) == np.argmax(y[p]):
correct += 1
# run train_op will auto run loss and acts,
# but if we want to get loss and acts, need additionally run them
print("loss: " + str(sum_loss / self.train_data_size))
print("accuracy: " + str(correct / self.train_data_size))
records_acu.append(correct / self.train_data_size)
records_los.append(sum_loss / self.train_data_size)
if epoch % 30 == 0 and epoch != 0:
self.saver.save(self.sess, "save/multiple/binary/save%d.ckpt" % epoch)
with open('save/multiple/binary/records_acu', 'w') as f:
f.write(str(records_acu))
with open('save/multiple/binary/records_los', 'w') as f:
f.write(str(records_los))
"""
load vector_test data "vector_test", load trained save
"""
def predict_step(self):
print("Start predict on vector_test data ....")
# todo, build test data
self.load_data("./gccdata/multiple/binary", "vector_test", train=False)
self.saver.restore(self.sess, "save/multiple/binary/save30.ckpt")
correct = 0.0
for i in range(self.test_data_size):
x = np.array(self.x[i])
x = x[np.newaxis, :]
y = np.array(self.y[i])
y = y[np.newaxis, :]
feed_dict = {self.s: x, self.a: y}
acts = self.sess.run(self.acts_prob, feed_dict=feed_dict)
if np.argmax(acts[0]) == np.argmax(y[0]):
correct += 1
print("accuracy: " + str(correct / self.test_data_size))
def is_in_room(self, x):
print("call trained model to predict ... ")
self.saver.restore(self.sess, "save/multiple/binary/save60.ckpt")
acts = self.sess.run(self.acts_prob, feed_dict={self.s: x})
return acts
if __name__ == '__main__':
sup = BinSupervisor(366, 2)
# sup.train_step()
sup.predict_step()