This commit is contained in:
eynard
2021-12-22 22:08:20 +01:00
parent 7733de01d2
commit 151343b7bd
11 changed files with 22 additions and 61 deletions

BIN
tests/MNIST30epoch Normal file

Binary file not shown.

View File

@@ -0,0 +1,45 @@
import tkinter
from PIL import Image, ImageDraw
import numpy as np
from sys import path
path.insert(1, "..")
from sobek.network import network
class Sketchpad(tkinter.Canvas):
def __init__(self, parent, predictionLabel, **kwargs, ):
super().__init__(parent, **kwargs)
self.bind("<Button-3>", self.test)
self.bind("<B1-Motion>", self.add_line)
self.PILImage = Image.new("F", (560, 560), 100)
self.draw = ImageDraw.Draw(self.PILImage)
self.MNISTNN = network.networkFromFile("MNIST30epoch")
self.predictionLabel = predictionLabel
def add_line(self, event):
self.create_oval((event.x+32, event.y+32, event.x-32, event.y-32), fill="black")
self.draw.ellipse([event.x-32, event.y-32, event.x+32, event.y+32], fill="black")
smallerImage = self.PILImage.reduce(20)
imageAsArray = np.array(smallerImage.getdata())
imageAsArray = (100 - imageAsArray)/100
self.predictionLabel['text'] = ( "Predicted number : " + str(np.argmax(self.MNISTNN.process(imageAsArray))))
def test(self, event):
self.PILImage = Image.new("F", (560, 560), 100)
self.draw = ImageDraw.Draw(self.PILImage)
self.delete("all")
window = tkinter.Tk()
window.title("Number guesser")
window.resizable(False, False)
window.columnconfigure(0, weight=1)
window.rowconfigure(0, weight=1)
predictionLabel = tkinter.Label(window, text="Predicted number :")
sketch = Sketchpad(window, predictionLabel, width=560, height=560)
sketch.grid(column=0, row=0, sticky=(tkinter.N, tkinter.W, tkinter.E, tkinter.S))
predictionLabel.grid(column=0, row=1)
window.mainloop()

60
tests/MNISTLearning.py Normal file
View File

@@ -0,0 +1,60 @@
import numpy as np
import gzip
import time
from sys import path
path.insert(1, "..")
from sobek.network import network
print("--- Data loading ---")
def getData(fileName):
with open(fileName, 'rb') as f:
data = f.read()
return np.frombuffer(gzip.decompress(data), dtype=np.uint8).copy()
tempTrainImages = getData("./MNIST/train-images-idx3-ubyte.gz")[0x10:].reshape((-1, 784)).tolist()
trainImages = []
for image in tempTrainImages:
for pixel in range(784):
if image[pixel] !=0:
image[pixel] = image[pixel]/256
trainImages.append(np.array(image, dtype=np.float64))
tempTrainLabels = getData("./MNIST/train-labels-idx1-ubyte.gz")[8:]
trainLabels = []
for label in tempTrainLabels:
trainLabels.append(np.zeros(10))
trainLabels[-1][label] = 1.0
myNetwork = network(784, 30, 10)
learningRate = 3.0
print("--- Learning ---")
startTime = time.perf_counter()
"""
for i in range(1):
print("Epoch: " + str(i))
batchEnd = 10
while batchEnd < 1000:
batchImages = trainImages[:batchEnd]
batchLabels = trainLabels[:batchEnd]
myNetwork.train(batchImages, batchLabels, learningRate)
batchEnd += 10
if (batchEnd%100) == 0:
print(batchEnd)
"""
myNetwork.train(trainImages, trainLabels, learningRate, 10, 30)
endTime = time.perf_counter()
print("Learning time : " + str(endTime - startTime))
print(trainLabels[121])
print(myNetwork.process(trainImages[121]))
myNetwork.saveToFile("MNIST30epoch")

32
tests/MNISTLoadTest.py Normal file
View File

@@ -0,0 +1,32 @@
import numpy as np
import gzip
from sys import path
path.insert(1, "..")
from sobek.network import network
print("--- Data loading ---")
def getData(fileName):
with open(fileName, 'rb') as f:
data = f.read()
return np.frombuffer(gzip.decompress(data), dtype=np.uint8).copy()
tempTrainImages = getData("./MNIST/t10k-images-idx3-ubyte.gz")[0x10:].reshape((-1, 784)).tolist()
trainImages = []
for image in tempTrainImages:
for pixel in range(784):
if image[pixel] !=0:
image[pixel] = image[pixel]/256
trainImages.append(np.array(image, dtype=np.float64))
tempTrainLabels = getData("./MNIST/t10k-labels-idx1-ubyte.gz")[8:]
trainLabels = []
for label in tempTrainLabels:
trainLabels.append(np.zeros(10))
trainLabels[-1][label] = 1.0
print("--- Testing ---")
myNetwork = network.networkFromFile("MNIST30epoch")
print(myNetwork.accuracy(trainImages, trainLabels))

50
tests/testLearning.py Normal file
View File

