psi/src/neural_network.py
2021-06-21 02:49:08 +02:00

229 lines
7.1 KiB
Python

from emnist import extract_test_samples
from emnist import extract_training_samples
import numpy as np
import scipy.special
import glob
import imageio
""" pobranie obrazów cyfr i liter z biblioteki """
dig_train_images, dig_train_labels = extract_training_samples('digits')
dig_test_images, dig_test_labels = extract_test_samples('digits')
let_train_images, let_train_labels = extract_training_samples('letters')
let_test_images, let_test_labels = extract_test_samples('letters')
""" przekształcenie tablic """
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)
let_train_images = let_train_images.reshape(len(let_train_images),28*28)
let_test_images = let_test_images.reshape(len(let_test_images),28*28)
class NeuralNetwork:
""" inicjalizacja sieci neuronowej """
def __init__(self, inputNodes, hiddenNodes, outputNodes, learningGrade, fileWeight, fileHidden):
self.inodes = inputNodes
self.hnodes = hiddenNodes
self.onodes = outputNodes
""" używane przy uczeniu sieci """
self.weights = (np.random.rand(self.hnodes, self.inodes) - 0.5)
self.hidden = (np.random.rand(self.onodes, self.hnodes) - 0.5)
""" używane przy pobieraniu danych o nauczonej sieci, z pliku """
# self.weights = np.load(fileWeight)
# self.hidden = np.load(fileHidden)
self.lr = learningGrade
""" funkcja aktywacji """
self.activationFunction = lambda x: scipy.special.expit(x)
pass
"""trening sieci neuronowej"""
def train(self, inputsList, targetsList):
""" konwersja list na tablice 2d """
inputs = np.array(inputsList,ndmin=2).T
targets = np.array(targetsList,ndmin=2).T
""" forward pass """
hiddenInputs = np.dot(self.weights, inputs) # input -> hidden layer
hiddenOutputs = self.activationFunction(hiddenInputs)
finalInputs = np.dot(self.hidden, hiddenOutputs)
finalOutputs = self.activationFunction(finalInputs)
""" backward pass """
outputErrors = targets - finalOutputs
x =self.weights.T
hiddenErrors = np.dot(self.hidden.T, outputErrors)
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))
pass
""" zapisywanie wytrenowanej sieci do pliku """
def saveTraining(self, fileWeight, fileHidden):
np.save(fileWeight, self.weights)
np.save(fileHidden, self.hidden)
""" wykorzystanie sieci """
def query(self, inputsList):
""" konwersja listy na tablicę 2d """
inputs = np.array(inputsList, ndmin=2).T
hiddenInputs = np.dot(self.weights, inputs)
hiddenOutputs = self.activationFunction(hiddenInputs)
finalInputs = np.dot(self.hidden, hiddenOutputs)
finalOutputs = self.activationFunction(finalInputs)
return finalOutputs
""" tablice sieci neuronowych """
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):
epochs = 10
outputNodes = 27
for e in range(epochs):
m=0
print('Epoch', e+1)
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
targets = np.zeros(outputNodes) + 0.01
targets[trainingLabels[m]] = 0.99
n.train(inputs,targets)
m+=1
pass
pass
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]
outputs = n.query(inputs)
label = np.argmax(outputs)
if(label == correctLabels):
scorecard.append(1)
else:
scorecard.append(0)
k+=1
scorecardArray = np.asfarray(scorecard)
print('Performance', scorecardArray.sum() / scorecardArray.size)
testing(digitNetwork,dig_test_images,dig_test_labels)
record_cache = None
def testCase(inputWord):
len = len(inputWord)
li = []
ourOwnDataset = []
word = ""
imgArray = imageio.imread(imageFileName, as_gray=True)
for i in len-2:
imgData = 255 - imgArray.reshape(784)
imgData = (imgData/255 * 0.99) + 0.01
word = word + recognizeLet(inputWord[i],imgData)
word = word + recognizeNum[inputWord[-2]]
word = word + recognizeNum[inputWord[-1]]
assert record_cache.shape == ourOwnDataset[0].shape
labelInput = np.asfarray(li)
#print(labelInput)
print('slowo: ', word)
pass
def recognizeLet(let,imgData):
letters=['','a','b','c','d','e','f','g','h','i','j','k','l','m','n','o','p','q','r','s','t','u','v','w','x','y','z']
record = np.append(label,imgData)
label = np.argmax(outputs)
return letters[int(label)]
def recognizeNum():
pass
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
"""
li = []
#ourOwnDataset = np.asfarray(ood)
ourOwnDataset = []
record_cache = None
for imageFileName in glob.glob('cyfry/?.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,9):
correctLabels = labelInput[item]
outputs = n.query(ourOwnDataset[item][1:])
print(outputs)
label = np.argmax(outputs)
#print('Network says: ', label)
#labelString = np.array_str(label)
word = word + str(label)
print('slowo: ', word)
"""
##################################### 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)