-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtf1accelCNN.py
161 lines (131 loc) · 5.37 KB
/
tf1accelCNN.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
import tensorflow as tf
from tensorflow.keras import Sequential
from tensorflow.keras.layers import Flatten, Dense, Dropout, BatchNormalization
from tensorflow.keras.layers import Conv2D, MaxPool2D
from tensorflow.keras.optimizers import Adam
print(tf.__version__)
import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
import csv
from os import listdir
import scipy.stats as stats
from sklearn.model_selection import train_test_split
from sklearn.preprocessing import StandardScaler, LabelEncoder
def load_datasets():
subjects = list()
for filename in listdir('.'):
if filename.endswith("csv"):
values = csv.reader(open(filename, "r"), delimiter = ",") # opens training data
processedlist = []
for row in values:
temp = [row[0],row[1],row[2],row[3],row[4]]
processedlist.append(temp)
subjects.append(processedlist)
return subjects
def plot_subject(subject):
num = []
x = []
y = []
z = []
for row in subject:
num.append(float(row[0]))
x.append(float(row[1]))
y.append(float(row[2]))
z.append(float(row[3]))
fig, axis = plt.subplots(3)
axis[0].plot(num, x)
axis[1].plot(num, y)
axis[2].plot(num, z)
plt.show()
subjects = load_datasets()
#plot_subject(subjects[0])
columns = ["time", "x", "y", "z", "label"]
dataset = pd.DataFrame(data = subjects[0], columns = columns)
dataset2 = pd.DataFrame(data = subjects[1], columns = columns)
#print(dataset.head())
#print(dataset.shape)
#print(dataset.info())
def windows(data, size):
start = 0
while start < data.count():
yield int(start), int(start + size)
start += (size / 2)
def segment_signal(data,window_size = 1000):
segments = np.empty((0,window_size,3))
labels = np.empty((0))
for (start, end) in windows(data["time"], window_size):
#print(start, end)
x = data["x"][start:end]
y = data["y"][start:end]
z = data["z"][start:end]
if(len(dataset["time"][start:end]) == window_size):
segments = np.vstack([segments,np.dstack([x,y,z])])
labels = np.append(labels,stats.mode(data["label"][start:end])[0][0])
return segments, labels
segments, labels = segment_signal(dataset)
labels = np.asarray(pd.get_dummies(labels), dtype = np.int8)
reshaped_segments = segments.reshape(len(segments), 1,1000, 3)
segments2, labels2 = segment_signal(dataset2)
labels2 = np.asarray(pd.get_dummies(labels2), dtype = np.int8)
reshaped_segments2 = segments2.reshape(len(segments2), 1,1000, 3)
train_x = reshaped_segments
train_y = labels
test_x = reshaped_segments2
test_y = labels2
input_height = 1
input_width = 1000
num_labels = 1
num_channels = 3
batch_size = 10
kernel_size = 60
depth = 60
num_hidden = 1000
learning_rate = 0.0001
training_epochs = 5
total_batches = reshaped_segments.shape[0] // batch_size
def weight_variable(shape):
initial = tf.truncated_normal(shape, stddev = 0.1)
return tf.Variable(initial)
def bias_variable(shape):
initial = tf.constant(0.0, shape = shape)
return tf.Variable(initial)
def depthwise_conv2d(x, W):
return tf.nn.depthwise_conv2d(x,W, [1, 1, 1, 1], padding='VALID')
def apply_depthwise_conv(x,kernel_size,num_channels,depth):
weights = weight_variable([1, kernel_size, num_channels, depth])
biases = bias_variable([depth * num_channels])
return tf.nn.relu(tf.add(depthwise_conv2d(x, weights),biases))
def apply_max_pool(x,kernel_size,stride_size):
return tf.nn.max_pool(x, ksize=[1, 1, kernel_size, 1],
strides=[1, 1, stride_size, 1], padding='VALID')
X = tf.Variable(tf.float32, shape=[None,input_height,input_width,num_channels])
Y = tf.Variable(tf.float32, shape=[None,num_labels])
c = apply_depthwise_conv(X,kernel_size,num_channels,depth)
p = apply_max_pool(c,20,2)
c = apply_depthwise_conv(p,6,depth*num_channels,depth//10)
shape = c.get_shape().as_list()
c_flat = tf.reshape(c, [-1, shape[1] * shape[2] * shape[3]])
f_weights_l1 = weight_variable([shape[1] * shape[2] * depth * num_channels * (depth//10), num_hidden])
f_biases_l1 = bias_variable([num_hidden])
f = tf.nn.tanh(tf.add(tf.matmul(c_flat, f_weights_l1),f_biases_l1))
out_weights = weight_variable([num_hidden, num_labels])
out_biases = bias_variable([num_labels])
y_ = tf.nn.softmax(tf.matmul(f, out_weights) + out_biases)
loss = -tf.reduce_sum(Y * tf.log(y_))
optimizer = tf.train.GradientDescentOptimizer(learning_rate = learning_rate).minimize(loss)
correct_prediction = tf.equal(tf.argmax(y_,1), tf.argmax(Y,1))
accuracy = tf.reduce_mean(tf.cast(correct_prediction, tf.float32))
cost_history = np.empty(shape=[1],dtype=float)
with tf.Session() as session:
tf.global_variables_initializer().run()
for epoch in range(training_epochs):
for b in range(total_batches):
offset = (b * batch_size) % (train_y.shape[0] - batch_size)
batch_x = train_x[offset:(offset + batch_size), :, :, :]
batch_y = train_y[offset:(offset + batch_size), :]
_, c = session.run([optimizer, loss],feed_dict={X: batch_x, Y : batch_y})
cost_history = np.append(cost_history,c)
print ("Epoch: ",epoch," Training Loss: ",c," Training Accuracy: ",
session.run(accuracy, feed_dict={X: train_x, Y: train_y}))
print ("Testing Accuracy:", session.run(accuracy, feed_dict={X: test_x, Y: test_y}))