Skip to content

Commit

Permalink
cleaned up code
Browse files Browse the repository at this point in the history
  • Loading branch information
gholmes829 committed May 20, 2021
1 parent 44a5396 commit f0b4626
Show file tree
Hide file tree
Showing 3 changed files with 10 additions and 37 deletions.
14 changes: 2 additions & 12 deletions car/data.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
"""
Loading, preprocessing, and formatting data.
"""

import os
Expand Down Expand Up @@ -57,8 +57,6 @@ def process_data(self):
self.videos[i].append(self.preprocess(img))
self.videos[i] = np.array(self.videos[i])
self.episodes = list(zip(*self.get_data()))

# VIDEOS IS COMPRISED OF EPISODES, NOT RAW INDIVIDUAL DATA, MIGHT BE OTHER ISSUES TOO

def get_data(self):
return self.videos, self.labels
Expand Down Expand Up @@ -87,22 +85,14 @@ def get_shuffled_data(self, videos, labels):
for i in range(n):
rand_index = randint(0, len(indices)-1)
j = indices.pop(rand_index)
#print(n, i, rand_index, j, j//m, j%m)
#print(len(self.shuffled_videos), len(self.videos), len(self.labels))
#print(len(self.videos[0]), len(self.labels[0]))
self.shuffled_videos[i] = videos[j//m][j%m]
self.shuffled_labels[i] = labels[j//m][j%m]
#hash([])
#test = [l for label in self.labels for l in label]
#print(set(list(self.shuffled_labels)) == set(list(test)))
#print(len(self.shuffled_labels) == len(test))
#print(sum(self.shuffled_labels) == sum(test))
self.shuffled_videos = np.array(self.shuffled_videos)
self.shuffled_labels = np.array(self.shuffled_labels)

return self.shuffled_videos, self.shuffled_labels

def preprocess(self, img):
def preprocess(self, img): # uncommnet comments to view before and after
#self.disp_img(img)
img = cv2.resize(img, settings.img_size_rev)
img = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
Expand Down
6 changes: 2 additions & 4 deletions car/settings.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,8 @@
"""
Global perimeters.
"""

#img_width = 180
#img_height = 240

# sizes to resize images to in pre-processing
img_width = 180
img_height = 240

Expand Down
27 changes: 6 additions & 21 deletions car/train.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
"""
Training and analyzing networks.
"""

import os
Expand Down Expand Up @@ -45,6 +45,7 @@ def plotCosts(bin_cnn_loss, cnn_loss, name):
#axes.grid(alpha=0.25, ls="--")

def render_path(angles):
"""Displays angle data as track representation. Result doesn't look right, but may not be directly tranlatable as hopded..."""
mapping = {
0: "red",
1: "red",
Expand All @@ -68,7 +69,6 @@ def render_path(angles):
heading -= limit * np.radians(angle)
end = (start[0] + np.cos(heading), start[1] + np.sin(heading))
pos.append(end)
#print(start, end, angle)
plt.plot((start[0], end[0]), (start[1], end[1]), color=mapping[(i%1000)//100])
plt.title(str(limit))
plt.show()
Expand All @@ -95,14 +95,12 @@ def train_and_evaluate(model, trainingData, trainingLabels, testingData, testing

print("\nTraining time: " + str(round(elapsed, 3)) + " secs")

#loss = np.array([initialLoss] + history.history["loss"])
loss = history.history["loss"]
#print(history.history)
#accuracy = np.array([initialAccuracy] + history.history["root_mean_squared_error"])
print("-"*100)
return loss

def BinCNN():
"""Can experiment with model"""
kwargs = {
"input_quantizer": "ste_sign",
"kernel_quantizer": "ste_sign",
Expand Down Expand Up @@ -153,6 +151,7 @@ def BinCNN():
return model

def CNN():
"""Can experiment with model"""
model = tf.keras.models.Sequential()
model.add(tf.keras.layers.Conv2D(16, (3, 3), input_shape=(*settings.img_size, 1), activation='relu', use_bias=True))
model.add(tf.keras.layers.MaxPooling2D((2, 2)))
Expand Down Expand Up @@ -195,9 +194,6 @@ def main():
data = Data()
data.load_files()
data.process_data()
#episodes = data.get_episodes()

#num_episodes = len(episodes)

shuffled_data, shuffled_labels = data.get_shuffled_data(*data.get_data())

Expand All @@ -207,9 +203,6 @@ def main():

training_data, training_labels = shuffled_data[:num_training], shuffled_labels[:num_training]
testing_data, testing_labels = shuffled_data[num_training:], shuffled_labels[num_training:]
#training_data, training_labels = data.get_data()
#testing_data, testing_labels = data.get_collapsed_data(episodes[7:])
#training_data, training_labels = data.get_shuffled_data(training_data[:7], training_labels[:7])

print("Training size:", num_training)
print("Testing size:", num_testing)
Expand All @@ -220,22 +213,14 @@ def main():
print("Data loaded!")

# render test
#render_path(training_labels)
#return
render_path(training_labels)

cwd = os.getcwd()
model_path = os.path.join(cwd, "models")

# DEFINING ARCHITECTURE



bin_cnn = BinCNN()
cnn = CNN()



# tf.keras.models.summary(model)

# EVALUATING
bin_cnn_loss = train_and_evaluate(bin_cnn, training_data, training_labels, testing_data, testing_labels, name="BinCNN")
cnn_loss = train_and_evaluate(cnn, training_data, training_labels, testing_data, testing_labels, name="CNN")
Expand Down

0 comments on commit f0b4626

Please sign in to comment.