单层单步长CNN过程演示

以下编程演示单层单步长CNN过程,帮助大家理解卷积过程。对于彩色图片,需要对矩阵化后的数组进行处理之后才进行按层卷积,这里为了简单理解,不做图片处理过程。下面的程序中的数组生成参数和卷积核大家可以随意修改。

#!/usr/bin/env python
# encoding: utf-8
'''
@author: 陆博文
@license: 陆博文私人版权所有
@contact: 2287693887@qq.com
@software: pycharm2018.3.2
@file: cnn_test.py
@time: 19-1-14 上午1:52

'''
import numpy as np
import random

#随机生成m*n数组
mat = []
for _ in range(23):
    mat1 = []
    for _ in range(20):
        mat1.append(random.randint(10,30))
    mat.append(mat1)
print('input_matrix: ')
for i in mat:
    print(i)

# 确定卷积核
conv = [[1,0,1],
        [0,1,0],
        [1,0,1],]

# 定义卷积函数
def conv2d(input,conv):
    conv0_len = len(conv[0])
    conv_len = len(conv)
    print('*************************')
    print('input_matrix_shape: ',len(input),'*',len(input[0]))
    print('*************************')
    output = []
    sum = 0
    for i in conv:
        sum += len(i)
    for i in range(len(input)-conv_len+1):
        output0 = []
        for j in range(len(input[0])-conv0_len+1):
            mat1 = []
            for k in range(conv_len):
                mat0 = []
                for n in range(conv0_len):
                    mat0.append(input[i + k][j + n])
                mat1.append(mat0)
            mat1 = np.array(mat1)
            conv = np.array(conv)
            y0 = mat1*conv
            y = np.reshape(y0,[1,sum])

            '''
            ########################################
                可以通过修改这部分内容将卷积过程修改为池化过程,具体修改为平均池化还是修改为
                最大池化由自己摸索。
            '''
            sum0 = 0
            for l in y[0]:
                sum0 += l
            '''
            ########################################
            '''
            output0.append(sum0)
        output.append(output0)
    return output

# 实例化卷积过程
d = conv2d(mat,conv)
print('output_matrix: ')
for i in d:
    print(i)

ss = np.array(d).tolist()
print('*************************')
print('output_matrix_shape: ',len(ss),'*',len(ss[0]))
print('*************************')



运行结果如下:

