سلامو عرض احترام
من دارم سعی میکنم که یک تصویر رو کانوالو کنم و خروجی اونو به صورت plot شده ببینم.
برنامه به صورت زیر میباشد
%matplotlib inline
import matplotlib.pyplot as plt
import tensorflow as tf
import numpy as np
from sklearn.metrics import confusion_matrix
import time
from datetime import timedelta
import math
from scipy.misc import imread, imresize
###
'img=imread('img652.jpg)
(print(img.shape
flat=683*1024*3
sess = tf.Session()
x = tf.placeholder(tf.float32, shape=[None, flat])
x_image = tf.reshape(x, [-1,227,227,3])
print(x_image.shape)
def weight_variable(shape):
initial = tf.truncated_normal(shape, stddev=0.1)
return tf.Variable(initial)
def bias_variable(shape):
initial = tf.constant(0.1, shape=shape)
return tf.Variable(initial)
def conv2d(x, W):
return tf.nn.conv2d(x, W, strides=[1, 1, 1, 1], padding='SAME')
def max_pool_2x2(x):
return tf.nn.max_pool(x, ksize=[1, 2, 2, 1],
strides=[1, 2, 2, 1], padding='SAME')
W_conv1 = weight_variable([5, 5, 3,32])
b_conv1 = bias_variable([32]) # need 32 biases for 32 outputs
convolve1= conv2d(x_image, W_conv1) + b_conv1
h_conv1 = tf.nn.relu(convolve1)
h_pool1 = max_pool_2x2(h_conv1)
#تا اینج خطایی نمیده اما زمانی که از این تابع برای پلات کردن اثر کانولوشن استفاده میکنم خطا میده
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.
with tf.Session() as sess:
feed_dict = {x: [image]}
# Calculate and retrieve the output values of the layer
# when inputting that image.
values = session.run(layer, 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([])
# Ensure the plot is shown correctly with multiple plots
# in a single Notebook cell.
plt.show()
#
plot_conv_layer(layer=convolve1, image=img)
#خطا:
NameError Traceback (most recent call last)
<ipython-input-70-4187c4162d45> in <module>()
----> 1 plot_conv_layer(layer=convolve1, image=img)
<ipython-input-69-89fce3f39821> in plot_conv_layer(layer, image)
12 # Calculate and retrieve the output values of the layer
13 # when inputting that image.
---> 14 values = session.run(layer, feed_dict=feed_dict)
15
16 # Number of filters used in the conv. layer.
NameError: name 'session' is not defined
لطفا در صورت امکان راهنمایی کنید