From ef562590d1105a87653480efc1c5b831e8f14387 Mon Sep 17 00:00:00 2001 From: HombreLaser Date: Fri, 20 Oct 2023 15:17:53 -0600 Subject: Fix neural network --- dataset_utils.py | 10 ++++++++-- main.py | 17 +++++++++++++++++ neural_network.py | 25 +++++++++++++++++-------- utils.py | 8 +++----- 4 files changed, 45 insertions(+), 15 deletions(-) create mode 100644 main.py diff --git a/dataset_utils.py b/dataset_utils.py index fda75d2..927a78a 100644 --- a/dataset_utils.py +++ b/dataset_utils.py @@ -1,4 +1,4 @@ -import numpy +import numpy as np from PIL import Image from pathlib import Path @@ -16,8 +16,14 @@ class DatasetUtils: image = Image.open(str(file)).convert(mode='L') # Return the image's pixel values as an array alongside # the character that it represents. - yield (dir.name, numpy.asarray(image).ravel()) + yield (dir.name, np.asarray(image)) + """ + Get an image from the dataset. + """ + def get_image(self, path: str): + return np.asarray(Image.open(f"{self.data_path}/{path}").convert('1')) + """Search for a file in the dataset.""" def search(self, filename: str): pass diff --git a/main.py b/main.py new file mode 100644 index 0000000..9ca0a13 --- /dev/null +++ b/main.py @@ -0,0 +1,17 @@ +from PIL import Image +from dataset_utils import DatasetUtils +from neural_network import NeuralNetwork + +LEARNING_RATE = .5 +INPUT_RESOLUTION = 278 + +def main(): + data = DatasetUtils() + image = data.get_image('А/5a2f3c19c27bb.png') + neural_network = NeuralNetwork(LEARNING_RATE, INPUT_RESOLUTION) + + print(neural_network.guess(image)) + + +if __name__ == "__main__": + main() diff --git a/neural_network.py b/neural_network.py index 90fd304..68592f5 100644 --- a/neural_network.py +++ b/neural_network.py @@ -1,6 +1,7 @@ import numpy as np from scipy.special import expit from utils import random_array +from dataset_utils import DatasetUtils CYRILLIC_ALPHABET = ['I', 'А', 'Б', 'В', 'Г', 'Д', 'Е', 'Ë', 'Ж', 'З', 'И', 'Й', 'К', 'Л', 'М', 'Н', 'О', 'П', 'Р', 'С', @@ -11,11 +12,10 @@ CYRILLIC_ALPHABET = ['I', 'А', 'Б', 'В', 'Г', 'Д', 'Е', 'Ë', 'Ж', 'З', class NeuralNetwork: def __init__(self, learning_rate: float, input_resolution: int) -> None: self.learning_rate = learning_rate - self.output_layer_size = len(CYRILLIC_ALPHABET) - self.input_layer_size = input_resolution ** 2 - self.hidden_layer_size = round((self.input_layer_size + self.output_layer_size) / 2) - self._hidden_weights = random_array(self.hidden_layer_size, self.input_layer_size) - self._output_weights = random_array(self.output_layer_size, self.hidden_layer_size) + self.input_layer_size = input_resolution + self.hidden_layer_size = len(CYRILLIC_ALPHABET) + self._hidden_weights = np.random.rand(input_resolution, self.hidden_layer_size) + self._output_weights = np.random.rand(input_resolution) """ @@ -36,8 +36,10 @@ class NeuralNetwork: Guess the letter contained in the image file pointed by input_image (a path). """ - def guess(self, input_image: str) -> str: - pass + def guess(self, input_image: np.array) -> str: + output_layer = self._predict(input_image) + + return self.out(output_layer) """ Save the weights to a csv file. @@ -58,7 +60,7 @@ class NeuralNetwork: hidden_layer_inputs = np.dot(input_layer, self._hidden_weights) hidden_layer_outputs = self._get_layer_output(hidden_layer_inputs) output_layer_inputs = np.dot(self._output_weights, hidden_layer_outputs) - + # The output layer outputs. (Final output of the neural network). return self._get_layer_output(output_layer_inputs) @@ -67,3 +69,10 @@ class NeuralNetwork: """ def _get_layer_output(self, layer: np.array): return expit(layer) + + """ + Get the result from a sigmoid matrix (the index with the highest chance + of being the correct answer). + """ + def out(self, output_layer: np.array): + return CYRILLIC_ALPHABET[np.argmax(np.transpose(output_layer))] diff --git a/utils.py b/utils.py index d068023..a4f6cd3 100644 --- a/utils.py +++ b/utils.py @@ -1,6 +1,4 @@ -import numpy -from random import random +import numpy as np -def random_array(rows: int, columns: int): - random_matrix = [[random() for x in range(rows)] for y in range(columns)] - return numpy.asarray(random_matrix) +def random_array(length: int): + return np.random.rand(length, 1) -- cgit v1.2.3