利用双线性插值初始化反卷积权重

以pytorch为例,反卷积接口如:

class torch.nn.ConvTranspose2d(in_channels, out_channels, kernel_size, stride=1, padding=0, output_padding=0, bias=True)

其输入输出尺寸计算公式一般为:

N=(w-1)×s+k-2p + output_padding

其中,一般来说,卷积核k=2*s, padding参数p=s//2,outputing_padding=0.

利用双线性插值初始化卷积核的代码如下:

def bilinear_kernel(f_shape, groups=None):
    '''
    :param f_shape: [in_chn, out_chn, h, w]
    :return:
    '''
    width = f_shape[-1]
    height = f_shape[-2]
    f = np.ceil(width / 2.0)
    c = (2 * f - 1 - f % 2) / (2.0 * f)
    bilinear = np.zeros([height, width])
    for x in range(width):
        for y in range(height):
            value = (1 - abs(x / f - c)) * (1 - abs(y / f - c))
            bilinear[x, y] = value
    # pdb.set_trace()
    if groups is not None:
        den = f_shape[0]//groups
        bilinear = bilinear/den
    weights = np.zeros(f_shape)
    for i in range(f_shape[0]):
        for j in range(f_shape[1]):
            weights[i,j,:,:] = bilinear
    return torch.from_numpy(weights).float()

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值