CNN之池化层tf.nn.max_pool|tf.nn.avg_pool|tf.reduce_mean

摘要:池化层的主要目的是降维,通过滤波器映射区域内取最大值、平均值等操作。

均值池化:tf.nn.avg_pool(input,ksize,strides,padding)

最大池化:tf.nn.max_pool(input,ksize,strides,padding)

input:通常情况下是卷积层输出的featuremap,shape=[batch,height,width,channels]

               

  假定这个矩阵就是卷积层输出的featuremap(2通道输出)  他的shape=[1,4,4,2]

ksize:池化窗口大小    shape=[batch,height,width,channels]    比如[1,2,2,1]

strides: 窗口在每一个维度上的移动步长 shape=[batch,stride,stride,channel]  比如[1,2,2,1]

padding:“VALID”不填充  “SAME”填充0

返回:tensor        shape=[batch,height,width,channels]

上图是采用的最大池化,取红色框内最大的一个数。

import tensorflow as tf
feature_map = tf.constant([
    [[0.0,4.0],[0.0,4.0],[0.0,4.0],[0.0,4.0]],
    [[1.0,5.0],[1.0,5.0],[1.0,5.0],[1.0,5.0]],
    [[2.0,6.0],[2.0,6.0],[2.0,6.0],[2.0,6.0]] ,
    [[3.0,7.0],[3.0,7.0],[3.0,7.0],[3.0,7.0]]
    ])
feature_map = tf.reshape(feature_map,[1,4,4,2])##两通道featuremap输入

##定义池化层
pooling = tf.nn.max_pool(feature_map,[1,2,2,1],[1,2,2,1],padding=‘VALID‘)##池化窗口2*2,高宽方向步长都为2,不填充
pooling1 = tf.nn.max_pool(feature_map,[1,2,2,1],[1,1,1,1],padding=‘VALID‘)##池化窗口2*2,高宽方向步长都为1,不填充
pooling2 = tf.nn.avg_pool(feature_map,[1,4,4,1],[1,1,1,1],padding=‘SAME‘)##池化窗口4*4,高宽方向步长都为1,填充
pooling3 = tf.nn.avg_pool(feature_map,[1,4,4,1],[1,4,4,1],padding=‘SAME‘)##池化窗口4*4,高宽方向步长都为4,填充
##转置变形(详细解释参考另一篇博文)
tran_reshape = tf.reshape(tf.transpose(feature_map),[-1,16])
pooling4 = tf.reduce_mean(tran_reshape,1)    ###对行值求平均
with tf.Session() as sess:
    print(‘featuremap:\n‘,sess.run(feature_map))
    print(‘*‘*30)
    print(‘pooling:\n‘,sess.run(pooling))
    print(‘*‘*30)
    print(‘pooling1:\n‘,sess.run(pooling1))
    print(‘*‘*30)
    print(‘pooling2:\n‘,sess.run(pooling2))
    print(‘*‘*30)
    print(‘pooling3:\n‘,sess.run(pooling3))
    print(‘*‘*30)
    print(‘pooling4:\n‘,sess.run(pooling4))
‘‘‘
输出结果:
featuremap:
 [[[[ 0.  4.]
   [ 0.  4.]
   [ 0.  4.]
   [ 0.  4.]]

  [[ 1.  5.]
   [ 1.  5.]
   [ 1.  5.]
   [ 1.  5.]]

  [[ 2.  6.]
   [ 2.  6.]
   [ 2.  6.]
   [ 2.  6.]]

  [[ 3.  7.]
   [ 3.  7.]
   [ 3.  7.]
   [ 3.  7.]]]]
******************************
pooling:
 [[[[ 1.  5.]
   [ 1.  5.]]

  [[ 3.  7.]
   [ 3.  7.]]]]
******************************
pooling1:
 [[[[ 1.  5.]
   [ 1.  5.]
   [ 1.  5.]]

  [[ 2.  6.]
   [ 2.  6.]
   [ 2.  6.]]

  [[ 3.  7.]
   [ 3.  7.]
   [ 3.  7.]]]]
