python使用numpy实现卷积操作

python使用numpy实现卷积操作
talk is cheap,show you the code

import numpy as np

def Conv2(img, kernel, n, stride):
	#img:输入图片;kernel:卷积核值;n:卷积核大小为n*n;stride:步长。
	#return:feature map
    h, w = img.shape
    img = np.pad(img,((1,1),(1,1)),'constant',constant_values=0)
    res_h = ((h+2-n)//stride)+1 #卷积边长计算公式:((n+2*p-k)/stride)+1
    res_w = ((w+2-n)//stride)+1
    res = np.zeros([res_h, res_w])
    # print(res)
    for i in range(res_h):
        for j in range(res_w):
            temp = img[i*stride:i*stride+n, j*stride:j*stride+n]
            print((i*stride,i*stride+n,j*stride,j*stride+n)) #打印检查卷积核每次卷积的位置对否
            temp = np.multiply(kernel, temp)
            res[i][j] = temp.sum()
    return res


if __name__ == '__main__':
    A = np.array([[1, 1, 1, 1, 1], [1, 1, 1, 1, 1], [1, 1, 1, 1, 1], [1, 1, 1, 1, 1]])  # 4行5列
    ken = np.array([[2, 2], [2, 2]])
    print("result:",Conv2(A, ken, 2, 2))

你可能感兴趣的:(图像处理python,自学,机器学习,之路,剑指offer,python,cnn,深度学习)