-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrun.py
207 lines (171 loc) · 6.56 KB
/
run.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
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
from os import listdir
from os.path import isfile, join
from scripts.clasify_image import classify_image
from neural_network.utils import write_and_print
from scripts.test_model import measure_performance
from settings import MODELS_PATH
from scripts.train_model import train_model
go_to_main_menu_message = '\nAby powrocic do glownego menu wprowadz "q"'
def main():
action = main_view_input()
if action == "1":
measure_performance_input()
elif action == "2":
classify_image_input()
elif action == "3":
train_model_input()
elif action == "q":
quit()
def main_view_input():
control_message = """------------------------
W celu nawigacji wprowadz odpowiednia cyfre.
1 - Pomiar dokladnosci dokonywanych predykcji modelu.
2 - Klasyfikacja znaku na wskazanym obrazie.
3 - Wytrenowanie nowego modelu CNN.
q - Zakoncz dzialanie programu.
"""
write_and_print(control_message)
user_input = input()
write_and_print(user_input)
while user_input not in ("1", "2", "3", "q"):
user_input = input()
write_and_print(user_input)
return user_input
def measure_performance_input():
trained_models = get_trained_models()
model_name_message = f"""------------------------
Aby dokonac pomiaru dokladnosci wytrenowanego modelu podaj nazwe wybranego modelu sposrod dostepnych:
{get_trained_models_str(trained_models)}
{go_to_main_menu_message}
"""
write_and_print(model_name_message)
model_name = input()
write_and_print(model_name)
if not model_name.endswith('.pkl') and model_name != 'q' and model_name != '':
model_name += '.pkl'
if model_name == 'q':
return main()
elif model_name not in trained_models:
measure_performance_input()
else:
amount_of_test_data_message = f"""------------------------
Wskaz wielkosc zbioru testowego na ktorym chcesz dokonac pomiaru w przedziale 1 - 10000.
{go_to_main_menu_message}
"""
amount_of_test_data = get_int_input(amount_of_test_data_message, min_value=1, max_value=10000)
if amount_of_test_data == 'q':
return main()
else:
measure_performance(f"models_parameters/{model_name}", amount_of_test_data)
return main()
def classify_image_input():
trained_models = get_trained_models()
model_name_message = f"""------------------------
Aby dokonac klasyfikacji obrazu podaj nazwe wybranego modelu sposrod dostepnych:
{get_trained_models_str(trained_models)}
{go_to_main_menu_message}
"""
write_and_print(model_name_message)
model_name = input()
write_and_print(model_name)
if not model_name.endswith('.pkl') and model_name != 'q':
model_name += '.pkl'
if model_name == 'q':
return main()
elif model_name not in trained_models:
classify_image_input()
else:
found_image_by_path = False
path_to_image_message = f"""------------------------
Podaj sciezke do obrazu w formacie '.jpg' ze znakiem, ktory chcesz poddac klasyfiakcji.
{go_to_main_menu_message}
"""
while found_image_by_path is False:
try:
write_and_print(path_to_image_message)
path_to_image = input()
write_and_print(path_to_image)
if path_to_image == 'q':
return main()
if not isfile(path_to_image) or not path_to_image.endswith('.jpg'):
write_and_print("Wskazany adres jest nieprawidlowy")
continue
classify_image(f"models_parameters/{model_name}", path_to_image)
found_image_by_path = True
return main()
except:
write_and_print("Wskazany adres jest nieprawidlowy")
def train_model_input():
trained_models = get_trained_models()
model_name_message = f"""------------------------
Aby wytrenowac nowy model oparty o siec CNN wprowadz jego nazwe.
{go_to_main_menu_message}
"""
write_and_print(model_name_message)
model_name = input()
write_and_print(model_name)
if not model_name.endswith('.pkl') and model_name != 'q':
model_name += '.pkl'
if model_name == 'q':
return main()
elif model_name in trained_models:
write_and_print("Model o tej nazwie juz istnieje, podaj inna nazwe.")
train_model_input()
else:
train_data_q_message = f"""------------------------
Podaj wielkosc zbioru treningowego w zakresie od 1 do 50000.
{go_to_main_menu_message}
"""
test_data_q_message = f"""------------------------
Podaj wielkosc zbioru testowego w zakresie od 1 do 10000.
{go_to_main_menu_message}
"""
num_epochs_message = f"""------------------------
Podaj ilosc iteracji uczenia (epochs).
{go_to_main_menu_message}
"""
train_data_q = get_int_input(train_data_q_message, min_value=1, max_value=50000)
if train_data_q == 'q':
return main()
test_data_q = get_int_input(test_data_q_message, min_value=1, max_value=10000)
if test_data_q == 'q':
return main()
num_epochs = get_int_input(num_epochs_message, min_value=1)
if num_epochs == 'q':
return main()
train_model(f"models_parameters/{model_name}", train_data_q, test_data_q, num_epochs)
return main()
def get_int_input(message, min_value=None, max_value=None):
value_int = None
while value_int is None:
try:
write_and_print(message)
v = input()
write_and_print(v)
if v == 'q':
return 'q'
v_int = int(v)
if min_value is not None:
if min_value > v_int:
continue
if max_value is not None:
if max_value < v_int:
continue
value_int = v_int
except:
pass
return value_int
def get_trained_models():
return [f for f in listdir(MODELS_PATH) if isfile(join(MODELS_PATH, f))]
def get_trained_models_str(trained_models):
trained_models_str = ""
for trained_model in trained_models:
trained_models_str += f"\n - {trained_model}"
return trained_models_str
if __name__ == '__main__':
welcome_message = """------------------------
Niniejszy projekt umozliwia wytrenowanie modelu sztucznej sieci neuronowej, ktora umozliwi rozpoznawanie znakow pisanych - cyfr.
Dostepny jest juz wytrenowany model, ktory umozliwi natychmiastowe dokonanie predykcji z dokladnoscia okolo 98%.
"""
write_and_print(welcome_message)
main()