-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathplot.py
58 lines (46 loc) · 1.8 KB
/
plot.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
# -*- coding: utf-8 -*-
"""
Created on Wed Sep 20 15:05:00 2017
@author: ESTERIFIED
"""
import math
##########excess functions
x1 = tf.reshape(x, shape=[-1, 28, 28, 1])
conv1 = conv2d(x1, weights['wc1'], biases['bc1'])
def plot_conv_layer(layer, image):
# Assume layer is a TensorFlow op that outputs a 4-dim tensor
# which is the output of a convolutional layer,
# e.g. layer_conv1 or layer_conv2.
# Create a feed-dict containing just one image.
# Note that we don't need to feed y_true because it is
# not used in this calculation.
feed_dict = {x: image,keep_prob:1.}
# Calculate and retrieve the output values of the layer
# when inputting that image.
session=tf.Session()
session.run(tf.global_variables_initializer())
values = session.run(conv1, feed_dict=feed_dict)
# Number of filters used in the conv. layer.
num_filters = values.shape[3]
# Number of grids to plot.
# Rounded-up, square-root of the number of filters.
num_grids = math.ceil(math.sqrt(num_filters))
# Create figure with a grid of sub-plots.
fig, axes = plt.subplots(num_grids, num_grids)
# Plot the output images of all the filters.
for i, ax in enumerate(axes.flat):
# Only plot the images for valid filters.
if i<num_filters:
# Get the output image of using the i'th filter.
# See new_conv_layer() for details on the format
# of this 4-dim tensor.
img = values[0, :, :, i]
# Plot image.
ax.imshow(img, interpolation='nearest', cmap='binary')
# Remove ticks from the plot.
ax.set_xticks([])
ax.set_yticks([])
session.close()
# Ensure the plot is shown correctly with multiple plots
# in a single Notebook cell.
plt.show()