-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy path__visualization__.py
More file actions
348 lines (299 loc) · 14.1 KB
/
__visualization__.py
File metadata and controls
348 lines (299 loc) · 14.1 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
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
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
"""Deep dive libs"""
from input_data_levelDB_simulator import DataSetManager
from config import *
from utils import *
from features_optimization import optimize_feature
from simulator import *
"""Structure"""
import sys
sys.path.append('structures')
sys.path.append('utils')
from resnet_12 import create_structure
from alex_feature_extract import extract_features
"""Core libs"""
import tensorflow as tf
import numpy as np
"""Visualization libs"""
import matplotlib.pyplot as plt
import matplotlib.cm as cm
"""Python libs"""
import os
from optparse import OptionParser
from PIL import Image
import subprocess
import time
from ssim_tf import ssim_tf
from scipy import misc
import json
"""Verifying options integrity"""
def verifyConfig(config):
if config.save_features_to_disk not in (True, False):
raise Exception('Wrong save_features_to_disk option. (True or False)')
if config.save_json_summary not in (True, False):
raise Exception('Wrong save_json_summary option. (True or False)')
if config.use_tensorboard not in (True, False):
raise Exception('Wrong use_tensorboard option. (True or False)')
if config.save_error_transmission not in (True, False):
raise Exception('Wrong save_error_transmission option. (True or False)')
if config.use_deconv not in (True, False):
raise Exception('Wrong use_deconv option. (True or False)')
if config.use_depths not in (True, False):
raise Exception('Wrong use_depths option. (True or False)')
"""Verifying options integrity"""
config = configVisualization()
verifyConfig(config)
""" Creating section"""
sess = tf.InteractiveSession()
c,binf,range_array=acquireProperties(config,sess)
dataset = DataSetManager(config)
global_step = tf.Variable(0, trainable=False, name="global_step")
"""creating plaholders"""
batch_size=config.batch_size
tf_images=tf.placeholder("float",(None,) +config.input_size, name="images")
tf_depths=tf.placeholder("float",(None,) +config.depth_size, name="depths")
tf_range=tf.placeholder("float",(None,)+range_array.shape[1:], name="ranges")
tf_c=tf.placeholder("float",(None,)+c.shape[1:], name="c")
tf_binf=tf.placeholder("float",(None,)+binf.shape[1:], name="binf")
lr = tf.placeholder("float", name = "learning_rate")
"""defining simulator structure"""
y_image = tf_images
x=applyTurbidity(y_image, tf_depths, tf_c, tf_binf, tf_range)
with tf.variable_scope("network", reuse=None):
last_layer, dropoutDict, feature_maps,scalars,histograms = create_structure(tf, x,config.input_size,config.dropout,training=False)
" Creating comparation metrics"
mse_loss = tf.reduce_mean(tf.abs(tf.subtract(255.0*last_layer, 255.0*y_image)), reduction_indices=[1,2,3])
loss_function = mse_loss
"""Creating summaries"""
tf.summary.image('Input', x)
tf.summary.image('Output', last_layer)
tf.summary.image('GroundTruth', y_image)
ft_ops=[]
weights=[]
for key in config.features_list:
ft_ops.append(feature_maps[key][0])
weights.append(feature_maps[key][1])
for key in scalars:
tf.summary.scalar(key,scalars[key])
for key in config.histograms_list:
tf.summary.histogram('histograms_'+key, histograms[key])
tf.summary.scalar('Loss', tf.reduce_mean(loss_function))
for ft, key in zip(ft_ops,config.features_list):
for ch in xrange(ft.get_shape()[3]):
summary_name=key+"_"+str(ch).zfill(len(str(ft.get_shape()[3])))
tf.summary.scalar(summary_name, tf.reduce_mean(ft[:,:,:,ch]))
summary_op = tf.summary.merge_all()
ft_summaries={}
deconv_summaries={}
ft_grid_placeholder=tf.placeholder(tf.float32, shape=(None, None, None, 1), name="Feature_Map_Activation")
d_grid_placeholder=tf.placeholder(tf.float32, shape=(None, None, None, 3), name="Deconvolution")
for key in config.features_list:
ft_summaries[key]=tf.summary.image("features_map_"+key, ft_grid_placeholder)
if config.use_deconv:
deconv_summaries[key]=tf.summary.image("deconv_"+key, d_grid_placeholder)
saver = tf.train.Saver(tf.global_variables())
init_op=tf.global_variables_initializer()
sess.run(init_op)
summary_writer = tf.summary.FileWriter(config.summary_path, graph=sess.graph)
if not os.path.exists(config.models_path):
os.mkdir(config.models_path)
ckpt = tf.train.get_checkpoint_state(config.models_path)
dados={}
dados['summary_writing_period']=config.summary_writing_period
dados['batch_size']=config.batch_size
dados['variable_errors']=[]
dados['time']=[]
dados['variable_errors_val']=[]
if ckpt:
print 'Restoring from ', ckpt.model_checkpoint_path
saver.restore(sess,ckpt.model_checkpoint_path)
if config.save_json_summary:
if os.path.isfile(config.summary_path +'summary.json'):
outfile= open(config.summary_path +'summary.json','r+')
dados=json.load(outfile)
outfile.close()
else:
outfile= open(config.summary_path +'summary.json','w')
json.dump(dados, outfile)
outfile.close()
else:
print 'Can\'t Restore from ', config.models_path
sys.exit()
print 'Logging into ' + config.summary_path
initialIteration = 1
training_start_time =time.time()
max_actvs=[]
for key in config.features_list:
"descobrindo o tamanho de cada feature map"
ft_shape=feature_maps[key][0].get_shape()
"inicializando a variavel da ativacao maxima"
init_img=np.zeros((config.num_top_actvs,)+config.input_size+(ft_shape[3],),dtype=np.float32)-float("inf")
init_actv=np.zeros((config.num_top_actvs,)+tuple(ft_shape[1:]),dtype=np.float32)-float("inf")
init_avg=np.zeros((config.num_top_actvs,)+(ft_shape[3],),dtype=np.float32)-float("inf")
max_actvs.append((init_img,init_actv,init_avg))
dados[key]=[]
""" Optimization """
print("Running Optimization")
for key, channel in config.features_opt_list:
ft=feature_maps[key][0]
n_channels=ft.get_shape()[3]
opt_grid=np.empty((1,)+config.input_size+(n_channels,))
if channel<0:
#otimiza todos os canais
for ch in xrange(n_channels):
opt_output=optimize_feature(config.input_size, x, ft[:,:,:,ch])
#if config.use_tensorboard:
# opt_name="optimization_"+key+"_"+str(ch).zfill(len(str(n_channels)))
# opt_summary=tf.summary.image(opt_name, np.expand_dims(opt_output,0))
# summary_str=sess.run(opt_summary)
# summary_writer.add_summary(summary_str,0)
# salvando as imagens como bmp
if(config.save_features_to_disk):
save_optimized_image_to_disk(opt_output,ch,n_channels,key,config.summary_path)
opt_output -= opt_output.min()
opt_output *= (255/(opt_output.max()+0.0001))
opt_grid[0,:,:,:,ch]=opt_output
#opt_grid_img=put_grads_on_grid_np(opt_grid.astype(np.float32))
if config.use_tensorboard:
opt_grid_name="opt_"+key
opt_grid_img=put_grads_on_grid_np(opt_grid.astype(np.float32))
opt_grid_summary=tf.summary.image(opt_grid_name, opt_grid_img)
opt_grid_summary_str=sess.run(opt_grid_summary)
summary_writer.add_summary(opt_grid_summary_str,0)
#opt_grid_img=(opt_grid_img-opt_grid_img.min())
#opt_grid_img*=(255/opt_grid_img.max())
#opt_grid_img=opt_grid_img.astype(np.uint8)
#opt_im = Image.fromarray(opt_grid_img[0,:,:,:])
#folder_name=config.summary_path
#if not os.path.exists(folder_name):
# os.makedirs(folder_name)
#opt_im.save(folder_name+"/"+opt_grid_name+".bmp")
else:
opt_output=optimize_feature(config.input_size, x, ft[:,:,:,channel])
if config.use_tensorboard:
opt_name="optimization_"+key+"_"+str(channel).zfill(len(str(n_channels)))
opt_summary=tf.summary.image(opt_name, np.expand_dims(opt_output,0))
summary_str=sess.run(opt_summary)
summary_writer.add_summary(summary_str,0)
# salvando as imagens como bmp
if(config.save_features_to_disk):
save_optimized_image_to_disk(opt_output,channel,n_channels,key,config.summary_path)
for w, key in zip(weights, config.features_list):
if w is not None:
kernel=w.eval()
kernel_grid=put_kernels_on_grid_np(kernel)
kernel_summary=tf.summary.image("weights_"+key, kernel_grid)
kernel_summary_str=sess.run(kernel_summary)
summary_writer.add_summary(kernel_summary_str)
print("Images in dataset: %d"%(dataset.getNImagesDataset()))
for i in range(initialIteration, dataset.getNImagesDataset()/config.batch_size+1):
#for i in range(initialIteration, 3):
epoch_number = 1.0 + (float(i)*float(config.batch_size))/float(dataset.getNImagesDataset())
start_time = time.time()
batch = dataset.train.next_batch(config.batch_size)
if config.use_depths:
feedDict={tf_images: batch[0], tf_depths: batch[1], tf_range: range_array, tf_c: c, tf_binf: binf}
else:
constant_depths=np.ones((batch_size,)+config.depth_size, dtype=np.float32);
depths=constant_depths*10*np.random.rand(batch_size,1,1,1)
feedDict={tf_images: batch[0], tf_depths: depths, tf_range: range_array, tf_c: c, tf_binf: binf}
sim_output=sess.run(x,feed_dict=feedDict)
if len(ft_ops) > 0:
ft_maps= sess.run(ft_ops, feed_dict=feedDict)
else:
ft_maps= []
for ft, actv, key in zip(ft_maps, max_actvs, config.features_list):
batch_actv_sum=np.zeros(ft.shape[3],dtype=np.float32)
#Percorre todo o batch
for j in xrange(ft.shape[0]):
#percorre os canais do feature map
for k in xrange(ft.shape[3]):
ft_avg=np.average(ft[j,:,:,k])
ft_pos=-1
for p in xrange(0,config.num_top_actvs):
if ft_avg>actv[2][p,k]:
ft_pos=p
break
if ft_pos>=0:
for pos in xrange(config.num_top_actvs-1, ft_pos, -1):
actv[0][pos,:,:,:,k]=actv[0][pos-1,:,:,:,k]
actv[1][pos,:,:,k]=actv[1][pos-1,:,:,k]
actv[2][pos,k]=actv[2][pos-1,k]
actv[0][ft_pos,:,:,:,k]=sim_output[j,:,:,:]
actv[1][ft_pos,:,:,k]=ft[j,:,:,k]
actv[2][ft_pos,k]=ft_avg
dados[key].append(ft.mean(axis=(0,1,2)).tolist())
duration = time.time() - start_time
if i%4 == 0:
examples_per_sec = config.batch_size / duration
print("step %d, images used %d, examples per second %f"
%(i, i*config.batch_size, examples_per_sec))
if (i%config.summary_writing_period == 1 or i==(dataset.getNImagesDataset()/config.batch_size)) and (config.use_tensorboard or config.save_features_to_disk or config.save_json_summary):
output, result = sess.run([last_layer,loss_function], feed_dict=feedDict)
result = np.mean(result)
if config.use_deconv:
deconv=deconvolution(x, feedDict, ft_ops, config.features_list, config.batch_size, config.input_size)
else:
deconv=[None]*len(ft_ops)
if config.save_json_summary:
dados['variable_errors'].append(float(result))
dados['time'].append(time.time() - training_start_time)
outfile = open(config.summary_path +'summary.json','w')
json.dump(dados, outfile)
outfile.close()
if config.use_tensorboard:
summary_str = sess.run(summary_op, feed_dict=feedDict)
summary_writer.add_summary(summary_str,i)
if len(ft_ops) > 0:
for ft, w, d, actv, key in zip(ft_maps, weights, deconv, max_actvs, config.features_list):
#for ch in xrange(ft.shape[3]):
#summary_name=key+"_"+str(ch).zfill(len(str(ft.shape[3])))
# avg_actv_summary=ft_avg_summaries[summary_name]
# avg_actv_str = sess.run(avg_actv_summary, feed_dict={ft_avg_placeholder:np.average(ft[:,:,:,ch])})
# summary_writer.add_summary(avg_actv_str,i)
ft_grid=put_features_on_grid_np(ft)
ft_summary=ft_summaries[key]
summary_str=sess.run(ft_summary, feed_dict={ft_grid_placeholder:ft_grid})
summary_writer.add_summary(summary_str,i)
if d is not None:
deconv_grid=put_grads_on_grid_np(d.astype(np.float32))
deconv_summary=deconv_summaries[key]
deconv_summary_str=sess.run(deconv_summary, feed_dict={d_grid_placeholder:deconv_grid})
summary_writer.add_summary(deconv_summary_str,i)
if(config.save_features_to_disk):
save_images_to_disk(output,sim_output,batch[0],config.summary_path)
save_feature_maps_to_disk(ft_maps, weights, deconv, config.features_list,config.summary_path)
for actv, key in zip(max_actvs, config.features_list):
if(config.save_features_to_disk):
save_max_activations_to_disk(max_actvs, config.features_list,config.summary_path)
for n in xrange(0,config.num_top_actvs):
max_actv_grid=put_features_on_grid_np(np.expand_dims(actv[1][n,:,:,:],0))
max_actv_name="max_actv_"+key+"_N_"+str(n).zfill(len(str(config.num_top_actvs)))
max_actv_img=(max_actv_grid-max_actv_grid.min())
max_actv_img*=(255/(max_actv_img.max()+0.0001))
max_actv_img=max_actv_img.astype(np.uint8)
max_im = Image.fromarray(max_actv_img[0,:,:,0])
max_folder_name=config.summary_path
if not os.path.exists(max_folder_name):
os.makedirs(max_folder_name)
max_im.save(max_folder_name+"/"+max_actv_name+".png")
max_actv_input_grid=put_grads_on_grid_np(np.expand_dims(actv[0][n,:,:,:,:],0))
max_actv_input_name="max_actv_inputs_"+key+"_N_"+str(n).zfill(len(str(config.num_top_actvs)))
max_actv_input_img=(max_actv_input_grid-max_actv_input_grid.min())
max_actv_input_img*=(255/(max_actv_input_img.max()+0.0001))
max_actv_input_img=max_actv_input_img.astype(np.uint8)
max_in_im = Image.fromarray(max_actv_input_img[0,:,:,:])
max_folder_name=config.summary_path
if not os.path.exists(max_folder_name):
os.makedirs(max_folder_name)
max_in_im.save(max_folder_name+"/"+max_actv_input_name+".png")
if(config.use_tensorboard):
max_actv_grid=put_features_on_grid_np(actv[1])
max_actv_name="max_actv_"+key
max_actv_summary=tf.summary.image(max_actv_name, max_actv_grid,max_outputs=config.num_top_actvs)
max_actv_summary_str=sess.run(max_actv_summary)
summary_writer.add_summary(max_actv_summary_str,i)
max_actv_input_grid=put_grads_on_grid_np(actv[0])
max_actv_input_name="max_actv_inputs_"+key
max_actv_input_summary=tf.summary.image(max_actv_input_name, max_actv_input_grid,max_outputs=config.num_top_actvs)
max_actv_input_summary_str=sess.run(max_actv_input_summary)
summary_writer.add_summary(max_actv_input_summary_str,i)