-
Notifications
You must be signed in to change notification settings - Fork 0
/
adjust_results4_isadog_hints.py
155 lines (142 loc) · 8.42 KB
/
adjust_results4_isadog_hints.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
#!/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