-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.py
135 lines (116 loc) · 3.93 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
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
import os
import json
from datetime import datetime
from typing import cast
from loader import MNIST
from convolution_layer import ConvolutionLayer
from pool_layer import PoolLayer
from softmax_layer import SoftMaxLayer
from tests.run_testing_phase import run_testing_phase
from training.predict_in_dir import predict_in_dir
from training.run_epochs import run_epochs
from utils.save_model import save_model
from utils.shuffle import shuffle
from utils.logger import logger
img_size = 28
samples_dir = "samples"
outdir = "out"
mndata = MNIST("./dataset")
logger.info("Loading data...")
data_train, label_train = mndata.load_training()
data_test, label_test = mndata.load_testing()
logger.info("Data loaded.")
training_set_size = int(
input("Enter the size of the training set (default: 10 000, max: 60 000): ")
or 10_000
)
logger.info("Training set size: %s", training_set_size)
test_set_size = int(
input("Enter the size of the training set (default: 1 000, max: 10 000): ") or 1_000
)
logger.info("Test set size: %s", test_set_size)
logger.info("Shuffling data...")
train_images, train_labels = shuffle(
cast(list[int], data_train), cast(list[int], label_train), training_set_size
)
test_images, test_labels = shuffle(
cast(list[int], data_test), cast(list[int], label_test), test_set_size
)
logger.info("Data shuffled.")
should_train_again = input("Do you want to train the model? (Y/n): ")
if should_train_again.lower() == "n":
logger.info("Skipping training...")
path: str = input("Enter the relative path of the model to load: ")
logger.info("Loading model...")
with open(path, "r", encoding="utf-8") as f:
model = json.load(f)
logger.info("Model loaded.")
logger.info("Initializing layers...")
convolution_layer = ConvolutionLayer.deserialize(model)
max_pooling_layer = PoolLayer.deserialize(model)
softmax_output_layer = SoftMaxLayer.deserialize(model)
logger.info("Layers initialized.")
run_testing_phase(
test_images,
test_labels,
img_size,
convolution_layer,
max_pooling_layer,
softmax_output_layer,
)
predict_in_dir(
convolution_layer, max_pooling_layer, softmax_output_layer, samples_dir, outdir
)
exit(0)
output_classes = 10
filter_size = 3
filters_count = int(input("Enter the number of filters (default: 32): ").strip() or 32)
logger.info("Number of filters: %s", filters_count)
pool_size = int(input("Enter the pool size (default: 1): ").strip() or 1)
logger.info("Pool size: %s", pool_size)
softmax_edge = int((img_size - 2) / pool_size)
num_of_epochs = int(input("Enter the number of epochs (default: 5): ").strip() or 5)
logger.info("Number of epochs: %s", num_of_epochs)
learning_rate = float(
input("Enter the learning rate (default: 0.005): ").strip() or 0.005
)
logger.info("Learning rate: %s", learning_rate)
logger.info("Initializing layers...")
convolution_layer = ConvolutionLayer(filters_count, filter_size)
max_pooling_layer = PoolLayer(pool_size)
softmax_output_layer = SoftMaxLayer(softmax_edge**2 * filters_count, output_classes)
logger.info("Layers initialized.")
run_epochs(
train_images,
train_labels,
img_size,
learning_rate,
num_of_epochs,
convolution_layer,
max_pooling_layer,
softmax_output_layer,
)
run_testing_phase(
test_images,
test_labels,
img_size,
convolution_layer,
max_pooling_layer,
softmax_output_layer,
)
predict_in_dir(
convolution_layer, max_pooling_layer, softmax_output_layer, samples_dir, outdir
)
model_outdir = (
input("Enter the directory to save the model (default: 'model'): ") or "model"
)
if not os.path.exists(model_outdir):
os.makedirs(model_outdir)
current_date = datetime.today().strftime("%Y-%m-%d-%H:%m")
model_name = (
input(f"Enter the name of the model (default: {current_date}.json): ")
or current_date
)
save_model(
convolution_layer, max_pooling_layer, softmax_output_layer, model_name, model_outdir
)