-
Notifications
You must be signed in to change notification settings - Fork 7
/
sEMGReLuReLuSoftmax.py
168 lines (136 loc) · 6.1 KB
/
sEMGReLuReLuSoftmax.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
from classNinapro import Ninapro
import numpy as np
from usefulFcns import *
import tensorflow as tf
print(tf.__version__)
Debug = True # for tensor dimensionality checking
ninapro = Ninapro()
ninapro.splitImagesLabels()
# Train
print('ninapro.TrainImages shape: ', ninapro.TrainImages.shape) # m x 16 x 30
print('ninapro.TrainLabels shape: ', ninapro.TrainLabels.shape) # m x 8
# Test
print('ninapro.TestImages shape: ', ninapro.TestImages.shape) # m x 16 x 30
print('ninapro.TestLabels shape: ', ninapro.TestLabels.shape) # m x 8
# Validate
print('ninapro.ValidateImages shape: ', ninapro.ValidateImages.shape) # m x 16 x 30
print('ninapro.ValidateLabels shape: ', ninapro.ValidateLabels.shape) # m x 8
print('Read successfully done...')
# Scale the original RMS pixel value
#ninapro.TrainImages *= 1000
#ninapro.TestImages *= 1000
#ninapro.ValidateImages *= 1000
# number of total classes of movements, 8 for exampel.
nMV = ninapro.TrainLabels.shape[1]
partIndex = [0,1,2,3,4,5,6,10,11,12,13,14,15] # exclude [7,8,9] these three channels.
nCh = 13
# - build the Convolutional Neural Network
# Setup placeholders for input data
with tf.name_scope('Input'):
x = tf.placeholder(tf.float32, shape=[None, nCh,30], name='X')
y = tf.placeholder(tf.float32, shape=[None, nMV], name='Labels')
if Debug:
print('input x shape: ', x.shape)
print('input y shape: ', y.shape)
with tf.name_scope('Flattern'):
x_flatten = tf.reshape(x, [-1, nCh*30])
if Debug:
print('x_image shape: ', x_flatten.shape)
firstIn = nCh*30 # 13*30 = 480
firstOut = 1024 # ---- number of hidden units in the first layer.
with tf.name_scope('ReLu-1'):
#w1 = tf.Variable(tf.truncated_normal([firstIn, firstOut], stddev=0.1), name = 'W')
#b1 = tf.Variable(tf.constant(0.1, shape=[firstOut]), name = 'B' )
w1 = tf.get_variable('W1', [firstIn, firstOut], initializer=tf.contrib.layers.xavier_initializer())
b1 = tf.get_variable('B1', [1, firstOut], initializer=tf.contrib.layers.xavier_initializer())
z1 = tf.add(tf.matmul(x_flatten, w1), b1)
a1 = tf.nn.relu(z1)
# summary
tf.summary.histogram('weights', w1)
tf.summary.histogram('biases', b1)
tf.summary.histogram('z', z1)
tf.summary.histogram('activation', a1)
# dimensionality checking
if Debug:
print('w1 shape: ', w1.shape)
print('b1 shape: ', b1.shape)
print('z1 shape: ', z1.shape)
print('a1 shape: ', a1.shape)
secondIn = firstOut
secondOut = 16 # ------- number of hidden units in the second layer.
with tf.name_scope('ReLu-2'):
#w2 = tf.Variable(tf.truncated_normal([secondIn, secondOut], stddev=0.1), name='W')
#b2 = tf.Variable(tf.constant(0.1, shape=[secondOut]), name='B')
w2 = tf.get_variable('W2', [secondIn, secondOut], initializer=tf.contrib.layers.xavier_initializer())
b2 = tf.get_variable('B2', [1, secondOut], initializer=tf.contrib.layers.xavier_initializer())
z2 = tf.add(tf.matmul(a1, w2), b2)
a2 = tf.nn.relu(z2)
# summary
tf.summary.histogram('weights', w2)
tf.summary.histogram('biases', b2)
tf.summary.histogram('z', z2)
tf.summary.histogram('activation', a2)
# dimensionality checking
if Debug:
print('w2 shape: ', w2.shape)
print('b2 shape: ', b2.shape)
print('z2 shape: ', z2.shape)
print('a2 shape: ', a2.shape)
thirdIn = secondOut
thirdOut = nMV
with tf.name_scope('Softmax'):
#w3 = tf.Variable(tf.truncated_normal([thirdIn, thirdOut], stddev=0.1), name='W')
#b3 = tf.Variable(tf.constant(0.1, shape=[thirdOut]), name='B')
w3 = tf.get_variable('W3', [thirdIn, thirdOut], initializer=tf.contrib.layers.xavier_initializer())
b3 = tf.get_variable('B3', [1, thirdOut], initializer=tf.contrib.layers.xavier_initializer())
z3 = tf.add(tf.matmul(a2, w3), b3)
# summary
tf.summary.histogram('weights', w3)
tf.summary.histogram('biases', b3)
tf.summary.histogram('z', z3)
# dimensionality checking
if Debug:
print('w3 shape: ', w3.shape)
print('b3 shape: ', b3.shape)
print('z3 shape: ', z3.shape)
with tf.name_scope('Loss'):
cross_entropy = tf.reduce_mean(tf.nn.softmax_cross_entropy_with_logits(logits=z3, labels=y), name='Loss')
# summary
tf.summary.scalar('cross_entropy', cross_entropy)
with tf.name_scope('Accuracy'):
correct_prediction = tf.equal(tf.argmax(z3, 1), tf.argmax(y, 1))
accuracy = tf.reduce_mean(tf.cast(correct_prediction, tf.float32))
# summary
tf.summary.scalar('accuracy', accuracy)
# Use an AdamOptimizer to train the network
#train = tf.train.AdamOptimizer(1e-3).minimize(cross_entropy)
train = tf.train.GradientDescentOptimizer(1e-1).minimize(cross_entropy)
# Visualization directory
graph_dir = 'graphReLuReLuSoftmax'
import usefulFcns
usefulFcns.BuildNewlyDir(graph_dir)
# Train the model
with tf.Session() as sess:
sess.run(tf.global_variables_initializer())
merged_summary = tf.summary.merge_all()
writer = tf.summary.FileWriter(graph_dir)
writer.add_graph(sess.graph)
for i in range(5000):
x_batch, y_batch = ninapro.next_batch(1000)
# Occasionaly report accuracy of [train] and [test]
if i%100==0:
[train_accuracy] = sess.run([accuracy], feed_dict={x:x_batch[:, partIndex, :], y:y_batch})
[test_accuracy] = sess.run([accuracy], feed_dict={x:ninapro.TestImages[:, partIndex, :], y:ninapro.TestLabels})
print('Step %d, training %g, testing %g.' % (i, train_accuracy, test_accuracy) )
# backwards debug
[y_hat] = sess.run([tf.nn.softmax(z3)], feed_dict={x:x_batch[:, partIndex, :], y:y_batch})
print(y_batch.shape)
print(y_hat.shape)
print(np.argmax(y_batch, axis=1))
print(np.argmax(y_hat, axis=1))
# Occasionaly write visualization summary to disk file.
if i%5==0:
s = sess.run(merged_summary, feed_dict={x:x_batch[:, partIndex, :], y:y_batch})
writer.add_summary(s,i)
# Training the model
sess.run(train, feed_dict={x:x_batch[:, partIndex, :], y:y_batch})