以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()