/home/lbw/桌面/python_env/py3env/bin/python3 /home/lbw/桌面/python_pro/卷积神经网络/cnn_test.py
input_matrix: 
[14, 15, 15, 20, 18, 17, 28, 21, 21, 19, 23, 24, 13, 18, 20, 11, 14, 13, 20, 16]
[21, 12, 23, 17, 23, 10, 13, 20, 22, 24, 10, 14, 20, 10, 10, 16, 17, 27, 17, 29]
[16, 30, 29, 16, 20, 22, 11, 26, 22, 14, 16, 17, 19, 18, 22, 18, 29, 19, 11, 25]
[27, 18, 15, 24, 16, 20, 26, 14, 24, 14, 15, 27, 30, 21, 19, 17, 19, 12, 25, 18]
[12, 14, 24, 30, 30, 15, 19, 18, 25, 21, 29, 21, 16, 10, 18, 22, 11, 17, 16, 12]
[21, 13, 18, 29, 11, 20, 26, 12, 19, 11, 19, 16, 29, 29, 19, 24, 10, 19, 22, 21]
[11, 16, 24, 26, 19, 13, 27, 19, 24, 16, 28, 12, 18, 15, 26, 24, 15, 27, 28, 15]
[23, 15, 13, 14, 27, 15, 10, 18, 11, 23, 22, 26, 14, 26, 23, 28, 21, 18, 13, 10]
[14, 10, 12, 24, 27, 30, 25, 23, 24, 19, 14, 16, 19, 18, 13, 14, 14, 13, 23, 26]
[26, 11, 15, 22, 10, 16, 29, 14, 16, 25, 26, 29, 19, 25, 19, 19, 16, 25, 17, 20]
[22, 22, 15, 16, 11, 22, 12, 30, 23, 16, 16, 10, 17, 16, 24, 24, 23, 28, 14, 12]
[28, 23, 22, 24, 22, 22, 25, 25, 26, 24, 26, 19, 19, 10, 30, 19, 28, 14, 25, 15]
[21, 22, 23, 13, 20, 16, 14, 19, 22, 12, 21, 14, 23, 28, 13, 13, 16, 28, 26, 13]
[11, 17, 12, 21, 21, 16, 13, 25, 30, 23, 19, 30, 29, 14, 21, 19, 23, 25, 23, 20]
[24, 19, 17, 22, 17, 17, 23, 10, 16, 10, 10, 13, 10, 23, 29, 25, 30, 14, 18, 26]
[16, 16, 22, 27, 24, 23, 13, 10, 17, 28, 24, 29, 22, 27, 30, 23, 20, 15, 10, 21]
[18, 10, 28, 29, 23, 25, 16, 16, 17, 17, 11, 29, 11, 18, 16, 28, 12, 20, 14, 25]
[16, 12, 13, 20, 29, 17, 23, 16, 22, 10, 19, 24, 16, 19, 23, 26, 23, 24, 19, 26]
[18, 29, 27, 30, 27, 18, 25, 18, 16, 11, 25, 23, 19, 12, 12, 23, 13, 17, 25, 18]
[12, 23, 25, 24, 23, 11, 27, 27, 27, 13, 22, 11, 28, 24, 28, 21, 20, 25, 19, 12]
[19, 20, 30, 25, 11, 12, 26, 10, 27, 16, 15, 24, 25, 26, 12, 16, 26, 17, 10, 28]
[15, 13, 19, 24, 27, 22, 13, 27, 18, 18, 18, 24, 23, 25, 22, 14, 20, 17, 13, 11]
[17, 20, 12, 29, 12, 25, 22, 16, 12, 13, 21, 26, 22, 27, 17, 24, 20, 11, 27, 28]
*************************
input_matrix_shape:  23 * 20
*************************
output_matrix: 
[86, 104, 99, 98, 87, 99, 102, 102, 106, 84, 85, 97, 84, 75, 101, 78, 101, 90]
[116, 100, 93, 91, 100, 75, 111, 94, 85, 95, 92, 91, 97, 86, 83, 101, 97, 97]
[99, 105, 127, 99, 100, 107, 91, 103, 106, 88, 107, 96, 96, 87, 97, 95, 79, 98]
[95, 108, 90, 123, 94, 85, 113, 76, 98, 97, 114, 109, 107, 109, 89, 83, 93, 86]
[84, 104, 126, 95, 115, 91, 107, 93, 117, 89, 107, 87, 107, 90, 94, 100, 89, 93]
[91, 95, 95, 97, 87, 92, 85, 88, 87, 104, 96, 115, 100, 133, 97, 104, 93, 96]
[76, 89, 96, 120, 113, 95, 118, 88, 113, 85, 105, 75, 102, 94, 96, 99, 98, 94]
[87, 74, 89, 94, 106, 88, 89, 104, 94, 117, 97, 125, 93, 111, 93, 104, 80, 96]
[74, 87, 87, 102, 91, 134, 98, 104, 102, 87, 95, 79, 98, 91, 93, 95, 99, 96]
[113, 95, 85, 95, 108, 89, 126, 111, 110, 113, 100, 100, 103, 97, 117, 100, 114, 88]
[104, 95, 93, 89, 79, 112, 96, 103, 106, 78, 96, 87, 87, 111, 95, 121, 93, 106]
[95, 108, 90, 103, 97, 102, 113, 119, 113, 117, 107, 96, 127, 75, 115, 93, 127, 100]
[102, 88, 98, 89, 90, 75, 100, 81, 92, 68, 94, 107, 89, 110, 107, 103, 115, 104]
[80, 98, 101, 104, 88, 97, 83, 102, 100, 120, 107, 110, 125, 112, 119, 112, 90, 99]
[103, 102, 112, 117, 102, 81, 82, 70, 82, 93, 71, 105, 93, 124, 110, 107, 89, 95]
[77, 103, 117, 110, 114, 82, 91, 81, 99, 102, 110, 110, 109, 111, 124, 100, 92, 100]
[103, 111, 125, 131, 108, 100, 90, 84, 79, 99, 90, 98, 77, 104, 79, 111, 88, 99]
[95, 106, 120, 99, 120, 96, 117, 82, 101, 83, 108, 97, 107, 102, 117, 109, 98, 112]
[117, 129, 119, 108, 100, 85, 121, 82, 96, 96, 95, 113, 92, 105, 84, 93, 99, 99]
[91, 114, 119, 92, 102, 113, 95, 112, 101, 81, 115, 109, 127, 96, 106, 103, 89, 75]
[91, 113, 89, 118, 93, 76, 114, 73, 93, 97, 107, 126, 101, 115, 89, 88, 100, 97]
*************************
output_matrix_shape:  21 * 18
*************************

Process finished with exit code 0

可以看到,卷积结果正确。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值