sieci neuronowe - litery
This commit is contained in:
parent
aa3e0b59c1
commit
5930cb6d3c
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_training_samples
|
||||
import numpy as np
|
||||
import torch
|
||||
from torch import nn
|
||||
from torch import optim
|
||||
import scipy.special
|
||||
from matplotlib.pyplot import imshow
|
||||
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')
|
||||
|
||||
|
||||
|
||||
|
||||
#print(dig_train_images[0])
|
||||
""" 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)
|
||||
|
||||
#print(d_test.shape)
|
||||
print(d_labelstest)
|
||||
#print(dig_train_images[0])
|
||||
#print(dig_train_images.shape)
|
||||
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
|
||||
|
||||
"""te pierwsze dwa użyj przy nauce, potem zostaw cały czas te 2"""
|
||||
#self.weights = (np.random.rand(self.hnodes, self.inodes) - 0.5)
|
||||
#self.hidden = (np.random.rand(self.onodes, self.hnodes) - 0.5)
|
||||
self.weights = np.load(fileWeight)
|
||||
self.hidden = np.load(fileHidden)
|
||||
|
||||
#print( 'Matrix1 \n', self.weights)
|
||||
#print( 'Matrix2 \n', self.hidden)
|
||||
""" 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)
|
||||
""" 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
|
||||
#print(outputErrors.shape)
|
||||
x =self.weights.T
|
||||
#print(x.shape)
|
||||
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.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)
|
||||
|
||||
@ -95,25 +82,26 @@ class NeuralNetwork:
|
||||
return finalOutputs
|
||||
|
||||
|
||||
|
||||
""" dodaj tablicę literek"""
|
||||
#n = NeuralNetwork(inputNodes=3, hiddenNodes=5, outputNodes=2, learningGrade=0.2)
|
||||
""" 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 = 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
|
||||
#print(inputs.shape)
|
||||
|
||||
targets = np.zeros(outputNodes) + 0.01
|
||||
targets[trainingLabels[m]] = 0.99
|
||||
#print(targets)
|
||||
n.train(inputs,targets)
|
||||
|
||||
m+=1
|
||||
@ -122,14 +110,82 @@ def trainNetwork(n, fWeight, fHidden, trainingSamples, trainingLabels):
|
||||
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
|
||||
#trainNetwork(digitNetwork, "Dweights.npy", "Dhidden.npy", dig_train_images, dig_train_labels)
|
||||
outputs = n.query(inputs)
|
||||
label = np.argmax(outputs)
|
||||
|
||||
#record = d_test[0]
|
||||
#print('Label', d_labelstest[0])
|
||||
#inputs = np.asfarray(record[0:])/ 255 * 0.99 + 0.01
|
||||
#print(n.query(inputs))
|
||||
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)
|
||||
|
||||
|
||||
#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