3
3
import numpy as np
4
4
import pandas as pd
5
5
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
7
7
import seaborn as sns
8
8
import matplotlib .pyplot as plt
9
9
@@ -49,6 +49,13 @@ def avg_images(class_name, dataset='Training'):
49
49
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.
50
50
'''
51
51
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
+ '''
52
59
#initialize the cloest class variable
53
60
closest_class = None
54
61
#initialize the cloest class variable
@@ -71,7 +78,14 @@ def find_closest_class(mean_pixel_value, class_averages):
71
78
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.
72
79
'''
73
80
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
75
89
actual_classes = []
76
90
#create a list for the predictions
77
91
predicted_classes = []
@@ -94,7 +108,7 @@ def classify_images(test_folder_path, class_paths):
94
108
image_path = os .path .join (class_folder_path , image_file )
95
109
#reads the image path using Open CV
96
110
test_image = cv2 .imread (image_path )
97
- #gets the mean pixcel value of the image
111
+ #gets the mean pixel value of the image
98
112
mean_pixel_value = np .mean (test_image , axis = (0 , 1 ))
99
113
#uses the find_cloest_class function to find what class its closest to
100
114
closest_class = find_closest_class (mean_pixel_value , class_averages )
@@ -116,16 +130,26 @@ def classify_images(test_folder_path, class_paths):
116
130
'''
117
131
118
132
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
+ '''
119
142
#creates the confusion matrix
120
143
cm = confusion_matrix (actual_classes , predicted_classes ,
121
144
# gets the label of each class
122
145
labels = list (class_paths .keys ()))
123
146
#Finds the weighted scores for each metric
147
+ accuracy = accuracy_score (actual_classes , predicted_classes )
124
148
precision = precision_score (actual_classes , predicted_classes , average = 'weighted' )
125
149
recall = recall_score (actual_classes , predicted_classes , average = 'weighted' )
126
150
f1 = f1_score (actual_classes , predicted_classes , average = 'weighted' )
127
151
#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 ]}
129
153
metrics_df = pd .DataFrame (data , index = ['baseline' ])
130
154
131
155
return cm , metrics_df
@@ -137,6 +161,15 @@ def calculate_metrics(actual_classes, predicted_classes, class_paths):
137
161
There were slight modifications to fit the purposes of this code such as assigning class paths and a title parameter.
138
162
'''
139
163
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
+ '''
140
173
#sets the figure size
141
174
plt .figure (figsize = (10 ,10 ))
142
175
#Plots the confusion matrix and assigns the class names on the axis ticks
0 commit comments