-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
58 changed files
with
2,241 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -102,3 +102,5 @@ venv.bak/ | |
|
||
# mypy | ||
.mypy_cache/ | ||
|
||
.idea/ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,84 @@ | ||
#!/usr/bin/env python3 | ||
# -*- coding: utf-8 -*- | ||
# */AIPND-revision/intropyproject-classify-pet-images/adjust_results4_isadog.py | ||
# | ||
# PROGRAMMER: Mariot Tsitoara | ||
# DATE CREATED: 18 Nov 2019 | ||
# REVISED DATE: | ||
# PURPOSE: Create a function adjust_results4_isadog that adjusts the results | ||
# dictionary to indicate whether or not the pet image label is of-a-dog, | ||
# and to indicate whether or not the classifier image label is of-a-dog. | ||
# All dog labels from both the pet images and the classifier function | ||
# will be found in the dognames.txt file. We recommend reading all the | ||
# dog names in dognames.txt into a dictionary where the 'key' is the | ||
# dog name (from dognames.txt) and the 'value' is one. If a label is | ||
# found to exist within this dictionary of dog names then the label | ||
# is of-a-dog, otherwise the label isn't of a dog. Alternatively one | ||
# could also read all the dog names into a list and then if the label | ||
# is found to exist within this list - the label is of-a-dog, otherwise | ||
# the label isn't of a dog. | ||
# This function inputs: | ||
# -The results dictionary as results_dic within adjust_results4_isadog | ||
# function and results for the function call within main. | ||
# -The text file with dog names as dogfile within adjust_results4_isadog | ||
# function and in_arg.dogfile for the function call within main. | ||
# This function uses the extend function to add items to the list | ||
# that's the 'value' of the results dictionary. You will be adding the | ||
# whether or not the pet image label is of-a-dog as the item at index | ||
# 3 of the list and whether or not the classifier label is of-a-dog as | ||
# the item at index 4 of the list. Note we recommend setting the values | ||
# at indices 3 & 4 to 1 when the label is of-a-dog and to 0 when the | ||
# label isn't a dog. | ||
# | ||
## | ||
# TODO 4: Define adjust_results4_isadog function below, specifically replace the None | ||
# below by the function definition of the adjust_results4_isadog function. | ||
# Notice that this function doesn't return anything because the | ||
# results_dic dictionary that is passed into the function is a mutable | ||
# data type so no return is needed. | ||
# | ||
def adjust_results4_isadog(results_dic, dogfile): | ||
""" | ||
Adjusts the results dictionary to determine if classifier correctly | ||
classified images 'as a dog' or 'not a dog' especially when not a match. | ||
Demonstrates if model architecture correctly classifies dog images even if | ||
it gets dog breed wrong (not a match). | ||
Parameters: | ||
results_dic - Dictionary with 'key' as image filename and 'value' as a | ||
List. Where the list will contain the following items: | ||
index 0 = pet image label (string) | ||
index 1 = classifier label (string) | ||
index 2 = 1/0 (int) where 1 = match between pet image | ||
and classifer labels and 0 = no match between labels | ||
------ where index 3 & index 4 are added by this function ----- | ||
NEW - index 3 = 1/0 (int) where 1 = pet image 'is-a' dog and | ||
0 = pet Image 'is-NOT-a' dog. | ||
NEW - index 4 = 1/0 (int) where 1 = Classifier classifies image | ||
'as-a' dog and 0 = Classifier classifies image | ||
'as-NOT-a' dog. | ||
dogfile - A text file that contains names of all dogs from the classifier | ||
function and dog names from the pet image files. This file has | ||
one dog name per line dog names are all in lowercase with | ||
spaces separating the distinct words of the dog name. Dog names | ||
from the classifier function can be a string of dog names separated | ||
by commas when a particular breed of dog has multiple dog names | ||
associated with that breed (ex. maltese dog, maltese terrier, | ||
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) as f: | ||
for line in f: | ||
dognames_dic[line.rstrip()] = 1 | ||
for result in results_dic: | ||
if results_dic[result][0] in dognames_dic: | ||
results_dic[result].append(1) | ||
else: | ||
results_dic[result].append(0) | ||
|
||
if results_dic[result][1] in dognames_dic: | ||
results_dic[result].append(1) | ||
else: | ||
results_dic[result].append(0) | ||
None |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,114 @@ | ||
#!/usr/bin/env python3 | ||
# -*- coding: utf-8 -*- | ||
# */AIPND-revision/intropyproject-classify-pet-images/calculates_results_stats.py | ||
# | ||
# PROGRAMMER: Mariot Tsitoara | ||
# DATE CREATED: 18 Nov 2019 | ||
# REVISED DATE: | ||
# PURPOSE: Create a function calculates_results_stats that calculates the | ||
# statistics of the results of the programrun using the classifier's model | ||
# architecture to classify the images. This function will use the | ||
# results in the results dictionary to calculate these statistics. | ||
# This function will then put the results statistics in a dictionary | ||
# (results_stats_dic) that's created and returned by this function. | ||
# This will allow the user of the program to determine the 'best' | ||
# model for classifying the images. The statistics that are calculated | ||
# will be counts and percentages. Please see "Intro to Python - Project | ||
# classifying Images - xx Calculating Results" for details on the | ||
# how to calculate the counts and percentages for this function. | ||
# This function inputs: | ||
# -The results dictionary as results_dic within calculates_results_stats | ||
# function and results for the function call within main. | ||
# This function creates and returns the Results Statistics Dictionary - | ||
# results_stats_dic. This dictionary 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 value | ||
# is the statistic's value. This dictionary should contain the | ||
# following keys: | ||
# n_images - number of images | ||
# n_dogs_img - number of dog images | ||
# n_notdogs_img - number of NON-dog images | ||
# n_match - number of matches between pet & classifier labels | ||
# n_correct_dogs - number of correctly classified dog images | ||
# n_correct_notdogs - number of correctly classified NON-dog images | ||
# n_correct_breed - number of correctly classified dog breeds | ||
# pct_match - percentage of correct matches | ||
# pct_correct_dogs - percentage of correctly classified dogs | ||
# pct_correct_breed - percentage of correctly classified dog breeds | ||
# pct_correct_notdogs - percentage of correctly classified NON-dogs | ||
# | ||
## | ||
# TODO 5: Define calculates_results_stats function below, please be certain to replace None | ||
# in the return statement with the results_stats_dic dictionary that you create | ||
# with this function | ||
# | ||
def calculates_results_stats(results_dic): | ||
""" | ||
Calculates statistics of the results of the program run using classifier's model | ||
architecture to classifying pet images. Then puts the results statistics in a | ||
dictionary (results_stats_dic) so that it's returned for printing as to help | ||
the user to determine the 'best' model for classifying images. Note that | ||
the statistics calculated as the results are either percentages or counts. | ||
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. | ||
Returns: | ||
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. See comments above | ||
and the previous topic Calculating Results in the class for details | ||
on how to calculate the counts and statistics. | ||
""" | ||
# Replace None with the results_stats_dic dictionary that you created with | ||
# this function | ||
results_stats_dic = dict() | ||
n_images = len(results_dic) | ||
n_dogs_img = 0 | ||
n_notdogs_img = 0 | ||
n_match = 0 | ||
n_correct_dogs = 0 | ||
n_correct_notdogs = 0 | ||
n_correct_breed = 0 | ||
|
||
for result in results_dic: | ||
if results_dic[result][3] == 1: | ||
n_dogs_img += 1 | ||
|
||
if results_dic[result][4] == 1: | ||
n_correct_dogs += 1 | ||
else: | ||
n_notdogs_img += 1 | ||
if results_dic[result][4] == 0: | ||
n_correct_notdogs += 1 | ||
|
||
if results_dic[result][2] == 1: | ||
n_match += 1 | ||
|
||
if results_dic[result][2] == 1 and results_dic[result][3] == 1: | ||
n_correct_breed += 1 | ||
|
||
pct_match = (n_match / n_images) * 100 | ||
pct_correct_dogs = (n_correct_dogs / n_dogs_img) * 100 | ||
pct_correct_notdogs = (n_correct_notdogs / n_notdogs_img) * 100 | ||
pct_correct_breed = (n_correct_breed / n_correct_dogs) * 100 | ||
results_stats_dic['n_images'] = n_images | ||
results_stats_dic['n_dogs_img'] = n_dogs_img | ||
results_stats_dic['n_notdogs_img'] = n_notdogs_img | ||
results_stats_dic['n_match'] = n_match | ||
results_stats_dic['n_correct_dogs'] = n_correct_dogs | ||
results_stats_dic['n_correct_notdogs'] = n_correct_notdogs | ||
results_stats_dic['n_correct_breed'] = n_correct_breed | ||
results_stats_dic['pct_match'] = pct_match | ||
results_stats_dic['pct_correct_dogs'] = pct_correct_dogs | ||
results_stats_dic['pct_correct_notdogs'] = pct_correct_notdogs | ||
results_stats_dic['pct_correct_breed'] = pct_correct_breed | ||
return results_stats_dic |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,130 @@ | ||
#!/usr/bin/env python3 | ||
# -*- coding: utf-8 -*- | ||
# */AIPND-revision/intropyproject-classify-pet-images/check_images.py | ||
# | ||
# TODO 0: Add your information below for Programmer & Date Created. | ||
# PROGRAMMER: Mariot Tsitoara | ||
# DATE CREATED: 18 Nov 2019 | ||
# 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 | ||
# summarizes how well the CNN performed on the image classification task. | ||
# Note that the true identity of the pet (or object) in the image is | ||
# indicated by the filename of the image. Therefore, your program must | ||
# first extract the pet image label from the filename before | ||
# classifying the images using the pretrained CNN model. With this | ||
# program we will be comparing the performance of 3 different CNN model | ||
# architectures to determine which provides the 'best' classification. | ||
# | ||
# Use argparse Expected Call with <> indicating expected user input: | ||
# python check_images.py --dir <directory with images> --arch <model> | ||
# --dogfile <file that contains dognames> | ||
# Example call: | ||
# python check_images.py --dir pet_images/ --arch vgg --dogfile dognames.txt | ||
## | ||
|
||
# Imports python modules | ||
from time import time, sleep | ||
|
||
# Imports print functions that check the lab | ||
from print_functions_for_lab_checks import * | ||
|
||
# Imports functions created for this program | ||
from get_input_args import get_input_args | ||
from get_pet_labels import get_pet_labels | ||
from classify_images import classify_images | ||
from adjust_results4_isadog import adjust_results4_isadog | ||
from calculates_results_stats import calculates_results_stats | ||
from print_results import print_results | ||
|
||
# Main program function defined below | ||
def main(): | ||
# TODO 0: Measures total program runtime by collecting start time | ||
start_time = time() | ||
|
||
# TODO 1: Define get_input_args function within the file get_input_args.py | ||
# This function retrieves 3 Command Line Arugments from user as input from | ||
# the user running the program from a terminal window. This function returns | ||
# the collection of these command line arguments from the function call as | ||
# the variable in_arg | ||
in_arg = get_input_args() | ||
|
||
# Function that checks command line arguments using in_arg | ||
check_command_line_arguments(in_arg) | ||
|
||
|
||
# TODO 2: Define get_pet_labels function within the file get_pet_labels.py | ||
# Once the get_pet_labels function has been defined replace 'None' | ||
# in the function call with in_arg.dir Once you have done the replacements | ||
# your function call should look like this: | ||
# 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(in_arg.dir) | ||
|
||
# Function that checks Pet Images in the results Dictionary using results | ||
check_creating_pet_image_labels(results) | ||
|
||
|
||
# TODO 3: Define classify_images function within the file classiy_images.py | ||
# Once the classify_images function has been defined replace first 'None' | ||
# in the function call with in_arg.dir and replace the last 'None' in the | ||
# function call with in_arg.arch Once you have done the replacements your | ||
# function call should look like this: | ||
# 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(in_arg.dir, results, in_arg.arch) | ||
|
||
# Function that checks Results Dictionary using results | ||
check_classifying_images(results) | ||
|
||
|
||
# TODO 4: Define adjust_results4_isadog function within the file adjust_results4_isadog.py | ||
# Once the adjust_results4_isadog function has been defined replace 'None' | ||
# in the function call with in_arg.dogfile Once you have done the | ||
# replacements your function call should look like this: | ||
# adjust_results4_isadog(results, in_arg.dogfile) | ||
# 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, in_arg.dogfile) | ||
|
||
# Function that checks Results Dictionary for is-a-dog adjustment using results | ||
check_classifying_labels_as_dogs(results) | ||
|
||
|
||
# TODO 5: Define calculates_results_stats function within the file calculates_results_stats.py | ||
# This function creates the results statistics dictionary that contains a | ||
# summary of the results statistics (this includes counts & percentages). This | ||
# dictionary is returned from the function call as the variable results_stats | ||
# Calculates results of run and puts statistics in the Results Statistics | ||
# Dictionary - called results_stats | ||
results_stats = calculates_results_stats(results) | ||
|
||
# Function that checks Results Statistics Dictionary using results_stats | ||
check_calculating_results(results, results_stats) | ||
|
||
|
||
# TODO 6: Define print_results function within the file print_results.py | ||
# Once the print_results function has been defined replace 'None' | ||
# in the function call with in_arg.arch Once you have done the | ||
# replacements your function call should look like this: | ||
# 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, in_arg.arch, True, True) | ||
|
||
# TODO 0: Measure total program runtime by collecting end time | ||
end_time = time() | ||
|
||
# TODO 0: Computes overall runtime in seconds & prints it in hh:mm:ss format | ||
tot_time = end_time - start_time | ||
print("\n** Total Elapsed Runtime:", | ||
str(int((tot_time/3600)))+":"+str(int((tot_time%3600)/60))+":" | ||
+str(int((tot_time%3600)%60)) ) | ||
|
||
|
||
# Call to main function to run the program | ||
if __name__ == "__main__": | ||
main() |
Oops, something went wrong.