【手撕算法系列】2D卷积

2023-12-17 14:45:38

2D卷积相关的内容

经过卷积后输出的尺寸是MxM,假设输入的是spatial size是N x N,kernel size是 K,步幅stride是S,填充padding为P,那么:

M = (N ? k + 2P )/S+1

2D卷积中,卷积核的权重shape:(out_ch, in_ch , k, k)

代码实现

def conv2d(inputs, kernels, bias, stride, padding):
    """"
    inputs: c, h, w
    kernels: out_ch , in_ch, kh, kw; in_ch == c
    bias: out_ch
    """
    c, h, w =inputs.shape
    out_ch, in_ch, kh, kw = kernels.shape
    h_out = 1 + (h + 2*padding - kh) // stride
    w_out = 1 + (h + 2*padding - kh) // stride

    outputs = np.zeros((out_ch, h_out, w_out))
    
    inputs_pad = np.pad(inputs, ((0, 0), (padding, padding), (padding, padding)))

    for i in range(h_out):
        for j in range(w_out):
            area = inputs_pad[:, i*stride:i*stride+kh, j*stride:j*stride+kw] # in_ch, k, k
            outputs[:, i, j] = np.sum(area[None,:] * kernels, axis=(1,2,3)) + bias  
    return outputs

文章来源:https://blog.csdn.net/weixin_41845840/article/details/135039524
本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。