******************************
pooling2:
 [[[[ 1.   5. ]
   [ 1.   5. ]
   [ 1.   5. ]
   [ 1.   5. ]]

  [[ 1.5  5.5]
   [ 1.5  5.5]
   [ 1.5  5.5]
   [ 1.5  5.5]]

  [[ 2.   6. ]
   [ 2.   6. ]
   [ 2.   6. ]
   [ 2.   6. ]]

  [[ 2.5  6.5]
   [ 2.5  6.5]
   [ 2.5  6.5]
   [ 2.5  6.5]]]]
******************************
pooling3:
 [[[[ 1.5  5.5]]]]
******************************
pooling4:
 [ 1.5  5.5]

‘‘‘

池化层常用函数及参数

现在我们对代码中的内容加以解释:

padding的规则

  •   padding=‘VALID’时,输出的宽度和高度的计算公式(下图gif为例)

         

    输出宽度:output_width = (in_width-filter_width+1)/strides_width  =(5-3+1)/2=1.5【向上取整=2】

    输出高度:output_height = (in_height-filter_height+1)/strides_height  =(5-3+1)/2=1.5【向上取整=2】

    输出的形状[1,2,2,1]

        

    

import tensorflow as tf
image = [0,1.0,1,2,2,0,1,1,0,0,1,1,0,1,0,1,0,1,1,1,0,2,0,1,0]
input = tf.Variable(tf.constant(image,shape=[1,5,5,1]))  ##1通道输入
fil1 = [-1.0,0,1,-2,0,2,-1,0,1]
filter = tf.Variable(tf.constant(fil1,shape=[3,3,1,1]))  ##1个卷积核对应1个featuremap输出

op = tf.nn.conv2d(input,filter,strides=[1,2,2,1],padding=‘VALID‘)  ##步长2,VALID不补0操作

init = tf.global_variables_initializer()

with tf.Session() as  sess:
    sess.run(init)
    # print(‘input:\n‘, sess.run(input))
    # print(‘filter:\n‘, sess.run(filter))
    print(‘op:\n‘,sess.run(op))

##输出结果
‘‘‘
 [[[[ 2.]
   [-1.]]

  [[-1.]
   [ 0.]]]]
‘‘‘

tensorflow中实现(步长2)

    如果strides=[1,3,3,1]的情况又是如何呢?   

    输出宽度:output_width  = (in_width-filter_width+1)/strides_width  =(5-3+1)/3=1

    输出高度:output_height = (in_height-filter_height+1)/strides_height  =(5-3+1)/3=1

    输出的形状[1,1,1,1],因此输出的结果只有一个

    

    

import tensorflow as tf
image = [0,1.0,1,2,2,0,1,1,0,0,1,1,0,1,0,1,0,1,1,1,0,2,0,1,0]
input = tf.Variable(tf.constant(image,shape=[1,5,5,1]))  ##1通道输入
fil1 = [-1.0,0,1,-2,0,2,-1,0,1]
filter = tf.Variable(tf.constant(fil1,shape=[3,3,1,1]))  ##1个卷积核对应1个featuremap输出

op = tf.nn.conv2d(input,filter,strides=[1,3,3,1],padding=‘VALID‘)  ##步长2,VALID不补0操作

init = tf.global_variables_initializer()

with tf.Session() as  sess:
    sess.run(init)
    # print(‘input:\n‘, sess.run(input))
    # print(‘filter:\n‘, sess.run(filter))
    print(‘op:\n‘,sess.run(op))

##输出结果
‘‘‘
op:
 [[[[ 2.]]]]
‘‘‘

tensorflow中实现(步长3)

    padding=‘SAME’时,输出的宽度和高度的计算公式(下图gif为例)

    

    输出宽度:output_width  = in_width/strides_width=5/2=2.5【向上取整3】

    输出高度:output_height = in_height/strides_height=5/2=2.5【向上取整3】

    则输出的形状:[1,3,3,1]

    那么padding补0的规则又是如何的呢?【先确定输出形状,再计算补多少0】

    pad_width = max((out_width-1)*strides_width+filter_width-in_width,0)=max((3-1)*2+3-5,0)=2

    pad_height = max((out_height-1)*strides_height+filter_height-in_height,0)=max((3-1)*2+3-5,0)=2

    pad_top = pad_height/2=1

    pad_bottom = pad_height-pad_top=1

    pad_left = pad_width/2=1

    pad_right = pad_width-pad_left=1

    

    

