-
Notifications
You must be signed in to change notification settings - Fork 0
/
monoc_detect.py
172 lines (141 loc) · 5.35 KB
/
monoc_detect.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
"""
Utilizes a neural net to predict depth (specifically, distance
from wall) given audio data (inspired by bats' echolocation)
data by Nathan Sprague
developed by Nhung Hoang
May 2017
"""
import matplotlib.pyplot as plt
import numpy as np
import os.path
from keras.layers import Conv2D, Dense
from keras.layers.core import Flatten
from keras.models import load_model, Sequential
from random import shuffle
from scipy import io, signal
from sys import argv
def preprocess_data(filename):
"""
@PURPOSE: Turn raw audio data into spectrograms and select target depths
@PARAMS: filename - npz file of data
@RETURN: numpy array of spectrograms, numpy array of target depths
"""
data = np.load(filename)
audio = data['audio']
depth = data['depth']
# Depth vectors become single depth value
shrunk = depth[:, 230:250, 310:330] # square wall space directly in front of apparatus
shrunk_reshaped = np.reshape(shrunk, (shrunk.shape[0],-1))
target_set = np.max(shrunk_reshaped, axis=1)
target_set = target_set[np.where(target_set!=0)]
target_set = np.log(target_set)
# Turn audio data into spectrograms
input_set = np.empty((audio.shape[0], 129, 11)) # (129, 11) shape of spectrograms
for i in range(audio.shape[0]):
freq, time, spectro = signal.spectrogram(audio[i,:])
print spectro.shape
input_set[i] = spectro
input_set = np.log(input_set)/20
# Shuffle the data
combined = zip(input_set, target_set)
shuffle(combined)
input_set, target_set = zip(*combined)
input_set = np.asarray(input_set)
target_set = np.asarray(target_set)
return input_set, target_set
######################################################
def get_model(in_train, out_train):
# Build neural net
net = Sequential()
INPUT_SHAPE = in_train.shape[1:]
net.add(Conv2D(64, (5, 5), strides=(1,1), activation='relu', input_shape=INPUT_SHAPE))
net.add(Flatten())
net.add(Dense(450, activation='relu'))
net.add(Dense(1, activation='linear'))
net.compile(optimizer='adam', loss='mean_squared_error')
net.fit(in_train, out_train, validation_split=0.2, epochs=50)
net.save("depth_model.h5")
return load_model("depth_model.h5")
######################################################
def run_nn(net, in_test, out_test, summary=False):
"""
@PURPOSE: Train a neural network and use it to make predictions
@PARAMS: training_set - numpy array of spectrogram
target_set - numpy array of target depths for training set
summary - boolean option to print neural network information
@RETURN: mean squared error based on network performance using training and test sets
"""
loss = net.evaluate(in_test, out_test)
scale_loss = np.exp(loss)
predictions = net.predict(in_test)
plot_data(np.exp(out_test), np.exp(predictions))
if summary:
print "\n"
net.summary()
return scale_loss
######################################################
def plot_data(y_data, predictions):
"""
@PURPOSE: display graph of expected depths vs. predicted depths for each data input
@PARAMS: y_data - the true/expected depth values
prediction - the predicted depth values given by the neural network
@RETURN: None
"""
pts = 20 # number of data points to show
indices = range(1, len(y_data)+1)
plt.figure(1)
plt.plot(indices[:pts], y_data[:pts], 'bs')
plt.plot(indices[:pts], predictions[:pts], 'g^')
plt.title("True and Predicted Depth Values")
plt.xlabel("data point")
plt.ylabel("millimeters from wall")
plt.legend(["true","predicted"])
plt.show()
plt.figure(2)
plt.plot(y_data[:pts], predictions[:pts], 'ro')
plt.title("True vs. Predicted Depth Values")
plt.xlabel("true (mm)")
plt.ylabel("predicted (mm)")
plt.show()
######################################################
def main():
if len(argv) != 3:
print "\nusage: depth_detect.py training_data(npz_file) test_data(npz_file)\n"
return
# Get data sets
in_train, out_train = preprocess_data(argv[1])
in_train = np.reshape(in_train, (in_train.shape[0],129,11,1))
in_test, out_test = preprocess_data(argv[2])
in_test = np.reshape(in_test, (in_test.shape[0],129,11,1))
if not os.path.isfile("depth_model.h5"):
net = get_model(in_train, out_train)
else:
net = load_model("depth_model.h5")
losses = []
runs = 1
for i in range(runs):
if i == runs-1:
loss = run_nn(net, in_test, out_test, summary=True)
else:
loss = run_nn(net, in_test, out_test)
print "\n"
losses.append(loss)
print "\n"
for j in range(runs):
print "Run", j+1, "Loss:", losses[j]
print "\nAverage Loss:", sum(losses)/runs
print "\n","-"*30,"\n"," "*12,"DONE\n","-"*30
main()
######################################################
######################################################
def check_data():
data = np.load(argv[1])
depth = data['depth']
print depth.shape
shrunk = depth[:, 230:250, 310:330]
shrunk_reshaped = np.reshape(shrunk, (shrunk.shape[0],-1))
target_set = np.max(shrunk_reshaped, axis=1)
target_set = target_set[np.where(target_set!=0)]
target_set = np.log(target_set)
plot_data(target_set, np.zeros_like(target_set))
#check_data()