卷积运算

"""
卷积运算,两个输入张量(输入数据和卷积核)进行卷积,输出代表来自每个输入的信息张量。
tf.nn.conv2d完成卷积运算。
卷积核(kernel),权值、滤波器、卷积矩阵或模版,filter。
权值训练习得。
卷积核(filter参数)权值数量决定需要学习卷积核数量。
通道,计算机器视觉,描述输出向量。
RGB图像,3个代表秩1张量[red,green,blue]通道。
输出与input_batch同秩张量,与卷积核维数相同。
两个张量卷积生成特征图(feature map)。
特征图为输出添加新层代表张量卷积。访问输入批数据和特征图元素用相同索引,可了解输入与kernel卷积运算值变化。层,输出新维度。

计算机视觉卷积价值,修改卷积核strides(跨度)参数实现输入降维。
strides参数使卷积核无需遍历每个输入元素,跳过部分图像像素。
kernel在input_batch滑动,跨过部分元素,每次移动以input_batch一个元素为中心。
位置重叠值相乘,乘积相加,得卷积结果。逐点相乘,整合两个输入。设置跨度,调整输入张量维数。
降维减少运算量,避免重叠感受域。strides参数格式与输入向量相同(image_batch_size_stride、image_height_stride、image_width_stride、image_channels_stride)。

边界填充,卷积核与图像尺寸不匹配,填充图像缺失区域。
TensorFlow用0填充。padding参数控制conv2d零填充数或错误状态。
SAME:卷积输出输入尺寸相同,不考虑滤波器尺寸,缺失像素填充0,卷积核扫像素数大于图像实际像素数。
VALID:考虑滤波器尺寸。尽量不越过图像边界,也可能边界被填充。

data_format修改数据格式。
NHWC指定输入输出数据格式,[batch_size(批数据张量数)、in_height(批数据张量高度)、in_width(批数据张量宽度)、in_channels(批数据张量通道数)]。
NCHW指定输入输出数据格式,[batch_size(批数据张量数)、in_channels(批数据张量通道数)、in_height(批数据张量高度)、in_width(批数据张量宽度)]。

TensorFlow滤波器参数指定输入卷积运算卷积核。
滤波器使用特定模式突出图像中感兴趣特征。图像与边缘检测卷积核的卷积输出是所有检测边缘区域。
tf.minimum和tf.nn.relu使卷积值保持在RGB颜色值合法范围[0,255]内。
卷积核初值随机设定,训练迭代,值由CNN学习层自动调整,训练一迭代,接收图像,与卷积核卷积,预测结果与图像真实标签是否一致,调整卷积核。

"""
import tensorflow as tf
input_batch = tf.constant([
        [#第1个输入
            [[0.0],[1.0]],
            [[2.0],[3.0]]
        ],
        [#第2个输入
            [[2.0],[4.0]],
            [[6.0],[8.0]]
        ]
    ])
print input_batch
kernel = tf.constant([
        [
            [[1.0, 2.0]]
        ]
    ])
print kernel
conv2d = tf.nn.conv2d(input_batch, kernel, strides=[1, 1, 1, 1], padding='SAME')#conv2d卷积运算
print conv2d
sess = tf.Session()
print sess.run(conv2d)
lower_right_image_pixel = sess.run(input_batch)[0][1][1]
lower_right_kernel_pixel = sess.run(conv2d)[0][1][1]
print lower_right_image_pixel, lower_right_kernel_pixel
input_batch2 = tf.constant([
        [#第1个输入(6x6x1)
            [[0.0],[1.0],[2.0],[3.0],[4.0],[5.0]],
            [[0.1],[1.1],[2.1],[3.1],[4.1],[5.1]],
            [[0.2],[1.2],[2.2],[3.2],[4.2],[5.2]],
            [[0.3],[1.3],[2.3],[3.3],[4.3],[5.3]],
            [[0.4],[1.4],[2.4],[3.4],[4.4],[5.4]],
            [[0.5],[1.5],[2.5],[3.5],[4.5],[5.5]]
        ]
    ])