import tensorflow as tf
image = [0,1.0,1,2,2,0,1,1,0,0,1,1,0,1,0,1,0,1,1,1,0,2,0,1,0]
input = tf.Variable(tf.constant(image,shape=[1,5,5,1]))  ##1通道输入
fil1 = [-1.0,0,1,-2,0,2,-1,0,1]
filter = tf.Variable(tf.constant(fil1,shape=[3,3,1,1]))  ##1个卷积核对应1个featuremap输出

op = tf.nn.conv2d(input,filter,strides=[1,2,2,1],padding=‘SAME‘)  ##步长2,VALID不补0操作

init = tf.global_variables_initializer()

with tf.Session() as  sess:
    sess.run(init)
    # print(‘input:\n‘, sess.run(input))
    # print(‘filter:\n‘, sess.run(filter))
    print(‘op:\n‘,sess.run(op))

##输出结果
‘‘‘
op:
 [[[[ 3.]
   [ 1.]
   [-4.]]

  [[ 3.]
   [ 0.]
   [-3.]]

  [[ 4.]
   [-1.]
   [-3.]]]]
‘‘‘

SAME步长2

    如果步长为3呢?补0的规则又如何?

    输出宽度:output_width  = in_width/strides_width=5/3=2

    输出高度:output_height = in_height/strides_height=5/3=2

    则输出的形状:[1,2,2,1]

    那么padding补0的规则又是如何的呢?【先确定输出形状,再计算补多少0】

    pad_width = max((out_width-1)*strides_width+filter_width-in_width,0)=max((2-1)*3+3-5,0)=1

    pad_height = max((out_height-1)*strides_height+filter_height-in_height,0)=max((2-1)*3+3-5,0)=1

    pad_top = pad_height/2=0【向下取整】

    pad_bottom = pad_height-pad_top=1

    pad_left = pad_width/2=0【向下取整】

    pad_right = pad_width-pad_left=1

    

    

    

import tensorflow as tf
print(3/2)
image = [0,1.0,1,2,2,0,1,1,0,0,1,1,0,1,0,1,0,1,1,1,0,2,0,1,0]
input = tf.Variable(tf.constant(image,shape=[1,5,5,1]))  ##1通道输入
fil1 = [-1.0,0,1,-2,0,2,-1,0,1]
filter = tf.Variable(tf.constant(fil1,shape=[3,3,1,1]))  ##1个卷积核对应1个featuremap输出

op = tf.nn.conv2d(input,filter,strides=[1,3,3,1],padding=‘SAME‘)  ##步长2,VALID不补0操作

init = tf.global_variables_initializer()

with tf.Session() as  sess:
    sess.run(init)
    # print(‘input:\n‘, sess.run(input))
    # print(‘filter:\n‘, sess.run(filter))
    print(‘op:\n‘,sess.run(op))

##输出结果
‘‘‘
op:
 [[[[ 2.]
   [-3.]]

  [[ 0.]
   [-3.]]]]
‘‘‘

SAME步长3

    这里借用的卷积中的padding规则,在池化层中的padding规则与卷积中的padding规则一致

   

原文地址:https://www.cnblogs.com/liuhuacai/p/12003697.html

时间: 2024-08-27 06:53:01

CNN之池化层tf.nn.max_pool|tf.nn.avg_pool|tf.reduce_mean的相关文章

理解CNN卷积层与池化层计算

概述 深度学习中CNN网络是核心,对CNN网络来说卷积层与池化层的计算至关重要,不同的步长.填充方式.卷积核大小.池化层策略等都会对最终输出模型与参数.计算复杂度产生重要影响,本文将从卷积层与池化层计算这些相关参数出发,演示一下不同步长.填充方式.卷积核大小计算结果差异. 一:卷积层 卷积神经网络(CNN)第一次提出是在1997年,杨乐春(LeNet)大神的一篇关于数字OCR识别的论文,在2012年的ImageNet竞赛中CNN网络成功击败其它非DNN模型算法,从此获得学术界的关注与工业界的兴趣

