-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathvisualize_classifiers.py
More file actions
144 lines (128 loc) · 4.46 KB
/
visualize_classifiers.py
File metadata and controls
144 lines (128 loc) · 4.46 KB
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
import numpy as np
from PIL import Image, ImageDraw
img_width = 28
visualization_file_path = "../RCFC-kb/cmake-build-debug/output/output25/visualization.txt"
image_file_path = "../RCFC-kb/data/mnist/mnist_validation_3_8.txt"
cl_file_path = "../cmake-build-debug/remote/output/4digits_1/classifier.txt"
cf_file_path = "../cmake-build-debug/remote/output/4digits_1/code_fragment.txt"
# load classifiers is and their code fragment ids
cl_cf = {}
f = open(cl_file_path)
line = f.readline()
while line:
tokens = line.strip().split()
cl_id = int(tokens[1])
line = f.readline()
tokens = line.strip().split()
cf = []
for token in tokens:
cf.append(int(token))
cl_cf[cl_id] = cf
line = f.readline()
f.close()
# load code fragments and their filter ids
cf_filter = {}
f = open(cf_file_path)
line = f.readline()
while line:
tokens = line.strip().split()
cl_id = int(tokens[0])
filters = []
for token in tokens:
if token.startswith("D"):
filters.append(int(token[1:]))
cf_filter[cl_id] = filters
line = f.readline()
f.close()
def get_image(img_id):
img_file = np.loadtxt(image_file_path)
item = img_file[img_id]
img_class = int(item[-1])
data = item[:-1]
# denormalize
data = data * 255
data = data.reshape(28, 28)
return img_class, data
cl_clclass = []
filter_position = {}
def visualize_image(img_id, rectangle):
# load visualization data
actual_class = -1
predicted_class = -1
f = open(visualization_file_path)
line = f.readline()
while line:
# skip img_id lines to get to the the right image
for i in range(img_id):
line = f.readline()
line = f.readline()
line = f.readline()
tokens = line.strip().split()
read_img_id = int(tokens[0])
assert(read_img_id == img_id)
actual_class = int(tokens[1])
predicted_class = int(tokens[2])
print("actual class: " + str(actual_class) + " predicted class: " + str(predicted_class) + "\n")
line = f.readline() # classifier_id predicted_class ...
tokens = line.strip().split()
i = 0
while i < len(tokens):
classifier = tokens[i]
i += 1
classifier_class = tokens[i]
i += 1
cl_clclass.append((int(classifier), int(classifier_class)))
line = f.readline() # filter_id matched_position
tokens = line.strip().split()
i = 0
while i < len(tokens):
filter_id = int(tokens[i])
i += 1
position = int(tokens[i])
i += 1
size = int(tokens[i])
i += 1
dilated = int(tokens[i])
i += 1
filter_position[filter_id] = (position, size, dilated)
break
f.close()
img = get_image(img_id)
assert(img[0] == actual_class)
base_img = Image.fromarray(img[1]).convert("RGB")
dc = ImageDraw.Draw(base_img) # draw context
# draw dots/rectangles based on filter position from positive classifiers
filters_drawn = 0
for classifier in cl_clclass:
if classifier[1] == actual_class: # if positive classifier
classifier_id = classifier[0]
# get classifier code fragments
code_fragments = cl_cf[classifier_id]
for cf in code_fragments:
filters = cf_filter[cf] # filter
for filter in filters:
position = filter_position[filter][0]
size = filter_position[filter][1]
dilated = filter_position[filter][2]
if position >= 0:
filters_drawn += 1
if dilated:
size += (size-1)
# top right corner
y = position // img_width
x = position % img_width
if rectangle:
shape = [(x,y), (x+size,y+size)]
dc.rectangle(shape, outline="#ff0000")
else:
# center point
x += size//2
y += size//2
dc.point((x, y), fill="#ff0000")
print("filters drawn: "+str(filters_drawn))
base_img = base_img.resize((300, 300))
base_img.show()
for i in range(100):
visualize_image(i, True)
input("press any key to continue")
print('done')