Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 32 additions & 1 deletion intropyproject-classify-pet-images/adjust_results4_isadog.py
Original file line number Diff line number Diff line change
Expand Up @@ -66,5 +66,36 @@ def adjust_results4_isadog(results_dic, dogfile):
maltese) (string - indicates text file's filename)
Returns:
None - results_dic is mutable data type so no return needed.
"""
"""


dognames_dic=dict()
with open(dogfile,"r") as infile:
for line in infile:
dogname=line.strip().lower()
if dogname not in dognames_dic:
dognames_dic[dogname]=1


for key in results_dic:
if results_dic[key][0] in dognames_dic:
if results_dic[key][q] in dognames_dic:
#both dog
results_dic[key].extend((1,1))
else:
#pet dog
results_dic[key].extend((1,0))
else:
if results_dic[key][1] in dognames_dic:
#classfier dog
results_dic[key].extend((0,1))
else:
#both not dog
results_dic[key].extend((0,0))






None
47 changes: 46 additions & 1 deletion intropyproject-classify-pet-images/calculates_results_stats.py
Original file line number Diff line number Diff line change
Expand Up @@ -70,4 +70,49 @@ def calculates_results_stats(results_dic):
"""
# Replace None with the results_stats_dic dictionary that you created with
# this function
return None
results_stats_dic = dict()

# Initialize counters
results_stats_dic['n_dogs_img'] = 0
results_stats_dic['n_match'] = 0
results_stats_dic['n_correct_dogs'] = 0
results_stats_dic['n_correct_notdogs'] = 0
results_stats_dic['n_correct_breed'] = 0

for key in results_dic:
# Check for matching labels
if results_dic[key][2] == 1:
results_stats_dic['n_match'] += 1

# Check for correct breed classification if image is a dog
if results_dic[key][3] == 1:
results_stats_dic['n_correct_breed'] += 1

# Count dog images and correct dog classifications
if results_dic[key][3] == 1:
results_stats_dic['n_dogs_img'] += 1
if results_dic[key][4] == 1:
results_stats_dic['n_correct_dogs'] += 1
else:
# Count correct NOT-a-dog classifications
if results_dic[key][4] == 0:
results_stats_dic['n_correct_notdogs'] += 1

# Calculate total images
results_stats_dic['n_images'] = len(results_dic)

# Calculate number of NOT-a-dog images
results_stats_dic['n_notdogs_img'] = results_stats_dic['n_images'] - results_stats_dic['n_dogs_img']

# Calculate percentages with checks for zero values
if results_stats_dic['n_images'] > 0:
results_stats_dic['pct_match'] = (results_stats_dic['n_match'] / results_stats_dic['n_images']) * 100.0
if results_stats_dic['n_dogs_img'] > 0:
results_stats_dic['pct_correct_dogs'] = (results_stats_dic['n_correct_dogs'] / results_stats_dic['n_dogs_img']) * 100.0
results_stats_dic['pct_correct_breed'] = (results_stats_dic['n_correct_breed'] / results_stats_dic['n_dogs_img']) * 100.0
if results_stats_dic['n_notdogs_img'] > 0:
results_stats_dic['pct_correct_notdogs'] = (results_stats_dic['n_correct_notdogs'] / results_stats_dic['n_notdogs_img']) * 100.0
else:
results_stats_dic['pct_correct_notdogs'] = 0.0

return results_stats_dic
12 changes: 6 additions & 6 deletions intropyproject-classify-pet-images/check_images.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@
# */AIPND-revision/intropyproject-classify-pet-images/check_images.py
#
# TODO 0: Add your information below for Programmer & Date Created.
# PROGRAMMER:
# DATE CREATED:
# PROGRAMMER: Raghad
# DATE CREATED: 10Nov2024
# REVISED DATE:
# PURPOSE: Classifies pet images using a pretrained CNN model, compares these
# classifications to the true identity of the pets in the images, and
Expand Down Expand Up @@ -60,7 +60,7 @@ def main():
# get_pet_labels(in_arg.dir)
# This function creates the results dictionary that contains the results,
# this dictionary is returned from the function call as the variable results
results = get_pet_labels(None)
results = get_pet_labels(in_arg.dir)

# Function that checks Pet Images in the results Dictionary using results
check_creating_pet_image_labels(results)
Expand All @@ -74,7 +74,7 @@ def main():
# classify_images(in_arg.dir, results, in_arg.arch)
# Creates Classifier Labels with classifier function, Compares Labels,
# and adds these results to the results dictionary - results
classify_images(None, results, None)
classify_images(in_arg.dir, results, in_arg.arch)

# Function that checks Results Dictionary using results
check_classifying_images(results)
Expand All @@ -88,7 +88,7 @@ def main():
# Adjusts the results dictionary to determine if classifier correctly
# classified images as 'a dog' or 'not a dog'. This demonstrates if
# model can correctly classify dog images as dogs (regardless of breed)
adjust_results4_isadog(results, None)
adjust_results4_isadog(results, in_arg.dogfile)

# Function that checks Results Dictionary for is-a-dog adjustment using results
check_classifying_labels_as_dogs(results)
Expand All @@ -113,7 +113,7 @@ def main():
# print_results(results, results_stats, in_arg.arch, True, True)
# Prints summary results, incorrect classifications of dogs (if requested)
# and incorrectly classified breeds (if requested)
print_results(results, results_stats, None, True, True)
print_results(results, results_stats, in_arg.arch, True, True)

# TODO 0: Measure total program runtime by collecting end time
end_time = time()
Expand Down
15 changes: 14 additions & 1 deletion intropyproject-classify-pet-images/classify_images.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
#
def classify_images(images_dir, results_dic, model):
"""

Creates classifier labels with classifier function, compares pet labels to
the classifier labels, and adds the classifier label and the comparison of
the labels to the results dictionary using the extend function. Be sure to
Expand Down Expand Up @@ -65,4 +66,16 @@ def classify_images(images_dir, results_dic, model):
Returns:
None - results_dic is mutable data type so no return needed.
"""
None
for key in results_dic:
model_label= classifier(images_dir+key,model)

model_label=model_label.lower().strip()

truth=results_dic[key][0]

if truth in model_label:
results_dic[key].extend([model_label, 1])
else:
results_dic[key].extend([model_label, 0])


7 changes: 6 additions & 1 deletion intropyproject-classify-pet-images/get_input_args.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,11 @@ def get_input_args():
Returns:
parse_args() -data structure that stores the command line arguments object
"""
parser = argparse.ArgumentParser()
parser.add_argument('--dir', type = str, default = 'pet_images/', help = 'path to the folder of pet images')
parser.add_argument('--arch',type=str,default='vgg',help = 'The CNN model architecture ')
parser.add_argument('--dogfile',type=str,default='dognames.txt',help = 'The file that contains the list of valid dognames')

# Replace None with parser.parse_args() parsed argument collection that
# you created with this function
return None
return parser.parse_args()
21 changes: 18 additions & 3 deletions intropyproject-classify-pet-images/get_pet_labels.py
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,21 @@ def get_pet_labels(image_dir):
List. The list contains for following item:
index 0 = pet image label (string)
"""
# Replace None with the results_dic dictionary that you created with this
# function
return None
filename_list = listdir(image_dir)
## Creates empty dictionary named results_dic
results_dic = dict()
## Adds new key-value pairs to dictionary ONLY when key doesn't already exist. This dictionary's value is
## a List that contains only one item - the pet image label
for file in filename_list:
word_List=file.lower().split("_")

pet_labels=""
for word in word_List:
if word.isalpha():
pet_labels+=word +" "

pet_labels=pet_labels.strip()
if file not in results_dic:
results_dic[file]=[pet_labels]
return results_dic

66 changes: 0 additions & 66 deletions intropyproject-classify-pet-images/print_results.py
Original file line number Diff line number Diff line change
@@ -1,66 +0,0 @@
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
# */AIPND-revision/intropyproject-classify-pet-images/print_results.py
#
# PROGRAMMER:
# DATE CREATED:
# REVISED DATE:
# PURPOSE: Create a function print_results that prints the results statistics
# from the results statistics dictionary (results_stats_dic). It
# should also allow the user to be able to print out cases of misclassified
# dogs and cases of misclassified breeds of dog using the Results
# dictionary (results_dic).
# This function inputs:
# -The results dictionary as results_dic within print_results
# function and results for the function call within main.
# -The results statistics dictionary as results_stats_dic within
# print_results function and results_stats for the function call within main.
# -The CNN model architecture as model wihtin print_results function
# and in_arg.arch for the function call within main.
# -Prints Incorrectly Classified Dogs as print_incorrect_dogs within
# print_results function and set as either boolean value True or
# False in the function call within main (defaults to False)
# -Prints Incorrectly Classified Breeds as print_incorrect_breed within
# print_results function and set as either boolean value True or
# False in the function call within main (defaults to False)
# This function does not output anything other than printing a summary
# of the final results.
##
# TODO 6: Define print_results function below, specifically replace the None
# below by the function definition of the print_results function.
# Notice that this function doesn't to return anything because it
# prints a summary of the results using results_dic and results_stats_dic
#
def print_results(results_dic, results_stats_dic, model,
print_incorrect_dogs = False, print_incorrect_breed = False):
"""
Prints summary results on the classification and then prints incorrectly
classified dogs and incorrectly classified dog breeds if user indicates
they want those printouts (use non-default values)
Parameters:
results_dic - Dictionary with key as image filename and value as a List
(index)idx 0 = pet image label (string)
idx 1 = classifier label (string)
idx 2 = 1/0 (int) where 1 = match between pet image and
classifer labels and 0 = no match between labels
idx 3 = 1/0 (int) where 1 = pet image 'is-a' dog and
0 = pet Image 'is-NOT-a' dog.
idx 4 = 1/0 (int) where 1 = Classifier classifies image
'as-a' dog and 0 = Classifier classifies image
'as-NOT-a' dog.
results_stats_dic - Dictionary that contains the results statistics (either
a percentage or a count) where the key is the statistic's
name (starting with 'pct' for percentage or 'n' for count)
and the value is the statistic's value
model - Indicates which CNN model architecture will be used by the
classifier function to classify the pet images,
values must be either: resnet alexnet vgg (string)
print_incorrect_dogs - True prints incorrectly classified dog images and
False doesn't print anything(default) (bool)
print_incorrect_breed - True prints incorrectly classified dog breeds and
False doesn't print anything(default) (bool)
Returns:
None - simply printing results.
"""
None

Loading