-
Notifications
You must be signed in to change notification settings - Fork 7
/
deconv_utils.py
256 lines (225 loc) · 7.85 KB
/
deconv_utils.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
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
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
import cv2
import numpy as np
import matplotlib.pyplot as plt
from scipy.misc import imread, imresize
from scipy import ndimage
#Global Variables
#Parameters to Process blob
class Param:
process_conv_size = (4,4)
num_dilation = 20
num_erosion = 15
def get_filter_scores2(amax, model, im, activs,caches,layer, class_no, percentile_thresh=80):
filter_scores = []
for zero,i,x,y in amax:
#print i,(x,y)
back_grad = deconv(model,activs,caches,layer,(0,i,x,y))
xmin,xmax,ymin,ymax = find_box(back_grad,percentile_thresh=80)
if (xmin,xmax,ymin,ymax) == (0,0,0,0):
continue
n_score = get_score(im,class_no,model, xmin,xmax,ymin,ymax)
filter_scores += [[i,n_score,xmin,xmax,ymin,ymax]]
return filter_scores
def get_filter_scores(amax, model, im, activs,caches,layer, class_no, percentile_thresh=80,use_blob = False, verbose=False):
filter_scores = []
for zero,i,x,y in amax:
back_grad = deconv(model,activs,caches,layer,(0,i,x,y))
plt.figure()
plt.imshow(back_grad[0].transpose(1,2,0))
if use_blob:
blob = find_blob(back_grad,percentile_thresh=80)
else:
xmin,xmax,ymin,ymax = find_box(back_grad,percentile_thresh=percentile_thresh)
blob = np.zeros(im.shape)
blob[0,:,xmin:(xmax+1),ymin:(ymax+1)] = 1
if np.sum(blob) == 0:
continue
n_score = get_score(im,class_no,model, mask = blob)
if (verbose):
print "score =" ,n_score
if use_blob:
filter_scores += [[i,n_score,blob,0,0,0,0]]
else:
filter_scores += [[i,n_score,blob,xmin,xmax,ymin,ymax]]
return filter_scores
def get_fast_filter_scores(amax, model, im, activs,caches,layer, class_no, percentile_thresh=80,use_blob = False, verbose=False):
filter_scores = []
k=0
for zero,i,x,y in amax:
#if np.sum(activs[layer][0,i]>0)
back_grad = deconv(model,activs,caches,layer,(0,i,x,y))
if use_blob:
blob = find_blob(back_grad,percentile_thresh=80)
else:
xmin,xmax,ymin,ymax = find_box(back_grad,percentile_thresh=percentile_thresh)
blob = np.zeros(im.shape)
blob[0,:,xmin:(xmax+1),ymin:(ymax+1)] = 1
if np.sum(blob) == 0:
continue
n_score = -k
if verbose:
print "score =" ,n_score
if use_blob:
filter_scores += [[i,n_score,blob,0,0,0,0]]
else:
filter_scores += [[i,n_score,blob,xmin,xmax,ymin,ymax]]
k=k+1
return filter_scores
def get_backgrad (activs, model, class_no, layer, caches):
back_grad= np.zeros(activs[15].shape)
back_grad[0,class_no]=1
for i in reversed(range(layer,16)):
back_grad = (back_grad>0)*back_grad
back_grad, _ = model.backward(back_grad,caches[i])
return back_grad
def deconv(model,activs,caches,layer,neuron):
back_grad = np.zeros(activs[layer].shape)
back_grad[neuron] = 1
for i in reversed(range(layer+1)):
back_grad = (back_grad>0)*back_grad
back_grad, _ = model.backward(back_grad,caches[i])
return back_grad
#Define Function for deconv
def deconv_2(model,activs, caches,layer,neuron,slayer):
back_grad = np.zeros(activs[slayer].shape)
print back_grad.shape
if(len(neuron)==3):
back_grad[neuron[0],neuron[1]] = 1
else:
back_grad[neuron] = 1
for i in reversed(range(layer,slayer+1)):
back_grad = (back_grad>0)*back_grad
back_grad, _ = model.backward(back_grad,caches[i])
return back_grad
#Takes the processed image to give activations and caches for entire forward pass
def get_activs(model, im, num_layers=16):
activ = im
caches = []
activs = []
for i in range(num_layers):
out,cache = model.forward(activ,start = i, end=i)
activ = out;
activs += [activ]
caches += [cache]
return activs,caches
###############TO-DO###################
def deconv_batch(model, ims, layer=10):
#Function to get deconv of a batch of images
#ims : N X 3 X 224 X 224
pass
##############TO DO###########
#Function to get blob
def get_blob():
pass
#Image Utilities
def load_image(imgf):
im = imread(imgf)
im = resize_image(im)
return process_image(im)
def load_image_cv2(imgf):
im = cv2.imread(imgf)
im = resize_image(im)
return process_image(im)
def resize_image(im):
return cv2.resize(im, (224, 224)).astype(np.float32)
def process_image(im):
im[:,:,0] -= 103.939
im[:,:,1] -= 116.779
im[:,:,2] -= 123.68
im = im.transpose((2,0,1))
im = np.expand_dims(im, axis=0)
return im
def deprocess_image(img):
im = img[0].transpose(1,2,0)
im[:,:,0] += 103.939
im[:,:,1] += 116.779
im[:,:,2] += 123.68
im = im.astype(np.uint8)
return im
#Takes input
def plot_image(im):
im = deprocess_image(im)
plt.figure()
plt.imshow(im)
def plot_image_cv2(im):
im = deprocess_image(im)
im = cv2.cvtColor(im, cv2.cv.CV_BGR2RGB)
plt.figure()
plt.imshow(im)
#cv.imshow("Image",im)
#import matplotlib.pyplot as plt
def grid_plot_activs(act):
grid = visualize_grid((act).transpose(1,2,3,0))
plt.imshow(grid.transpose(2,0,1)[0])
#plt.axis('off')
plt.gcf().set_size_inches(10, 10)
plt.show()
def find_box(back_grad, percentile_thresh = 40):
meanimg = np.mean(back_grad[0],axis=0)
if np.sum(abs(meanimg)) < 1e-16:
return 0,0,0,0
thresh = np.percentile(meanimg[meanimg>0],[percentile_thresh])[0]
meanimg = np.mean(back_grad[0],axis=0)
threshimg = np.mean(back_grad[0],axis=0)>thresh
#plt.imshow(np.mean(back_grad[0],axis=0)>thresh)
idxs = np.where(np.sum(threshimg,axis = 1)>0)[0]
xmin,xmax = min(idxs),max(idxs)
idxs = np.where(np.sum(threshimg,axis = 0)>0)[0]
ymin,ymax = min(idxs),max(idxs)
return xmin,xmax,ymin,ymax
#Get Score for a given image and class number. If mask != 0, it uses the mask area, else defaults to the area specified by the rectangle specified by (xmin,ymin) (xmax,ymax)
def get_score(im,class_no, model, xmin=0,xmax=0,ymin=0,ymax=0,mask=0):
#print xmin,xmax,ymin,ymax
if np.sum(mask )== 0:
mask = np.zeros(im.shape)
mask[0,:,xmin:(xmax+1),ymin:(ymax+1)] = 1
newim = im.copy()*mask
scores,_ = model.forward(newim)
return scores[0,class_no]
#Given Activations, Deconv, Layer Number
#k = No of Neurons of Interest
#Returns the filters of interest
def filter_of_intr(activs,back_grad,kmax,layer):
cum =[]
tally = []
tmp1 = activs[layer] *back_grad
k=0
while k<kmax:
index = np.unravel_index(tmp1.argmax(), tmp1.shape)
#print index[1]
if index[1] not in tally:
tally += [index[1]]
cum+= [index]
k+=1
tmp1[index] = -1000
return cum
def grabCut(im2,xmin,xmax,ymin,ymax):
h,w = im2.shape[:2]
mask = np.zeros((h,w),dtype='uint8')
rect = (ymin,xmin,ymax-ymin,xmax-xmin)
#rect = (10,10,213,213)
tmp1 = np.zeros((1, 13 * 5))
tmp2 = np.zeros((1, 13 * 5))
cv2.grabCut(im2,mask,rect,tmp1,tmp2,10,mode=cv2.GC_INIT_WITH_RECT)
mask[mask==2]=0
return mask
def process_blob(cim):
#cim = ndimage.binary_erosion(cim>0)
for i in range(4):
cim = ndimage.binary_erosion(cim>0)
cim=ndimage.binary_dilation(cim>0)
filterk = np.ones(Param.process_conv_size);
cim = ndimage.convolve(cim, filterk, mode='constant', cval=0.0)
for i in range(Param.num_dilation):
cim = ndimage.binary_dilation(cim>0)
for i in range(Param.num_erosion):
cim = ndimage.binary_erosion(cim>0)
return cim
def find_blob(back_grad, percentile_thresh = 40):
meanimg = np.mean(back_grad[0],axis=0)
if np.sum(abs(meanimg)) < 1e-16:
return 0,0,0,0
thresh = np.percentile(meanimg[meanimg>0],[percentile_thresh])[0]
meanimg = np.mean(back_grad[0],axis=0)
threshimg = np.mean(back_grad[0],axis=0)>thresh
return process_blob(threshimg)