-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.py
80 lines (62 loc) · 2.67 KB
/
main.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
import utils
from NeuralNetwork import NeuralNetwork
# Functions
# ---------
# Функції
# Returns images for learning
# ---------------------------
# Повертає вибірку зображень для навчання
def get_learning_images():
return utils.get_images(learning_directory)
# Returns images for testing
# --------------------------
# Повертає вибірку зображень для перевірки роботи мережі
def get_test_images():
return utils.get_images(test_directory)
# Checks results of 'network' work on 'images' set
# ------------------------------------------------
# Перевіряє результат роботи мережі на вибірці 'images'
def check_network(network, images):
for image in images:
print('Image is taken from file: ' + image.filename)
print('Is number ' + str(image.answer) + ' ? ' + str(image.answer == network.evaluate(image.pixels)))
# Learns 'network' with 'image_models' set 'epoch_count' times
# ------------------------------------------------------------
# Навчає мережу на виборці 'image_models' 'epoch_count' разів
def study(network, image_models, epoch_count):
for i in range(epoch_count):
for model in image_models:
# Learns network
# --------------
# Навчаємо мережу
network.study(model.pixels, model.answer)
# Constants
learning_directory = "images/learning"
test_directory = "images/test"
# Image has size 100 x 100, so the number of signals is 100^2
# -----------------------------------------------------------
# Розмір зображеннь 100 х 100, тому нейронна мережа приймає 100^2 сигналів
number_of_inputs = 100 * 100
number_of_outputs = 10 # ten digits / 10 цифр
epoch_count = 100
# Main
learning_images = get_learning_images()
# Creates network and studies it
# ------------------------------
# Створюємо об'єкт 'NeuralNetwork' і навчаємо його
network = NeuralNetwork(number_of_inputs, number_of_outputs)
study(network, learning_images, epoch_count)
# Checks on images for learning
# -----------------------------
# Перевіряємо роботу мережі на навчальній виборці
print('##### LEARNING IMAGES #####')
check_network(network, learning_images)
print('###########################')
print()
print()
# Checks on images for testing
# ----------------------------
# Перевіряємо роботу мережі на тестовій виборці
testing_images = get_test_images()
print('##### TESTING IMAGES #####')
check_network(network, testing_images)