Skip to content

Commit 69682b0

Browse files
committed
commited baseline model and created a inital neural network
1 parent 72e02c1 commit 69682b0

File tree

4 files changed

+480
-11
lines changed

4 files changed

+480
-11
lines changed
3.23 KB
Loading

Notebooks/02_Baseline Model.ipynb

+6-7
Large diffs are not rendered by default.

Notebooks/03_Neural_network.ipynb

+437
Large diffs are not rendered by default.

modules/model.py

+37-4
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
import numpy as np
44
import pandas as pd
55
from tensorflow.keras.preprocessing.image import img_to_array, load_img
6-
from sklearn.metrics import confusion_matrix, precision_score, recall_score, f1_score
6+
from sklearn.metrics import confusion_matrix, precision_score, recall_score, f1_score, accuracy_score
77
import seaborn as sns
88
import matplotlib.pyplot as plt
99

@@ -49,6 +49,13 @@ def avg_images(class_name, dataset='Training'):
4949
was later prompted to adjust the code to be able to pass a parameter to the classify_images function. Took a total of 8 prompts and manual adjustments.
5050
'''
5151
def find_closest_class(mean_pixel_value, class_averages):
52+
'''
53+
This function finds which class an image is closest to
54+
=============================================================================
55+
Keyword arguments:
56+
mean_pixel_value -- The mean pixel value of an image
57+
class_averages -- The main folder either Train and Test folder (default = Training)
58+
'''
5259
#initialize the cloest class variable
5360
closest_class = None
5461
#initialize the cloest class variable
@@ -71,7 +78,14 @@ def find_closest_class(mean_pixel_value, class_averages):
7178
was later prompted to adjust the code to be able to pass a parameter to the find_closest_class function. Took a total of 8 prompts and manual adjustments.
7279
'''
7380
def classify_images(test_folder_path, class_paths):
74-
#create a list for the actual images
81+
'''
82+
This function finds predicts which class belongs to based on which avg pixel value its closest to
83+
=============================================================================
84+
Keyword arguments:
85+
test_folder_path -- The file path to the test folder
86+
class_paths -- The file path to the class
87+
'''
88+
#create a list for the actual classes of an images
7589
actual_classes = []
7690
#create a list for the predictions
7791
predicted_classes = []
@@ -94,7 +108,7 @@ def classify_images(test_folder_path, class_paths):
94108
image_path = os.path.join(class_folder_path, image_file)
95109
#reads the image path using Open CV
96110
test_image = cv2.imread(image_path)
97-
#gets the mean pixcel value of the image
111+
#gets the mean pixel value of the image
98112
mean_pixel_value = np.mean(test_image, axis=(0, 1))
99113
#uses the find_cloest_class function to find what class its closest to
100114
closest_class = find_closest_class(mean_pixel_value, class_averages)
@@ -116,16 +130,26 @@ def classify_images(test_folder_path, class_paths):
116130
'''
117131

118132
def calculate_metrics(actual_classes, predicted_classes, class_paths):
133+
'''
134+
This function calculates the precisision, recall, and F1 scores in the for of a data frame.
135+
This funcsion also creats a confusion matrix.
136+
=============================================================================
137+
Keyword arguments:
138+
actual_classes -- The actual class an image belongs to
139+
predicted_classes -- The predicted class an image belongs to
140+
class_paths -- The file path to the class
141+
'''
119142
#creates the confusion matrix
120143
cm = confusion_matrix(actual_classes, predicted_classes,
121144
# gets the label of each class
122145
labels=list(class_paths.keys()))
123146
#Finds the weighted scores for each metric
147+
accuracy = accuracy_score(actual_classes, predicted_classes)
124148
precision = precision_score(actual_classes, predicted_classes, average='weighted')
125149
recall = recall_score(actual_classes, predicted_classes, average='weighted')
126150
f1 = f1_score(actual_classes, predicted_classes, average='weighted')
127151
#adds the scores into a data frame
128-
data = {'Precision': [precision], 'Recall': [recall], 'F1 Score': [f1]}
152+
data = {'Accuracy': [accuracy],'Precision': [precision], 'Recall': [recall], 'F1 Score': [f1]}
129153
metrics_df = pd.DataFrame(data, index=['baseline'])
130154

131155
return cm, metrics_df
@@ -137,6 +161,15 @@ def calculate_metrics(actual_classes, predicted_classes, class_paths):
137161
There were slight modifications to fit the purposes of this code such as assigning class paths and a title parameter.
138162
'''
139163
def plot_confusion_matrix(confusion_matrix, class_paths, title):
164+
'''
165+
This function displays the actual confusion matrix and plots it
166+
=============================================================================
167+
Keyword arguments:
168+
confusion_matrix -- The confusion matrix generated from the calculate_metrics function
169+
class_paths -- The file path to the class
170+
title -- The title of the confusion matrix as well as the title of the saved image
171+
The tile will be a prefix to {title} confusion matrix
172+
'''
140173
#sets the figure size
141174
plt.figure(figsize=(10,10))
142175
#Plots the confusion matrix and assigns the class names on the axis ticks

0 commit comments

Comments
 (0)