print input_batch2
kernel2 = tf.constant([#卷积核(3x3x1)
        [[[0.0]], [[0.5]], [[0.0]]],
        [[[0.0]], [[1.0]], [[0.0]]],
        [[[0.0]], [[0.5]], [[0.0]]]
    ])
print kernel2
conv2d2 = tf.nn.conv2d(input_batch2, kernel2, strides=[1, 3, 3, 1], padding='SAME')
print conv2d2
print sess.run(conv2d2)
lower_right_image_pixel2 = sess.run(input_batch2)[0][1][1]
lower_right_kernel_pixel2 = sess.run(conv2d2)[0][1][1]
print lower_right_image_pixel2, lower_right_kernel_pixel2
input_batch3 = tf.constant([
        [#第1个输入(6x6x1)
            [[0.0,1.0,2.0],[1.0,2.0,3.0]],
            [[0.1,1.1,2.1],[1.1,2.1,3.1]],
            [[0.2,1.2,2.2],[1.2,2.2,3.2]],
            [[0.3,1.3,2.3],[1.3,2.3,3.3]],
            [[0.4,1.4,2.4],[1.4,2.4,3.4]],
            [[0.5,1.5,2.5],[1.5,2.5,3.5]]
        ]
    ])
print input_batch3
kernel3 = tf.constant([
        [
            [[-1., 0., 0.],[0., -1., 0.],[0., 0., -1.]],
            [[-1., 0., 0.],[0., -1., 0.],[0., 0., -1.]],
            [[-1., 0., 0.],[0., -1., 0.],[0., 0., -1.]]
        ],
        [
            [[-1., 0., 0.],[0., -1., 0.],[0., 0., -1.]],
            [[8., 0., 0.],[0., 8., 0.],[0., 0., 8.]],
            [[-1., 0., 0.],[0., -1., 0.],[0., 0., -1.]]
        ],
        [
            [[-1., 0., 0.],[0., -1., 0.],[0., 0., -1.]],
            [[-1., 0., 0.],[0., -1., 0.],[0., 0., -1.]],
            [[-1., 0., 0.],[0., -1., 0.],[0., 0., -1.]]
        ]
    ])
print kernel3
conv2d3 = tf.nn.conv2d(input_batch3, kernel3, strides=[1, 1, 1, 1], padding='SAME')
print conv2d3
activation_map3 = sess.run(tf.minimum(tf.nn.relu(conv2d3), 255))
print activation_map3
lower_right_image_pixel3 = sess.run(input_batch3)[0][1][1]
lower_right_kernel_pixel3 = sess.run(conv2d3)[0][1][1]
print lower_right_image_pixel3, lower_right_kernel_pixel3
kernel4 = tf.constant([
        [
            [[0., 0., 0.],[0., 0., 0.],[0., 0., 0.]],
            [[-1., 0., 0.],[0., -1., 0.],[0., 0., -1.]],
            [[0., 0., 0.],[0., 0., 0.],[0., 0., 0.]]
        ],
        [
            [[-1., 0., 0.],[0., -1., 0.],[0., 0., -1.]],
            [[5., 0., 0.],[0., 5., 0.],[0., 0., 5.]],
            [[-1., 0., 0.],[0., -1., 0.],[0., 0., -1.]]
        ],
        [
            [[0., 0., 0.],[0., 0., 0.],[0., 0., 0.]],
            [[-1., 0., 0.],[0., -1., 0.],[0., 0., -1.]],
            [[0., 0., 0.],[0., 0., 0.],[0., 0., 0.]]
        ]
    ])
print kernel4
conv2d4 = tf.nn.conv2d(input_batch3, kernel4, strides=[1, 1, 1, 1], padding='SAME')
print conv2d4
activation_map4 = sess.run(tf.minimum(tf.nn.relu(conv2d4), 255))
print activation_map4
lower_right_image_pixel4 = sess.run(input_batch3)[0][1][1]
lower_right_kernel_pixel4 = sess.run(conv2d4)[0][1][1]
print lower_right_image_pixel4, lower_right_kernel_pixel4
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值