Merge branch 'main' of https://git.wmi.amu.edu.pl/s452639/psi
This commit is contained in:
commit
b141c4ef80
BIN
src/litery/1.png
Normal file
BIN
src/litery/1.png
Normal file
Binary file not shown.
After Width: | Height: | Size: 383 B |
BIN
src/litery/2.png
Normal file
BIN
src/litery/2.png
Normal file
Binary file not shown.
After Width: | Height: | Size: 402 B |
BIN
src/litery/3.png
Normal file
BIN
src/litery/3.png
Normal file
Binary file not shown.
After Width: | Height: | Size: 403 B |
BIN
src/litery/4.png
Normal file
BIN
src/litery/4.png
Normal file
Binary file not shown.
After Width: | Height: | Size: 454 B |
BIN
src/litery/5.png
Normal file
BIN
src/litery/5.png
Normal file
Binary file not shown.
After Width: | Height: | Size: 376 B |
@ -1,91 +1,78 @@
|
|||||||
|
|
||||||
from emnist import list_datasets
|
|
||||||
from emnist import extract_test_samples
|
from emnist import extract_test_samples
|
||||||
from emnist import extract_training_samples
|
from emnist import extract_training_samples
|
||||||
import numpy as np
|
import numpy as np
|
||||||
import torch
|
|
||||||
from torch import nn
|
|
||||||
from torch import optim
|
|
||||||
import scipy.special
|
import scipy.special
|
||||||
from matplotlib.pyplot import imshow
|
|
||||||
import glob
|
import glob
|
||||||
import imageio
|
import imageio
|
||||||
|
|
||||||
|
""" pobranie obrazów cyfr i liter z biblioteki """
|
||||||
dig_train_images, dig_train_labels = extract_training_samples('digits')
|
dig_train_images, dig_train_labels = extract_training_samples('digits')
|
||||||
dig_test_images, dig_test_labels = extract_test_samples('digits')
|
dig_test_images, dig_test_labels = extract_test_samples('digits')
|
||||||
let_train_images, let_train_labels = extract_training_samples('letters')
|
let_train_images, let_train_labels = extract_training_samples('letters')
|
||||||
let_test_images, let_test_labels = extract_test_samples('letters')
|
let_test_images, let_test_labels = extract_test_samples('letters')
|
||||||
|
|
||||||
|
""" przekształcenie tablic """
|
||||||
|
|
||||||
|
|
||||||
#print(dig_train_images[0])
|
|
||||||
dig_train_images = dig_train_images.reshape(len(dig_train_images),28*28)
|
dig_train_images = dig_train_images.reshape(len(dig_train_images),28*28)
|
||||||
dig_test_images = dig_test_images.reshape(len(dig_test_images),28*28)
|
dig_test_images = dig_test_images.reshape(len(dig_test_images),28*28)
|
||||||
|
|
||||||
#print(d_test.shape)
|
let_train_images = let_train_images.reshape(len(let_train_images),28*28)
|
||||||
print(d_labelstest)
|
let_test_images = let_test_images.reshape(len(let_test_images),28*28)
|
||||||
#print(dig_train_images[0])
|
|
||||||
#print(dig_train_images.shape)
|
|
||||||
|
|
||||||
|
|
||||||
class NeuralNetwork:
|
class NeuralNetwork:
|
||||||
|
""" inicjalizacja sieci neuronowej """
|
||||||
def __init__(self, inputNodes, hiddenNodes, outputNodes, learningGrade, fileWeight, fileHidden):
|
def __init__(self, inputNodes, hiddenNodes, outputNodes, learningGrade, fileWeight, fileHidden):
|
||||||
self.inodes = inputNodes
|
self.inodes = inputNodes
|
||||||
self.hnodes = hiddenNodes
|
self.hnodes = hiddenNodes
|
||||||
self.onodes = outputNodes
|
self.onodes = outputNodes
|
||||||
|
|
||||||
"""te pierwsze dwa użyj przy nauce, potem zostaw cały czas te 2"""
|
""" używane przy uczeniu sieci """
|
||||||
#self.weights = (np.random.rand(self.hnodes, self.inodes) - 0.5)
|
self.weights = (np.random.rand(self.hnodes, self.inodes) - 0.5)
|
||||||
#self.hidden = (np.random.rand(self.onodes, self.hnodes) - 0.5)
|
self.hidden = (np.random.rand(self.onodes, self.hnodes) - 0.5)
|
||||||
self.weights = np.load(fileWeight)
|
""" używane przy pobieraniu danych o nauczonej sieci, z pliku """
|
||||||
self.hidden = np.load(fileHidden)
|
# self.weights = np.load(fileWeight)
|
||||||
|
# self.hidden = np.load(fileHidden)
|
||||||
#print( 'Matrix1 \n', self.weights)
|
|
||||||
#print( 'Matrix2 \n', self.hidden)
|
|
||||||
|
|
||||||
self.lr = learningGrade
|
self.lr = learningGrade
|
||||||
|
|
||||||
|
""" funkcja aktywacji """
|
||||||
self.activationFunction = lambda x: scipy.special.expit(x)
|
self.activationFunction = lambda x: scipy.special.expit(x)
|
||||||
|
|
||||||
pass
|
pass
|
||||||
|
|
||||||
|
"""trening sieci neuronowej"""
|
||||||
def train(self, inputsList, targetsList):
|
def train(self, inputsList, targetsList):
|
||||||
|
""" konwersja list na tablice 2d """
|
||||||
inputs = np.array(inputsList,ndmin=2).T
|
inputs = np.array(inputsList,ndmin=2).T
|
||||||
targets = np.array(targetsList,ndmin=2).T
|
targets = np.array(targetsList,ndmin=2).T
|
||||||
|
|
||||||
#forward pass
|
""" forward pass """
|
||||||
hiddenInputs = np.dot(self.weights, inputs)
|
hiddenInputs = np.dot(self.weights, inputs) # input -> hidden layer
|
||||||
hiddenOutputs = self.activationFunction(hiddenInputs)
|
hiddenOutputs = self.activationFunction(hiddenInputs)
|
||||||
|
|
||||||
finalInputs = np.dot(self.hidden, hiddenOutputs)
|
finalInputs = np.dot(self.hidden, hiddenOutputs)
|
||||||
finalOutputs = self.activationFunction(finalInputs)
|
finalOutputs = self.activationFunction(finalInputs)
|
||||||
|
|
||||||
|
""" backward pass """
|
||||||
outputErrors = targets - finalOutputs
|
outputErrors = targets - finalOutputs
|
||||||
#print(outputErrors.shape)
|
|
||||||
x =self.weights.T
|
x =self.weights.T
|
||||||
#print(x.shape)
|
|
||||||
hiddenErrors = np.dot(self.hidden.T, outputErrors)
|
hiddenErrors = np.dot(self.hidden.T, outputErrors)
|
||||||
|
|
||||||
#print('OutputErrors', outputErrors.shape)
|
|
||||||
#print('finalOutputs',finalOutputs.shape)
|
|
||||||
#print(x.shape)
|
|
||||||
self.hidden += self.lr * np.dot((outputErrors * finalOutputs * (1.0 - finalOutputs)) , np.transpose(hiddenOutputs))
|
self.hidden += self.lr * np.dot((outputErrors * finalOutputs * (1.0 - finalOutputs)) , np.transpose(hiddenOutputs))
|
||||||
self.weights += self.lr * np.dot((hiddenErrors * hiddenOutputs * (1.0 - hiddenOutputs)) , np.transpose(inputs))
|
self.weights += self.lr * np.dot((hiddenErrors * hiddenOutputs * (1.0 - hiddenOutputs)) , np.transpose(inputs))
|
||||||
|
|
||||||
|
|
||||||
pass
|
pass
|
||||||
|
|
||||||
|
""" zapisywanie wytrenowanej sieci do pliku """
|
||||||
def saveTraining(self, fileWeight, fileHidden):
|
def saveTraining(self, fileWeight, fileHidden):
|
||||||
np.save(fileWeight, self.weights)
|
np.save(fileWeight, self.weights)
|
||||||
np.save(fileHidden, self.hidden)
|
np.save(fileHidden, self.hidden)
|
||||||
|
|
||||||
|
""" wykorzystanie sieci """
|
||||||
def query(self, inputsList):
|
def query(self, inputsList):
|
||||||
|
""" konwersja listy na tablicę 2d """
|
||||||
inputs = np.array(inputsList, ndmin=2).T
|
inputs = np.array(inputsList, ndmin=2).T
|
||||||
|
|
||||||
|
|
||||||
hiddenInputs = np.dot(self.weights, inputs)
|
hiddenInputs = np.dot(self.weights, inputs)
|
||||||
hiddenOutputs = self.activationFunction(hiddenInputs)
|
hiddenOutputs = self.activationFunction(hiddenInputs)
|
||||||
|
|
||||||
@ -95,25 +82,26 @@ class NeuralNetwork:
|
|||||||
return finalOutputs
|
return finalOutputs
|
||||||
|
|
||||||
|
|
||||||
|
""" tablice sieci neuronowych """
|
||||||
""" dodaj tablicę literek"""
|
|
||||||
#n = NeuralNetwork(inputNodes=3, hiddenNodes=5, outputNodes=2, learningGrade=0.2)
|
|
||||||
digitNetwork = NeuralNetwork(inputNodes=784, hiddenNodes=200, outputNodes=10, learningGrade=0.1, fileWeight="Dweights.npy", fileHidden="Dhidden.npy")
|
digitNetwork = NeuralNetwork(inputNodes=784, hiddenNodes=200, outputNodes=10, learningGrade=0.1, fileWeight="Dweights.npy", fileHidden="Dhidden.npy")
|
||||||
|
letterNetwork = NeuralNetwork(inputNodes=784, hiddenNodes=200, outputNodes=27, learningGrade=0.1, fileWeight="Lweights.npy", fileHidden="Lhidden.npy")
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
# trainNetwork(digitNetwork, "Dweights_test.npy", "Dhidden_test.npy", let_train_images, let_train_labels)
|
||||||
def trainNetwork(n, fWeight, fHidden, trainingSamples, trainingLabels):
|
def trainNetwork(n, fWeight, fHidden, trainingSamples, trainingLabels):
|
||||||
epochs = 10
|
epochs = 10
|
||||||
outputNodes = 10
|
outputNodes = 27
|
||||||
for e in range(epochs):
|
for e in range(epochs):
|
||||||
m=0
|
m=0
|
||||||
print('Epoch', e+1)
|
print('Epoch', e+1)
|
||||||
|
|
||||||
for record in trainingSamples:
|
for record in trainingSamples:
|
||||||
|
""" zmiana wartości przedziału z [0,255] na [0,1] """
|
||||||
inputs = (np.asfarray(record[0:])/255 * 0.99) + 0.01
|
inputs = (np.asfarray(record[0:])/255 * 0.99) + 0.01
|
||||||
#print(inputs.shape)
|
|
||||||
|
|
||||||
targets = np.zeros(outputNodes) + 0.01
|
targets = np.zeros(outputNodes) + 0.01
|
||||||
targets[trainingLabels[m]] = 0.99
|
targets[trainingLabels[m]] = 0.99
|
||||||
#print(targets)
|
|
||||||
n.train(inputs,targets)
|
n.train(inputs,targets)
|
||||||
|
|
||||||
m+=1
|
m+=1
|
||||||
@ -122,14 +110,82 @@ def trainNetwork(n, fWeight, fHidden, trainingSamples, trainingLabels):
|
|||||||
n.saveTraining(fileWeight=fWeight, fileHidden=fHidden)
|
n.saveTraining(fileWeight=fWeight, fileHidden=fHidden)
|
||||||
|
|
||||||
|
|
||||||
|
def testing(n, testingSamples, testingLabels):
|
||||||
|
scorecard = []
|
||||||
|
k = 0
|
||||||
|
for record in testingSamples:
|
||||||
|
inputs = (np.asfarray(record[0:])/255 * 0.99) + 0.01
|
||||||
|
correctLabels = testingLabels[k]
|
||||||
|
|
||||||
##################################### ODPALANIE TRAINING
|
outputs = n.query(inputs)
|
||||||
#trainNetwork(digitNetwork, "Dweights.npy", "Dhidden.npy", dig_train_images, dig_train_labels)
|
label = np.argmax(outputs)
|
||||||
|
|
||||||
#record = d_test[0]
|
if(label == correctLabels):
|
||||||
#print('Label', d_labelstest[0])
|
scorecard.append(1)
|
||||||
#inputs = np.asfarray(record[0:])/ 255 * 0.99 + 0.01
|
else:
|
||||||
#print(n.query(inputs))
|
scorecard.append(0)
|
||||||
|
k+=1
|
||||||
|
|
||||||
|
scorecardArray = np.asfarray(scorecard)
|
||||||
|
print('Performance', scorecardArray.sum() / scorecardArray.size)
|
||||||
|
|
||||||
|
testing(digitNetwork,dig_test_images,dig_test_labels)
|
||||||
|
|
||||||
|
|
||||||
#testing
|
|
||||||
|
|
||||||
|
|
||||||
|
li = []
|
||||||
|
ourOwnDataset = []
|
||||||
|
|
||||||
|
record_cache = None
|
||||||
|
for imageFileName in glob.glob('litery/?.png'):
|
||||||
|
label = int(imageFileName[-5:-4])
|
||||||
|
print('loading...', imageFileName)
|
||||||
|
|
||||||
|
imgArray = imageio.imread(imageFileName, as_gray=True)
|
||||||
|
#print(' imgArray: ', imgArray)
|
||||||
|
imgData = 255 - imgArray.reshape(784)
|
||||||
|
#print('imgData1: ',imgData)
|
||||||
|
imgData = (imgData/255 * 0.99) + 0.01
|
||||||
|
#print('imgData2: ',imgData)
|
||||||
|
|
||||||
|
#print(np.min(imgData))
|
||||||
|
#print(np.max(imgData))
|
||||||
|
|
||||||
|
record = np.append(label,imgData)
|
||||||
|
#print('Record: ',record)
|
||||||
|
ourOwnDataset.append(record)
|
||||||
|
if record_cache is None:
|
||||||
|
record_cache = record
|
||||||
|
#print(ood[0])
|
||||||
|
li.append(label)
|
||||||
|
pass
|
||||||
|
|
||||||
|
assert record_cache.shape == ourOwnDataset[0].shape
|
||||||
|
labelInput = np.asfarray(li)
|
||||||
|
#print(labelInput)
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
word = ""
|
||||||
|
for item in range(0,5):
|
||||||
|
correctLabels = labelInput[item]
|
||||||
|
outputs = letterNetwork.query(ourOwnDataset[item][1:])
|
||||||
|
print(outputs)
|
||||||
|
|
||||||
|
label = np.argmax(outputs)
|
||||||
|
print('label: ',label)
|
||||||
|
#print('Network says: ', label)
|
||||||
|
#labelString = np.array_str(label)
|
||||||
|
letters=['','a','b','c']
|
||||||
|
word = word + str(label)
|
||||||
|
|
||||||
|
print('slowo: ', word)
|
||||||
|
print('yep')
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
##################################### URUCHOMIENIE TRENINGU
|
||||||
|
trainNetwork(letterNetwork, "Lweights_test.npy", "Lhidden_test.npy", let_train_images, let_train_labels)
|
||||||
|
# trainNetwork(digitNetwork, "Dweights_test.npy", "Dhidden_test.npy", let_train_images, let_train_labels)
|
Loading…
Reference in New Issue
Block a user