commit d521a5a74be806212be916f7533a3ff0f2a3f366
Author: John Kubach <johnkubach@gmail.com>
Date: Tue, 28 Sep 2021 11:17:46 -0400
Add Python code
Diffstat:
4 files changed, 207 insertions(+), 0 deletions(-)
diff --git a/src/create_test_data.py b/src/create_test_data.py
@@ -0,0 +1,51 @@
+from PIL import Image
+import cv2 as cv
+import numpy as np
+import random as rand
+import glob
+
+IMG_SIZE = 8
+
+testingDog = []
+testingCat = []
+testingBird = []
+testingDolphin = []
+
+pics = sorted(glob.glob("../test/dog/*.png"))
+for image in pics:
+ img = cv.resize(cv.imread(image, cv.IMREAD_GRAYSCALE), (IMG_SIZE, IMG_SIZE))
+ # ret, img = cv.threshold(img, 127, 255, 1)
+ testingDog.append([np.array(img)])
+
+pics = sorted(glob.glob("../test/cat/*.png"))
+for image in pics:
+ img = cv.resize(cv.imread(image, cv.IMREAD_GRAYSCALE), (IMG_SIZE, IMG_SIZE))
+ # ret, img = cv.threshold(img, 127, 255, 1)
+ testingCat.append([np.array(img)])
+
+pics = sorted(glob.glob("../test/bird/*.png"))
+for image in pics:
+ img = cv.resize(cv.imread(image, cv.IMREAD_GRAYSCALE), (IMG_SIZE, IMG_SIZE))
+ # ret, img = cv.threshold(img, 127, 255, 1)
+ testingBird.append([np.array(img)])
+
+pics = sorted(glob.glob("../test/dolphin/*.png"))
+for image in pics:
+ img = cv.resize(cv.imread(image, cv.IMREAD_GRAYSCALE), (IMG_SIZE, IMG_SIZE))
+ # ret, img = cv.threshold(img, 127, 255, 1)
+ testingDolphin.append([np.array(img)])
+
+testingDog = np.array(testingDog) #as mnist
+testingCat = np.array(testingCat) #as mnist
+testingBird = np.array(testingBird) #as mnist
+testingDolphin = np.array(testingDolphin) #as mnist
+
+testingDog = np.reshape(testingDog, [testingDog.shape[0], testingDog.shape[1] * testingDog.shape[2] * testingDog.shape[3]])
+testingCat= np.reshape(testingCat, [testingCat.shape[0], testingCat.shape[1] * testingCat.shape[2] * testingCat.shape[3]])
+testingBird= np.reshape(testingBird, [testingBird.shape[0], testingBird.shape[1] * testingBird.shape[2] * testingBird.shape[3]])
+testingDolphin= np.reshape(testingDolphin, [testingDolphin.shape[0], testingDolphin.shape[1] * testingDolphin.shape[2] * testingDolphin.shape[3]])
+
+np.save('../test/testingDog', testingDog)
+np.save('../test/testingCat', testingCat)
+np.save('../test/testingBird', testingBird)
+np.save('../test/testingDolphin', testingDolphin)
diff --git a/src/create_training_data.py b/src/create_training_data.py
@@ -0,0 +1,53 @@
+from PIL import Image
+import cv2 as cv
+import numpy as np
+import random as rand
+import glob
+
+IMG_SIZE = 8
+
+training = []
+trainingLabels = []
+
+
+pics = glob.glob("../train/dog/*.png")
+for image in pics:
+ img = cv.resize(cv.imread(image, cv.IMREAD_GRAYSCALE), (IMG_SIZE, IMG_SIZE))
+ # ret, img = cv.threshold(img, 127, 255, 1)
+ labels = [1, 0, 0, 0]
+ training.append([np.array(img)])
+ trainingLabels.append([np.array(labels)])
+
+pics = glob.glob("../train/cat/*.png")
+for image in pics:
+ img = cv.resize(cv.imread(image, cv.IMREAD_GRAYSCALE), (IMG_SIZE, IMG_SIZE))
+ # ret, img = cv.threshold(img, 127, 255, 1)
+ labels = [0, 1, 0, 0]
+ training.append([np.array(img)])
+ trainingLabels.append([np.array(labels)])
+
+pics = glob.glob("../train/bird/*.png")
+for image in pics:
+ img = cv.resize(cv.imread(image, cv.IMREAD_GRAYSCALE), (IMG_SIZE, IMG_SIZE))
+ # ret, img = cv.threshold(img, 127, 255, 1)
+ labels = [0, 0, 1, 0]
+ training.append([np.array(img)])
+ trainingLabels.append([np.array(labels)])
+
+pics = glob.glob("../train/dolphin/*.png")
+for image in pics:
+ img = cv.resize(cv.imread(image, cv.IMREAD_GRAYSCALE), (IMG_SIZE, IMG_SIZE))
+ # ret, img = cv.threshold(img, 127, 255, 1)
+ labels = [0, 0, 0, 1]
+ training.append([np.array(img)])
+ trainingLabels.append([np.array(labels)])
+
+training = np.array(training) #as mnist
+trainingLabels = np.array(trainingLabels) #as mnist
+
+training = np.reshape(training, [training.shape[0], training.shape[1] * training.shape[2] * training.shape[3]])
+trainingLabels = np.reshape(trainingLabels, [trainingLabels.shape[0], trainingLabels.shape[1] * trainingLabels.shape[2]])
+np.save('../train/training', training)
+np.save('../train/trainingLabels', trainingLabels)
+print(training.shape)
+print(trainingLabels.shape)
diff --git a/src/test_neural_net.py b/src/test_neural_net.py
@@ -0,0 +1,47 @@
+import numpy as np
+import operator as op
+
+def sigmoid(x, deriv = False):
+ if (deriv == False):
+ x = np.clip(x, -500, 500)
+ result = 1/(1+np.exp(-x))
+ else:
+ result = x*(1-x)
+ return result
+
+testDog = np.load('../test/testingDog.npy')
+testCat = np.load('../test/testingCat.npy')
+testBird = np.load('../test/testingBird.npy')
+testDolphin = np.load('../test/testingDolphin.npy')
+
+syn0 = np.loadtxt('../neuralnet/syn0')
+syn1 = np.loadtxt('../neuralnet/syn1')
+syn2 = np.loadtxt('../neuralnet/syn2')
+
+inputLayer = testDog
+layer1 = sigmoid(np.dot(inputLayer, syn0))
+layer2 = sigmoid(np.dot(layer1, syn1))
+layer3 = sigmoid(np.dot(layer2, syn2))
+print ("Dog Output:")
+print (layer3)
+
+inputLayer = testCat
+layer1 = sigmoid(np.dot(inputLayer, syn0))
+layer2 = sigmoid(np.dot(layer1, syn1))
+layer3 = sigmoid(np.dot(layer2, syn2))
+print ("Cat Output:")
+print (layer3)
+
+inputLayer = testBird
+layer1 = sigmoid(np.dot(inputLayer, syn0))
+layer2 = sigmoid(np.dot(layer1, syn1))
+layer3 = sigmoid(np.dot(layer2, syn2))
+print ("Bird Output:")
+print (layer3)
+
+inputLayer = testDolphin
+layer1 = sigmoid(np.dot(inputLayer, syn0))
+layer2 = sigmoid(np.dot(layer1, syn1))
+layer3 = sigmoid(np.dot(layer2, syn2))
+print ("Dolphin Output:")
+print (layer3)
diff --git a/src/train_neural_net.py b/src/train_neural_net.py
@@ -0,0 +1,56 @@
+import numpy as np
+
+inputSize = 64
+hiddenLayer = 64
+hiddenLayer2 = 16
+outputSize = 4
+
+iterations = 30000
+epochs = 2
+
+def sigmoid(x, deriv = False):
+ if (deriv == False):
+ x = np.clip(x, -500, 500)
+ result = 1/(1+np.exp(-x))
+ else:
+ result = x*(1-x)
+ return result
+
+x = np.load('../train/training.npy')
+y = np.load('../train/trainingLabels.npy')
+
+syn0 = 2*np.random.random((inputSize, hiddenLayer)) - 1
+syn1 = 2*np.random.random((hiddenLayer, hiddenLayer2)) - 1
+syn2 = 2*np.random.random((hiddenLayer2, outputSize)) - 1
+
+for j in range(epochs):
+ print ("EPOCH " + str(j + 1))
+ for i in range(iterations):
+ inputLayer = x
+ layer1 = sigmoid(np.dot(inputLayer, syn0))
+ layer2 = sigmoid(np.dot(layer1, syn1))
+ layer3 = sigmoid(np.dot(layer2, syn2))
+ layer3Err = layer3 - y
+
+ if (i % 1000) == 0:
+ print ("Error at iteration " + str(i) + ": " + str(np.mean(np.abs(layer3Err))))
+
+ layer3Delta = layer3Err * sigmoid(layer3, True)
+ layer2Err = layer3Delta.dot(syn2.T)
+
+ layer2Delta = layer2Err * sigmoid(layer2, True)
+ layer1Err = layer2Delta.dot(syn1.T)
+
+ layer1Delta = layer1Err * sigmoid(layer1, True)
+
+ syn2 -= layer2.T.dot(layer3Delta)
+ syn1 -= layer1.T.dot(layer2Delta)
+ syn0 -= inputLayer.T.dot(layer1Delta)
+
+print ("Training Output:")
+print (layer3)
+print ("Training Accuracy: " + str(100 - (np.mean(np.abs(layer3Err)) * 100)) + "%")
+
+np.savetxt('../neuralnet/syn0', syn0)
+np.savetxt('../neuralnet/syn1', syn1)
+np.savetxt('../neuralnet/syn2', syn2)