学习笔记TF014:卷积层、激活函数、池化层、归一化层、高级层

CNN神经网络架构至少包含一个卷积层 (tf.nn.conv2d).单层CNN检测边缘.图像识别分类,使用不同层类型支持卷积层,减少过拟合,加速训练过程,降低内存占用率. TensorFlow加速所有不同类弄卷积层卷积运算.tf.nn.depthwise_conv2d,一个卷积层输出边接到另一个卷积层输入,创建遵循Inception架构网络 Rethinking the Inception Architecture for Computer Vision https://arxiv.org/ab

TensorFlow 池化层

在 TensorFlow 中使用池化层 在下面的练习中,你需要设定池化层的大小,strides,以及相应的 padding.你可以参考 tf.nn.max_pool().Padding 与卷积 padding 的原理一样. 说明 完成 maxpool 函数中所有的 TODO. 设定 strides,padding 和 ksize 使得池化的结果维度为 (1, 2, 2, 1). """ Set the values to `strides` and `ksize` such

Tensorflow 池化层(pooling)和全连接层(dense)

一.池化层(pooling) 池化层定义在 tensorflow/python/layers/pooling.py. 有最大值池化和均值池化. 1. 最大池化层 tf.layers.max_pooling2d max_pooling2d( inputs, pool_size, strides, padding='valid', data_format='channels_last', name=None ) inputs: 进行池化的数据.pool_size: 池化的核大小(pool_heigh

卷积层,池化层等,前向/反向传播原理讲解

今天闲来无事,考虑到以前都没有好好研究过卷积层.池化层等等的前向/反向传播的原理,所以今天就研究了一下,参考了一篇微信好文,讲解如下: 参考链接:https://www.zybuluo.com/hanbingtao/note/485480 https://github.com/hanbt/learn_dl/blob/master/cnn.py 一.卷积层 (1)首先是卷积神经网络中的卷积操作: 计算公式为: 注意上式的使用场景:stride = 1 , channel = 1 我们可以将其扩展到

『cs231n』作业2选讲_通过代码理解卷积层&池化层

卷积层 卷积层向前传播示意图: def conv_forward_naive(x, w, b, conv_param): """ A naive implementation of the forward pass for a convolutional layer. The input consists of N data points, each with C channels, height H and width W. We convolve each input w

Pooling Layer:池化层

1. 池化层:由1个filter组成,对图片 / 输入矩阵进行空间上的降采样处理,压缩图像的高度和宽度.池化层的filter不是用来得到feature map,而是用来获取filter范围内的特定值.池化层的filter并不设置特定的权值,通常只是用来获取感受野范围内的最大值或平均值. 降采样处理:改变输入矩阵的高度和宽度,但不会改变通道数 / 深度 2.池化: 主要分为最大池化和平均池化 ①最大池化:用filter依次扫描输入矩阵的局部,每次选取该区域的最大值 ②平均池化:用filter依次扫

池化层的作用和种类

原连接:https://blog.csdn.net/XX_123_1_RJ/article/details/86677482 池化的原理或者是过程:pooling是在不同的通道上分开执行的(就是池化操作不改变通道数),且不需要参数控制.然后根据窗口大小进行相应的操作.一般有max pooling.average pooling等. 一. 池化层主要的作用 首要作用,下采样(downsamping) 降维.去除冗余信息.对特征进行压缩.简化网络复杂度.减少计算量.减少内存消耗等等.各种说辞吧,总的

Tensorflow之CNN卷积层池化层padding规则

padding的规则 ·        padding=‘VALID’时,输出的宽度和高度的计算公式(下图gif为例) 输出宽度:output_width = (in_width-filter_width+1)/strides_width  =(5-3+1)/2=1.5[向上取整=2] 输出高度:output_height = (in_height-filter_height+1)/strides_height  =(5-3+1)/2=1.5[向上取整=2] 输出的形状[1,2,2,1] imp