-
Notifications
You must be signed in to change notification settings - Fork 0
/
adjust_results4_isadog.py
69 lines (64 loc) · 3.07 KB
/
adjust_results4_isadog.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
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
import logging
def adjust_results4_isadog(results_dic, dogfile):
"""
Adjusts the results dictionary to determine if classifier correctly
classified images 'as a dog' or 'not a dog' especially when not a match.
Demonstrates if model architecture correctly classifies dog images even if
it gets dog breed wrong (not a match).
Parameters:
results_dic - Dictionary with 'key' as image filename and 'value' as a
List. Where the list will contain the following items:
index 0 = pet image label (string)
index 1 = classifier label (string)
index 2 = 1/0 (int) where 1 = match between pet image
and classifer labels and 0 = no match between labels
------ where index 3 & index 4 are added by this function -----
NEW - index 3 = 1/0 (int) where 1 = pet image 'is-a' dog and
0 = pet Image 'is-NOT-a' dog.
NEW - index 4 = 1/0 (int) where 1 = Classifier classifies image
'as-a' dog and 0 = Classifier classifies image
'as-NOT-a' dog.
dogfile - A text file that contains names of all dogs from the classifier
function and dog names from the pet image files. This file has
one dog name per line dog names are all in lowercase with
spaces separating the distinct words of the dog name. Dog names
from the classifier function can be a string of dog names separated
by commas when a particular breed of dog has multiple dog names
associated with that breed (ex. maltese dog, maltese terrier,
maltese) (string - indicates text file's filename)
Returns:
None - results_dic is mutable data type so no return needed.
"""
dognames_dic = dict()
with open(dogfile, "r") as infile:
line = infile.readline()
while line != "":
line = line.rstrip()
if line not in dognames_dic:
dognames_dic[line] = 1
else:
logging.warning('Dog name already exists in dic. Should not find any duplicate dog names in dognames.txt')
line = infile.readline()
for key in results_dic:
if results_dic[key][0] in dognames_dic:
is_dog = False
labels = results_dic[key][1].split(',')
for label in labels:
if label.strip() in dognames_dic:
is_dog = True
if is_dog:
results_dic[key].extend((1, 1))
else:
results_dic[key].extend((1, 0))
else:
is_dog = False
labels = results_dic[key][1].split(',')
for label in labels:
if label.strip() in dognames_dic:
is_dog = True
if is_dog:
results_dic[key].extend((0, 1))
else:
results_dic[key].extend((0, 0))