import tensorflow as tf
import matplotlib as mil
mil.use("nbagg")
from matplotlib import pyplot
fig = pyplot.gcf()
fig.set_size_inches(4, 4)
image_filename = "n02085936_804.jpg"
filename_queue = tf.train.string_input_producer([image_filename])
image_reader = tf.WholeFileReader()
_, image_file = image_reader.read(filename_queue)
image = tf.image.decode_jpeg(image_file)
sess = tf.Session()
coord = tf.train.Coordinator()
threads = tf.train.start_queue_runners(coord=coord,sess=sess)
image_batch = tf.image.convert_image_dtype(tf.expand_dims(image, 0), tf.float32, saturate=False)
# 显示原图
image_val = sess.run(image)
pyplot.imshow(image_val, interpolation='nearest')
pyplot.show()
# 如下的tf.nn.conv2d()的操作,相当于对原始图像的RGB三个通道分别进行卷积操作;
# 使用tf.nn.conv2d()时是多通道融合的,因此R通道卷积的时候G和B通道的卷积核应该为0,
# 同理对G通道进行卷积的时候R和B通道的卷积核应该为0,对B通道进行卷积的时候R和G通道的卷积核应该为0
# 相比图像处理和计算机视觉中的卷积核数据的排布上,tf.nn.conv2d()对卷积核数据的排布较抽象
# -1 -1 -1
# -1 8 -1
# -1 -1 -1
kernel = tf.constant([
[ # R_1 R_2 R_3 # G_1 G_2 G_3 # B_1 B_2 B_3 # RGB代表输入有3个通道,_1,_2,_3代表输出有3个通道
[[ -1., 0., 0.], [ 0., -1., 0.], [ 0., 0., -1.]], # width_1
[[ -1., 0., 0.], [ 0., -1., 0.], [ 0., 0., -1.]], # width_2
[[ -1., 0., 0.], [ 0., -1., 0.], [ 0., 0., -1.]] # width_3
], # height_1
[
[[ -1., 0., 0.], [ 0., -1., 0.], [ 0., 0., -1.]], # width_1
[[ 8., 0., 0.], [ 0., 8., 0.], [ 0., 0., 8.]], # width_2
[[ -1., 0., 0.], [ 0., -1., 0.], [ 0., 0., -1.]] # width_3
], # height_2
[
[[ -1., 0., 0.], [ 0., -1., 0.], [ 0., 0., -1.]], # width_1
[[ -1., 0., 0.], [ 0., -1., 0.], [ 0., 0., -1.]], # width_2
[[ -1., 0., 0.], [ 0., -1., 0.], [ 0., 0., -1.]] # width_3
] # height_3
])
# 卷积操作
conv2d = tf.nn.conv2d(image_batch, kernel, [1, 1, 1, 1], padding="SAME")
sess.run(tf.global_variables_initializer())
activation_map = sess.run(tf.minimum(tf.nn.relu(conv2d), 255))
# 显示卷积的效果
fig = pyplot.gcf()
pyplot.imshow(activation_map[0], interpolation='nearest')
pyplot.show()
fig.set_size_inches(4, 4)
fig.savefig("example-edge-detection.png")
原图:
![ellipse](https://i-blog.csdnimg.cn/blog_migrate/528580d5b2dd2ac64f8ebee7d6611722.png)
图1 n02085936_804.jpg
边缘检测后的图:
![ellipse](https://i-blog.csdnimg.cn/blog_migrate/369934ccff9de53e484455eba6ae7856.png)
图2 边缘检测后的图
kernel的数据排布更形象地表示:
![ellipse](https://i-blog.csdnimg.cn/blog_migrate/caba14ba8ef8d16841d4e6ed1029c8ce.png)
图3 tf.nn.conv2d()-kernel数据排布
Reference
TensorFlow for Machine Intelligence. Sam Abrahams, Danijar Hafner, Erik Erwitt, Ariel Scarpinelli.
Stanford Dogs Dataset