diff --git a/Arduino_Code/LCD_Test/LCD_Test.ino b/Arduino_Code/LCD_Test/LCD_Test.ino new file mode 100644 index 0000000000000000000000000000000000000000..b7a292107687ec85741560a0ef4ed682e55fdf7e --- /dev/null +++ b/Arduino_Code/LCD_Test/LCD_Test.ino @@ -0,0 +1,68 @@ + +#include <LiquidCrystal.h> +// initialize the library by associating any needed LCD interface pin +// with the arduino pin number it is connected to +const int rs = 12, en = 11, d4 = 5, d5 = 4, d6 = 3, d7 = 2; +LiquidCrystal lcd(rs, en, d4, d5, d6, d7); +int incomingByte = 0; +void setup() { + Serial.begin(19200); //Setup the baud rate. The amount of times it samples per second. + // set up the LCD's number of columns and rows: + lcd.begin(16, 2); + // Print a message to the LCD. + lcd.print("Prediction:"); +} + +void loop() { + if (Serial.available() > 0) + { + // read the incoming byte: + incomingByte = Serial.read(); + // say what you got: + Serial.print("I received: "); + Serial.println(incomingByte, DEC); + } + // set the cursor to column 0, line 1 + // (note: line 1 is the second row, since counting begins with 0): + lcd.setCursor(0, 1); + if(incomingByte == 57) + { + lcd.print("9"); + } + else if(incomingByte == 56) + { + lcd.print("8"); + } + else if(incomingByte == 55) + { + lcd.print("7"); + } + else if(incomingByte == 54) + { + lcd.print("6"); + } + else if(incomingByte == 53) + { + lcd.print("5"); + } + else if(incomingByte == 52) + { + lcd.print("4"); + } + else if(incomingByte == 51) + { + lcd.print("3"); + } + else if(incomingByte == 50) + { + lcd.print("2"); + } + else if(incomingByte == 49) + { + lcd.print("1"); + } + else if(incomingByte == 48) + { + lcd.print("0"); + } +} diff --git a/Arduino_Code/testing.ino b/Arduino_Code/testing.ino new file mode 100644 index 0000000000000000000000000000000000000000..3de3aed7f0992f45d7f300b6b45688d4ba3d56ab --- /dev/null +++ b/Arduino_Code/testing.ino @@ -0,0 +1,103 @@ +int incomingByte = 0; + +void setup() { + Serial.begin(19200); + pinMode(13,OUTPUT); + pinMode(12,OUTPUT); + pinMode(11,OUTPUT); + pinMode(10,OUTPUT); + pinMode(9,OUTPUT); + pinMode(8,OUTPUT); + pinMode(7,OUTPUT); + pinMode(6,OUTPUT); + pinMode(5,OUTPUT); + pinMode(4,OUTPUT); +} + +void loop() { + // send data only when you receive data: + if (Serial.available() > 0) { + // read the incoming byte: + incomingByte = Serial.read(); + // say what you got: + Serial.print("I received: "); + Serial.println(incomingByte, DEC); + } + if(incomingByte == 57) + { + digitalWrite(13,HIGH); + delay(1000); + digitalWrite(13,LOW); + delay(1000); + } + else if(incomingByte == 56) + { + digitalWrite(12,HIGH); + delay(1000); + digitalWrite(12,LOW); + delay(1000); + } + else if(incomingByte == 55) + { + digitalWrite(11,HIGH); + delay(1000); + digitalWrite(11,LOW); + delay(1000); + } + + else if(incomingByte == 54) + { + digitalWrite(10,HIGH); + delay(1000); + digitalWrite(10,LOW); + delay(1000); + } + + else if(incomingByte == 53) + { + digitalWrite(9,HIGH); + delay(1000); + digitalWrite(9,LOW); + delay(1000); + } + + else if(incomingByte == 52) + { + digitalWrite(8,HIGH); + delay(1000); + digitalWrite(8,LOW); + delay(1000); + } + + else if(incomingByte == 51) + { + digitalWrite(7,HIGH); + delay(1000); + digitalWrite(7,LOW); + delay(1000); + } + + else if(incomingByte == 50) + { + digitalWrite(6,HIGH); + delay(1000); + digitalWrite(6,LOW); + delay(1000); + } + + else if(incomingByte == 49) + { + digitalWrite(5,HIGH); + delay(1000); + digitalWrite(5,LOW); + delay(1000); + } + + else if(incomingByte == 48) + { + digitalWrite(4,HIGH); + delay(1000); + digitalWrite(4,LOW); + delay(1000); + } +} diff --git a/MNIST_Code/keras_training.py b/MNIST_Code/keras_training.py new file mode 100644 index 0000000000000000000000000000000000000000..09c08ee7dc80926bdcb3c6f41a7e64929f7b9a89 --- /dev/null +++ b/MNIST_Code/keras_training.py @@ -0,0 +1,104 @@ +import tensorflow as tf +from tensorflow import keras +import numpy as np +import matplotlib.pyplot as plt +import serial +import time +ser = serial.Serial( + port='/dev/ttyACM0', + baudrate=19200, + parity=serial.PARITY_ODD, + stopbits=serial.STOPBITS_TWO, + bytesize=serial.SEVENBITS +) +plt.rcParams['interactive'] == True +fashion_mnist = keras.datasets.fashion_mnist +(train_images, train_labels), (test_images, test_labels) = fashion_mnist.load_data() +class_names = ['T-shirt/top', 'Trouser', 'Pullover', 'Dress', 'Coat', + 'Sandal', 'Shirt', 'Sneaker', 'Bag', 'Ankle boot'] +train_images = train_images / 255.0 +test_images = test_images / 255.0 +plt.figure(figsize=(10,10)) +for i in range(25): + plt.subplot(5,5,i+1) + plt.xticks([]) + plt.yticks([]) + plt.grid(False) + plt.imshow(train_images[i], cmap=plt.cm.binary) + plt.xlabel(class_names[train_labels[i]]) +plt.show() +model = keras.Sequential([ + keras.layers.Flatten(input_shape=(28, 28)), #Transforms the format of the images from a 2d-array (of 28 by 28 pixels), to a 1d-array of 28 * 28 = 784 pixels + keras.layers.Dense(128, activation=tf.nn.relu), #This is the fully connected layers + keras.layers.Dense(10, activation=tf.nn.softmax) #Output layer +]) +model.compile(optimizer=tf.train.AdamOptimizer(), + loss='sparse_categorical_crossentropy', + metrics=['accuracy']) +model.fit(train_images, train_labels, epochs=5) +test_loss, test_acc = model.evaluate(test_images, test_labels) + +print('Test accuracy:', test_acc) +predictions = model.predict(test_images) +np.argmax(predictions[0]) +def plot_image(i, predictions_array, true_label, img): + predictions_array, true_label, img = predictions_array[i], true_label[i], img[i] + plt.grid(False) + plt.xticks([]) + plt.yticks([]) + + plt.imshow(img, cmap=plt.cm.binary) + + predicted_label = np.argmax(predictions_array) + if predicted_label == true_label: + color = 'blue' + else: + color = 'red' + + plt.xlabel("{} {:2.0f}% ({})".format(class_names[predicted_label], + 100*np.max(predictions_array), + class_names[true_label]), + color=color) + +def plot_value_array(i, predictions_array, true_label): + predictions_array, true_label = predictions_array[i], true_label[i] + plt.grid(False) + plt.xticks([]) + plt.yticks([]) + thisplot = plt.bar(range(10), predictions_array, color="#777777") + plt.ylim([0, 1]) + predicted_label = np.argmax(predictions_array) + + thisplot[predicted_label].set_color('red') + thisplot[true_label].set_color('blue') +i = 0 +plt.figure(figsize=(6,3)) +plt.subplot(1,2,1) +plot_image(i, predictions, test_labels, test_images) +plt.subplot(1,2,2) +plot_value_array(i, predictions, test_labels) +plt.show() +num_rows = 5 +num_cols = 3 +num_images = num_rows*num_cols +plt.figure(figsize=(2*2*num_cols, 2*num_rows)) +for i in range(num_images): + plt.subplot(num_rows, 2*num_cols, 2*i+1) + plot_image(i, predictions, test_labels, test_images) + plt.subplot(num_rows, 2*num_cols, 2*i+2) + plot_value_array(i, predictions, test_labels) +# Single image testing +img = test_images[0] +print(img.shape) +img = (np.expand_dims(img,0)) +print(img.shape) +predictions_single = model.predict(img) +print(predictions_single) +print(np.argmax(predictions_single[0])) +for i in range(0,len(test_images)): + img = test_images[i] + img = (np.expand_dims(img,0)) + predictions_single = model.predict(img) + predict = int(np.argmax(predictions_single[0])) + time.sleep(3) + ser.write(b'%d' % predict) \ No newline at end of file diff --git a/MNIST_Code/keras_training_mnist.py b/MNIST_Code/keras_training_mnist.py new file mode 100644 index 0000000000000000000000000000000000000000..56c657a8a19653cdbe6ee0aa6de3409b38ed4b04 --- /dev/null +++ b/MNIST_Code/keras_training_mnist.py @@ -0,0 +1,103 @@ +import tensorflow as tf +from tensorflow import keras +import numpy as np +import matplotlib.pyplot as plt +import serial +import time +ser = serial.Serial( + port='/dev/ttyACM0', + baudrate=19200, + parity=serial.PARITY_ODD, + stopbits=serial.STOPBITS_TWO, + bytesize=serial.SEVENBITS +) +plt.rcParams['interactive'] == True +mnist = keras.datasets.mnist +(train_images, train_labels), (test_images, test_labels) = mnist.load_data() +class_names = ['0', '1', '2', '3', '4', '5', '6', '7', '8', '9'] +train_images = train_images / 255.0 +test_images = test_images / 255.0 +plt.figure(figsize=(10,10)) +for i in range(25): + plt.subplot(5,5,i+1) + plt.xticks([]) + plt.yticks([]) + plt.grid(False) + plt.imshow(train_images[i], cmap=plt.cm.binary) + plt.xlabel(class_names[train_labels[i]]) +plt.show() +model = keras.Sequential([ + keras.layers.Flatten(input_shape=(28, 28)), #Transforms the format of the images from a 2d-array (of 28 by 28 pixels), to a 1d-array of 28 * 28 = 784 pixels + keras.layers.Dense(128, activation=tf.nn.relu), #This is the fully connected layers + keras.layers.Dense(10, activation=tf.nn.softmax) #Output layer +]) +model.compile(optimizer=tf.train.AdamOptimizer(), + loss='sparse_categorical_crossentropy', + metrics=['accuracy']) +model.fit(train_images, train_labels, epochs=5) +test_loss, test_acc = model.evaluate(test_images, test_labels) + +print('Test accuracy:', test_acc) +predictions = model.predict(test_images) +np.argmax(predictions[0]) +def plot_image(i, predictions_array, true_label, img): + predictions_array, true_label, img = predictions_array[i], true_label[i], img[i] + plt.grid(False) + plt.xticks([]) + plt.yticks([]) + + plt.imshow(img, cmap=plt.cm.binary) + + predicted_label = np.argmax(predictions_array) + if predicted_label == true_label: + color = 'blue' + else: + color = 'red' + + plt.xlabel("{} {:2.0f}% ({})".format(class_names[predicted_label], + 100*np.max(predictions_array), + class_names[true_label]), + color=color) + +def plot_value_array(i, predictions_array, true_label): + predictions_array, true_label = predictions_array[i], true_label[i] + plt.grid(False) + plt.xticks([]) + plt.yticks([]) + thisplot = plt.bar(range(10), predictions_array, color="#777777") + plt.ylim([0, 1]) + predicted_label = np.argmax(predictions_array) + + thisplot[predicted_label].set_color('red') + thisplot[true_label].set_color('blue') +i = 0 +plt.figure(figsize=(6,3)) +plt.subplot(1,2,1) +plot_image(i, predictions, test_labels, test_images) +plt.subplot(1,2,2) +plot_value_array(i, predictions, test_labels) +plt.show() +num_rows = 5 +num_cols = 3 +num_images = num_rows*num_cols +plt.figure(figsize=(2*2*num_cols, 2*num_rows)) +for i in range(num_images): + plt.subplot(num_rows, 2*num_cols, 2*i+1) + plot_image(i, predictions, test_labels, test_images) + plt.subplot(num_rows, 2*num_cols, 2*i+2) + plot_value_array(i, predictions, test_labels) +# Single image testing +img = test_images[0] +print(img.shape) +img = (np.expand_dims(img,0)) +print(img.shape) +predictions_single = model.predict(img) +print(predictions_single) +print(np.argmax(predictions_single[0])) +for i in range(0,len(test_images)): + img = test_images[i] + img = (np.expand_dims(img,0)) + predictions_single = model.predict(img) + predict = int(np.argmax(predictions_single[0])) + time.sleep(2) + ser.write(b'%d' % predict) diff --git a/README.md b/README.md index 76e579ae4c9106f3b62fb9203ec5b49d8014d87c..e9a3884db16c38830b4fd665eff6870ad66ce249 100644 --- a/README.md +++ b/README.md @@ -1,2 +1,2 @@ -test - +# Senior Design +This project's purpose is to explore machine learning algorithms for use of autonomous vehicles.