TensorFlow中tf.nn.conv2d()的卷积核的数据排布

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
图1 n02085936_804.jpg
边缘检测后的图:
ellipse
图2 边缘检测后的图

kernel的数据排布更形象地表示:

ellipse
图3 tf.nn.conv2d()-kernel数据排布

Reference

TensorFlow for Machine Intelligence. Sam Abrahams, Danijar Hafner, Erik Erwitt, Ariel Scarpinelli.
Stanford Dogs Dataset

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值