当前位置:   article > 正文

【深度学习】Python实现CNN操作(附代码)_python cnn

python cnn

>参考: https://zhuanlan.zhihu.com/p/102119808 (可以直接看这个)

0 动机

通过普通的神经网络可以实现,但是现在图片越来越大,如果通过 NN 来实现,训练的参数太多。例如 224 x 224 x 3 = 150,528,隐藏层设置为 1024 就需要训练参数 150,528 x 1024 = 1.5 亿 个,这还是第一层,因此会导致我们的网络很庞大。

另一个问题就是特征位置在不同的图片中会发生变化。例如小猫的脸在不同图片中可能位于左上角或者右下角,因此小猫的脸不会激活同一个神经元。

CNN相较于全连接能够实现参数的共享。当使用一个具有9个卷积核、大小为5*5、步长为1的滤波器对一个大小为224 x 224 x 3 的图片进行卷积时,其参数量大小:(5 x 5+1) x 9 x 3 = 702
注: 不同通道之间的参数不共享。

1. Conv

  • Conv的原理示意图:
    请添加图片描述

  • 代码:

    class Conv3x3:
        # 卷积层使用3*3的filter.
        def __init__(self, num_filters):
            self.num_filters = num_filters
            self.filters = np.random.randn(num_filters, 3, 3) / 9       # 除以9是为了减小初始值的方差
            
        def iterate_regions(self, image):
            h, w = image.shape
            
            for i in range(h - 2):                                   # (h-2)/(w-2)是滤波以单位为1的步长,所需要移动的步数
                for j in range(w - 2):
                    im_region = image[i:(i + 3), j:(j + 3)]          # (i+3) 3*3的filter所移动的区域
                    yield im_region, i, j
                    
        def forward(self, input):
            # 28x28
            self.last_input = input
            
            h, w = input.shape
            output = np.zeros((h - 2, w - 2, self.num_filters))      # 创建一个(h-2)*(w-2)的零矩阵用于填充每次滤波后的值
            
            for im_region, i, j in self.iterate_regions(input):
                output[i, j] = np.sum(im_region * self.filters, axis=(1, 2))
                
            return output                                            # 4*4的矩阵经过3*3的filter后得到一个2*2的矩阵
        
        def backprop(self, d_L_d_out, learn_rate):
            # d_L_d_out: the loss gradient for this layer's outputs
            # learn_rate: a float
            d_L_d_filters = np.zeros(self.filters.shape)
            
            for im_region, i, j in self.iterate_regions(self.last_input):
                for f in range(self.num_filters):
                    # d_L_d_filters[f]: 3x3 matrix
                    # d_L_d_out[i, j, f]: num
                    # im_region: 3x3 matrix in image
                    d_L_d_filters[f] += d_L_d_out[i, j, f] * im_region
                    
            # Update filters
            self.filters -= learn_rate * d_L_d_filters
            
            # We aren't returning anything here since we use Conv3x3 as
            # the first layer in our CNN. Otherwise, we'd need to return
            # the loss gradient for this layer's inputs, just like every
            # other layer in our CNN.
            return None
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46

2. MaxPool

  • MaxPool的原理示意图:
    请添加图片描述

  • 代码:

    class MaxPool2:
        # A Max Pooling layer using a pool size of 2.
    
        def iterate_regions(self, image):
            '''
            Generates non-overlapping 2x2 image regions to pool over.
            - image is a 2d numpy array
            '''
            # image: 3d matix of conv layer
            
            h, w, _ = image.shape
            new_h = h // 2
            new_w = w // 2
    
            for i in range(new_h):
                for j in range(new_w):
                    im_region = image[(i * 
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/2023面试高手/article/detail/691066
推荐阅读
相关标签
  

闽ICP备14008679号