Repository: udacity/AIPND-revision Branch: master Commit: b0c405fea09c Files: 29 Total size: 140.2 KB Directory structure: gitextract_n5fr_3b6/ ├── .gitignore ├── CODEOWNERS ├── LICENSE ├── README.md ├── intropyproject-classify-pet-images/ │ ├── adjust_results4_isadog.py │ ├── adjust_results4_isadog_hints.py │ ├── calculates_results_stats.py │ ├── calculates_results_stats_hints.py │ ├── check_images.py │ ├── classifier.py │ ├── classify_images.py │ ├── classify_images_hints.py │ ├── create_images.txt │ ├── dognames.txt │ ├── get_input_args.py │ ├── get_input_args_hints.py │ ├── get_pet_labels.py │ ├── get_pet_labels_hints.py │ ├── imagenet1000_clsid_to_human.txt │ ├── print_functions_for_lab_checks.py │ ├── print_results.py │ ├── print_results_hints.py │ ├── run_models_batch.bat │ ├── run_models_batch.sh │ ├── run_models_batch_uploaded.bat │ ├── run_models_batch_uploaded.sh │ └── test_classifier.py ├── notes/ │ └── project_intro-to-python.md └── requirements.txt ================================================ FILE CONTENTS ================================================ ================================================ FILE: .gitignore ================================================ # Byte-compiled / optimized / DLL files __pycache__/ *.py[cod] *$py.class # C extensions *.so # Distribution / packaging .Python env/ build/ develop-eggs/ dist/ downloads/ eggs/ .eggs/ lib/ lib64/ parts/ sdist/ var/ wheels/ *.egg-info/ .installed.cfg *.egg # PyInstaller # Usually these files are written by a python script from a template # before PyInstaller builds the exe, so as to inject date/other infos into it. *.manifest *.spec # Installer logs pip-log.txt pip-delete-this-directory.txt # Unit test / coverage reports htmlcov/ .tox/ .coverage .coverage.* .cache nosetests.xml coverage.xml *.cover .hypothesis/ # Translations *.mo *.pot # Django stuff: *.log local_settings.py # Flask stuff: instance/ .webassets-cache # Scrapy stuff: .scrapy # Sphinx documentation docs/_build/ # PyBuilder target/ # Jupyter Notebook .ipynb_checkpoints # pyenv .python-version # celery beat schedule file celerybeat-schedule # SageMath parsed files *.sage.py # dotenv .env # virtualenv .venv venv/ ENV/ # Spyder project settings .spyderproject .spyproject # Rope project settings .ropeproject # mkdocs documentation /site # mypy .mypy_cache/ # mac osx .DS_Store .github/** ================================================ FILE: CODEOWNERS ================================================ * @udacity/active-public-content ================================================ FILE: LICENSE ================================================ MIT License Copyright (c) 2018 Udacity Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. ================================================ FILE: README.md ================================================ # AIPND-revision This repository contains _REVISED_ code and associated files for the AI Programming with Python Nanodegree program. This repository consists of a number of tutorial notebooks for various coding exercises and programming labs that will be used to supplement the lessons of the course. ## Table Of Contents ### Tutorial Notebooks * No revisions ### Programming Project * [Intro to Python Project - Classifying Pet Images:](https://github.com/udacity/AIPND-revision/tree/master/intropyproject-classify-pet-images "Classifying Pet Images Project") Determine which CNN architecture model works best at classifying images of dogs and their breeds. ### NumPy and Pandas Mini-Projects * No revisions ### Matplotlib * No revisions ### Quiz Notes * [Notes:](https://github.com/udacity/AIPND-revision/tree/master/notes "Notes") This directory contains more information about certain quizzes that are testing more challenging concepts. Additionally, one will find the [Frequently Asked Questions](https://github.com/udacity/AIPND-revision/blob/master/notes/project_intro-to-python.md) for the _Intro to Python Project_. Click on the filename to view the contents of the notes on a _quiz_ or the _Intro to Python Project_. ## Dependencies Each directory has a `requirements.txt` describing the minimal dependencies required to run the notebooks in that directory. ### pip To install these dependencies with pip, you can issue `pip3 install -r requirements.txt`. ================================================ FILE: intropyproject-classify-pet-images/adjust_results4_isadog.py ================================================ #!/usr/bin/env python3 # -*- coding: utf-8 -*- # */AIPND-revision/intropyproject-classify-pet-images/adjust_results4_isadog.py # # PROGRAMMER: # DATE CREATED: # 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. """ None ================================================ FILE: intropyproject-classify-pet-images/adjust_results4_isadog_hints.py ================================================ #!/usr/bin/env python3 # -*- coding: utf-8 -*- # */AIPND-revision/intropyproject-classify-pet-images/adjust_results4_isadog_hints.py # # PROGRAMMER: # DATE CREATED: # REVISED DATE: # PURPOSE: This is a *hints* file to help guide students in creating the # 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: EDIT and ADD code BELOW to do the following that's stated in the # comments below that start with "TODO: 4" for the adjust_results4_isadog # function. Specifically EDIT and ADD code to define 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. """ # Creates dognames dictionary for quick matching to results_dic labels from # real answer & classifier's answer dognames_dic = dict() # Reads in dognames from file, 1 name per line & automatically closes file with open(dogfile, "r") as infile: # Reads in dognames from first line in file line = infile.readline() # Processes each line in file until reaching EOF (end-of-file) by # processing line and adding dognames to dognames_dic with while loop while line != "": # TODO: 4a. REPLACE pass with CODE to remove the newline character # from the variable line # # Process line by striping newline from line pass # TODO: 4b. REPLACE pass with CODE to check if the dogname(line) # exists within dognames_dic, then if the dogname(line) # doesn't exist within dognames_dic then add the dogname(line) # to dognames_dic as the 'key' with the 'value' of 1. # # adds dogname(line) to dogsnames_dic if it doesn't already exist # in the dogsnames_dic dictionary pass # Reads in next line in file to be processed with while loop # if this line isn't empty (EOF) line = infile.readline() # Add to whether pet labels & classifier labels are dogs by appending # two items to end of value(List) in results_dic. # List Index 3 = whether(1) or not(0) Pet Image Label is a dog AND # List Index 4 = whether(1) or not(0) Classifier Label is a dog # How - iterate through results_dic if labels are found in dognames_dic # then label "is a dog" index3/4=1 otherwise index3/4=0 "not a dog" for key in results_dic: # Pet Image Label IS of Dog (e.g. found in dognames_dic) if results_dic[key][0] in dognames_dic: # Classifier Label IS image of Dog (e.g. found in dognames_dic) # appends (1, 1) because both labels are dogs if results_dic[key][1] in dognames_dic: results_dic[key].extend((1, 1)) # TODO: 4c. REPLACE pass BELOW with CODE that adds the following to # results_dic dictionary for the key indicated by the # variable key - append (1,0) to the value using # the extend list function. This indicates # the pet label is-a-dog, classifier label is-NOT-a-dog. # # Classifier Label IS NOT image of dog (e.g. NOT in dognames_dic) # appends (1,0) because only pet label is a dog else: pass # Pet Image Label IS NOT a Dog image (e.g. NOT found in dognames_dic) else: # TODO: 4d. REPLACE pass BELOW with CODE that adds the following to # results_dic dictionary for the key indicated by the # variable key - append (0,1) to the value uisng # the extend list function. This indicates # the pet label is-NOT-a-dog, classifier label is-a-dog. # # Classifier Label IS image of Dog (e.g. found in dognames_dic) # appends (0, 1)because only Classifier labe is a dog if results_dic[key][1] in dognames_dic: pass # TODO: 4e. REPLACE pass BELOW with CODE that adds the following to # results_dic dictionary for the key indicated by the # variable key - append (0,0) to the value using the # extend list function. This indicates # the pet label is-NOT-a-dog, classifier label is-NOT-a-dog. # # Classifier Label IS NOT image of Dog (e.g. NOT in dognames_dic) # appends (0, 0) because both labels aren't dogs else: pass ================================================ FILE: intropyproject-classify-pet-images/calculates_results_stats.py ================================================ #!/usr/bin/env python3 # -*- coding: utf-8 -*- # */AIPND-revision/intropyproject-classify-pet-images/calculates_results_stats.py # # PROGRAMMER: # DATE CREATED: # 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 classroom Item XX Calculating Results for details on how to calculate the counts and statistics. """ # Replace None with the results_stats_dic dictionary that you created with # this function return None ================================================ FILE: intropyproject-classify-pet-images/calculates_results_stats_hints.py ================================================ #!/usr/bin/env python3 # -*- coding: utf-8 -*- # */AIPND-revision/intropyproject-classify-pet-images/calculates_results_stats_hints.py # # PROGRAMMER: # DATE CREATED: # REVISED DATE: # PURPOSE: This is a *hints* file to help guide students in creating the # 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: EDIT and ADD code BELOW to do the following that's stated in the # comments below that start with "TODO: 5" for the calculates_results_stats # function. 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 classroom Item XX Calculating Results for details on how to calculate the counts and statistics. """ # Creates empty dictionary for results_stats_dic results_stats_dic = dict() # Sets all counters to initial values of zero so that they can # be incremented while processing through the images in results_dic 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 # process through the results dictionary for key in results_dic: # Labels Match Exactly if results_dic[key][2] == 1: results_stats_dic['n_match'] += 1 # TODO: 5a. REPLACE pass with CODE that counts how many pet images of # dogs had their breed correctly classified. This happens # when the pet image label indicates the image is-a-dog AND # the pet image label and the classifier label match. You # will need to write a conditional statement that determines # when the dog breed is correctly classified and then # increments 'n_correct_breed' by 1. Recall 'n_correct_breed' # is a key in the results_stats_dic dictionary with it's value # representing the number of correctly classified dog breeds. # # Pet Image Label is a Dog AND Labels match- counts Correct Breed pass # Pet Image Label is a Dog - counts number of dog images if results_dic[key][3] == 1: results_stats_dic['n_dogs_img'] += 1 # Classifier classifies image as Dog (& pet image is a dog) # counts number of correct dog classifications if results_dic[key][4] == 1: results_stats_dic['n_correct_dogs'] += 1 # TODO: 5b. REPLACE pass with CODE that counts how many pet images # that are NOT dogs were correctly classified. This happens # when the pet image label indicates the image is-NOT-a-dog # AND the classifier label indicates the images is-NOT-a-dog. # You will need to write a conditional statement that # determines when the classifier label indicates the image # is-NOT-a-dog and then increments 'n_correct_notdogs' by 1. # Recall the 'else:' above 'pass' already indicates that the # pet image label indicates the image is-NOT-a-dog and # 'n_correct_notdogs' is a key in the results_stats_dic dictionary # with it's value representing the number of correctly # classified NOT-a-dog images. # # Pet Image Label is NOT a Dog else: # Classifier classifies image as NOT a Dog(& pet image isn't a dog) # counts number of correct NOT dog clasifications. pass # Calculates run statistics (counts & percentages) below that are calculated # using the counters from above. # calculates number of total images results_stats_dic['n_images'] = len(results_dic) # calculates number of not-a-dog images using - images & dog images counts results_stats_dic['n_notdogs_img'] = (results_stats_dic['n_images'] - results_stats_dic['n_dogs_img']) # TODO: 5c. REPLACE zero(0.0) with CODE that calculates the % of correctly # matched images. Recall that this can be calculated by the # number of correctly matched images ('n_match') divided by the # number of images('n_images'). This result will need to be # multiplied by 100.0 to provide the percentage. # # Calculates % correct for matches results_stats_dic['pct_match'] = 0.0 # TODO: 5d. REPLACE zero(0.0) with CODE that calculates the % of correctly # classified dog images. Recall that this can be calculated by # the number of correctly classified dog images('n_correct_dogs') # divided by the number of dog images('n_dogs_img'). This result # will need to be multiplied by 100.0 to provide the percentage. # # Calculates % correct dogs results_stats_dic['pct_correct_dogs'] = 0.0 # TODO: 5e. REPLACE zero(0.0) with CODE that calculates the % of correctly # classified breeds of dogs. Recall that this can be calculated # by the number of correctly classified breeds of dog('n_correct_breed') # divided by the number of dog images('n_dogs_img'). This result # will need to be multiplied by 100.0 to provide the percentage. # # Calculates % correct breed of dog results_stats_dic['pct_correct_breed'] = 0.0 # Calculates % correct not-a-dog images # Uses conditional statement for when no 'not a dog' images were submitted 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 # TODO 5f. REPLACE None with the results_stats_dic dictionary that you # created with this function return None ================================================ FILE: intropyproject-classify-pet-images/check_images.py ================================================ #!/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: # DATE CREATED: # 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 --arch # --dogfile # Example call: # python check_images_solution.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(None) # 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 classify_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(None, results, None) # 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, None) # 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, None, 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() ================================================ FILE: intropyproject-classify-pet-images/classifier.py ================================================ import ast from PIL import Image import torchvision.transforms as transforms from torch.autograd import Variable import torchvision.models as models from torch import __version__ resnet18 = models.resnet18(pretrained=True) alexnet = models.alexnet(pretrained=True) vgg16 = models.vgg16(pretrained=True) models = {'resnet': resnet18, 'alexnet': alexnet, 'vgg': vgg16} # obtain ImageNet labels with open('imagenet1000_clsid_to_human.txt') as imagenet_classes_file: imagenet_classes_dict = ast.literal_eval(imagenet_classes_file.read()) def classifier(img_path, model_name): # load the image img_pil = Image.open(img_path) # define transforms preprocess = transforms.Compose([ transforms.Resize(256), transforms.CenterCrop(224), transforms.ToTensor(), transforms.Normalize(mean=[0.485, 0.456, 0.406], std=[0.229, 0.224, 0.225]) ]) # preprocess the image img_tensor = preprocess(img_pil) # resize the tensor (add dimension for batch) img_tensor.unsqueeze_(0) # wrap input in variable, wrap input in variable - no longer needed for # v 0.4 & higher code changed 04/26/2018 by Jennifer S. to handle PyTorch upgrade pytorch_ver = __version__.split('.') # pytorch versions 0.4 & hihger - Variable depreciated so that it returns # a tensor. So to address tensor as output (not wrapper) and to mimic the # affect of setting volatile = True (because we are using pretrained models # for inference) we can set requires_gradient to False. Here we just set # requires_grad_ to False on our tensor if int(pytorch_ver[0]) > 0 or int(pytorch_ver[1]) >= 4: img_tensor.requires_grad_(False) # pytorch versions less than 0.4 - uses Variable because not-depreciated else: # apply model to input # wrap input in variable data = Variable(img_tensor, volatile = True) # apply model to input model = models[model_name] # puts model in evaluation mode # instead of (default)training mode model = model.eval() # apply data to model - adjusted based upon version to account for # operating on a Tensor for version 0.4 & higher. if int(pytorch_ver[0]) > 0 or int(pytorch_ver[1]) >= 4: output = model(img_tensor) # pytorch versions less than 0.4 else: # apply data to model output = model(data) # return index corresponding to predicted class pred_idx = output.data.numpy().argmax() return imagenet_classes_dict[pred_idx] ================================================ FILE: intropyproject-classify-pet-images/classify_images.py ================================================ #!/usr/bin/env python3 # -*- coding: utf-8 -*- # */AIPND-revision/intropyproject-classify-pet-images/classify_images.py # # PROGRAMMER: # DATE CREATED: # REVISED DATE: # PURPOSE: Create a function classify_images that uses the classifier function # to create the classifier labels and then compares the classifier # labels to the pet image labels. This function inputs: # -The Image Folder as image_dir within classify_images and function # and as in_arg.dir for function call within main. # -The results dictionary as results_dic within classify_images # function and results for the functin call within main. # -The CNN model architecture as model within classify_images function # and in_arg.arch 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 # classifier label as the item at index 1 of the list and the comparison # of the pet and classifier labels as the item at index 2 of the list. # ## # Imports classifier function for using CNN to classify images from classifier import classifier # TODO 3: Define classify_images function below, specifically replace the None # below by the function definition of the classify_images 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 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 format the classifier labels so that they will match your pet image labels. The format will include putting the classifier labels in all lower case letters and strip the leading and trailing whitespace characters from them. For example, the Classifier function returns = 'Maltese dog, Maltese terrier, Maltese' so the classifier label = 'maltese dog, maltese terrier, maltese'. Recall that 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. For example, you will find pet images of a 'dalmatian'(pet label) and it will match to the classifier label 'dalmatian, coach dog, carriage dog' if the classifier function correctly classified the pet images of dalmatians. PLEASE NOTE: This function uses the classifier() function defined in classifier.py within this function. The proper use of this function is in test_classifier.py Please refer to this program prior to using the classifier() function to classify images within this function Parameters: images_dir - The (full) path to the folder of images that are to be classified by the classifier function (string) results_dic - Results 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) --- where index 1 & index 2 are added by this function --- NEW - index 1 = classifier label (string) NEW - index 2 = 1/0 (int) where 1 = match between pet image and classifer labels and 0 = no match between labels 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) Returns: None - results_dic is mutable data type so no return needed. """ None ================================================ FILE: intropyproject-classify-pet-images/classify_images_hints.py ================================================ #!/usr/bin/env python3 # -*- coding: utf-8 -*- # */AIPND-revision/intropyproject-classify-pet-images/classify_images_hints.py # # PROGRAMMER: # DATE CREATED: # REVISED DATE: # PURPOSE: This is a *hints* file to help guide students in creating the # function classify_images that uses the classifier function # to create the classifier labels and then compares the classifier # labels to the pet image labels. This function inputs: # -The Image Folder as image_dir within classify_images and function # and as in_arg.dir for function call within main. # -The results dictionary as results_dic within classify_images # function and results for the functin call within main. # -The CNN model architecture as model within classify_images function # and in_arg.arch 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 # classifier label as the item at index 1 of the list and the comparison # of the pet and classifier labels as the item at index 2 of the list. # ## # Imports classifier function for using CNN to classify images from classifier import classifier # TODO 3: EDIT and ADD code BELOW to do the following that's stated in the # comments below that start with "TODO: 3" for the classify_images function # Specifically EDIT and ADD code to define the classify_images 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 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 format the classifier labels so that they will match your pet image labels. The format will include putting the classifier labels in all lower case letters and strip the leading and trailing whitespace characters from them. For example, the Classifier function returns = 'Maltese dog, Maltese terrier, Maltese' so the classifier label = 'maltese dog, maltese terrier, maltese'. Recall that 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. For example, you will find pet images of a 'dalmatian'(pet label) and it will match to the classifier label 'dalmatian, coach dog, carriage dog' if the classifier function correctly classified the pet images of dalmatians. PLEASE NOTE: This function uses the classifier() function defined in classifier.py within this function. The proper use of this function is in test_classifier.py Please refer to this program prior to using the classifier() function to classify images within this function Parameters: images_dir - The (full) path to the folder of images that are to be classified by the classifier function (string) results_dic - Results 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) --- where index 1 & index 2 are added by this function --- NEW - index 1 = classifier label (string) NEW - index 2 = 1/0 (int) where 1 = match between pet image and classifer labels and 0 = no match between labels 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) Returns: None - results_dic is mutable data type so no return needed. """ # Process all files in the results_dic - use images_dir to give fullpath # that indicates the folder and the filename (key) to be used in the # classifier function for key in results_dic: # TODO: 3a. Set the string variable model_label to be the string that's # returned from using the classifier function instead of the # empty string below. # # Runs classifier function to classify the images classifier function # inputs: path + filename and model, returns model_label # as classifier label model_label = "" # TODO: 3b. BELOW REPLACE pass with CODE to process the model_label to # convert all characters within model_label to lowercase # letters and then remove whitespace characters from the ends # of model_label. Be certain the resulting processed string # is named model_label. # # Processes the results so they can be compared with pet image labels # set labels to lowercase (lower) and stripping off whitespace(strip) pass # defines truth as pet image label truth = results_dic[key][0] # TODO: 3c. REPLACE pass BELOW with CODE that uses the extend list function # to add the classifier label (model_label) and the value of # 1 (where the value of 1 indicates a match between pet image # label and the classifier label) to the results_dic dictionary # for the key indicated by the variable key # # If the pet image label is found within the classifier label list of terms # as an exact match to on of the terms in the list - then they are added to # results_dic as an exact match(1) using extend list function if truth in model_label: pass # TODO: 3d. REPLACE pass BELOW with CODE that uses the extend list function # to add the classifier label (model_label) and the value of # 0 (where the value of 0 indicates NOT a match between the pet # image label and the classifier label) to the results_dic # dictionary for the key indicated by the variable key # # if not found then added to results dictionary as NOT a match(0) using # the extend function else: pass ================================================ FILE: intropyproject-classify-pet-images/create_images.txt ================================================ Questions regarding Uploaded Image Classification: 1. Did the three model architectures classify the breed of dog in Dog_01.jpg to be the same breed? If not, report the differences in the classifications. Answer: 2. Did each of the three model architectures classify the breed of dog in Dog_01.jpg to be the same breed of dog as that model architecture classified Dog_02.jpg? If not, report the differences in the classifications. Answer: 3. Did the three model architectures correctly classify Animal_Name_01.jpg and Object_Name_01.jpg to not be dogs? If not, report the misclassifications. Answer: 4. Based upon your answers for questions 1. - 3. above, select the model architecture that you feel did the best at classifying the four uploaded images. Describe why you selected that model architecture as the best on uploaded image classification. Answer: ================================================ FILE: intropyproject-classify-pet-images/dognames.txt ================================================ chihuahua japanese spaniel maltese dog, maltese terrier, maltese pekinese, pekingese, peke shih-tzu blenheim spaniel papillon toy terrier rhodesian ridgeback afghan hound, afghan basset, basset hound beagle bloodhound, sleuthhound bluetick black-and-tan coonhound walker hound, walker foxhound english foxhound redbone borzoi, russian wolfhound irish wolfhound italian greyhound whippet ibizan hound, ibizan podenco norwegian elkhound, elkhound otterhound, otter hound saluki, gazelle hound scottish deerhound, deerhound weimaraner staffordshire bullterrier, staffordshire bull terrier american staffordshire terrier, staffordshire terrier, american pit bull terrier, pit bull terrier bedlington terrier border terrier kerry blue terrier irish terrier norfolk terrier norwich terrier yorkshire terrier wire-haired fox terrier lakeland terrier sealyham terrier, sealyham airedale, airedale terrier cairn, cairn terrier australian terrier dandie dinmont, dandie dinmont terrier boston bull, boston terrier miniature schnauzer giant schnauzer standard schnauzer, schnauzer scotch terrier, scottish terrier, scottie tibetan terrier, chrysanthemum dog silky terrier, sydney silky soft-coated wheaten terrier west highland white terrier lhasa, lhasa apso flat-coated retriever curly-coated retriever golden retriever labrador retriever chesapeake bay retriever german shorthaired pointer vizsla, hungarian pointer english setter irish setter, red setter gordon setter brittany spaniel clumber, clumber spaniel english springer, english springer spaniel welsh springer spaniel cocker spaniel, english cocker spaniel, cocker sussex spaniel irish water spaniel kuvasz schipperke groenendael malinois briard kelpie komondor old english sheepdog, bobtail shetland sheepdog, shetland sheep dog, shetland collie border collie bouvier des flandres, bouviers des flandres rottweiler german shepherd, german shepherd dog, german police dog, alsatian doberman, doberman pinscher miniature pinscher greater swiss mountain dog bernese mountain dog appenzeller entlebucher boxer bull mastiff tibetan mastiff french bulldog great dane saint bernard, st bernard eskimo dog, husky malamute, malemute, alaskan malamute siberian husky dalmatian, coach dog, carriage dog affenpinscher, monkey pinscher, monkey dog basenji pug, pug-dog leonberg newfoundland, newfoundland dog great pyrenees samoyed, samoyede pomeranian chow, chow chow keeshond brabancon griffon pembroke, pembroke welsh corgi, corgi cardigan, cardigan welsh corgi, corgi toy poodle miniature poodle standard poodle, poodle mexican hairless affenpinscher afghan hound airedale terrier akita alaskan malamute american eskimo dog american foxhound american staffordshire terrier american water spaniel anatolian shepherd dog australian cattle dog australian shepherd basset hound bearded collie beauceron belgian malinois belgian sheepdog belgian tervuren bichon frise black and tan coonhound black russian terrier bloodhound bluetick coonhound borzoi boston terrier bouvier des flandres boykin spaniel brittany brussels griffon bull terrier bulldog bullmastiff cairn terrier canaan dog cane corso cardigan welsh corgi cavalier king charles spaniel chinese crested chinese shar-pei chow chow clumber spaniel cocker spaniel corgi dachshund dalmatian dandie dinmont terrier deerhound doberman pinscher dogue de bordeaux english cocker spaniel english springer spaniel english toy spaniel entlebucher mountain dog field spaniel finnish spitz german pinscher german shepherd dog german wirehaired pointer glen of imaal terrier greyhound havanese ibizan hound icelandic sheepdog irish red and white setter irish setter japanese chin leonberger lhasa apso lowchen maltese manchester terrier mastiff neapolitan mastiff newfoundland norwegian buhund norwegian elkhound norwegian lundehund nova scotia duck tolling retriever old english sheepdog otterhound parson russell terrier pekingese pembroke welsh corgi petit basset griffon vendeen pharaoh hound plott pointer poodle portuguese water dog pug saint bernard saluki samoyed schnauzer scottish terrier sealyham terrier shetland sheepdog silky terrier smooth fox terrier staffordshire bull terrier tibetan terrier vizsla walker hound wirehaired pointing griffon xoloitzcuintli dog ================================================ FILE: intropyproject-classify-pet-images/get_input_args.py ================================================ #!/usr/bin/env python3 # -*- coding: utf-8 -*- # */AIPND-revision/intropyproject-classify-pet-images/get_input_args.py # # PROGRAMMER: # DATE CREATED: # REVISED DATE: # PURPOSE: Create a function that retrieves the following 3 command line inputs # from the user using the Argparse Python module. If the user fails to # provide some or all of the 3 inputs, then the default values are # used for the missing inputs. Command Line Arguments: # 1. Image Folder as --dir with default value 'pet_images' # 2. CNN Model Architecture as --arch with default value 'vgg' # 3. Text File with Dog Names as --dogfile with default value 'dognames.txt' # ## # Imports python modules import argparse # TODO 1: Define get_input_args function below please be certain to replace None # in the return statement with parser.parse_args() parsed argument # collection that you created with this function # def get_input_args(): """ Retrieves and parses the 3 command line arguments provided by the user when they run the program from a terminal window. This function uses Python's argparse module to created and defined these 3 command line arguments. If the user fails to provide some or all of the 3 arguments, then the default values are used for the missing arguments. Command Line Arguments: 1. Image Folder as --dir with default value 'pet_images' 2. CNN Model Architecture as --arch with default value 'vgg' 3. Text File with Dog Names as --dogfile with default value 'dognames.txt' This function returns these arguments as an ArgumentParser object. Parameters: None - simply using argparse module to create & store command line arguments Returns: parse_args() -data structure that stores the command line arguments object """ # Replace None with parser.parse_args() parsed argument collection that # you created with this function return None ================================================ FILE: intropyproject-classify-pet-images/get_input_args_hints.py ================================================ #!/usr/bin/env python3 # -*- coding: utf-8 -*- # */AIPND-revision/intropyproject-classify-pet-images/get_input_args_hints.py # # PROGRAMMER: # DATE CREATED: # REVISED DATE: # PURPOSE: This is a *hints* file to help guide students in creating the # function that retrieves the following 3 command line inputs from # the user using the Argparse Python module. If the user fails to # provide some or all of the 3 inputs, then the default values are # used for the missing inputs. Command Line Arguments: # 1. Image Folder as --dir with default value 'pet_images' # 2. CNN Model Architecture as --arch with default value 'vgg' # 3. Text File with Dog Names as --dogfile with default value 'dognames.txt' # ## # Imports python modules import argparse # TODO 1: EDIT and ADD code BELOW to do the following that's stated in the # comments below that start with "TODO: 1" for the get_input_args function # Please be certain to replace None in the return statement with # parser.parse_args() parsed argument collection that you created with # this function # def get_input_args(): """ Retrieves and parses the 3 command line arguments provided by the user when they run the program from a terminal window. This function uses Python's argparse module to created and defined these 3 command line arguments. If the user fails to provide some or all of the 3 arguments, then the default values are used for the missing arguments. Command Line Arguments: 1. Image Folder as --dir with default value 'pet_images' 2. CNN Model Architecture as --arch with default value 'vgg' 3. Text File with Dog Names as --dogfile with default value 'dognames.txt' This function returns these arguments as an ArgumentParser object. Parameters: None - simply using argparse module to create & store command line arguments Returns: parse_args() -data structure that stores the command line arguments object """ # Creates parse parser = argparse.ArgumentParser() # Creates 3 command line arguments args.dir for path to images files, # args.arch which CNN model to use for classification, args.labels path to # text file with names of dogs. parser.add_argument('--dir', type=str, default='pet_images/', help='path to folder of images') # TODO: 1a. EDIT parse.add_argument statements BELOW to add type & help for: # --arch - the CNN model architecture # --dogfile - text file of names of dog breeds parser.add_argument('--arch', default = 'vgg' ) parser.add_argument('--dogfile', default = 'dognames.txt' ) # TODO: 1b. Replace None with parser.parse_args() parsed argument # collection that you created with this function return None ================================================ FILE: intropyproject-classify-pet-images/get_pet_labels.py ================================================ #!/usr/bin/env python3 # -*- coding: utf-8 -*- # */AIPND-revision/intropyproject-classify-pet-images/get_pet_labels.py # # PROGRAMMER: # DATE CREATED: # REVISED DATE: # PURPOSE: Create the function get_pet_labels that creates the pet labels from # the image's filename. This function inputs: # - The Image Folder as image_dir within get_pet_labels function and # as in_arg.dir for the function call within the main function. # This function creates and returns the results dictionary as results_dic # within get_pet_labels function and as results within main. # The results_dic dictionary has a 'key' that's the image filename and # a 'value' that's a list. This list will contain the following item # at index 0 : pet image label (string). # ## # Imports python modules from os import listdir # TODO 2: Define get_pet_labels function below please be certain to replace None # in the return statement with results_dic dictionary that you create # with this function # def get_pet_labels(image_dir): """ Creates a dictionary of pet labels (results_dic) based upon the filenames of the image files. These pet image labels are used to check the accuracy of the labels that are returned by the classifier function, since the filenames of the images contain the true identity of the pet in the image. Be sure to format the pet labels so that they are in all lower case letters and with leading and trailing whitespace characters stripped from them. (ex. filename = 'Boston_terrier_02259.jpg' Pet label = 'boston terrier') Parameters: image_dir - The (full) path to the folder of images that are to be classified by the classifier function (string) Returns: results_dic - Dictionary with 'key' as image filename and 'value' as a 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 ================================================ FILE: intropyproject-classify-pet-images/get_pet_labels_hints.py ================================================ #!/usr/bin/env python3 # -*- coding: utf-8 -*- # */AIPND-revision/intropyproject-classify-pet-images/get_pet_labels_hints.py # # PROGRAMMER: # DATE CREATED: # REVISED DATE: # PURPOSE: This is a *hints* file to help guide students in creating the # function get_pet_labels that creates the pet labels from the image's # filename. This function inputs: # - The Image Folder as image_dir within get_pet_labels function and # as in_arg.dir for the function call within the main function. # This function creates and returns the results dictionary as results_dic # within get_pet_labels function and as results within main. # The results_dic dictionary has a 'key' that's the image filename and # a 'value' that's a list. This list will contain the following item # at index 0 : pet image label (string). # ## # Imports python modules from os import listdir # TODO 2: EDIT and ADD code BELOW to do the following that's stated in the # comments below that start with "TODO: 2" for the get_pet_labels function # Please be certain to replace None in the return statement with # results_dic dictionary that you create with this function # def get_pet_labels(image_dir): """ Creates a dictionary of pet labels (results_dic) based upon the filenames of the image files. These pet image labels are used to check the accuracy of the labels that are returned by the classifier function, since the filenames of the images contain the true identity of the pet in the image. Be sure to format the pet labels so that they are in all lower case letters and with leading and trailing whitespace characters stripped from them. (ex. filename = 'Boston_terrier_02259.jpg' Pet label = 'boston terrier') Parameters: image_dir - The (full) path to the folder of images that are to be classified by the classifier function (string) Returns: results_dic - Dictionary with 'key' as image filename and 'value' as a List. The list contains for following item: index 0 = pet image label (string) """ # Creates list of files in directory in_files = listdir(image_dir) # Processes each of the files to create a dictionary where the key # is the filename and the value is the picture label (below). # Creates empty dictionary for the results (pet labels, etc.) results_dic = dict() # Processes through each file in the directory, extracting only the words # of the file that contain the pet image label for idx in range(0, len(in_files), 1): # Skips file if starts with . (like .DS_Store of Mac OSX) because it # isn't an pet image file if in_files[idx][0] != ".": # Creates temporary label variable to hold pet label name extracted pet_label = "" # TODO: 2a. BELOW REPLACE pass with CODE that will process each # filename in the in_files list to extract the dog breed # name from the filename. Recall that each filename can be # accessed by in_files[idx]. Be certain to place the # extracted dog breed name in the variable pet_label # that's created as an empty string ABOVE pass # If filename doesn't already exist in dictionary add it and it's # pet label - otherwise print an error message because indicates # duplicate files (filenames) if in_files[idx] not in results_dic: results_dic[in_files[idx]] = [pet_label] else: print("** Warning: Duplicate files exist in directory:", in_files[idx]) # TODO 2b. Replace None with the results_dic dictionary that you created # with this function return None ================================================ FILE: intropyproject-classify-pet-images/imagenet1000_clsid_to_human.txt ================================================ {0: 'tench, Tinca tinca', 1: 'goldfish, Carassius auratus', 2: 'great white shark, white shark, man-eater, man-eating shark, Carcharodon carcharias', 3: 'tiger shark, Galeocerdo cuvieri', 4: 'hammerhead, hammerhead shark', 5: 'electric ray, crampfish, numbfish, torpedo', 6: 'stingray', 7: 'cock', 8: 'hen', 9: 'ostrich, Struthio camelus', 10: 'brambling, Fringilla montifringilla', 11: 'goldfinch, Carduelis carduelis', 12: 'house finch, linnet, Carpodacus mexicanus', 13: 'junco, snowbird', 14: 'indigo bunting, indigo finch, indigo bird, Passerina cyanea', 15: 'robin, American robin, Turdus migratorius', 16: 'bulbul', 17: 'jay', 18: 'magpie', 19: 'chickadee', 20: 'water ouzel, dipper', 21: 'kite', 22: 'bald eagle, American eagle, Haliaeetus leucocephalus', 23: 'vulture', 24: 'great grey owl, great gray owl, Strix nebulosa', 25: 'European fire salamander, Salamandra salamandra', 26: 'common newt, Triturus vulgaris', 27: 'eft', 28: 'spotted salamander, Ambystoma maculatum', 29: 'axolotl, mud puppy, Ambystoma mexicanum', 30: 'bullfrog, Rana catesbeiana', 31: 'tree frog, tree-frog', 32: 'tailed frog, bell toad, ribbed toad, tailed toad, Ascaphus trui', 33: 'loggerhead, loggerhead turtle, Caretta caretta', 34: 'leatherback turtle, leatherback, leathery turtle, Dermochelys coriacea', 35: 'mud turtle', 36: 'terrapin', 37: 'box turtle, box tortoise', 38: 'banded gecko, gecko', 39: 'common iguana, iguana, Iguana iguana', 40: 'American chameleon, anole, Anolis carolinensis', 41: 'whiptail, whiptail lizard', 42: 'agama', 43: 'frilled lizard, Chlamydosaurus kingi', 44: 'alligator lizard', 45: 'Gila monster, Heloderma suspectum', 46: 'green lizard, Lacerta viridis', 47: 'African chameleon, Chamaeleo chamaeleon', 48: 'Komodo dragon, Komodo lizard, dragon lizard, giant lizard, Varanus komodoensis', 49: 'African crocodile, Nile crocodile, Crocodylus niloticus', 50: 'American alligator, Alligator mississipiensis', 51: 'triceratops', 52: 'thunder snake, worm snake, Carphophis amoenus', 53: 'ringneck snake, ring-necked snake, ring snake', 54: 'hognose snake, puff adder, sand viper', 55: 'green snake, grass snake', 56: 'king snake, kingsnake', 57: 'garter snake, grass snake', 58: 'water snake', 59: 'vine snake', 60: 'night snake, Hypsiglena torquata', 61: 'boa constrictor, Constrictor constrictor', 62: 'rock python, rock snake, Python sebae', 63: 'Indian cobra, Naja naja', 64: 'green mamba', 65: 'sea snake', 66: 'horned viper, cerastes, sand viper, horned asp, Cerastes cornutus', 67: 'diamondback, diamondback rattlesnake, Crotalus adamanteus', 68: 'sidewinder, horned rattlesnake, Crotalus cerastes', 69: 'trilobite', 70: 'harvestman, daddy longlegs, Phalangium opilio', 71: 'scorpion', 72: 'black and gold garden spider, Argiope aurantia', 73: 'barn spider, Araneus cavaticus', 74: 'garden spider, Aranea diademata', 75: 'black widow, Latrodectus mactans', 76: 'tarantula', 77: 'wolf spider, hunting spider', 78: 'tick', 79: 'centipede', 80: 'black grouse', 81: 'ptarmigan', 82: 'ruffed grouse, partridge, Bonasa umbellus', 83: 'prairie chicken, prairie grouse, prairie fowl', 84: 'peacock', 85: 'quail', 86: 'partridge', 87: 'African grey, African gray, Psittacus erithacus', 88: 'macaw', 89: 'sulphur-crested cockatoo, Kakatoe galerita, Cacatua galerita', 90: 'lorikeet', 91: 'coucal', 92: 'bee eater', 93: 'hornbill', 94: 'hummingbird', 95: 'jacamar', 96: 'toucan', 97: 'drake', 98: 'red-breasted merganser, Mergus serrator', 99: 'goose', 100: 'black swan, Cygnus atratus', 101: 'tusker', 102: 'echidna, spiny anteater, anteater', 103: 'platypus, duckbill, duckbilled platypus, duck-billed platypus, Ornithorhynchus anatinus', 104: 'wallaby, brush kangaroo', 105: 'koala, koala bear, kangaroo bear, native bear, Phascolarctos cinereus', 106: 'wombat', 107: 'jellyfish', 108: 'sea anemone, anemone', 109: 'brain coral', 110: 'flatworm, platyhelminth', 111: 'nematode, nematode worm, roundworm', 112: 'conch', 113: 'snail', 114: 'slug', 115: 'sea slug, nudibranch', 116: 'chiton, coat-of-mail shell, sea cradle, polyplacophore', 117: 'chambered nautilus, pearly nautilus, nautilus', 118: 'Dungeness crab, Cancer magister', 119: 'rock crab, Cancer irroratus', 120: 'fiddler crab', 121: 'king crab, Alaska crab, Alaskan king crab, Alaska king crab, Paralithodes camtschatica', 122: 'American lobster, Northern lobster, Maine lobster, Homarus americanus', 123: 'spiny lobster, langouste, rock lobster, crawfish, crayfish, sea crawfish', 124: 'crayfish, crawfish, crawdad, crawdaddy', 125: 'hermit crab', 126: 'isopod', 127: 'white stork, Ciconia ciconia', 128: 'black stork, Ciconia nigra', 129: 'spoonbill', 130: 'flamingo', 131: 'little blue heron, Egretta caerulea', 132: 'American egret, great white heron, Egretta albus', 133: 'bittern', 134: 'crane', 135: 'limpkin, Aramus pictus', 136: 'European gallinule, Porphyrio porphyrio', 137: 'American coot, marsh hen, mud hen, water hen, Fulica americana', 138: 'bustard', 139: 'ruddy turnstone, Arenaria interpres', 140: 'red-backed sandpiper, dunlin, Erolia alpina', 141: 'redshank, Tringa totanus', 142: 'dowitcher', 143: 'oystercatcher, oyster catcher', 144: 'pelican', 145: 'king penguin, Aptenodytes patagonica', 146: 'albatross, mollymawk', 147: 'grey whale, gray whale, devilfish, Eschrichtius gibbosus, Eschrichtius robustus', 148: 'killer whale, killer, orca, grampus, sea wolf, Orcinus orca', 149: 'dugong, Dugong dugon', 150: 'sea lion', 151: 'Chihuahua', 152: 'Japanese spaniel', 153: 'Maltese dog, Maltese terrier, Maltese', 154: 'Pekinese, Pekingese, Peke', 155: 'Shih-Tzu', 156: 'Blenheim spaniel', 157: 'papillon', 158: 'toy terrier', 159: 'Rhodesian ridgeback', 160: 'Afghan hound, Afghan', 161: 'basset, basset hound', 162: 'beagle', 163: 'bloodhound, sleuthhound', 164: 'bluetick', 165: 'black-and-tan coonhound', 166: 'Walker hound, Walker foxhound', 167: 'English foxhound', 168: 'redbone', 169: 'borzoi, Russian wolfhound', 170: 'Irish wolfhound', 171: 'Italian greyhound', 172: 'whippet', 173: 'Ibizan hound, Ibizan Podenco', 174: 'Norwegian elkhound, elkhound', 175: 'otterhound, otter hound', 176: 'Saluki, gazelle hound', 177: 'Scottish deerhound, deerhound', 178: 'Weimaraner', 179: 'Staffordshire bullterrier, Staffordshire bull terrier', 180: 'American Staffordshire terrier, Staffordshire terrier, American pit bull terrier, pit bull terrier', 181: 'Bedlington terrier', 182: 'Border terrier', 183: 'Kerry blue terrier', 184: 'Irish terrier', 185: 'Norfolk terrier', 186: 'Norwich terrier', 187: 'Yorkshire terrier', 188: 'wire-haired fox terrier', 189: 'Lakeland terrier', 190: 'Sealyham terrier, Sealyham', 191: 'Airedale, Airedale terrier', 192: 'cairn, cairn terrier', 193: 'Australian terrier', 194: 'Dandie Dinmont, Dandie Dinmont terrier', 195: 'Boston bull, Boston terrier', 196: 'miniature schnauzer', 197: 'giant schnauzer', 198: 'standard schnauzer, schnauzer', 199: 'Scotch terrier, Scottish terrier, Scottie', 200: 'Tibetan terrier, chrysanthemum dog', 201: 'silky terrier, Sydney silky', 202: 'soft-coated wheaten terrier', 203: 'West Highland white terrier', 204: 'Lhasa, Lhasa apso', 205: 'flat-coated retriever', 206: 'curly-coated retriever', 207: 'golden retriever', 208: 'Labrador retriever', 209: 'Chesapeake Bay retriever', 210: 'German shorthaired pointer', 211: 'vizsla, Hungarian pointer', 212: 'English setter', 213: 'Irish setter, red setter', 214: 'Gordon setter', 215: 'Brittany spaniel', 216: 'clumber, clumber spaniel', 217: 'English springer, English springer spaniel', 218: 'Welsh springer spaniel', 219: 'cocker spaniel, English cocker spaniel, cocker', 220: 'Sussex spaniel', 221: 'Irish water spaniel', 222: 'kuvasz', 223: 'schipperke', 224: 'groenendael', 225: 'malinois', 226: 'briard', 227: 'kelpie', 228: 'komondor', 229: 'Old English sheepdog, bobtail', 230: 'Shetland sheepdog, Shetland sheep dog, Shetland', 231: 'collie', 232: 'Border collie', 233: 'Bouvier des Flandres, Bouviers des Flandres', 234: 'Rottweiler', 235: 'German shepherd, German shepherd dog, German police dog, alsatian', 236: 'Doberman, Doberman pinscher', 237: 'miniature pinscher', 238: 'Greater Swiss Mountain dog', 239: 'Bernese mountain dog', 240: 'Appenzeller', 241: 'EntleBucher', 242: 'boxer', 243: 'bull mastiff', 244: 'Tibetan mastiff', 245: 'French bulldog', 246: 'Great Dane', 247: 'Saint Bernard, St Bernard', 248: 'Eskimo dog, husky', 249: 'malamute, malemute, Alaskan malamute', 250: 'Siberian husky', 251: 'dalmatian, coach dog, carriage dog', 252: 'affenpinscher, monkey pinscher, monkey dog', 253: 'basenji', 254: 'pug, pug-dog', 255: 'Leonberg', 256: 'Newfoundland, Newfoundland dog', 257: 'Great Pyrenees', 258: 'Samoyed, Samoyede', 259: 'Pomeranian', 260: 'chow, chow chow', 261: 'keeshond', 262: 'Brabancon griffon', 263: 'Pembroke, Pembroke Welsh corgi, corgi', 264: 'Cardigan, Cardigan Welsh corgi, corgi', 265: 'toy poodle', 266: 'miniature poodle', 267: 'standard poodle, poodle', 268: 'Mexican hairless', 269: 'timber wolf, grey wolf, gray wolf, Canis lupus', 270: 'white wolf, Arctic wolf, Canis lupus tundrarum', 271: 'red wolf, maned wolf, Canis rufus, Canis niger', 272: 'coyote, prairie wolf, brush wolf, Canis latrans', 273: 'dingo, warrigal, warragal, Canis dingo', 274: 'dhole, Cuon alpinus', 275: 'African hunting dog, hyena dog, Cape hunting dog, Lycaon pictus', 276: 'hyena, hyaena', 277: 'red fox, Vulpes vulpes', 278: 'kit fox, Vulpes macrotis', 279: 'Arctic fox, white fox, Alopex lagopus', 280: 'grey fox, gray fox, Urocyon cinereoargenteus', 281: 'tabby, tabby cat, cat', 282: 'tiger cat, cat', 283: 'Persian cat, cat', 284: 'Siamese cat, Siamese, cat', 285: 'Egyptian cat, cat', 286: 'cougar, puma, mountain lion, painter, panther, Felis concolor', 287: 'lynx', 288: 'leopard, Panthera pardus', 289: 'snow leopard, ounce, Panthera uncia', 290: 'jaguar, panther, Panthera onca, Felis onca', 291: 'lion, king of beasts, Panthera leo', 292: 'tiger, Panthera tigris', 293: 'cheetah, chetah, Acinonyx jubatus', 294: 'brown bear, bruin, Ursus arctos', 295: 'American black bear, black bear, Ursus americanus, Euarctos americanus', 296: 'ice bear, polar bear, Ursus Maritimus, Thalarctos maritimus', 297: 'sloth bear, Melursus ursinus, Ursus ursinus', 298: 'mongoose', 299: 'meerkat, mierkat', 300: 'tiger beetle', 301: 'ladybug, ladybeetle, lady beetle, ladybird, ladybird beetle', 302: 'ground beetle, carabid beetle', 303: 'long-horned beetle, longicorn, longicorn beetle', 304: 'leaf beetle, chrysomelid', 305: 'dung beetle', 306: 'rhinoceros beetle', 307: 'weevil', 308: 'fly', 309: 'bee', 310: 'ant, emmet, pismire', 311: 'grasshopper, hopper', 312: 'cricket', 313: 'walking stick, walkingstick, stick insect', 314: 'cockroach, roach', 315: 'mantis, mantid', 316: 'cicada, cicala', 317: 'leafhopper', 318: 'lacewing, lacewing fly', 319: "dragonfly, darning needle, devil's darning needle, sewing needle, snake feeder, snake doctor, mosquito hawk, skeeter hawk", 320: 'damselfly', 321: 'admiral', 322: 'ringlet, ringlet butterfly', 323: 'monarch, monarch butterfly, milkweed butterfly, Danaus plexippus', 324: 'cabbage butterfly', 325: 'sulphur butterfly, sulfur butterfly', 326: 'lycaenid, lycaenid butterfly', 327: 'starfish, sea star', 328: 'sea urchin', 329: 'sea cucumber, holothurian', 330: 'wood rabbit, cottontail, cottontail rabbit, rabbit', 331: 'hare', 332: 'Angora, Angora rabbit', 333: 'hamster', 334: 'porcupine, hedgehog', 335: 'fox squirrel, eastern fox squirrel, Sciurus niger', 336: 'marmot', 337: 'beaver', 338: 'guinea pig, Cavia cobaya', 339: 'sorrel', 340: 'zebra', 341: 'hog, pig, grunter, squealer, Sus scrofa', 342: 'wild boar, boar, Sus scrofa', 343: 'warthog', 344: 'hippopotamus, hippo, river horse, Hippopotamus amphibius', 345: 'ox', 346: 'water buffalo, water ox, Asiatic buffalo, Bubalus bubalis', 347: 'bison', 348: 'ram, tup', 349: 'bighorn, bighorn sheep, cimarron, Rocky Mountain bighorn, Rocky Mountain sheep, Ovis canadensis', 350: 'ibex, Capra ibex', 351: 'hartebeest', 352: 'impala, Aepyceros melampus', 353: 'gazelle', 354: 'Arabian camel, dromedary, Camelus dromedarius', 355: 'llama', 356: 'weasel', 357: 'mink', 358: 'polecat, fitch, foulmart, foumart, Mustela putorius', 359: 'black-footed ferret, ferret, Mustela nigripes', 360: 'otter', 361: 'skunk, polecat, wood pussy', 362: 'badger', 363: 'armadillo', 364: 'three-toed sloth, ai, Bradypus tridactylus', 365: 'orangutan, orang, orangutang, Pongo pygmaeus', 366: 'gorilla, Gorilla gorilla', 367: 'chimpanzee, chimp, Pan troglodytes', 368: 'gibbon, Hylobates lar', 369: 'siamang, Hylobates syndactylus, Symphalangus syndactylus', 370: 'guenon, guenon monkey', 371: 'patas, hussar monkey, Erythrocebus patas', 372: 'baboon', 373: 'macaque', 374: 'langur', 375: 'colobus, colobus monkey', 376: 'proboscis monkey, Nasalis larvatus', 377: 'marmoset', 378: 'capuchin, ringtail, Cebus capucinus', 379: 'howler monkey, howler', 380: 'titi, titi monkey', 381: 'spider monkey, Ateles geoffroyi', 382: 'squirrel monkey, Saimiri sciureus', 383: 'Madagascar cat, ring-tailed lemur, Lemur catta', 384: 'indri, indris, Indri indri, Indri brevicaudatus', 385: 'Indian elephant, Elephas maximus', 386: 'African elephant, Loxodonta africana', 387: 'lesser panda, red panda, panda, bear cat, cat bear, Ailurus fulgens', 388: 'giant panda, panda, panda bear, coon bear, Ailuropoda melanoleuca', 389: 'barracouta, snoek', 390: 'eel', 391: 'coho, cohoe, coho salmon, blue jack, silver salmon, Oncorhynchus kisutch', 392: 'rock beauty, Holocanthus tricolor', 393: 'anemone fish', 394: 'sturgeon', 395: 'gar, garfish, garpike, billfish, Lepisosteus osseus', 396: 'lionfish', 397: 'puffer, pufferfish, blowfish, globefish', 398: 'abacus', 399: 'abaya', 400: "academic gown, academic robe, judge's robe", 401: 'accordion, piano accordion, squeeze box', 402: 'acoustic guitar', 403: 'aircraft carrier, carrier, flattop, attack aircraft carrier', 404: 'airliner', 405: 'airship, dirigible', 406: 'altar', 407: 'ambulance', 408: 'amphibian, amphibious vehicle', 409: 'analog clock', 410: 'apiary, bee house', 411: 'apron', 412: 'ashcan, trash can, garbage can, wastebin, ash bin, ash-bin, ashbin, dustbin, trash barrel, trash bin', 413: 'assault rifle, assault gun', 414: 'backpack, back pack, knapsack, packsack, rucksack, haversack', 415: 'bakery, bakeshop, bakehouse', 416: 'balance beam, beam', 417: 'balloon', 418: 'ballpoint, ballpoint pen, ballpen, Biro', 419: 'Band Aid', 420: 'banjo', 421: 'bannister, banister, balustrade, balusters, handrail', 422: 'barbell', 423: 'barber chair', 424: 'barbershop', 425: 'barn', 426: 'barometer', 427: 'barrel, cask', 428: 'barrow, garden cart, lawn cart, wheelbarrow', 429: 'baseball', 430: 'basketball', 431: 'bassinet', 432: 'bassoon', 433: 'bathing cap, swimming cap', 434: 'bath towel', 435: 'bathtub, bathing tub, bath, tub', 436: 'beach wagon, station wagon, wagon, estate car, beach waggon, station waggon, waggon', 437: 'beacon, lighthouse, beacon light, pharos', 438: 'beaker', 439: 'bearskin, busby, shako', 440: 'beer bottle', 441: 'beer glass', 442: 'bell cote, bell cot', 443: 'bib', 444: 'bicycle-built-for-two, tandem bicycle, tandem', 445: 'bikini, two-piece', 446: 'binder, ring-binder', 447: 'binoculars, field glasses, opera glasses', 448: 'birdhouse', 449: 'boathouse', 450: 'bobsled, bobsleigh, bob', 451: 'bolo tie, bolo, bola tie, bola', 452: 'bonnet, poke bonnet', 453: 'bookcase', 454: 'bookshop, bookstore, bookstall', 455: 'bottlecap', 456: 'bow', 457: 'bow tie, bow-tie, bowtie', 458: 'brass, memorial tablet, plaque', 459: 'brassiere, bra, bandeau', 460: 'breakwater, groin, groyne, mole, bulwark, seawall, jetty', 461: 'breastplate, aegis, egis', 462: 'broom', 463: 'bucket, pail', 464: 'buckle', 465: 'bulletproof vest', 466: 'bullet train, bullet', 467: 'butcher shop, meat market', 468: 'cab, hack, taxi, taxicab', 469: 'caldron, cauldron', 470: 'candle, taper, wax light', 471: 'cannon', 472: 'canoe', 473: 'can opener, tin opener', 474: 'cardigan', 475: 'car mirror', 476: 'carousel, carrousel, merry-go-round, roundabout, whirligig', 477: "carpenter's kit, tool kit", 478: 'carton', 479: 'car wheel', 480: 'cash machine, cash dispenser, automated teller machine, automatic teller machine, automated teller, automatic teller, ATM', 481: 'cassette', 482: 'cassette player', 483: 'castle', 484: 'catamaran', 485: 'CD player', 486: 'cello, violoncello', 487: 'cellular telephone, cellular phone, cellphone, cell, mobile phone', 488: 'chain', 489: 'chainlink fence', 490: 'chain mail, ring mail, mail, chain armor, chain armour, ring armor, ring armour', 491: 'chain saw, chainsaw', 492: 'chest', 493: 'chiffonier, commode', 494: 'chime, bell, gong', 495: 'china cabinet, china closet', 496: 'Christmas stocking', 497: 'church, church building', 498: 'cinema, movie theater, movie theatre, movie house, picture palace', 499: 'cleaver, meat cleaver, chopper', 500: 'cliff dwelling', 501: 'cloak', 502: 'clog, geta, patten, sabot', 503: 'cocktail shaker', 504: 'coffee mug', 505: 'coffeepot', 506: 'coil, spiral, volute, whorl, helix', 507: 'combination lock', 508: 'computer keyboard, keypad', 509: 'confectionery, confectionary, candy store', 510: 'container ship, containership, container vessel', 511: 'convertible', 512: 'corkscrew, bottle screw', 513: 'cornet, horn, trumpet, trump', 514: 'cowboy boot', 515: 'cowboy hat, ten-gallon hat', 516: 'cradle', 517: 'crane', 518: 'crash helmet', 519: 'crate', 520: 'crib, cot', 521: 'Crock Pot', 522: 'croquet ball', 523: 'crutch', 524: 'cuirass', 525: 'dam, dike, dyke', 526: 'desk', 527: 'desktop computer', 528: 'dial telephone, dial phone', 529: 'diaper, nappy, napkin', 530: 'digital clock', 531: 'digital watch', 532: 'dining table, board', 533: 'dishrag, dishcloth', 534: 'dishwasher, dish washer, dishwashing machine', 535: 'disk brake, disc brake', 536: 'dock, dockage, docking facility', 537: 'dogsled, dog sled, dog sleigh', 538: 'dome', 539: 'doormat, welcome mat', 540: 'drilling platform, offshore rig', 541: 'drum, membranophone, tympan', 542: 'drumstick', 543: 'dumbbell', 544: 'Dutch oven', 545: 'electric fan, blower', 546: 'electric guitar', 547: 'electric locomotive', 548: 'entertainment center', 549: 'envelope', 550: 'espresso maker', 551: 'face powder', 552: 'feather boa, boa', 553: 'file, file cabinet, filing cabinet', 554: 'fireboat', 555: 'fire engine, fire truck', 556: 'fire screen, fireguard', 557: 'flagpole, flagstaff', 558: 'flute, transverse flute', 559: 'folding chair', 560: 'football helmet', 561: 'forklift', 562: 'fountain', 563: 'fountain pen', 564: 'four-poster', 565: 'freight car', 566: 'French horn, horn', 567: 'frying pan, frypan, skillet', 568: 'fur coat', 569: 'garbage truck, dustcart', 570: 'gasmask, respirator, gas helmet', 571: 'gas pump, gasoline pump, petrol pump, island dispenser', 572: 'goblet', 573: 'go-kart', 574: 'golf ball', 575: 'golfcart, golf cart', 576: 'gondola', 577: 'gong, tam-tam', 578: 'gown', 579: 'grand piano, grand', 580: 'greenhouse, nursery, glasshouse', 581: 'grille, radiator grille', 582: 'grocery store, grocery, food market, market', 583: 'guillotine', 584: 'hair slide', 585: 'hair spray', 586: 'half track', 587: 'hammer', 588: 'hamper', 589: 'hand blower, blow dryer, blow drier, hair dryer, hair drier', 590: 'hand-held computer, hand-held microcomputer', 591: 'handkerchief, hankie, hanky, hankey', 592: 'hard disc, hard disk, fixed disk', 593: 'harmonica, mouth organ, harp, mouth harp', 594: 'harp', 595: 'harvester, reaper', 596: 'hatchet', 597: 'holster', 598: 'home theater, home theatre', 599: 'honeycomb', 600: 'hook, claw', 601: 'hoopskirt, crinoline', 602: 'horizontal bar, high bar', 603: 'horse cart, horse-cart', 604: 'hourglass', 605: 'iPod', 606: 'iron, smoothing iron', 607: "jack-o'-lantern", 608: 'jean, blue jean, denim', 609: 'jeep, landrover', 610: 'jersey, T-shirt, tee shirt', 611: 'jigsaw puzzle', 612: 'jinrikisha, ricksha, rickshaw', 613: 'joystick', 614: 'kimono', 615: 'knee pad', 616: 'knot', 617: 'lab coat, laboratory coat', 618: 'ladle', 619: 'lampshade, lamp shade', 620: 'laptop, laptop computer', 621: 'lawn mower, mower', 622: 'lens cap, lens cover', 623: 'letter opener, paper knife, paperknife', 624: 'library', 625: 'lifeboat', 626: 'lighter, light, igniter, ignitor', 627: 'limousine, limo', 628: 'liner, ocean liner', 629: 'lipstick, lip rouge', 630: 'Loafer', 631: 'lotion', 632: 'loudspeaker, speaker, speaker unit, loudspeaker system, speaker system', 633: "loupe, jeweler's loupe", 634: 'lumbermill, sawmill', 635: 'magnetic compass', 636: 'mailbag, postbag', 637: 'mailbox, letter box', 638: 'maillot', 639: 'maillot, tank suit', 640: 'manhole cover', 641: 'maraca', 642: 'marimba, xylophone', 643: 'mask', 644: 'matchstick', 645: 'maypole', 646: 'maze, labyrinth', 647: 'measuring cup', 648: 'medicine chest, medicine cabinet', 649: 'megalith, megalithic structure', 650: 'microphone, mike', 651: 'microwave, microwave oven', 652: 'military uniform', 653: 'milk can', 654: 'minibus', 655: 'miniskirt, mini', 656: 'minivan', 657: 'missile', 658: 'mitten', 659: 'mixing bowl', 660: 'mobile home, manufactured home', 661: 'Model T', 662: 'modem', 663: 'monastery', 664: 'monitor', 665: 'moped', 666: 'mortar', 667: 'mortarboard', 668: 'mosque', 669: 'mosquito net', 670: 'motor scooter, scooter', 671: 'mountain bike, all-terrain bike, off-roader', 672: 'mountain tent', 673: 'mouse, computer mouse', 674: 'mousetrap', 675: 'moving van', 676: 'muzzle', 677: 'nail', 678: 'neck brace', 679: 'necklace', 680: 'nipple', 681: 'notebook, notebook computer', 682: 'obelisk', 683: 'oboe, hautboy, hautbois', 684: 'ocarina, sweet potato', 685: 'odometer, hodometer, mileometer, milometer', 686: 'oil filter', 687: 'organ, pipe organ', 688: 'oscilloscope, scope, cathode-ray oscilloscope, CRO', 689: 'overskirt', 690: 'oxcart', 691: 'oxygen mask', 692: 'packet', 693: 'paddle, boat paddle', 694: 'paddlewheel, paddle wheel', 695: 'padlock', 696: 'paintbrush', 697: "pajama, pyjama, pj's, jammies", 698: 'palace', 699: 'panpipe, pandean pipe, syrinx', 700: 'paper towel', 701: 'parachute, chute', 702: 'parallel bars, bars', 703: 'park bench', 704: 'parking meter', 705: 'passenger car, coach, carriage', 706: 'patio, terrace', 707: 'pay-phone, pay-station', 708: 'pedestal, plinth, footstall', 709: 'pencil box, pencil case', 710: 'pencil sharpener', 711: 'perfume, essence', 712: 'Petri dish', 713: 'photocopier', 714: 'pick, plectrum, plectron', 715: 'pickelhaube', 716: 'picket fence, paling', 717: 'pickup, pickup truck', 718: 'pier', 719: 'piggy bank, penny bank', 720: 'pill bottle', 721: 'pillow', 722: 'ping-pong ball', 723: 'pinwheel', 724: 'pirate, pirate ship', 725: 'pitcher, ewer', 726: "plane, carpenter's plane, woodworking plane", 727: 'planetarium', 728: 'plastic bag', 729: 'plate rack', 730: 'plow, plough', 731: "plunger, plumber's helper", 732: 'Polaroid camera, Polaroid Land camera', 733: 'pole', 734: 'police van, police wagon, paddy wagon, patrol wagon, wagon, black Maria', 735: 'poncho', 736: 'pool table, billiard table, snooker table', 737: 'pop bottle, soda bottle', 738: 'pot, flowerpot', 739: "potter's wheel", 740: 'power drill', 741: 'prayer rug, prayer mat', 742: 'printer', 743: 'prison, prison house', 744: 'projectile, missile', 745: 'projector', 746: 'puck, hockey puck', 747: 'punching bag, punch bag, punching ball, punchball', 748: 'purse', 749: 'quill, quill pen', 750: 'quilt, comforter, comfort, puff', 751: 'racer, race car, racing car', 752: 'racket, racquet', 753: 'radiator', 754: 'radio, wireless', 755: 'radio telescope, radio reflector', 756: 'rain barrel', 757: 'recreational vehicle, RV, R.V.', 758: 'reel', 759: 'reflex camera', 760: 'refrigerator, icebox', 761: 'remote control, remote', 762: 'restaurant, eating house, eating place, eatery', 763: 'revolver, six-gun, six-shooter', 764: 'rifle', 765: 'rocking chair, rocker', 766: 'rotisserie', 767: 'rubber eraser, rubber, pencil eraser', 768: 'rugby ball', 769: 'rule, ruler', 770: 'running shoe', 771: 'safe', 772: 'safety pin', 773: 'saltshaker, salt shaker', 774: 'sandal', 775: 'sarong', 776: 'sax, saxophone', 777: 'scabbard', 778: 'scale, weighing machine', 779: 'school bus', 780: 'schooner', 781: 'scoreboard', 782: 'screen, CRT screen', 783: 'screw', 784: 'screwdriver', 785: 'seat belt, seatbelt', 786: 'sewing machine', 787: 'shield, buckler', 788: 'shoe shop, shoe-shop, shoe store', 789: 'shoji', 790: 'shopping basket', 791: 'shopping cart', 792: 'shovel', 793: 'shower cap', 794: 'shower curtain', 795: 'ski', 796: 'ski mask', 797: 'sleeping bag', 798: 'slide rule, slipstick', 799: 'sliding door', 800: 'slot, one-armed bandit', 801: 'snorkel', 802: 'snowmobile', 803: 'snowplow, snowplough', 804: 'soap dispenser', 805: 'soccer ball', 806: 'sock', 807: 'solar dish, solar collector, solar furnace', 808: 'sombrero', 809: 'soup bowl', 810: 'space bar', 811: 'space heater', 812: 'space shuttle', 813: 'spatula', 814: 'speedboat', 815: "spider web, spider's web", 816: 'spindle', 817: 'sports car, sport car', 818: 'spotlight, spot', 819: 'stage', 820: 'steam locomotive', 821: 'steel arch bridge', 822: 'steel drum', 823: 'stethoscope', 824: 'stole', 825: 'stone wall', 826: 'stopwatch, stop watch', 827: 'stove', 828: 'strainer', 829: 'streetcar, tram, tramcar, trolley, trolley car', 830: 'stretcher', 831: 'studio couch, day bed', 832: 'stupa, tope', 833: 'submarine, pigboat, sub, U-boat', 834: 'suit, suit of clothes', 835: 'sundial', 836: 'sunglass', 837: 'sunglasses, dark glasses, shades', 838: 'sunscreen, sunblock, sun blocker', 839: 'suspension bridge', 840: 'swab, swob, mop', 841: 'sweatshirt', 842: 'swimming trunks, bathing trunks', 843: 'swing', 844: 'switch, electric switch, electrical switch', 845: 'syringe', 846: 'table lamp', 847: 'tank, army tank, armored combat vehicle, armoured combat vehicle', 848: 'tape player', 849: 'teapot', 850: 'teddy, teddy bear', 851: 'television, television system', 852: 'tennis ball', 853: 'thatch, thatched roof', 854: 'theater curtain, theatre curtain', 855: 'thimble', 856: 'thresher, thrasher, threshing machine', 857: 'throne', 858: 'tile roof', 859: 'toaster', 860: 'tobacco shop, tobacconist shop, tobacconist', 861: 'toilet seat', 862: 'torch', 863: 'totem pole', 864: 'tow truck, tow car, wrecker', 865: 'toyshop', 866: 'tractor', 867: 'trailer truck, tractor trailer, trucking rig, rig, articulated lorry, semi', 868: 'tray', 869: 'trench coat', 870: 'tricycle, trike, velocipede', 871: 'trimaran', 872: 'tripod', 873: 'triumphal arch', 874: 'trolleybus, trolley coach, trackless trolley', 875: 'trombone', 876: 'tub, vat', 877: 'turnstile', 878: 'typewriter keyboard', 879: 'umbrella', 880: 'unicycle, monocycle', 881: 'upright, upright piano', 882: 'vacuum, vacuum cleaner', 883: 'vase', 884: 'vault', 885: 'velvet', 886: 'vending machine', 887: 'vestment', 888: 'viaduct', 889: 'violin, fiddle', 890: 'volleyball', 891: 'waffle iron', 892: 'wall clock', 893: 'wallet, billfold, notecase, pocketbook', 894: 'wardrobe, closet, press', 895: 'warplane, military plane', 896: 'washbasin, handbasin, washbowl, lavabo, wash-hand basin', 897: 'washer, automatic washer, washing machine', 898: 'water bottle', 899: 'water jug', 900: 'water tower', 901: 'whiskey jug', 902: 'whistle', 903: 'wig', 904: 'window screen', 905: 'window shade', 906: 'Windsor tie', 907: 'wine bottle', 908: 'wing', 909: 'wok', 910: 'wooden spoon', 911: 'wool, woolen, woollen', 912: 'worm fence, snake fence, snake-rail fence, Virginia fence', 913: 'wreck', 914: 'yawl', 915: 'yurt', 916: 'web site, website, internet site, site', 917: 'comic book', 918: 'crossword puzzle, crossword', 919: 'street sign', 920: 'traffic light, traffic signal, stoplight', 921: 'book jacket, dust cover, dust jacket, dust wrapper', 922: 'menu', 923: 'plate', 924: 'guacamole', 925: 'consomme', 926: 'hot pot, hotpot', 927: 'trifle', 928: 'ice cream, icecream', 929: 'ice lolly, lolly, lollipop, popsicle', 930: 'French loaf', 931: 'bagel, beigel', 932: 'pretzel', 933: 'cheeseburger', 934: 'hotdog, hot dog, red hot', 935: 'mashed potato', 936: 'head cabbage', 937: 'broccoli', 938: 'cauliflower', 939: 'zucchini, courgette', 940: 'spaghetti squash', 941: 'acorn squash', 942: 'butternut squash', 943: 'cucumber, cuke', 944: 'artichoke, globe artichoke', 945: 'bell pepper', 946: 'cardoon', 947: 'mushroom', 948: 'Granny Smith', 949: 'strawberry', 950: 'orange', 951: 'lemon', 952: 'fig', 953: 'pineapple, ananas', 954: 'banana', 955: 'jackfruit, jak, jack', 956: 'custard apple', 957: 'pomegranate', 958: 'hay', 959: 'carbonara', 960: 'chocolate sauce, chocolate syrup', 961: 'dough', 962: 'meat loaf, meatloaf', 963: 'pizza, pizza pie', 964: 'potpie', 965: 'burrito', 966: 'red wine', 967: 'espresso', 968: 'cup', 969: 'eggnog', 970: 'alp', 971: 'bubble', 972: 'cliff, drop, drop-off', 973: 'coral reef', 974: 'geyser', 975: 'lakeside, lakeshore', 976: 'promontory, headland, head, foreland', 977: 'sandbar, sand bar', 978: 'seashore, coast, seacoast, sea-coast', 979: 'valley, vale', 980: 'volcano', 981: 'ballplayer, baseball player', 982: 'groom, bridegroom', 983: 'scuba diver', 984: 'rapeseed', 985: 'daisy', 986: "yellow lady's slipper, yellow lady-slipper, Cypripedium calceolus, Cypripedium parviflorum", 987: 'corn', 988: 'acorn', 989: 'hip, rose hip, rosehip', 990: 'buckeye, horse chestnut, conker', 991: 'coral fungus', 992: 'agaric', 993: 'gyromitra', 994: 'stinkhorn, carrion fungus', 995: 'earthstar', 996: 'hen-of-the-woods, hen of the woods, Polyporus frondosus, Grifola frondosa', 997: 'bolete', 998: 'ear, spike, capitulum', 999: 'toilet tissue, toilet paper, bathroom tissue'} ================================================ FILE: intropyproject-classify-pet-images/print_functions_for_lab_checks.py ================================================ #!/usr/bin/env python3 # -*- coding: utf-8 -*- # */AIPND/intropylab-classifying-images/print_functions_for_lab_checks.py # # PROGRAMMER: Jennifer S. # DATE CREATED: 05/14/2018 # REVISED DATE: <=(Date Revised - if any) # PURPOSE: This set of functions can be used to check your code after programming # each function. The top section of each part of the lab contains # the section labeled 'Checking your code'. When directed within this # section of the lab one can use these functions to more easily check # your code. See the docstrings below each function for details on how # to use the function within your code. # ## # Functions below defined to help with "Checking your code", specifically # running these functions with the appropriate input arguments within the # main() funtion will print out what's needed for "Checking your code" # def check_command_line_arguments(in_arg): """ For Lab: Classifying Images - 7. Command Line Arguments Prints each of the command line arguments passed in as parameter in_arg, assumes you defined all three command line arguments as outlined in '7. Command Line Arguments' Parameters: in_arg -data structure that stores the command line arguments object Returns: Nothing - just prints to console """ if in_arg is None: print("* Doesn't Check the Command Line Arguments because 'get_input_args' hasn't been defined.") else: # prints command line agrs print("Command Line Arguments:\n dir =", in_arg.dir, "\n arch =", in_arg.arch, "\n dogfile =", in_arg.dogfile) def check_creating_pet_image_labels(results_dic): """ For Lab: Classifying Images - 9/10. Creating Pet Image Labels Prints first 10 key-value pairs and makes sure there are 40 key-value pairs in your results_dic dictionary. Assumes you defined the results_dic dictionary as was outlined in '9/10. Creating Pet Image Labels' Parameters: results_dic - Dictionary with key as image filename and value as a List (index)idx 0 = pet image label (string) Returns: Nothing - just prints to console """ if results_dic is None: print("* Doesn't Check the Results Dictionary because 'get_pet_labels' hasn't been defined.") else: # Code to print 10 key-value pairs (or fewer if less than 10 images) # & makes sure there are 40 pairs, one for each file in pet_images/ stop_point = len(results_dic) if stop_point > 10: stop_point = 10 print("\nPet Image Label Dictionary has", len(results_dic), "key-value pairs.\nBelow are", stop_point, "of them:") # counter - to count how many labels have been printed n = 0 # for loop to iterate through the dictionary for key in results_dic: # prints only first 10 labels if n < stop_point: print("{:2d} key: {:>30} label: {:>26}".format(n+1, key, results_dic[key][0]) ) # Increments counter n += 1 # If past first 10 (or fewer) labels the breaks out of loop else: break def check_classifying_images(results_dic): """ For Lab: Classifying Images - 11/12. Classifying Images Prints Pet Image Label and Classifier Label for ALL Matches followed by ALL NOT matches. Next prints out the total number of images followed by how many were matches and how many were not-matches to check all 40 images are processed. Assumes you defined the results_dic dictionary as was outlined in '11/12. Classifying Images' 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 Returns: Nothing - just prints to console """ if results_dic is None: print("* Doesn't Check the Results Dictionary because 'classify_images' hasn't been defined.") elif len(results_dic[next(iter(results_dic))]) < 2: print("* Doesn't Check the Results Dictionary because 'classify_images' hasn't been defined.") else: # Code for checking classify_images - # Checks matches and not matches are classified correctly # Checks that all 40 images are classified as a Match or Not-a Match # Sets counters for matches & NOT-matches n_match = 0 n_notmatch = 0 # Prints all Matches first print("\n MATCH:") for key in results_dic: # Prints only if a Match Index 2 == 1 if results_dic[key][2] == 1: # Increments Match counter n_match += 1 print("\n{:>30}: \nReal: {:>26} Classifier: {:>30}".format(key, results_dic[key][0], results_dic[key][1])) # Prints all NOT-Matches next print("\n NOT A MATCH:") for key in results_dic: # Prints only if NOT-a-Match Index 2 == 0 if results_dic[key][2] == 0: # Increments Not-a-Match counter n_notmatch += 1 print("\n{:>30}: \nReal: {:>26} Classifier: {:>30}".format(key, results_dic[key][0], results_dic[key][1])) # Prints Total Number of Images - expects 40 from pet_images folder print("\n# Total Images",n_match + n_notmatch, "# Matches:",n_match , "# NOT Matches:",n_notmatch) def check_classifying_labels_as_dogs(results_dic): """ For Lab: Classifying Images - 13. Classifying Labels as Dogs Prints Pet Image Label, Classifier Label, whether Pet Label is-a-dog(1=Yes, 0=No), and whether Classifier Label is-a-dog(1=Yes, 0=No) for ALL Matches followed by ALL NOT matches. Next prints out the total number of images followed by how many were matches and how many were not-matches to check all 40 images are processed. Assumes you defined the results_dic dictionary as was outlined in '13. Classifying Labels as Dogs' 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: Nothing - just prints to console """ if results_dic is None: print("* Doesn't Check the Results Dictionary because 'adjust_results4_isadog' hasn't been defined.") elif len(results_dic[next(iter(results_dic))]) < 4 : print("* Doesn't Check the Results Dictionary because 'adjust_results4_isadog' hasn't been defined.") else: # Code for checking adjust_results4_isadog - # Checks matches and not matches are classified correctly as "dogs" and # "not-dogs" Checks that all 40 images are classified as a Match or Not-a # Match # Sets counters for matches & NOT-matches n_match = 0 n_notmatch = 0 # Prints all Matches first print("\n MATCH:") for key in results_dic: # Prints only if a Match Index 2 == 1 if results_dic[key][2] == 1: # Increments Match counter n_match += 1 print("\n{:>30}: \nReal: {:>26} Classifier: {:>30} \nPetLabelDog: {:1d} ClassLabelDog: {:1d}".format(key, results_dic[key][0], results_dic[key][1], results_dic[key][3], results_dic[key][4])) # Prints all NOT-Matches next print("\n NOT A MATCH:") for key in results_dic: # Prints only if NOT-a-Match Index 2 == 0 if results_dic[key][2] == 0: # Increments Not-a-Match counter n_notmatch += 1 print("\n{:>30}: \nReal: {:>26} Classifier: {:>30} \nPetLabelDog: {:1d} ClassLabelDog: {:1d}".format(key, results_dic[key][0], results_dic[key][1], results_dic[key][3], results_dic[key][4])) # Prints Total Number of Images - expects 40 from pet_images folder print("\n# Total Images",n_match + n_notmatch, "# Matches:",n_match , "# NOT Matches:",n_notmatch) def check_calculating_results(results_dic, results_stats_dic): """ For Lab: Classifying Images - 14. Calculating Results Prints First statistics from the results stats dictionary (that was created by the calculates_results_stats() function), then prints the same statistics that were calculated in this function using the results dictionary. Assumes you defined the results_stats dictionary and the statistics as was outlined in '14. Calculating Results ' 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 Returns: Nothing - just prints to console """ if results_stats_dic is None: print("* Doesn't Check the Results Dictionary because 'calculates_results_stats' hasn't been defined.") else: # Code for checking results_stats_dic - # Checks calculations of counts & percentages BY using results_dic # to re-calculate the values and then compare to the values # in results_stats_dic # Initialize counters to zero and number of images total n_images = len(results_dic) n_pet_dog = 0 n_class_cdog = 0 n_class_cnotd = 0 n_match_breed = 0 # Interates through results_dic dictionary to recompute the statistics # outside of the calculates_results_stats() function for key in results_dic: # match (if dog then breed match) if results_dic[key][2] == 1: # isa dog (pet label) & breed match if results_dic[key][3] == 1: n_pet_dog += 1 # isa dog (classifier label) & breed match if results_dic[key][4] == 1: n_class_cdog += 1 n_match_breed += 1 # NOT dog (pet_label) else: # NOT dog (classifier label) if results_dic[key][4] == 0: n_class_cnotd += 1 # NOT - match (not a breed match if a dog) else: # NOT - match # isa dog (pet label) if results_dic[key][3] == 1: n_pet_dog += 1 # isa dog (classifier label) if results_dic[key][4] == 1: n_class_cdog += 1 # NOT dog (pet_label) else: # NOT dog (classifier label) if results_dic[key][4] == 0: n_class_cnotd += 1 # calculates statistics based upon counters from above n_pet_notd = n_images - n_pet_dog pct_corr_dog = ( n_class_cdog / n_pet_dog )*100 pct_corr_notdog = ( n_class_cnotd / n_pet_notd )*100 pct_corr_breed = ( n_match_breed / n_pet_dog )*100 # prints calculated statistics print("\n ** Statistics from calculates_results_stats() function:") print("N Images: {:2d} N Dog Images: {:2d} N NotDog Images: {:2d} \nPct Corr dog: {:5.1f} Pct Corr NOTdog: {:5.1f} Pct Corr Breed: {:5.1f}".format( results_stats_dic['n_images'], results_stats_dic['n_dogs_img'], results_stats_dic['n_notdogs_img'], results_stats_dic['pct_correct_dogs'], results_stats_dic['pct_correct_notdogs'], results_stats_dic['pct_correct_breed'])) print("\n ** Check Statistics - calculated from this function as a check:") print("N Images: {:2d} N Dog Images: {:2d} N NotDog Images: {:2d} \nPct Corr dog: {:5.1f} Pct Corr NOTdog: {:5.1f} Pct Corr Breed: {:5.1f}".format( n_images, n_pet_dog, n_pet_notd, pct_corr_dog, pct_corr_notdog, pct_corr_breed)) ================================================ FILE: intropyproject-classify-pet-images/print_results.py ================================================ #!/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 ================================================ FILE: intropyproject-classify-pet-images/print_results_hints.py ================================================ #!/usr/bin/env python3 # -*- coding: utf-8 -*- # */AIPND-revision/intropyproject-classify-pet-images/print_results_hints.py # # PROGRAMMER: # DATE CREATED: # REVISED DATE: # PURPOSE: This is a *hints* file to help guide students in creating the # 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: EDIT and ADD code BELOW to do the following that's stated in the # comments below that start with "TODO: 6" for the print_results function. # Specifically edit and add code below within the the print_results function. # Notice that this function doesn't 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. """ # Prints summary statistics over the run print("\n\n*** Results Summary for CNN Model Architecture",model.upper(), "***") print("{:20}: {:3d}".format('N Images', results_stats_dic['n_images'])) print("{:20}: {:3d}".format('N Dog Images', results_stats_dic['n_dogs_img'])) # TODO: 6a. REPLACE print("") with CODE that prints the text string # 'N Not-Dog Images' and then the number of NOT-dog images # that's accessed by key 'n_notdogs_img' using dictionary # results_stats_dic # print("") # Prints summary statistics (percentages) on Model Run print(" ") for key in results_stats_dic: # TODO: 6b. REPLACE pass with CODE that prints out all the percentages # in the results_stats_dic dictionary. Recall that all # percentages in results_stats_dic have 'keys' that start with # the letter p. You will need to write a conditional # statement that determines if the key starts with the letter # 'p' and then you want to use a print statement to print # both the key and the value. Remember the value is accessed # by results_stats_dic[key] # pass # IF print_incorrect_dogs == True AND there were images incorrectly # classified as dogs or vice versa - print out these cases if (print_incorrect_dogs and ( (results_stats_dic['n_correct_dogs'] + results_stats_dic['n_correct_notdogs']) != results_stats_dic['n_images'] ) ): print("\nINCORRECT Dog/NOT Dog Assignments:") # process through results dict, printing incorrectly classified dogs for key in results_dic: # TODO: 6c. REPLACE pass with CODE that prints out the pet label # and the classifier label from results_dic dictionary # ONLY when the classifier function (classifier label) # misclassified dogs specifically: # pet label is-a-dog and classifier label is-NOT-a-dog # -OR- # pet label is-NOT-a-dog and classifier label is-a-dog # You will need to write a conditional statement that # determines if the classifier function misclassified dogs # See 'Adjusting Results Dictionary' section in # 'Classifying Labels as Dogs' for details on the # format of the results_dic dictionary. Remember the value # is accessed by results_dic[key] and the value is a list # so results_dic[key][idx] - where idx represents the # index value of the list and can have values 0-4. # # Pet Image Label is a Dog - Classified as NOT-A-DOG -OR- # Pet Image Label is NOT-a-Dog - Classified as a-DOG pass # IF print_incorrect_breed == True AND there were dogs whose breeds # were incorrectly classified - print out these cases if (print_incorrect_breed and (results_stats_dic['n_correct_dogs'] != results_stats_dic['n_correct_breed']) ): print("\nINCORRECT Dog Breed Assignment:") # process through results dict, printing incorrectly classified breeds for key in results_dic: # Pet Image Label is-a-Dog, classified as-a-dog but is WRONG breed if ( sum(results_dic[key][3:]) == 2 and results_dic[key][2] == 0 ): print("Real: {:>26} Classifier: {:>30}".format(results_dic[key][0], results_dic[key][1])) ================================================ FILE: intropyproject-classify-pet-images/run_models_batch.bat ================================================ @echo off REM */AIPND-revision/intropyproject-classify-pet-images/run_models_batch.bat REM REM PROGRAMMER: Jennifer S. REM DATE CREATED: 02/08/2018 REM REVISED DATE: 04/23/2018 - revised run_models_batch.sh to run on REM windows OS using bat and Anaconda Prompt REM PURPOSE: Runs all three models to test which provides 'best' solution. REM Please note output from each run has been piped into a text file. REM REM Usage: run_models_batch.bat -- will run program from commandline on Window OS REM @echo on python check_images.py --dir pet_images/ --arch resnet --dogfile dognames.txt > resnet_pet-images.txt python check_images.py --dir pet_images/ --arch alexnet --dogfile dognames.txt > alexnet_pet-images.txt python check_images.py --dir pet_images/ --arch vgg --dogfile dognames.txt > vgg_pet-images.txt ================================================ FILE: intropyproject-classify-pet-images/run_models_batch.sh ================================================ #!/bin/sh # */AIPND-revision/intropyproject-classify-pet-images/run_models_batch.sh # # PROGRAMMER: Jennifer S. # DATE CREATED: 02/08/2018 # REVISED DATE: 02/27/2018 - # PURPOSE: Runs all three models to test which provides 'best' solution. # Please note output from each run has been piped into a text file. # # Usage: sh run_models_batch.sh -- will run program from commandline within Project Workspace # python check_images.py --dir pet_images/ --arch resnet --dogfile dognames.txt > resnet_pet-images.txt python check_images.py --dir pet_images/ --arch alexnet --dogfile dognames.txt > alexnet_pet-images.txt python check_images.py --dir pet_images/ --arch vgg --dogfile dognames.txt > vgg_pet-images.txt ================================================ FILE: intropyproject-classify-pet-images/run_models_batch_uploaded.bat ================================================ @echo off REM */AIPND-revision/intropyproject-classify-pet-images/run_models_batch_uploaded.bat REM REM PROGRAMMER: Jennifer S. REM DATE CREATED: 02/08/2018 REM REVISED DATE: 04/23/2018 - revised run_models_batch.sh to run on REM windows OS using bat and Anaconda Prompt REM PURPOSE: Runs all three models to test which provides 'best' solution on the Uploaded Images. REM Please note output from each run has been piped into a text file. REM REM Usage: run_models_batch_uploaded.bat -- will run program from commandline on Window OS REM @echo on python check_images.py --dir uploaded_images/ --arch resnet --dogfile dognames.txt > resnet_uploaded-images.txt python check_images.py --dir uploaded_images/ --arch alexnet --dogfile dognames.txt > alexnet_uploaded-images.txt python check_images.py --dir uploaded_images/ --arch vgg --dogfile dognames.txt > vgg_uploaded-images.txt ================================================ FILE: intropyproject-classify-pet-images/run_models_batch_uploaded.sh ================================================ #!/bin/sh # */AIPND-revision/intropyproject-classify-pet-images/run_models_batch_uploaded.sh # # PROGRAMMER: Jennifer S. # DATE CREATED: 02/08/2018 # REVISED DATE: 02/27/2018 - # PURPOSE: Runs all three models to test which provides 'best' solution on the Uploaded Images. # Please note output from each run has been piped into a text file. # # Usage: sh run_models_batch_uploaded.sh -- will run program from commandline within Project Workspace # python check_images.py --dir uploaded_images/ --arch resnet --dogfile dognames.txt > resnet_uploaded-images.txt python check_images.py --dir uploaded_images/ --arch alexnet --dogfile dognames.txt > alexnet_uploaded-images.txt python check_images.py --dir uploaded_images/ --arch vgg --dogfile dognames.txt > vgg_uploaded-images.txt ================================================ FILE: intropyproject-classify-pet-images/test_classifier.py ================================================ #!/usr/bin/env python3 # -*- coding: utf-8 -*- # */AIPND-revision/intropyproject-classify-pet-images/test_classifier.py # # PROGRAMMER: Jennifer S. # DATE CREATED: 01/30/2018 # REVISED DATE: <=(Date Revised - if any) # PURPOSE: To demonstrate the proper usage of the classifier() function that # is defined in classifier.py This function uses CNN model # architecture that has been pretrained on the ImageNet data to # classify images. The only model architectures that this function # will accept are: 'resnet', 'alexnet', and 'vgg'. See the example # usage below. # # Usage: python test_classifier.py -- will run program from commandline # Imports classifier function for using pretrained CNN to classify images from classifier import classifier # Defines a dog test image from pet_images folder test_image="pet_images/Collie_03797.jpg" # Defines a model architecture to be used for classification # NOTE: this function only works for model architectures: # 'vgg', 'alexnet', 'resnet' model = "vgg" # Demonstrates classifier() functions usage # NOTE: image_classication is a text string - It contains mixed case(both lower # and upper case letter) image labels that can be separated by commas when a # label has more than one word that can describe it. image_classification = classifier(test_image, model) # prints result from running classifier() function print("\nResults from test_classifier.py\nImage:", test_image, "using model:", model, "was classified as a:", image_classification) ================================================ FILE: notes/project_intro-to-python.md ================================================ # Notes: Frequently Asked Questions for Classifying Images Project These notes pertain to Frequently Asked Questions (FAQ) for the **_2. Intro to Python_**, **_Lesson 6. Project: Classify Images_** that were posted and addressed on AIPND slack. We recommend that you review these notes prior to starting the **_Classify Images Project_** to help clarify potential points of confusion regarding the Project.     ## Quick Links to Frequently Asked Questions * [GitHub AIPND Repository Link](https://github.com/udacity/AIPND-revision) * [Issues with the Project Workspace](https://github.com/udacity/AIPND-revision/blob/master/notes/project_intro-to-python.md#issues-with-the-project-workspace) * [Running the Project on a Local Computer](https://github.com/udacity/AIPND-revision/blob/master/notes/project_intro-to-python.md#running-the-project-on-a-local-computer) * [Files Required to Run **_check_images.py_** Locally](https://github.com/udacity/AIPND-revision/blob/master/notes/project_intro-to-python.md#files-required-to-run-check_imagespy-locally) * [Running Batch Files on Windows OS Locally](https://github.com/udacity/AIPND-revision/blob/master/notes/project_intro-to-python.md#running-batch-files-on-windows-os-locally) * [**_Hints_** for this Project](https://github.com/udacity/AIPND-revision/blob/master/notes/project_intro-to-python.md#hints-for-this-project) * [Eliminating Syntax Errors with Text Editor/Integrated Development Environment](https://github.com/udacity/AIPND-revision/blob/master/notes/project_intro-to-python.md#eliminating-syntax-errors-with-text-editorintegrated-development-environment) * [Cutting and Pasting Code in the Classroom](https://github.com/udacity/AIPND-revision/blob/master/notes/project_intro-to-python.md#cutting-and-pasting-code-in-the-classroom) * [Indention of Python Code](https://github.com/udacity/AIPND-revision/blob/master/notes/project_intro-to-python.md#indention-of-python-code) * [Replacing None and Pass Statements](https://github.com/udacity/AIPND-revision/blob/master/notes/project_intro-to-python.md#replacing-none-and-pass-statements)     ## Issues with the Project Workspace While it is recommended that you work on the project within the **_Project Workspace_**, a few students _may_ have experienced issues trying to work within the _Project Workspace_. Some students found these issues resolved when they switched their internet browsers. Specifically, some students found that **_Chrome_** worked best; while others found that **_Firefox_** worked better for them. If you are running into the problem where the _files_ in the workspace don't load and/or running code within the workspace runs extremely slowly; please try the following: * Quit and exit out of the **_web browser_** you are using, then open it back up and restart it. * Switch to a different **_web browser_**. If you run into issues with the **_Project Workspace_** and the above recommendations didn't work; alternatively, you are welcome to complete the project on a local computer using the instructions in the next [FAQ](https://github.com/udacity/AIPND-revision/blob/master/notes/project_intro-to-python.md#running-the-project-on-a-local-computer).     ## Running the Project on a Local Computer While it is recommended that you work on the project within the **_Project Workspace_**, to run the project on a local computer, you will have needed to have Python 3.6 intalled on your computer. ### Installing Anaconda The easiest way to install python and the appropriate python modules is to install [Anaconda](https://www.anaconda.com/download). You will also have found the directions to install Anaconda in **_2. Intro to Python_**, **_Lesson 5. Scripting_**, **_Section 3. Install Python Using Anaconda_**. ### Installing PyTorch and torchvision ### Linux, OSX(Mac), Windows For this project you will also need to install the python packages pytorch and torchvision. If your local computer has a Linux, OSX (Mac), or Windows operating system look to [*Get Started.*](http://pytorch.org/) for installation instructions.     ## Files Required to Run **_check_images.py_** Locally The following files and folders need to be put in the same folder as the **_check_images.py_** python program on your local computer. You will find these files and folders within the [GitHub AIPND Repository](https://github.com/udacity/AIPND-revision/tree/master/intropyproject-classify-pet-images). There are more programs in the repository than you will need, these extra programs are there to provide the code within the lessons in a format that can be copied and pasted from. ### Needed Files: * **pet_images** (folder of 40 pet image) * **uploaded_images** (a folder you will have to create to hold your uploaded images in that section of the project) * **classifier.py** (classifier function you will be using to classify the images) * **dognames.txt** (file that contains all the valid dog names from the classifier function and the pet image files) * **imagenet1000_clsid_to_human.txt** (dictionary that converts the classifier function ids to text labels) * **adjust_results4_isadog.py** (a program that contains the **adjust_results4_isadog** function that you will be defining as part of the project) * **calculates_results_stats.py** (a program that contains the **calculates_results_stats** function that you will be defining as part of the project) * **classify_images.py** (a program that contains the **classify_images** function that you will be defining as part of the project) * **get_input_args.py** (a program that contains the **get_input_args** function that you will be defining as part of the project) * **get_pet_labels.py** (a program that contains the **get_pet_labels** function that you will be defining as part of the project) * **print_results.py** (a program that contains the **print_results** function that you will be defining as part of the project) * **run_models_batch.sh** (a bash script that will run check_images.py sequentially for all 3 model architectures and output their results to text files - on Unix/Linux/OSX/Project Workspace from a terminal window) * **run_models_batch.bat** (a batch script that will run check_images.py sequentially for all 3 model architectures and output their results to text files - on Windows from the Anaconda Prompt window) * **run_models_batch_uploaded.sh** (a bash script that will run check_images.py sequentially for all 3 model architectures on the uploaded images folder and output their results to text files - on Unix/Linux/OSX/Project Workspace from a terminal window) * **run_models_batch_uploaded.bat** (a batch script that will run check_images.py sequentially for all 3 model architectures on the uploaded images folder and output their results to text files - on Windows from the Anaconda Prompt window) * **test_classifier.py** (an example program that demonstrates how to use the classifier function) * **print_functions_for_lab_checks.py** (a program that contains functions that will allow you to check your code) Also be aware that instructor provided **_hints_** files are provided for each of the functions used within this project, these files will end with **_hints.py**. These **_hints_** files contain extra code to provide a few **_hints_** to help guide students to the solution. You will find these **_hints_** files within the **_Project Workspace_** and also within the [**_GitHub repository_**](https://github.com/udacity/AIPND-revision/tree/master/intropyproject-classify-pet-images) as the following files: * **adjust_results4_isadog_hints.py** (a program that contains **_hints_** for the **adjust_results4_isadog** function that you will be defining as part of the project) * **calculates_results_stats_hints.py** (a program that contains **_hints_** for the **calculates_results_stats** function that you will be defining as part of the project) * **classify_images_hints.py** (a program that contains **_hints_** for the **classify_images** function that you will be defining as part of the project) * **get_input_args_hints.py** (a program that contains **_hints_** for the **get_input_args** function that you will be defining as part of the project) * **get_pet_labels_hints.py** (a program that contains **_hints_** for the **get_pet_labels** function that you will be defining as part of the project) * **print_results_hints.py** (a program that contains **_hints_** for the **print_results** function that you will be defining as part of the project)     ## Running Batch Files on Windows OS Locally To run the files **_run_models_batch_** or **_run_models_batch_uploaded_** that run all 3 model architectures using **_check_images.py_** on a Windows OS locally; you will need to use the files that end with the extention **_.bat_** instead of the extension **_.sh_**. You will have also needed to have installed Anaconda on your computer (see following [FAQ](https://github.com/udacity/AIPND-revision/blob/master/notes/project_intro-to-python.md#running-the-project-on-a-local-computer) for details on Anaconda installation). ### Directions: * Open the **Anaconda Prompt** - either from typing **_Anaconda Prompt_** within the search bar and selecting it _or_ by clicking on it once it's found within the **Anaconda** folder of programs. * Navigate to the _folder_ within the **Anaconda Prompt** that contains the _Project files_ including **_check_images.py_** and **_run_models_batch.bat_** using the command [_cd_](https://en.wikipedia.org/wiki/Cd_(command)). * Type the command within the **Anaconda Prompt**: ```terminal run_models_batch.bat ``` If instead you are working with the uploaded images , you will replace all instances of **_run_models_batch.bat_** from the _directions_ above with **_run_models_batch_uploaded.bat_**.     ## **_Hints_** for this Project Instructor provided **_hints_** files are provided for each of the functions used within this project, these files will end with **_hints.py**. These **_hints_** files contain extra code to provide a few **_hints_** to help guide students to the solution. You will find these **_hints_** files within the **_Project Workspace_** and also within the [**_GitHub repository_**](https://github.com/udacity/AIPND-revision/tree/master/intropyproject-classify-pet-images) as the following files: * **adjust_results4_isadog_hints.py** (a program that contains **_hints_** for the **adjust_results4_isadog** function that you will be defining as part of the project) * **calculates_results_stats_hints.py** (a program that contains **_hints_** for the **calculates_results_stats** function that you will be defining as part of the project) * **classify_images_hints.py** (a program that contains **_hints_** for the **classify_images** function that you will be defining as part of the project) * **get_input_args_hints.py** (a program that contains **_hints_** for the **get_input_args** function that you will be defining as part of the project) * **get_pet_labels_hints.py** (a program that contains **_hints_** for the **get_pet_labels** function that you will be defining as part of the project) * **print_results_hints.py** (a program that contains **_hints_** for the **print_results** function that you will be defining as part of the project)     ## Eliminating Syntax Errors with Text Editor/Integrated Development Environment If you are experiencing a lot of syntax errors with your code, you may consider downloading your code and looking at it with your favorite text editor/IDE to help eliminate the syntax errors from your program. Recall in **_2. Intro to Python_**, **_Lesson 5. Scripting_**, **_Section 6. Programming Environment Setup_** you were provided with a number of text editors that are available to use with python (like _Atom_, _Sublime Text_, _Notepad++_). Additionally, when you installed Anaconda, the Spyder IDE (Integrated Development Environment) for python should be available through the _Anaconda Navigator_.     ## Cutting and Pasting Code in the Classroom If you cut and paste code directly from the classroom, it is very likely you will generate syntax errors with the single and double quotes. This is because the font type differences. If you are going to cut and paste code from the classroom, you will need to erase and replace any copied double or single quotes. Additionally, cutting and pasting code from the classroom may also result in issues regarding the proper code indention; therefore, it is not recommended to cut and paste code directly from the classroom.     ## Indention of Python Code Indention is used within Python to distinquish between blocks of code; whereas, with other programming languages, like Java and C++, they may have used curly brackets. The [PEP8 Style guide](https://www.python.org/dev/peps/pep-0008/) provides the standard for Python code and is what has been used for the programs within the Github respository and the Project workspace. The [PEP8 standard for indention](https://www.python.org/dev/peps/pep-0008/#indentation) is to use 4 spaces for each indention level. Not using 4 spaces for indention when editing **_check_images.py_**, will likely result in syntax errors. Be aware that using the _tab_ key within most text editors might not guarantee the proper 4 space indention. Additionally, not all text editors (including the **_Project Workspace_**) provide the proper 4 space indention as is used in the Python programs within the repository for this project.     ## Replacing None and Pass Statements When editing the functions provided in **_check_images.py_** you will need to replace [_None_](https://docs.python.org/3/library/constants.html#None) or the [_pass_](https://docs.python.org/3/tutorial/controlflow.html#pass-statements) statement with your code for that function. The pass statement does nothing, it's used so that the program will still run even though the functions have not been fully defined. Similarly the value of _None_ represents the absence of a value, it's used so that the program will still run even though the functions have not been fully defined.     ================================================ FILE: requirements.txt ================================================ torch==1.4.0 Pillow==7.1.2 torchvision==0.2.1