This commit is contained in:
Robert Bendun 2021-06-21 12:00:56 +02:00
commit 6721c16d3d
13 changed files with 204 additions and 58 deletions

17
.vscode/launch.json vendored Normal file
View File

@ -0,0 +1,17 @@
{
// Use IntelliSense to learn about possible attributes.
// Hover to view descriptions of existing attributes.
// For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387
"version": "0.2.0",
"configurations": [
{
"type": "pwa-chrome",
"request": "launch",
"name": "Launch Chrome against localhost",
"url": "http://localhost:8080",
"webRoot": "${workspaceFolder}"
}
]
}

BIN
src/Dhidden.npy Normal file

Binary file not shown.

BIN
src/Dweights.npy Normal file

Binary file not shown.

BIN
src/Lhidden_test.npy Normal file

Binary file not shown.

BIN
src/Lweights_test.npy Normal file

Binary file not shown.

BIN
src/litery/1.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 383 B

BIN
src/litery/2.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 402 B

BIN
src/litery/3.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 403 B

BIN
src/litery/4.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 454 B

BIN
src/litery/5.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 376 B

View File

@ -1,96 +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)
d_train = dig_train_images[:1000]
d_labels = dig_train_labels[:1000]
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)
d_test = dig_test_images[:600]
d_labelstest = dig_test_labels[:600]
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) + 2 hiddenInputs = np.dot(self.weights, inputs) # input -> hidden layer
hiddenOutputs = self.activationFunction(hiddenInputs) hiddenOutputs = self.activationFunction(hiddenInputs)
finalInputs = np.dot(self.hidden, hiddenOutputs) + 1 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)
@ -98,27 +80,28 @@ class NeuralNetwork:
finalOutputs = self.activationFunction(finalInputs) finalOutputs = self.activationFunction(finalInputs)
return finalOutputs return finalOutputs
""" dodaj tablicę literek""" """ tablice sieci neuronowych """
#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")
def trainNetwork(n, fWeight, fHidden, trainingSamples):
# trainNetwork(digitNetwork, "Dweights_test.npy", "Dhidden_test.npy", let_train_images, let_train_labels)
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[d_labels[m]] = 0.99 targets[trainingLabels[m]] = 0.99
#print(targets)
n.train(inputs,targets) n.train(inputs,targets)
m+=1 m+=1
@ -127,14 +110,135 @@ def trainNetwork(n, fWeight, fHidden, trainingSamples):
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", d_train) 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(letterNetwork,let_test_images,let_test_labels)
li = []
ourOwnDataset = []
record_cache = None
def testCase(inputWord):
len = len(inputWord)
word = ""
for i in range(0,len-2):
imgArray = imageio.imread(imageFileName, as_gray=True)
imgData = 255 - imgArray.reshape(784)
imgData = (imgData/255 * 0.99) + 0.01
#inputWord[i]
word = word + recognizeLet(letterNetwork ,imgData)
i=len-2
for i in range(i,len):
imgArray = imageio.imread(imageFileName, as_gray=True)
imgData = 255 - imgArray.reshape(784)
imgData = (imgData/255 * 0.99) + 0.01
#inputWord[i]
word = word + recognizeNum(digitNetwork, imgData)
#testing #assert record_cache.shape == ourOwnDataset[0].shape
#labelInput = np.asfarray(li)
#print(labelInput)
print('slowo: ', word)
pass
def recognizeLet(n,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)
outputs = n.query(imgData)
label = np.argmax(outputs)
return letters[int(label)]
def recognizeNum(n, imgData):
pass
#record = np.append(label,imgData)
outputs = n.query(imgData)
#print('Record: ',record)
#ourOwnDataset.append(record)
#if record_cache is None:
# record_cache = record
#print(ood[0])
#li.append(label)
label = np.argmax(outputs)
return str(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)

View File

@ -5,7 +5,7 @@
* @param {...any[]} rows * @param {...any[]} rows
* @returns * @returns
*/ */
function fromTable(header, ...rows) { function fromTable(header, ...rows) {
function tupleToRecord(names, array) { function tupleToRecord(names, array) {
return names.reduce((p, v, i) => ({ ...p, [v]: array[i] }), {}) return names.reduce((p, v, i) => ({ ...p, [v]: array[i] }), {})
} }
@ -64,6 +64,12 @@ function randomFromSet(set) {
unreachable(); unreachable();
} }
function mapToJson(map) {
return JSON.stringify([...map]);
}
function jsonToMap(jsonStr) {
return new Map(JSON.parse(jsonStr));
}
function nice(v) { return `${(v * 100).toFixed(1)}%` } function nice(v) { return `${(v * 100).toFixed(1)}%` }
@ -75,7 +81,9 @@ async function requestJSONCached(url, params = {}) {
const response = await fetch(url, params); const response = await fetch(url, params);
const json = await response.json(); const json = await response.json();
requestJSONCached.cache.set(key, json); requestJSONCached.cache.set(key, json);
const cache = mapToJson(requestJSONCached.cache);
localStorage.setItem("cache", cache);
return json; return json;
} }
requestJSONCached.cache = new Map(); requestJSONCached.cache = jsonToMap(localStorage.getItem("cache"));

View File

@ -12,8 +12,25 @@ class OrdersView {
[...document.querySelectorAll('.orders-window .canvases canvas')].map((canv, index) => { [...document.querySelectorAll('.orders-window .canvases canvas')].map((canv, index) => {
return new Promise((resolve, reject) => { return new Promise((resolve, reject) => {
canv.toBlob(blob => { canv.toBlob(blob => {
formData.append(`file-${index}`, blob,`file-${index}.png`); let blobUrl = URL.createObjectURL(blob);
resolve();
const img = new Image();
img.src = blobUrl;
img.onload = () => {
const canvas = document.createElement('canvas');
canvas.width = 28;
canvas.height = 28;
const ctx = canvas.getContext('2d');
ctx.drawImage(img, 0, 0, 28, 28);
canvas.toBlob((blob) => {
// blobUrl = URL.createObjectURL(blob);
formData.append(`file-${index}`, blob,`file-${index}.png`);
resolve();
}, 'image/png');
}
}); });
}); });
}) })