Code 计算conv2d,maxpooling操作后特征图的大小

Code 计算Conv2d,MaxPooling操作后特征图的大小

1. 计算公式:

  1. Conv2d 输出特征图计算公式参考:
    Tensorflow 1.x 和 Pytorch 中 Conv2d Padding的区别
  2. MaxPooling 输出特征图计算公式参考:
    【pytorch系列】torch.nn.MaxPool2d详解与尺寸计算
  3. Conv2d向下取整,MaxPooling向上取整。

2. Code:

	输入:操作模式,输入特征图大小,卷积核大小,步长等。
	输出:对应操作后,输出特征图大小。
import math


def count_conv(operation, input_size, k, d, s, p):
    input_size = [int(x1) for x1 in input_size]
    H_out, W_out = None, None
    if len(input_size) != 2:
        print("The height and width of the data is wrong!")
        return None

    k = [int(x2) for x2 in k]
    if len(k) == 1:
        k = 2 * k
    else:
        print("The kernel size is wrong!")
        return None

    d = [int(x3) for x3 in d]
    if len(d) == 1:
        d = 2 * d
    else:
        print("The dilatation is wrong!")
        return None

    s = [int(x4) for x4 in s]
    if len(s) == 1:
        s = 2 * s
    else:
        print("The stride is wrong!")
        return None

    if p[0] == 'same':
        if operation == 'conv':
            H_out = math.floor(input_size[0] / s[0])
            W_out = math.floor(input_size[1] / s[1])
        elif operation == 'maxpool':
            H_out = math.ceil(input_size[0] / s[0])
            W_out = math.ceil(input_size[1] / s[1])
        return [H_out, W_out]
    elif p[0] == 'valid':
        p = [0, 0]
    else:
        p = [int(x5) for x5 in p]
        if isinstance(p[0], int):
            if len(p) == 1:
                p = 2 * p
        else:
            print("The padding is wrong!")
            return None
    if operation == 'conv':
        H_out = math.floor((input_size[0] + 2 * p[0] - d[0] * (k[0] - 1) - 1) / s[0] + 1)
        W_out = math.floor((input_size[1] + 2 * p[1] - d[1] * (k[1] - 1) - 1) / s[1] + 1)
    elif operation == 'maxpool':
        H_out = math.ceil((input_size[0] + 2 * p[0] - d[0] * (k[0] - 1) - 1) / s[0] + 1)
        W_out = math.ceil((input_size[1] + 2 * p[1] - d[1] * (k[1] - 1) - 1) / s[1] + 1)
    return [H_out, W_out]


if __name__ == "__main__":
    operation = input("DL operation is: (input format: 'conv' or 'maxpool')  ")
    if operation == 'conv':
        input_size = input("The height and width of the data: (input format: H W)  ").split()
        k = input("The kernel size of conv:  ").split()
        d = input("The dilatation of kernel: (Default: 1)  ").split()
        s = input("The stride of conv: (Default: 1)  ").split()
        p = input("The padding of conv: (Default: 0)  ").split()
        out_size = count_conv(operation, input_size, k, d, s, p)
        print("The height and width of the output data is ([H_out, W_out])  ", out_size)
    elif operation == 'maxpool':
        input_size = input("The height and width of the data: (input format: H W)  ").split()
        k = input("The kernel size of conv:  ").split()
        d = input("The dilatation of kernel: (Default: 1)  ").split()
        s = input("The stride of conv: (Default: 1)  ").split()
        p = input("The padding of conv: (Default: 0)  ").split()
        out_size = count_conv(operation, input_size, k, d, s, p)
        print("The height and width of the output data is ([H_out, W_out])  ", out_size)

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值