@@ -0,0 +1,50 @@
import numpy as np
import random
from sys import path
path.insert(1, "..")
from sobek.network import network
random.seed()
myNetwork = network(10, 10)
learningRate = 3
for j in range(1000):
rand = []
inputs = []
desiredOutputs = []
if (j%50 == 0):
print(j)
for i in range(10):
rand.append( random.randrange(10)/10)
for i in range(10):
desiredOutputs.append(np.zeros(10))
desiredOutputs[i][9 - int(rand[i]*10)] = 1.0
for i in range(10):
inputs.append(np.zeros(10))
inputs[i][int(rand[i]*10)] = 1.0
myNetwork.train(inputs, desiredOutputs, learningRate)
test = []
test.append(np.zeros(10))
test.append(np.zeros(10))
test[0][1] = 1.0
test[1][5] = 1.0
print(test[0])
print(myNetwork.process(test[0]))
print(test[1])
print(myNetwork.process(test[1]))
print("Save and load test :")
myNetwork.saveToFile("test")
myNetwork2 = network.networkFromFile("test")
print(myNetwork.process(test[0]).all() == myNetwork2.process(test[0]).all())

68
tests/testLearningNAND.py Normal file
View File

@@ -0,0 +1,68 @@
import numpy as np
import random
import time
from sys import path
path.insert(1, "..")
from sobek.network import network
random.seed()
myNetwork = network(2, 2, 1)
learningRate = 3
test = []
result = []
test.append(np.zeros(2))
test.append(np.zeros(2))
test.append(np.zeros(2))
test.append(np.zeros(2))
test[1][1] = 1.0
test[2][0] = 1.0
test[3][0] = 1.0
test[3][1] = 1.0
result.append(np.ones(1))
result.append(np.ones(1))
result.append(np.ones(1))
result.append(np.zeros(1))
learningTime = 0
nbRep = 1
for i in range(nbRep):
if (i%(nbRep/10) == 0): print(i)
startTime = time.perf_counter()
myNetwork.train(test, result, learningRate, len(test), 10000, visualize=False)
endTime = time.perf_counter()
learningTime += endTime - startTime
learningTime = learningTime / nbRep
test = []
result = []
test.append(np.zeros(2))
test.append(np.zeros(2))
test.append(np.zeros(2))
test.append(np.zeros(2))
test[1][1] = 1.0
test[2][0] = 1.0
test[3][0] = 1.0
test[3][1] = 1.0
result.append(np.ones(1))
result.append(np.ones(1))
result.append(np.ones(1))
result.append(np.zeros(1))
#print(myNetwork.weights)
#print(myNetwork.biases)
print("0 0 : " + str(myNetwork.process(test[0])) + " == 1 ?")
print("0 1 : " + str(myNetwork.process(test[1])) + " == 1 ?")
print("1 0 : " + str(myNetwork.process(test[2])) + " == 1 ?")
print("1 1 : " + str(myNetwork.process(test[3])) + " == 0 ?")
myNetwork.saveToFile("NAND")
print("Learning time : " + str(endTime - startTime))

26
tests/testNAND.py Normal file
View File

@@ -0,0 +1,26 @@
import numpy as np
from sys import path
path.insert(1, "..")
from sobek.network import network
myNetwork = network(2, 1)
test = []
test.append(np.zeros(2))
test.append(np.zeros(2))
test.append(np.zeros(2))
test.append(np.zeros(2))
test[1][1] = 1.0
test[2][0] = 1.0
test[3][0] = 1.0
test[3][1] = 1.0
myNetwork.weights = [np.array([[-10.0, -10.0]])]
myNetwork.biases = [np.array([15.0])]
print(myNetwork.weights)
print(myNetwork.biases)
print("0 0 : " + str(myNetwork.process(test[0])) + " == 1 ?")
print("0 1 : " + str(myNetwork.process(test[1])) + " == 1 ?")
print("1 0 : " + str(myNetwork.process(test[2])) + " == 1 ?")
print("1 1 : " + str(myNetwork.process(test[3])) + " == 0 ?")

12
tests/timeTest.py Normal file
View File

@@ -0,0 +1,12 @@
import random
import numpy as np
inputs = []
for i in range(10000000):
inputs.append([random.randrange(10)])
inputs = np.array(inputs, dtype=object)
inputs = np.insert(inputs, 0, 1, axis=1)
print(inputs)

40
tests/timeTest2.py Normal file
View File

@@ -0,0 +1,40 @@
import random
import numpy as np
import time
weights = np.random.default_rng(42).random((10, 10))
biases = np.random.default_rng(42).random(10)
biases = np.array(biases, dtype=object)
time1 = time.perf_counter()
for k in range(1000):
_input = []
for i in range(10):
_input.append(random.randrange(10))
_input = np.array(_input, dtype=object)
for f in range(100):
_input = np.matmul(_input, weights)
_input = np.add(_input, biases)
time2 = time.perf_counter()
weights = np.random.default_rng(42).random((11, 10))
time3 = time.perf_counter()
for k in range(1000):
_input = []
for i in range(10):
_input.append(random.randrange(10))
_input = np.array(_input, dtype=object)
for f in range(100):
_input = np.insert(_input, 0, 1, axis=0)
_input = np.matmul(_input, weights)
time4 = time.perf_counter()
print("Multiplication et addition : " + str(time2-time1) + " secondes")
print("Insertion puis multiplication : " + str(time4-time3) + " secondes")