-
Notifications
You must be signed in to change notification settings - Fork 0
/
Face_Perceptron.py
396 lines (339 loc) · 15.1 KB
/
Face_Perceptron.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
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
import os
import random
import time
from numpy import mean, std
import numpy
import seaborn as sns
import matplotlib.pyplot as plt
import pickle
##############################
#### ADJUSTABLE VARIABLES ####
##############################
directory = './perceptron_weights/stored_weights_face' #path to weights directory
imageHeight = 70 #do not change
imageWidth = 60 #do not change
rowFeatures = 70 #features per row
colFeatures = 60 #features per column
Trials = 1 #how many trials for each percentage of dataset
epochs = 3 #how many epochs for each trial
round_digits = 4 #how many digits to round display results to
show_indidivual_results = False #debugging info (WARNING: spam)
show_group_results = False #debugging info
##############################
#### PERCEPTRON FUNCTIONS ####
##############################
#Function to read all faces from a file into an array of faces
def read_faces_from_file(filename):
with open(filename, 'r') as file:
lines = file.readlines()
faces = [] #faces array to return
current_face = [] #temporary current face data
line_count = 0
for line in lines:
#convert line to list of 1s and 0s
row = [1 if char == '#' else 0 for char in line]
current_face.append(row)
line_count += 1
#check if we have read 70 lines
if line_count == imageHeight:
faces.append(current_face)
current_face = []
line_count = 0
#add the last face if it wasn't added yet (# of lines was not multiple of 70)
if current_face:
faces.append(current_face)
return faces
#Function to read labels into a 1D array
def read_labels(filename):
labels = []
with open(filename, 'r') as file:
content = file.read()
labels = [int(char) for char in content if char in '01']
return labels
#Function to print a face in the console
def print_face(faces, index):
if index < len(faces):
face = faces[index]
for row in face:
print(''.join('#' if pixel == 1 else ' ' for pixel in row))
else:
print("Index out of range.\n")
#Function to extract the value for each feature in a face
def feature_extract(face, rows, cols):
feature_height = len(face) // rows
feature_width = len(face[0]) // cols
feature_counts = [[0] * cols for _ in range(rows)] #2D list for feature counts
for i in range(len(face)):
for j in range(len(face[0])):
if face[i][j] == 1:
feature_row = i // feature_height
feature_col = j // feature_width
#check boundaries for features at the edges (for when its not perfectly divisible)
if feature_row >= rows:
feature_row = rows - 1
if feature_col >= cols:
feature_col = cols - 1
feature_counts[feature_row][feature_col] += 1
#PRINT FOR TESTING
# for row in feature_counts:
# for item in row:
# print(item, end=' ')
# print()
#PRINT FOR TESTING
linear_feature_counts = sum(feature_counts, []) #linearize 2D array so its easy to do later calculations
return linear_feature_counts
#Randomly initializes weights between -1 and 1
def initialize_weights(feature_count):
random_weights = [0] * (feature_count + 1)
for i in range (len(random_weights)):
random_weights[i] = round(random.uniform(-1,1),round_digits)
return random_weights
#Add together all the (weight, features) pairs and return True if its above 0, False otherwise
def isFace(weights, features):
total = 0
for i in range(len(weights) - 1):
total += weights[i] * features[i]
total += weights[-1]
return (total >= 0)
#update the weights through training with a given percentage of the input data
def train_once(weights, faces, labels, percentage):
#loop through every face and update weights
for i in range(int(len(faces) * percentage)):
face = faces[i]
features = feature_extract(face,rowFeatures,colFeatures)
result = isFace(weights, features)
correct = labels[i]
if show_indidivual_results:
print("Training Result " + str(i) + ": " + str(result) + " Correct Label: " + str(correct))
#update weights
if result == True and correct == 0:
for i in range(len(weights) - 1):
weights[i] -= features[i]
weights[-1] -= 1
if result == False and correct == 1:
for i in range(len(weights) - 1):
weights[i] += features[i]
weights[-1] += 1
return weights
#tests the perceptron on the face data given and report accuracy
def test_once(weights, faces, labels):
total_correct = len(faces)
total = len(faces)
#loop through every face and test accuracy
for i in range(total):
face = faces[i]
features = feature_extract(face,rowFeatures,colFeatures)
result = isFace(weights, features)
correct = labels[i]
if show_indidivual_results:
print("Test Result: " + str(result) + " Correct Label: " + str(correct))
if result == True and correct == 0:
total_correct -= 1
elif result == False and correct == 1:
total_correct -= 1
if show_group_results:
print(str(round(total_correct / total, round_digits)) + " percent correct --> " + str(total_correct) + " out of " + str(total) + ".\n")
return total_correct / total
#########################
#### STORING WEIGHTS ####
#########################
#stores current weights and additional parameters
def store_weights(weights, file_name, rowFeatures, colFeatures, Trials, epochs):
data_to_store = {
'weights': weights,
'rowFeatures': rowFeatures,
'colFeatures': colFeatures,
'Trials': Trials,
'epochs': epochs,
}
with open(os.path.join(directory, file_name + '.pkl'), 'wb') as f:
pickle.dump(data_to_store, f)
print(f"\nWeights and parameters saved to {file_name + '.pkl'}\n")
#retrieves current weights and parameters
def retrieve_weights(file_name):
if os.path.exists(file_name):
with open(file_name, 'rb') as f:
data_loaded = pickle.load(f)
return data_loaded
else:
raise FileNotFoundError(f"No such file: {file_name}\n")
#lists all current available weights
def list_weight_files(directory):
files = [f for f in os.listdir(directory) if f.endswith('.pkl')]
if not files:
print("\nNo weight files found.\n")
return None
for idx, file in enumerate(files, start=1):
print(f"{idx}: {file}")
return files
#returns selected weights and parameters
def select_and_load_weights(directory):
files = list_weight_files(directory)
if files is None:
return None
while True:
try:
choice = int(input("\nWhich weights would you like to retrieve? Enter the number: "))
if 1 <= choice <= len(files):
file_name = files[choice - 1]
return retrieve_weights(os.path.join(directory, file_name))
else:
print("Invalid choice. Please choose a number from the list.\n")
except ValueError:
print("Please enter a valid number.\n")
#######################
#### TESTING SUITE ####
#######################
#Variables can be adjusted at the top of the script!
training_done = False
full_training_done = False
#Extract Faces and Labels
facesDataTrain = read_faces_from_file('./data/facedata/facedatatrain.txt')
labels = read_labels("./data/facedata/facedatatrainlabels.txt")
facesDataValidation = read_faces_from_file('./data/facedata/facedatavalidation.txt')
validlabels = read_labels("./data/facedata/facedatavalidationlabels.txt")
#train from scratch if user requests
test_percents = input("\nWould you like to train from scratch? (y/n): ")
if(test_percents == "y"):
#determine what percentage of the training data to use
percentage = input("\nWhat percentage of data should be used for training? (0.1, 0.2, 0.3, 0.4, 0.5, 0.6, 0.7, 0.8, 0.9, 1, all): ")
if(percentage == 'all'):
percent_loops = 11
choice = 0.1
percentages = [0,0.1,0.2,0.3,0.4,0.5,0.6,0.7,0.8,0.9,1.0]
else:
percent_loops = 2
choice = float(percentage)
percentages = [0, float(percentage)]
#set up arrays to record averages
average_accuracies = []
average_times = []
average_stds = []
trained_weights = []
#Percentage Loop
for i in range(percent_loops):
total_accuracy = []
total_time = []
#Trial Loop
for j in range(Trials):
#Randomly shuffle the training data
combined_data = list(zip(facesDataTrain, labels))
random.shuffle(combined_data)
shuffled_facesDataTrain, shuffled_labels = zip(*combined_data)
#Reinitialize weights
weights = initialize_weights(rowFeatures * colFeatures)
start_train_time = time.time() #training start
#Epoch Loop
for k in range(epochs):
weights = train_once(weights, shuffled_facesDataTrain, shuffled_labels, i * choice)
train_time = time.time() - start_train_time #training end
if(show_indidivual_results):
print("Completed training in: " + str(round(train_time,round_digits))) #debugging print
#Append accuracy and time for trial
total_accuracy.append(test_once(weights, facesDataValidation, validlabels))
total_time.append(train_time)
#Calculate average and standard deviation of all trials
average_accuracy = mean(total_accuracy)
average_time = mean(total_time)
std_accuracy = std(total_accuracy)
std_time = std(total_time)
total_time = sum(total_time)
#Display all results in terminal
print("\n-------------------------------------------------------------------")
print(str(Trials) + " Trials, " + str(epochs) + " Epochs, " + str(round(i * choice * 100,3))
+ " percent of data, " + str(rowFeatures) + " row features, " + str(colFeatures)
+ " column features")
print("-------------------------------------------------------------------")
print("Average accuracy: " + str(round(average_accuracy,round_digits)) + " percent.")
print("Standard Deviation of accuracy: " + str(round(std_accuracy,round_digits)) + " percent.\n")
print("Average training time: " + str(round(average_time,round_digits)) + " seconds.")
print("Standard Deviation of training time: " + str(round(std_time,round_digits)) + " seconds.")
print("Total Training Time: " + str(round(total_time,round_digits)) + " seconds.")
print("-------------------------------------------------------------------\n")
#Save trial averages for plotting
average_accuracies.append(average_accuracy)
average_times.append(average_time)
average_stds.append(std_accuracy)
trained_weights.append(weights)
training_done = True
full_training_done = True
#store new weights
store = input("\nWould you like to save these weights? (y/n): ")
if(store == 'y'):
name = input("\nWhat would you like the name them?: ")
store_weights(trained_weights[-1], name, rowFeatures, colFeatures, Trials, epochs)
else:
#load saved weights
data = select_and_load_weights(directory)
if data is not None:
weights = data['weights']
rowFeatures = data['rowFeatures']
colFeatures = data['colFeatures']
Trials = data['Trials']
epochs = data['epochs']
print("Loaded weights.\n")
training_done = True
#display weight map of loaded weights
show_weight = input("\nWould you like to see the weight map? (y/n): ")
if(show_weight == 'y'):
#Weights visualization heatmap
plt.figure()
numpy_trained_weights = numpy.array(weights)
numpy_trained_weights = numpy_trained_weights[:-1].reshape(rowFeatures,colFeatures)
sns.heatmap(numpy_trained_weights, annot=False, cmap='Greys', cbar=True)
plt.title('Perceptron Weights Visualization for Faces:\n' + str(epochs) + " Epochs, " + str(rowFeatures) + " row features, " + str(colFeatures) + " column features")
plt.show()
#Show plots if user requests
if(full_training_done): #only plot if they trained through every percentage
show_plot = input("\nWould you like to plot results? (y/n): ")
if(show_plot == 'y'):
#Average Accuracy plot
plt.figure()
plt.plot(percentages, average_accuracies, label= 'Average Accuracy (Percentage)')
plt.plot(percentages, average_stds, label = 'Standard Deviation of Accuracy')
plt.title('Accuracy Results: \n' + str(Trials) + " Trials, " + str(epochs) + " Epochs, " + str(rowFeatures) + " row features, " + str(colFeatures) + " column features")
plt.xlabel('Percentage of Training Data')
plt.xlim(0, 1)
plt.ylim(0, 1)
plt.xticks([i * 0.1 for i in range(11)])
plt.yticks([i * 0.1 for i in range(11)])
plt.legend()
plt.show()
#Average Train time and standard deviation plot
plt.figure()
plt.title('Train Time Results: \n' + str(Trials) + " Trials, " + str(epochs) + " Epochs, " + str(rowFeatures) + " row features, " + str(colFeatures) + " column features")
plt.plot(percentages, average_times, label= 'Average Train Time (Seconds)')
plt.ylabel('Average Train Time (seconds)')
plt.xlabel('Percentage of Training Data')
plt.xlim(0, 1)
plt.xticks([i * 0.1 for i in range(11)])
plt.legend()
plt.show()
#Weights visualization heatmap
plt.figure()
numpy_trained_weights = numpy.array(trained_weights[-1])
numpy_trained_weights = numpy_trained_weights[:-1].reshape(rowFeatures,colFeatures)
sns.heatmap(numpy_trained_weights, annot=False, cmap='Greys', cbar=True)
plt.title('Perceptron Weights Visualization for Faces:\n' + str(epochs) + " Epochs, " + str(rowFeatures) + " row features, " + str(colFeatures) + " column features")
plt.show()
#Tests trained perceptron on specified index
def test_one(index):
digit = facesDataValidation[index]
features = feature_extract(digit,rowFeatures,colFeatures)
result = isFace(weights, features)
correct = validlabels[index]
print("Perceptron Guess: " + str(result) + " Correct Label: " + ("True" if correct == 1 else "False"))
#Promt user for request
if(training_done):
cont = True
while(cont):
index = input("\nWhich image to test? (0-300, s to exit): ")
if(index == "s"):
cont = False
else:
if(abs(int(index)) < len(facesDataValidation)):
print_face(facesDataValidation, int(index))
test_one(int(index))
else:
print("Index out of range.\n")
print('\n\nHave a great day!\n\n')