-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathvisualize_filters.py
More file actions
197 lines (158 loc) · 5.72 KB
/
visualize_filters.py
File metadata and controls
197 lines (158 loc) · 5.72 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
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
import numpy as np
from PIL import Image, ImageDraw
# this is old file
total_images = 1984
img_width = 28
resize_factor = 10
base_path = '../remote/output/RXCSi/output-10-digit/10-digits-39/'
image_file_path = "../RCFC-kb/data/mnist/mnist_validation_all.txt"
# base_path = '../RCFC-kb/cmake-build-debug/output/out11/'
# image_file_path = "../RCFC-kb/data/mnist/mnist_validation_3_8.txt"
# visualization_file_path: str = base_path + 'visualization.txt'
cl_file_path = base_path + '5000000/classifier.txt'
cf_file_path = base_path + '5000000/code_fragment.txt'
filter_file_path = base_path + '5000000/promising_filter.txt'
min_experience = 20
max_error = 10
img_file = np.loadtxt(image_file_path)
def get_blank_image(val):
data = np.zeros((img_width, img_width))
data += val
return data
def get_image(img_id):
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
filter_data = {}
def load_filter_data():
f_id = -1
x = -1
y = -1
size = -1
dilated = False
f = open(filter_file_path)
line = f.readline()
while line:
tokens = line.strip().split()
f_id = int(tokens[1])
x = int(tokens[3])
y = int(tokens[5])
size = int(tokens[7])
dilated = bool(int(tokens[9]))
line = f.readline()
tokens = line.strip().split()
lb = []
ub = []
for i in range(size*size+1):
if i == 0: # skip the first string
continue
lb.append(float(tokens[i]))
line = f.readline()
tokens = line.strip().split()
for i in range(size*size+1):
if i == 0:
continue
ub.append(float(tokens[i]))
line = f.readline()
filter_data[f_id] = (lb, ub, x, y, size, dilated)
load_filter_data()
# update lower and upper bounds from filter
def update_bounds(img_l, img_u, lb, ub, start_x, start_y, size, dilated):
step = 1
if dilated:
step = 2
effective_size = size + size - 1
# for y in range(size):
# for x in range(size):
# img_l[start_x+x*step, (start_y+y*step)] = .4
# img_u[start_x+x*step, (start_y+y*step)] = .6
for y in range(size):
for x in range(size):
if img_l[start_x+x*step, (start_y+y*step)] > lb[y*size + x]:
img_l[start_x+x*step, (start_y+y*step)] = lb[y*size + x]
if img_u[start_x+x*step, (start_y+y*step)] < ub[y*size + x]:
img_u[start_x+x*step, (start_y+y*step)] = ub[y*size + x]
def get_pixel_color(img_l, img_u, x, y):
color = (255, 0, 0)
if img_l[x, y] == 1 and img_u[x, y] == 0: # if the pixel interval has not be initialized then its don't care
return color
# if img_u[x, y] - img_l[x, y] > 0.5: # wide interval means don't care
# return color
# if img_l[x, y] < 0.25 and img_u[x, y] > 0.75: # wide interval means don't care
# return color
mid = (img_l[x,y] + img_u[x,y]) / 2
# real to 255 scal
# e
c = int(mid*255)
color = (c, c, c)
# color = "ff0000"
# if mid < 0.25:
# color = "000000" # black
# elif mid < 0.5:
# color = "D3D3D3" # light grey
return color
def visualize_intervals(img_l, img_u, dc):
for y in range(img_width):
for x in range(img_width):
dc.point((x, y), get_pixel_color(img_l, img_u, x,y))
def match_filter_with_image(fid, image):
lb, ub, x, y, size, dilated = filter_data[fid]
img = image.reshape(784,)
step = 1
effective_filter_size = size
if dilated:
step = 2
effective_filter_size = size + size -1;
match_failed = False # flag that controls if the next position to be evaluated when current does not match
i = y
j = x
k = 0
while k<size and not match_failed:
l = 0
while l<size and not match_failed:
if(img[i*img_width+j + k*step*img_width+l*step] < lb[k*size+l]
or img[i*img_width+j + k*step*img_width+l*step] > ub[k*size+l]):
match_failed = True
l += 1
k += 1
if not match_failed:
return True
return False
def visualize_image(img_id, rectangle):
for img_id in range(total_images):
img_class, img = get_image(img_id)
base_img = Image.fromarray(img).convert("RGB")
dc = ImageDraw.Draw(base_img) # draw context
base_img_intervals = Image.fromarray(img).convert("RGB")
dc_intervals = ImageDraw.Draw(base_img_intervals) # draw context
# try to match all filters
img_l = get_blank_image(1)
img_u = get_blank_image(0)
img = img / 255
filters_drawn = 0
for fid in filter_data:
lb, ub, x, y, size, dilated = filter_data[fid]
if match_filter_with_image(fid, img):
filters_drawn += 1
update_bounds(img_l, img_u, lb, ub, x, y, size, dilated)
if rectangle:
shape = [(x, y), (x + size-1, y + size-1)]
dc.rectangle(shape, fill="#0000ff")
else:
# center point
x += size // 2
y += size // 2
dc.point((x, y), fill="#0000ff")
base_img = base_img.resize((img_width*resize_factor, img_width*resize_factor))
base_img.show()
visualize_intervals(img_l, img_u, dc_intervals)
base_img_intervals = base_img_intervals.resize((img_width*resize_factor, img_width*resize_factor))
base_img_intervals.show()
print("filters drawn: "+str(filters_drawn))
input("press any key to continue")
visualize_image(-1, True)
print('done')