卷积神经网络(CNN)的理解

来源:互联网 发布:iptv第三方软件 编辑:程序博客网 时间:2024/04/29 14:19

0. 核心思想

  • two main ideas:
    • use only local features
    • 在不同位置上使用同样的特征;
  • 池化层的涵义在于,更高的层次能捕捉图像中更大的范围和区域;

1. feature map

依然是 feature map(特征映射),再次可见,深度神经网络其实就是一种 feature learning 框架。

如何获取一幅图像(输入图像)的特征映射(将原始图像映射到其特征空间中):

  • 用一个线性滤波器(linear filter)对输入图像进行卷积,
  • 加上一定的 bias
  • 将一个非线性函数应用在 卷积+bias 的结果上;

数学的方式描述如下:

将给定层对应的 feature map 记为 h(k)(也即任一层都可能存在多重 feature maps,以便获得更为丰富的特征表示,{h(k),k=0,,K}),它们(h(k),feature maps)各自对应的滤波器由权值矩阵 W(k) 和 bias b(k) 确定:

h(k)ij=tanh(W(k)x)ij+bk)

2. 卷积操作实战

二维卷积算子接口所在:

  • theano.tensor.nnet.conv.conv2d:最常使用;
  • theano.tensor.signal.conv2d:只作用在单 channel 的输入上

这两个函数接收两个符号输入:

  • input:4D 的 tensor,各个维度分别对应于,

    • mini-batch
    • number of input feature maps,比如一副彩色图像,对应着 3 个色彩通道(RGB)
    • image height
    • image width
  • weight Matrix W,其维度为:

    • number of feature maps at layer m(后一层)
    • number of feature maps at layer m-1(前一层)
    • filter height
    • filter width
import numpy as npimport theano.tensor as Tfrom theano.tensor.nnet import convrng = np.random.RandomState(23455);inpt = T.sensor4(name='inpt')W_shp = (2, 3, 9, 9)        # 第一维是 2,表示下一层的 map 数,                            # 第二维是 3,表示前一层的 map 是三(也就是 RGB),W_bound = np.sqrt(3*9*9)W = theano.shared(    np.asarray(        rng.uniform(            low=-1./W_bound,            high=1./W_bound,            size=W_shp,        )        dtype=theano.config.floatX    ), name='W')b_shp = (2, )       # 下一层的 maps 数b = theano.shared(np.asarray(rng.uniform(low=-.5, high=.5, size=b_shp), dtype=theano.config.floatX), name='b')conv_out = conv.conv2d(inpt, W)output = T.nnet.sigmoid(conv_out + b.dimshuffle('x', 0, 'x', 'x'))                  # b 从 (2, ) ⇒ (1, 2, 1, 1)f = theano.function([inpt], output)

3. maxpooling

max-pooling 本质上是一种非线性的降采样(down-sampling)。Maxpooling 机制将输入图像划分为不重叠的矩形区域,对于每一个子区域,输出其中的最大值。

maxpooling 之所以能在计算机视觉中应用基于以下两个原因:

  • 通过排除一些不是最大值的点,降低上一层的计算复杂度;
  • 它提供了一种平移不变性(translation invariance)的变换;

    image processing - Translation invariance in max-pooling and cascading with convolutional layer - Signal Processing Stack Exchange

    maxpooling 具有平移不变性,指的是,执行 maxpooling 之后得到的 feature map,不会因图像的平移而发生改变,而影响后续的处理结果。

    考虑 2*2 的 maxpooling 窗口,对于一个 2*2 的图像子区域,2*2 的图像子区域存在 8 个可行的平移方向(仅移动一个像素),分别是(上下左右,左上右上左下右下),此时针对这 8 种情况下,如下图:


    这里写图片描述

    只有右侧的三种情况,会使在原有位置上(中心的 4 个点)执行 maxpooling 后获得的最大值和未发生平移变化之前的是一致的,也即对平移保持不变;

    同理对于 3*3 的图像子区域,也是 8 个可以平移的方向,只有其中的 5 个保持平移不变。

0 0
原创粉丝点击