-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathvisualize_graph.py
More file actions
139 lines (127 loc) · 6.09 KB
/
Copy pathvisualize_graph.py
File metadata and controls
139 lines (127 loc) · 6.09 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
# -*- coding: utf-8 -*-
# @Author: Yulin Liu
# @Date: 2018-10-10 14:23:23
# @Last Modified by: Yulin Liu
# @Last Modified time: 2018-10-10 22:20:47
import numpy as np
import tensorflow as tf
import os
from configparser import ConfigParser
from rnn_encoder_decoder import LSTM_model
import matplotlib.pyplot as plt
class visual_graph:
def __init__(self,
conf_path,
restored_model_path):
self.restored_model_path = restored_model_path
self.conf_path = conf_path
self.load_configs()
def load_configs(self):
parser = ConfigParser(os.environ)
parser.read(self.conf_path)
config_header = 'nn'
self.n_input = parser.getint(config_header, 'n_input')
self.n_channels = parser.getint('convolution', 'n_channels')
self.n_controled_var = parser.getint('lstm', 'n_controled_var')
self.n_encode = parser.getint(config_header, 'n_encode')
self.state_size = parser.getint('lstm', 'n_cell_dim')
self.n_layer = parser.getint('lstm', 'n_lstm_layers')
# Number of contextual samples to include
self.batch_size = parser.getint(config_header, 'batch_size')
def define_placeholder(self):
# define placeholder
self.input_encode_tensor = tf.placeholder(dtype = tf.float32, shape = [None, None, self.n_encode], name = 'encode_tensor')
self.seq_len_encode = tf.placeholder(dtype = tf.int32, shape = [None], name = 'seq_length_encode')
self.input_tensor = tf.placeholder(dtype = tf.float32, shape = [None, None, self.n_input, self.n_input, self.n_channels], name = 'decode_feature_map')
self.input_decode_coords_tensor = tf.placeholder(dtype = tf.float32, shape = [None, None, self.n_controled_var], name = 'decode_coords')
self.target = tf.placeholder(dtype = tf.float32, shape = [None, None, self.n_controled_var], name = 'target')
self.target_end = tf.placeholder(dtype = tf.float32, shape = [None, None, 1], name = 'target_end')
self.target_end_neg = tf.placeholder(dtype = tf.float32, shape = [None, None, 1], name = 'target_end_neg')
self.seq_length = tf.placeholder(dtype = tf.int32, shape = [None], name = 'seq_length_decode')
return
def launchGraph(self):
self.define_placeholder()
self.MODEL = LSTM_model(conf_path = self.conf_path,
batch_x = self.input_encode_tensor,
seq_length = self.seq_len_encode,
n_input = self.n_encode,
batch_x_decode = self.input_tensor,
batch_xcoords_decode = self.input_decode_coords_tensor,
seq_length_decode = self.seq_length,
n_input_decode = self.n_input,
target = self.target,
train = False,
weight_summary = False)
return
def feed_fwd_convlayer(self, feed_input):
with tf.device('/cpu:0'):
self.graph = tf.Graph()
self.launchGraph()
self.sess = tf.Session()
self.saver = tf.train.Saver()
self.saver.restore(self.sess, self.restored_model_path)
self.sess.graph.finalize()
self.weights = self._return_weights()
conv1_out, conv2_out, conv3_out = self._feed_fwd_convlayer(feed_input)
self.sess.close()
return conv1_out, conv2_out, conv3_out
def _return_weights(self):
weight_list = tf.trainable_variables()
weights = {}
for v in weight_list:
weights[v.name] = self.sess.run(v)
return weights
def _feed_fwd_convlayer(self, feed_input):
# feed_input should have the shape of [?, ?, 20, 20, 4]
conv1_out = self.sess.run(self.MODEL.conv1, feed_dict={self.input_tensor: feed_input})
conv2_out = self.sess.run(self.MODEL.conv2, feed_dict={self.input_tensor: feed_input})
conv3_out = self.sess.run(self.MODEL.conv3, feed_dict={self.input_tensor: feed_input})
return conv1_out, conv2_out, conv3_out
def visualize_raw_weights(weight_var, fig_size = (8, 4)):
n_layers = weight_var.shape[3]
n_channels = weight_var.shape[2]
fig, axs = plt.subplots(n_channels, n_layers, figsize=fig_size, facecolor='w', edgecolor='k')
axs = axs.ravel()
for i in range(n_channels):
for j in range(n_layers):
axs[n_layers * i + j].imshow(weight_var[:, :, i, j],
cmap = 'bwr',
vmax = weight_var.max(),
vmin = weight_var.min())
axs[n_layers * i + j].set_axis_off()
plt.show()
return fig
def visualize_conv_layers(conv_layer,
nrow,
ncol,
fig_size):
print(conv_layer.shape)
# n_layers = weight_var.shape[3]
# n_channels = weight_var.shape[2]
fig, axs = plt.subplots(nrow, ncol, figsize=fig_size, facecolor='w', edgecolor='k')
fig.subplots_adjust(wspace = 0.01, hspace = 0.01)
axs = axs.ravel()
for i in range(nrow):
for j in range(ncol):
axs[ncol * i + j].imshow(conv_layer[j, :, :, i],
cmap = 'bwr',
vmax = conv_layer[:, :, :, i].max(),
vmin = conv_layer[:, :, :, i].min(),
origin = 'lower')
axs[ncol * i + j].set_axis_off()
plt.show()
return fig
'''
Example Code:
'''
'''
tf.reset_default_graph()
restored_model_path = 'visual_network/model.ckpt-99'
config_path = 'configs/encoder_decoder_nn.ini'
visual_graph_class = visual_graph(config_path, restored_model_path)
visual_graph_class.restore_model()
weights = visual_graph_class.weights
visualize_raw_weights(weight_var=weights['wc1:0'], fig_size = (8, 2))
visualize_raw_weights(weight_var=weights['wc2:0'], fig_size = (8,4))
visualize_raw_weights(weight_var=weights['wc3:0'], fig_size = (8,4))
'''