-
Notifications
You must be signed in to change notification settings - Fork 610
Expand file tree
/
Copy pathdp.py
More file actions
300 lines (256 loc) · 11.1 KB
/
dp.py
File metadata and controls
300 lines (256 loc) · 11.1 KB
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
# 为了 Python2 玩家们
from __future__ import print_function, division
# 第三方
import tensorflow as tf
from sklearn.metrics import confusion_matrix
import numpy as np
# 我们自己
import load
if(tf.__version__.startswith("1.")):
image_summary , scalar_summary= tf.summary.image , tf.summary.scalar
merge_summary , histogram_summary = tf.summary.merge , tf.summary.histogram
else:
image_summary , scalar_summary = tf.image_summary , tf.scalar_summary
merge_summary , histogram_summary = tf.merge_summary , tf.histogram_summary
train_samples, train_labels = load._train_samples, load._train_labels
test_samples, test_labels = load._test_samples, load._test_labels
print('Training set', train_samples.shape, train_labels.shape)
print(' Test set', test_samples.shape, test_labels.shape)
image_size = load.image_size
num_labels = load.num_labels
num_channels = load.num_channels
def get_chunk(samples, labels, chunkSize):
'''
Iterator/Generator: get a batch of data
这个函数是一个迭代器/生成器,用于每一次只得到 chunkSize 这么多的数据
用于 for loop, just like range() function
'''
if len(samples) != len(labels):
raise Exception('Length of samples and labels must equal')
stepStart = 0 # initial step
i = 0
while stepStart < len(samples):
stepEnd = stepStart + chunkSize
if stepEnd < len(samples):
yield i, samples[stepStart:stepEnd], labels[stepStart:stepEnd]
i += 1
stepStart = stepEnd
class Network():
def __init__(self, num_hidden, batch_size, conv_depth, patch_size, pooling_scale):
'''
@num_hidden: 隐藏层的节点数量
@batch_size:因为我们要节省内存,所以分批处理数据。每一批的数据量。
'''
self.batch_size = batch_size
self.test_batch_size = 500
# Hyper Parameters
self.num_hidden = num_hidden
self.patch_size = patch_size # 滑窗的大小
self.conv1_depth = conv_depth
self.conv2_depth = conv_depth
self.conv3_depth = conv_depth
self.conv4_depth = conv_depth
self.last_conv_depth = self.conv4_depth
self.pooling_scale = pooling_scale
self.pooling_stride = self.pooling_scale # Max Pooling Stride
# Graph Related
self.graph = tf.Graph()
self.tf_train_samples = None
self.tf_train_labels = None
self.tf_test_samples = None
self.tf_test_labels = None
self.tf_test_prediction = None
# 统计
self.merged = None
self.train_summaries = []
self.test_summaries = []
# 初始化
self.define_graph()
self.session = tf.Session(graph=self.graph)
self.writer = tf.train.SummaryWriter('./board', self.graph)
def define_graph(self):
'''
定义我的的计算图谱
'''
with self.graph.as_default():
# 这里只是定义图谱中的各种变量
with tf.name_scope('inputs'):
self.tf_train_samples = tf.placeholder(
tf.float32, shape=(self.batch_size, image_size, image_size, num_channels), name='tf_train_samples'
)
self.tf_train_labels = tf.placeholder(
tf.float32, shape=(self.batch_size, num_labels), name='tf_train_labels'
)
self.tf_test_samples = tf.placeholder(
tf.float32, shape=(self.test_batch_size, image_size, image_size, num_channels), name='tf_test_samples'
)
with tf.name_scope('conv1'):
conv1_weights = tf.Variable(
tf.truncated_normal([self.patch_size, self.patch_size, num_channels, self.conv1_depth], stddev=0.1))
conv1_biases = tf.Variable(tf.zeros([self.conv1_depth]))
with tf.name_scope('conv2'):
conv2_weights = tf.Variable(
tf.truncated_normal([self.patch_size, self.patch_size, self.conv1_depth, self.conv2_depth], stddev=0.1))
conv2_biases = tf.Variable(tf.constant(0.1, shape=[self.conv2_depth]))
with tf.name_scope('conv3'):
conv3_weights = tf.Variable(
tf.truncated_normal([self.patch_size, self.patch_size, self.conv2_depth, self.conv3_depth], stddev=0.1))
conv3_biases = tf.Variable(tf.constant(0.1, shape=[self.conv3_depth]))
with tf.name_scope('conv4'):
conv4_weights = tf.Variable(
tf.truncated_normal([self.patch_size, self.patch_size, self.conv3_depth, self.conv4_depth], stddev=0.1))
conv4_biases = tf.Variable(tf.constant(0.1, shape=[self.conv4_depth]))
# fully connected layer 1, fully connected
with tf.name_scope('fc1'):
down_scale = self.pooling_scale ** 2 # because we do 2 times pooling of stride 2
fc1_weights = tf.Variable(
tf.truncated_normal(
[(image_size // down_scale) * (image_size // down_scale) * self.last_conv_depth, self.num_hidden], stddev=0.1))
fc1_biases = tf.Variable(tf.constant(0.1, shape=[self.num_hidden]))
self.train_summaries.append(histogram_summary('fc1_weights', fc1_weights))
self.train_summaries.append(histogram_summary('fc1_biases', fc1_biases))
# fully connected layer 2 --> output layer
with tf.name_scope('fc2'):
fc2_weights = tf.Variable(tf.truncated_normal([self.num_hidden, num_labels], stddev=0.1), name='fc2_weights')
fc2_biases = tf.Variable(tf.constant(0.1, shape=[num_labels]), name='fc2_biases')
self.train_summaries.append(histogram_summary('fc2_weights', fc2_weights))
self.train_summaries.append(histogram_summary('fc2_biases', fc2_biases))
# 想在来定义图谱的运算
def model(data, train=True):
'''
@data: original inputs
@return: logits
'''
with tf.name_scope('conv1_model'):
with tf.name_scope('convolution'):
conv1 = tf.nn.conv2d(data, filter=conv1_weights, strides=[1, 1, 1, 1], padding='SAME')
addition = conv1 + conv1_biases
hidden = tf.nn.relu(addition)
if not train:
# transpose the output of an activation to image
# conv1_activation_relu shape: (8, 32, 32, 64)
# 64 filter maps from this convolution, that's 64 grayscale images
# image size is 32x32
# 8 is the batch_size, which means 8 times of convolution was performed
# just use the last one (index 7) as record
filter_map = hidden[-1]
filter_map = tf.transpose(filter_map, perm=[2, 0, 1])
filter_map = tf.reshape(filter_map, (self.conv1_depth, 32, 32, 1))
self.test_summaries.append(image_summary('conv1_relu', tensor=filter_map, max_images=self.conv1_depth))
with tf.name_scope('conv2_model'):
with tf.name_scope('convolution'):
conv2 = tf.nn.conv2d(hidden, filter=conv2_weights, strides=[1, 1, 1, 1], padding='SAME')
addition = conv2 + conv2_biases
hidden = tf.nn.relu(addition)
hidden = tf.nn.max_pool(
hidden,
ksize=[1,self.pooling_scale,self.pooling_scale,1],
strides=[1,self.pooling_stride,self.pooling_stride,1],
padding='SAME')
with tf.name_scope('conv3_model'):
with tf.name_scope('convolution'):
conv3 = tf.nn.conv2d(hidden, filter=conv3_weights, strides=[1, 1, 1, 1], padding='SAME')
addition = conv3 + conv3_biases
hidden = tf.nn.relu(addition)
with tf.name_scope('conv4_model'):
with tf.name_scope('convolution'):
conv4 = tf.nn.conv2d(hidden, filter=conv4_weights, strides=[1, 1, 1, 1], padding='SAME')
addition = conv4 + conv4_biases
hidden = tf.nn.relu(addition)
# if not train:
# filter_map = hidden[-1]
# filter_map = tf.transpose(filter_map, perm=[2, 0, 1])
# filter_map = tf.reshape(filter_map, (self.conv4_depth, 16, 16, 1))
# tf.image_summary('conv4_relu', tensor=filter_map, max_images=self.conv4_depth)
hidden = tf.nn.max_pool(
hidden,
ksize=[1,self.pooling_scale,self.pooling_scale,1],
strides=[1,self.pooling_stride,self.pooling_stride,1],
padding='SAME')
# fully connected layer 1
shape = hidden.get_shape().as_list()
reshape = tf.reshape(hidden, [shape[0], shape[1] * shape[2] * shape[3]])
with tf.name_scope('fc1_model'):
fc1_model = tf.matmul(reshape, fc1_weights) + fc1_biases
hidden = tf.nn.relu(fc1_model)
# fully connected layer 2
with tf.name_scope('fc2_model'):
return tf.matmul(hidden, fc2_weights) + fc2_biases
# Training computation.
logits = model(self.tf_train_samples)
with tf.name_scope('loss'):
self.loss = tf.reduce_mean(tf.nn.softmax_cross_entropy_with_logits(logits, self.tf_train_labels))
self.train_summaries.append(scalar_summary('Loss', self.loss))
# Optimizer.
with tf.name_scope('optimizer'):
self.optimizer = tf.train.GradientDescentOptimizer(0.0001).minimize(self.loss)
# Predictions for the training, validation, and test data.
with tf.name_scope('train'):
self.train_prediction = tf.nn.softmax(logits, name='train_prediction')
with tf.name_scope('test'):
self.test_prediction = tf.nn.softmax(model(self.tf_test_samples, train=False), name='test_prediction')
self.merged_train_summary = merge_summary(self.train_summaries)
self.merged_test_summary = merge_summary(self.test_summaries)
def run(self):
'''
用到Session
'''
# private function
def print_confusion_matrix(confusionMatrix):
print('Confusion Matrix:')
for i, line in enumerate(confusionMatrix):
print(line, line[i]/np.sum(line))
a = 0
for i, column in enumerate(np.transpose(confusionMatrix, (1, 0))):
a += (column[i]/np.sum(column))*(np.sum(column)/26000)
print(column[i]/np.sum(column),)
print('\n',np.sum(confusionMatrix), a)
with self.session as session:
tf.initialize_all_variables().run()
### 训练
print('Start Training')
# batch 1000
for i, samples, labels in get_chunk(train_samples, train_labels, chunkSize=self.batch_size):
_, l, predictions, summary = session.run(
[self.optimizer, self.loss, self.train_prediction, self.merged_train_summary],
feed_dict={self.tf_train_samples: samples, self.tf_train_labels: labels}
)
self.writer.add_summary(summary, i)
# labels is True Labels
accuracy, _ = self.accuracy(predictions, labels)
if i % 50 == 0:
print('Minibatch loss at step %d: %f' % (i, l))
print('Minibatch accuracy: %.1f%%' % accuracy)
###
### 测试
accuracies = []
confusionMatrices = []
for i, samples, labels in get_chunk(test_samples, test_labels, chunkSize=self.test_batch_size):
result, summary = session.run(
[self.test_prediction, self.merged_test_summary],
feed_dict={self.tf_test_samples: samples}
)
# result = self.test_prediction.eval()
self.writer.add_summary(summary, i)
accuracy, cm = self.accuracy(result, labels, need_confusion_matrix=True)
accuracies.append(accuracy)
confusionMatrices.append(cm)
print('Test Accuracy: %.1f%%' % accuracy)
print(' Average Accuracy:', np.average(accuracies))
print('Standard Deviation:', np.std(accuracies))
print_confusion_matrix(np.add.reduce(confusionMatrices))
###
def accuracy(self, predictions, labels, need_confusion_matrix=False):
'''
计算预测的正确率与召回率
@return: accuracy and confusionMatrix as a tuple
'''
_predictions = np.argmax(predictions, 1)
_labels = np.argmax(labels, 1)
cm = confusion_matrix(_labels, _predictions) if need_confusion_matrix else None
# == is overloaded for numpy array
accuracy = (100.0 * np.sum(_predictions == _labels) / predictions.shape[0])
return accuracy, cm
if __name__ == '__main__':
net = Network(num_hidden=16, batch_size=64, patch_size=3, conv_depth=16, pooling_scale=2)
net.run()