-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathutils.py
More file actions
197 lines (183 loc) · 7.68 KB
/
utils.py
File metadata and controls
197 lines (183 loc) · 7.68 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 tensorflow as tf
import numpy as np
import math
from PIL import Image
import os
from features_optimization import normalize_std
def save_optimized_image_to_disk(opt_output, channel,n_channels,key,path):
opt_output_rescaled = (opt_output - opt_output.min())
opt_output_rescaled *= (255/(opt_output_rescaled.max()+0.0001))
im = Image.fromarray(opt_output_rescaled.astype(np.uint8))
file_name="opt_"+str(channel).zfill(len(str(n_channels)))+".bmp"
folder_name=path+"/feature_maps/"+key+"/optimization"
if not os.path.exists(folder_name):
os.makedirs(folder_name)
im.save(folder_name+"/"+file_name)
def save_images_to_disk(result_imgs,input_imgs,gt_imgs, path):
result_imgs=(np.clip(result_imgs, 0, 1) * 255).round().astype(np.uint8)
for j in xrange(result_imgs.shape[0]):
im = Image.fromarray(result_imgs[j])
file_name="output.bmp"
im_folder=str(j).zfill(len(str(result_imgs.shape[0])))
folder_name=path+"/output/"+im_folder
if not os.path.exists(folder_name):
os.makedirs(folder_name)
im.save(folder_name+"/"+file_name)
input_imgs=(np.clip(input_imgs, 0, 1) * 255).round().astype(np.uint8)
for j in xrange(input_imgs.shape[0]):
im = Image.fromarray(input_imgs[j])
file_name="input.bmp"
im_folder=str(j).zfill(len(str(input_imgs.shape[0])))
folder_name=path+"/input/"+im_folder
if not os.path.exists(folder_name):
os.makedirs(folder_name)
im.save(folder_name+"/"+file_name)
gt_imgs=(np.clip(gt_imgs, 0, 1) * 255).round().astype(np.uint8)
for j in xrange(gt_imgs.shape[0]):
im = Image.fromarray(gt_imgs[j])
file_name="ground_truth.bmp"
im_folder=str(j).zfill(len(str(gt_imgs.shape[0])))
folder_name=path+"/ground_truth/"+im_folder
if not os.path.exists(folder_name):
os.makedirs(folder_name)
im.save(folder_name+"/"+file_name)
def save_feature_maps_to_disk(feature_maps, weights, deconv, feature_names,path):
for ft, w, d, key in zip(feature_maps, weights, deconv, feature_names):
for l in xrange(ft.shape[3]):
ft_img=ft[:,:,:,l]
ft_img = (ft_img - ft_img.min())
ft_img*=(255/(ft_img.max()+0.0001))
for k in xrange(ft.shape[0]):
ch_img=ft_img[k,:,:].astype(np.uint8)
im = Image.fromarray(ch_img)
file_name=str(l).zfill(len(str(ft.shape[3])))+".bmp"
im_folder=str(k).zfill(len(str(ft.shape[0])))
folder_name=path+"/feature_maps/"+key+"/"+im_folder
if not os.path.exists(folder_name):
os.makedirs(folder_name)
im.save(folder_name+"/"+file_name)
if w is not None:
kernel=w.eval()
kernel_img = (kernel - kernel.min())
kernel_img*=(255/(kernel_img.max()+0.0001))
for k in xrange(kernel_img.shape[3]):
im = Image.fromarray(kernel_img[:,:,:,k].astype(np.uint8))
k_file_name="W_"+str(k).zfill(len(str(ft.shape[3])))+".bmp"
k_folder_name=path+"/feature_maps/"+key+"/kernels"
if not os.path.exists(k_folder_name):
os.makedirs(k_folder_name)
im.save(k_folder_name+"/"+k_file_name)
if d is not None:
for i in xrange(d.shape[4]):
for j in xrange(d.shape[0]):
d_img=d[j,:,:,:,i]
d_img = (d_img-d_img.min())
d_img*=(255/(d_img.max()+0.0001))
deconv_img=d_img.astype(np.uint8)
im = Image.fromarray(deconv_img)
file_name=str(i).zfill(len(str(d.shape[4])))+".bmp"
im_folder=str(j).zfill(len(str(d.shape[0])))
folder_name=path+"/deconv/"+key+"/"+im_folder
if not os.path.exists(folder_name):
os.makedirs(folder_name)
im.save(folder_name+"/"+file_name)
def save_max_activations_to_disk(max_activation, feature_names,path):
for actv, key in zip(max_activation, feature_names):
for ch in xrange(actv[0].shape[4]):
for n in xrange(actv[0].shape[0]):
actv_img=actv[1][n,:,:,ch]
actv_img = (actv_img-actv_img.min())
actv_img*=(255/(actv_img.max()+0.0001))
max_actv_img=actv_img.astype(np.uint8)
input_img=actv[0][n,:,:,:,ch]
input_img = (input_img-input_img.min())
input_img*=(255/(input_img.max()+0.0001))
max_actv_input_img=input_img.astype(np.uint8)
actv_im = Image.fromarray(max_actv_img)
actv_file_name=str(n).zfill(len(str(actv[0].shape[0]-1)))+".bmp"
input_im = Image.fromarray(max_actv_input_img)
input_file_name="input_"+str(n).zfill(len(str(actv[0].shape[0]-1)))+".bmp"
folder_name=path+"/feature_maps/"+key+"/max_activations/"+str(ch).zfill(len(str(actv[0].shape[4]-1)))
if not os.path.exists(folder_name):
os.makedirs(folder_name)
actv_im.save(folder_name+"/"+actv_file_name)
input_im.save(folder_name+"/"+input_file_name)
def put_features_on_grid_np(features):
iy=features.shape[1]
ix=features.shape[2]
n_ch=features.shape[3]
b_size=features.shape[0]
square_size=int(math.ceil(np.sqrt(n_ch)))
z_pad=square_size**2-n_ch
black=min(0,features.min())
pad=1+int(ix/64)
features = np.pad(features, [[0,0],[0,0],[0,0],[0,z_pad]], mode='constant',constant_values=black)
features = np.reshape(features,[b_size,iy,ix,square_size,square_size])
features = np.pad(features, [[0,0],[pad,0],[pad,0],[0,0],[0,0]], mode='constant',constant_values=black)
iy+=pad
ix+=pad
features = np.transpose(features,(0,3,1,4,2))
features = np.reshape(features,[-1,square_size*iy,square_size*ix,1])
return np.pad(features, [[0,0],[0,pad],[0,pad],[0,0]], mode='constant',constant_values=black)
def put_kernels_on_grid_np(kernels):
iy=kernels.shape[0]
ix=kernels.shape[1]
n_ch=kernels.shape[3]
square_size=int(math.ceil(np.sqrt(n_ch)))
z_pad=square_size**2-n_ch
black=min(0,kernels.min())
pad=1+int(ix/64)
kernels = np.pad(kernels, [[0,0],[0,0],[0,0],[0,z_pad]], mode='constant',constant_values=black)
kernels = np.transpose(kernels,(0,1,3,2))
kernels = np.reshape(kernels,[iy,ix,square_size,square_size,3])
kernels = np.pad(kernels, [[pad,0],[pad,0],[0,0],[0,0],[0,0]], mode='constant',constant_values=black)
iy+=pad
ix+=pad
kernels = np.transpose(kernels,(2,0,3,1,4))
kernels = np.reshape(kernels,[square_size*iy,square_size*ix,3])
kernels = np.pad(kernels, [[0,pad],[0,pad],[0,0]], mode='constant',constant_values=black)
return np.expand_dims(kernels, axis=0)
def put_grads_on_grid_np(grads):
b_size=grads.shape[0]
iy=grads.shape[1]
ix=grads.shape[2]
n_ch=grads.shape[4]
square_size=int(math.ceil(np.sqrt(n_ch)))
z_pad=square_size**2-n_ch
black=min(0,grads.min())
pad=1+int(ix/64)
grads = np.pad(grads, [[0,0],[0,0],[0,0],[0,0],[0,z_pad]], mode='constant',constant_values=black)
grads = np.transpose(grads,(0,1,2,4,3))
grads = np.reshape(grads,[b_size,iy,ix,square_size,square_size,3])
grads = np.pad(grads, [[0,0],[pad,0],[pad,0],[0,0],[0,0],[0,0]], mode='constant',constant_values=black)
iy+=pad
ix+=pad
grads = np.transpose(grads,(0,3,1,4,2,5))
grads = np.reshape(grads,[b_size,square_size*iy,square_size*ix,3])
return np.pad(grads, [[0,0],[0,pad],[0,pad],[0,0]], mode='constant',constant_values=black)
def put_features_on_grid_tf (features, cy=1, pad=4):
iy=tf.shape(features)[1]
ix=tf.shape(features)[2]
b_size=tf.shape(features)[0]
features = tf.reshape(features,tf.pack([b_size,iy,ix,cy,-1]))
cx=tf.shape(features)[4]
features = tf.pad(features, tf.constant( [[0,0],[pad,0],[pad,0],[0,0],[0,0]]))
iy+=pad
ix+=pad
features = tf.transpose(features,(0,3,1,4,2))
return tf.reshape(features,tf.pack([-1,cy*iy,cx*ix,1]))
def deconvolution(x, feedDict, ft_maps, features_list, batch_size, input_size):
deconv=[]
sess=tf.get_default_session()
for ft_map, key in zip(ft_maps, features_list):
ft_shape=ft_map.get_shape()
img_shape=input_size
ft_deconv=np.empty((batch_size,)+img_shape+(ft_shape[3],))
for ch in xrange(ft_shape[3]):
score = tf.reduce_mean(ft_map[:,:,:,ch])
grad = tf.gradients(score, x)[0]
grad=normalize_std(grad)
deconv_op=tf.multiply(x,grad)
ft_deconv[:,:,:,:,ch]=sess.run(grad, feed_dict=feedDict)
deconv.append(ft_deconv)
return deconv