1
+ #!/usr/bin/env python3
2
+ # -*- coding: utf-8 -*-
3
+ # */AIPND-revision/intropyproject-classify-pet-images/adjust_results4_isadog_hints.py
4
+ #
5
+ # PROGRAMMER:
6
+ # DATE CREATED:
7
+ # REVISED DATE:
8
+ # PURPOSE: This is a *hints* file to help guide students in creating the
9
+ # function adjust_results4_isadog that adjusts the results dictionary
10
+ # to indicate whether or not the pet image label is of-a-dog,
11
+ # and to indicate whether or not the classifier image label is of-a-dog.
12
+ # All dog labels from both the pet images and the classifier function
13
+ # will be found in the dognames.txt file. We recommend reading all the
14
+ # dog names in dognames.txt into a dictionary where the 'key' is the
15
+ # dog name (from dognames.txt) and the 'value' is one. If a label is
16
+ # found to exist within this dictionary of dog names then the label
17
+ # is of-a-dog, otherwise the label isn't of a dog. Alternatively one
18
+ # could also read all the dog names into a list and then if the label
19
+ # is found to exist within this list - the label is of-a-dog, otherwise
20
+ # the label isn't of a dog.
21
+ # This function inputs:
22
+ # -The results dictionary as results_dic within adjust_results4_isadog
23
+ # function and results for the function call within main.
24
+ # -The text file with dog names as dogfile within adjust_results4_isadog
25
+ # function and in_arg.dogfile for the function call within main.
26
+ # This function uses the extend function to add items to the list
27
+ # that's the 'value' of the results dictionary. You will be adding the
28
+ # whether or not the pet image label is of-a-dog as the item at index
29
+ # 3 of the list and whether or not the classifier label is of-a-dog as
30
+ # the item at index 4 of the list. Note we recommend setting the values
31
+ # at indices 3 & 4 to 1 when the label is of-a-dog and to 0 when the
32
+ # label isn't a dog.
33
+ #
34
+ ##
35
+ # TODO 4: EDIT and ADD code BELOW to do the following that's stated in the
36
+ # comments below that start with "TODO: 4" for the adjust_results4_isadog
37
+ # function. Specifically EDIT and ADD code to define the
38
+ # adjust_results4_isadog function. Notice that this function doesn't return
39
+ # anything because the results_dic dictionary that is passed into the
40
+ # function is a mutable data type so no return is needed.
41
+ #
42
+ def adjust_results4_isadog (results_dic , dogfile ):
43
+ """
44
+ Adjusts the results dictionary to determine if classifier correctly
45
+ classified images 'as a dog' or 'not a dog' especially when not a match.
46
+ Demonstrates if model architecture correctly classifies dog images even if
47
+ it gets dog breed wrong (not a match).
48
+ Parameters:
49
+ results_dic - Dictionary with 'key' as image filename and 'value' as a
50
+ List. Where the list will contain the following items:
51
+ index 0 = pet image label (string)
52
+ index 1 = classifier label (string)
53
+ index 2 = 1/0 (int) where 1 = match between pet image
54
+ and classifer labels and 0 = no match between labels
55
+ ------ where index 3 & index 4 are added by this function -----
56
+ NEW - index 3 = 1/0 (int) where 1 = pet image 'is-a' dog and
57
+ 0 = pet Image 'is-NOT-a' dog.
58
+ NEW - index 4 = 1/0 (int) where 1 = Classifier classifies image
59
+ 'as-a' dog and 0 = Classifier classifies image
60
+ 'as-NOT-a' dog.
61
+ dogfile - A text file that contains names of all dogs from the classifier
62
+ function and dog names from the pet image files. This file has
63
+ one dog name per line dog names are all in lowercase with
64
+ spaces separating the distinct words of the dog name. Dog names
65
+ from the classifier function can be a string of dog names separated
66
+ by commas when a particular breed of dog has multiple dog names
67
+ associated with that breed (ex. maltese dog, maltese terrier,
68
+ maltese) (string - indicates text file's filename)
69
+ Returns:
70
+ None - results_dic is mutable data type so no return needed.
71
+ """
72
+ # Creates dognames dictionary for quick matching to results_dic labels from
73
+ # real answer & classifier's answer
74
+ dognames_dic = dict ()
75
+
76
+ # Reads in dognames from file, 1 name per line & automatically closes file
77
+ with open (dogfile , "r" ) as infile :
78
+ # Reads in dognames from first line in file
79
+ line = infile .readline ()
80
+
81
+ # Processes each line in file until reaching EOF (end-of-file) by
82
+ # processing line and adding dognames to dognames_dic with while loop
83
+ while line != "" :
84
+
85
+ # TODO: 4a. REPLACE pass with CODE to remove the newline character
86
+ # from the variable line
87
+ #
88
+ # Process line by striping newline from line
89
+ pass
90
+
91
+ # TODO: 4b. REPLACE pass with CODE to check if the dogname(line)
92
+ # exists within dognames_dic, then if the dogname(line)
93
+ # doesn't exist within dognames_dic then add the dogname(line)
94
+ # to dognames_dic as the 'key' with the 'value' of 1.
95
+ #
96
+ # adds dogname(line) to dogsnames_dic if it doesn't already exist
97
+ # in the dogsnames_dic dictionary
98
+ pass
99
+
100
+ # Reads in next line in file to be processed with while loop
101
+ # if this line isn't empty (EOF)
102
+ line = infile .readline ()
103
+
104
+
105
+ # Add to whether pet labels & classifier labels are dogs by appending
106
+ # two items to end of value(List) in results_dic.
107
+ # List Index 3 = whether(1) or not(0) Pet Image Label is a dog AND
108
+ # List Index 4 = whether(1) or not(0) Classifier Label is a dog
109
+ # How - iterate through results_dic if labels are found in dognames_dic
110
+ # then label "is a dog" index3/4=1 otherwise index3/4=0 "not a dog"
111
+ for key in results_dic :
112
+
113
+ # Pet Image Label IS of Dog (e.g. found in dognames_dic)
114
+ if results_dic [key ][0 ] in dognames_dic :
115
+
116
+ # Classifier Label IS image of Dog (e.g. found in dognames_dic)
117
+ # appends (1, 1) because both labels are dogs
118
+ if results_dic [key ][1 ] in dognames_dic :
119
+ results_dic [key ].extend ((1 , 1 ))
120
+
121
+ # TODO: 4c. REPLACE pass BELOW with CODE that adds the following to
122
+ # results_dic dictionary for the key indicated by the
123
+ # variable key - append (1,0) to the value using
124
+ # the extend list function. This indicates
125
+ # the pet label is-a-dog, classifier label is-NOT-a-dog.
126
+ #
127
+ # Classifier Label IS NOT image of dog (e.g. NOT in dognames_dic)
128
+ # appends (1,0) because only pet label is a dog
129
+ else :
130
+ pass
131
+
132
+ # Pet Image Label IS NOT a Dog image (e.g. NOT found in dognames_dic)
133
+ else :
134
+ # TODO: 4d. REPLACE pass BELOW with CODE that adds the following to
135
+ # results_dic dictionary for the key indicated by the
136
+ # variable key - append (0,1) to the value uisng
137
+ # the extend list function. This indicates
138
+ # the pet label is-NOT-a-dog, classifier label is-a-dog.
139
+ #
140
+ # Classifier Label IS image of Dog (e.g. found in dognames_dic)
141
+ # appends (0, 1)because only Classifier labe is a dog
142
+ if results_dic [key ][1 ] in dognames_dic :
143
+ pass
144
+
145
+ # TODO: 4e. REPLACE pass BELOW with CODE that adds the following to
146
+ # results_dic dictionary for the key indicated by the
147
+ # variable key - append (0,0) to the value using the
148
+ # extend list function. This indicates
149
+ # the pet label is-NOT-a-dog, classifier label is-NOT-a-dog.
150
+ #
151
+ # Classifier Label IS NOT image of Dog (e.g. NOT in dognames_dic)
152
+ # appends (0, 0) because both labels aren't dogs
153
+ else :
154
+ pass
155
+
0 commit comments