-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.py
executable file
·167 lines (132 loc) · 5.02 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
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
import neural as nn
import helpers as helpers
import config as cfg
import sys
import getopt
import imageConverter as converter
import os
import genetic as gen
# Main function that starts the program and handles user input
def main(argv):
net_name = ""
train_len = 500
alg = ""
autosave_name = ""
testing_name = ""
# read command line arguments
try:
opts, args = getopt.getopt(argv, "hn:l:c:a:as:t:")
except getopt.GetoptError:
print ("Usage: main.py -n <net file to use> -l <training length>, -c <convert images again>, -a <algorithm to use (GEN, BP)> -s <name for autosaving net> -t <name for auto testing file>")
sys.exit(2)
if not argv:
print ("Usage: main.py -n <net file to use> -l <training length>, -c <convert images again>, -a <algorithm to use (GEN, BP)> -s <name for autosaving net> -t <name for auto testing file>")
sys.exit(2)
for opt, arg in opts:
# print usage
if opt == '-h':
print ("Usage: main.py -n <net file to use> -l <training length>, -c <convert images again>, -a <algorithm to use (GEN, BP)> -s <name for autosaving net> -t <name for auto testing file>")
sys.exit()
# store the name of the net to be loaded
elif opt == "-n":
net_name = arg
# store training length
elif opt == "-l":
train_len = int(arg)
# convert images
elif opt == "-c":
converter.convert_images()
# store the name for the net to be saved
elif opt == "-s":
if arg != "":
autosave_name = arg
else:
print("Invalid input for -s")
# store the algorithm to use
elif opt == "-a":
print(arg)
if arg == "GEN" or arg == "BP":
alg = arg
else:
print ("Non-valid algorithm. Choose either GEN for genetic algorithm or BP for Backpropagation")
sys.exit()
elif opt == "-t":
testing_name = arg
# initialize net
# if a net name was given, load that net
if net_name != "":
net = helpers.load_net(net_name)
elif alg == "":
print ("Neither a previous net or training algorithm was chosen. Try again!")
sys.exit()
elif alg == "GEN":
# train the net using gen alg
net = gen.genetic_train(cfg.POP_SIZE, train_len, testing_name)
# if autosave name was given, save the net
if autosave_name != "":
helpers.save_net(net, autosave_name)
else:
# train the net using BP
net = nn.backprop_train(train_len, testing_name)
# if autosave name was given, save the net
if autosave_name != "":
helpers.save_net(net, autosave_name)
print("Net ready!")
# Ask the user for a command
while True:
training_data = []
print("Choose an action:")
print("Q - Quit")
print("T - Run training data")
print("F - Test a file")
print("S - Save the net")
print("C - Continue training")
print("E - Calculate Error")
choice = raw_input("")
# Exit
if choice in ("Q", "q"):
print("Exiting!")
sys.exit()
# run the training results
elif choice in ("T", "t"):
helpers.training_results(net)
# test a specific file
elif choice in ("F", "f"):
# get file path
test_file = raw_input("input path to file: ")
while test_file == "":
test_file = raw_input("Try again!")
# Check if file is accessible
if not os.access(test_file, os.R_OK):
print("file is not accessible :( ")
else:
helpers.test_image(test_file, net)
elif choice in ("S", "s"):
# Save the net
name = ""
while name == "":
name = raw_input("filename: ")
helpers.save_net(net, name)
elif choice in ("C", "c"):
# prompt the user for parameters
train_len = raw_input("training length? ")
while not train_len.isdigit():
train_len = raw_input("try again! ")
train_len = int(train_len)
alg = raw_input("Which algorithm to use? GEN/BP: ")
while not (alg == "GEN" or alg == "BP"):
alg = raw_input("Try again! GEN/BP: ")
# train the net
if alg == "GEN":
gen.continue_gen(net, train_len, cfg.POP_SIZE)
else:
nn.continue_bp(net, train_len)
elif choice in ("E", "e"):
# if training data is not already loaded, load it
if not training_data:
training_data = helpers.load_training_data()
print("Calculating...")
# print out the total error over training data
print("Total error for net: " + str(gen.net_error(net, training_data)))
if __name__ == "__main__":
main(sys.argv[1:])