|
| 1 | +#!/usr/bin/env python3 |
| 2 | +# -*- coding: utf-8 -*- |
| 3 | +# */AIPND-revision/intropyproject-classify-pet-images/adjust_results4_isadog.py |
| 4 | +# |
| 5 | +# PROGRAMMER: JAYARAJ J |
| 6 | +# DATE CREATED: 18 April 2020 |
| 7 | +# REVISED DATE: |
| 8 | +# PURPOSE: Create a function adjust_results4_isadog that adjusts the results |
| 9 | +# dictionary to indicate whether or not the pet image label is of-a-dog, |
| 10 | +# and to indicate whether or not the classifier image label is of-a-dog. |
| 11 | +# All dog labels from both the pet images and the classifier function |
| 12 | +# will be found in the dognames.txt file. We recommend reading all the |
| 13 | +# dog names in dognames.txt into a dictionary where the 'key' is the |
| 14 | +# dog name (from dognames.txt) and the 'value' is one. If a label is |
| 15 | +# found to exist within this dictionary of dog names then the label |
| 16 | +# is of-a-dog, otherwise the label isn't of a dog. Alternatively one |
| 17 | +# could also read all the dog names into a list and then if the label |
| 18 | +# is found to exist within this list - the label is of-a-dog, otherwise |
| 19 | +# the label isn't of a dog. |
| 20 | +# This function inputs: |
| 21 | +# -The results dictionary as results_dic within adjust_results4_isadog |
| 22 | +# function and results for the function call within main. |
| 23 | +# -The text file with dog names as dogfile within adjust_results4_isadog |
| 24 | +# function and in_arg.dogfile for the function call within main. |
| 25 | +# This function uses the extend function to add items to the list |
| 26 | +# that's the 'value' of the results dictionary. You will be adding the |
| 27 | +# whether or not the pet image label is of-a-dog as the item at index |
| 28 | +# 3 of the list and whether or not the classifier label is of-a-dog as |
| 29 | +# the item at index 4 of the list. Note we recommend setting the values |
| 30 | +# at indices 3 & 4 to 1 when the label is of-a-dog and to 0 when the |
| 31 | +# label isn't a dog. |
| 32 | +# |
| 33 | +## |
| 34 | + |
| 35 | +def adjust_results4_isadog(results_dic, dogfile): |
| 36 | + """ |
| 37 | + Adjusts the results dictionary to determine if classifier correctly |
| 38 | + classified images 'as a dog' or 'not a dog' especially when not a match. |
| 39 | + Demonstrates if model architecture correctly classifies dog images even if |
| 40 | + it gets dog breed wrong (not a match). |
| 41 | + Parameters: |
| 42 | + results_dic - Dictionary with 'key' as image filename and 'value' as a |
| 43 | + List. Where the list will contain the following items: |
| 44 | + index 0 = pet image label (string) |
| 45 | + index 1 = classifier label (string) |
| 46 | + index 2 = 1/0 (int) where 1 = match between pet image |
| 47 | + and classifer labels and 0 = no match between labels |
| 48 | + ------ where index 3 & index 4 are added by this function ----- |
| 49 | + NEW - index 3 = 1/0 (int) where 1 = pet image 'is-a' dog and |
| 50 | + 0 = pet Image 'is-NOT-a' dog. |
| 51 | + NEW - index 4 = 1/0 (int) where 1 = Classifier classifies image |
| 52 | + 'as-a' dog and 0 = Classifier classifies image |
| 53 | + 'as-NOT-a' dog. |
| 54 | + dogfile - A text file that contains names of all dogs from the classifier |
| 55 | + function and dog names from the pet image files. This file has |
| 56 | + one dog name per line dog names are all in lowercase with |
| 57 | + spaces separating the distinct words of the dog name. Dog names |
| 58 | + from the classifier function can be a string of dog names separated |
| 59 | + by commas when a particular breed of dog has multiple dog names |
| 60 | + associated with that breed (ex. maltese dog, maltese terrier, |
| 61 | + maltese) (string - indicates text file's filename) |
| 62 | + Returns: |
| 63 | + None - results_dic is mutable data type so no return needed. |
| 64 | + """ |
| 65 | + dognames_dic = {} |
| 66 | + with open(dogfile) as f: |
| 67 | + dogs = f.readlines() |
| 68 | + |
| 69 | + for dog in dogs: |
| 70 | + dog = dog.rstrip() |
| 71 | + dognames_dic[dog] = 1 |
| 72 | + |
| 73 | + for img_file in results_dic: |
| 74 | + img_is_a_dog = 1 if results_dic[img_file][0] in dognames_dic else 0 |
| 75 | + pred_is_a_dog = 1 if results_dic[img_file][1] in dognames_dic else 0 |
| 76 | + results_dic[img_file].append(img_is_a_dog) |
| 77 | + results_dic[img_file].append(pred_is_a_dog) |
| 78 | + return None |
0 commit comments