pytorch中使用interpolate可以很轻松的通过上下采样来实现改变Feature Map的Size。
import torch.nn.functional as F
#网络中的tensor都是float类型
q = torch.randint(-2,4,(1,3,5,5)).float()
print(q)
#直接设置size大小
y1 = F.interpolate(q, size=(3,3))
print(y1)
tensor([[[[-1., 0., -2., 2., 3.],
[ 3., 3., 2., 1., 0.],
[ 0., 1., 1., -2., 0.],
[-1., 1., 0., 0., 0.],
[ 1., 3., 2., -2., 3.]],
[[-1., -1., -1., 0., -2.],
[ 1., 1., 0., 3., 3.],
[ 2., 1., 1., 3., -2.],
[ 0., 2., 0., 2., -1.],
[ 0., 0., -1., -1., 0.]],
[[ 2., 2., 2., 2., 0.],
[ 2., 0., 2., 2., 0.],
[ 3., -2., 1., 1., -2.],
[-2., 1., 2., 1., 0.],
[ 0., 0., -1., 3., 1.]]]])
tensor([[[[-1., 0., 2.],
[ 3., 3., 1.],
[-1., 1., 0.]],
[[-1., -1., 0.],
[ 1., 1., 3.],
[ 0., 2., 2.]],
[[ 2., 2., 2.],
[ 2., 0., 2.],
[-2., 1., 1.]]]])
import torch.nn.functional as F
#网络中的tensor都是float类型
q = torch.randint(-2,4,(1,3,5,5)).float()
print(q)
#直接设置size大小
y1 = F.interpolate(q, scale_factor=2,mode="bilinear")
print(y1)
tensor([[[[ 3., 0.],
[ 3., 3.]],
[[-1., 3.],
[-1., -1.]],
[[ 3., -1.],
[ 1., 1.]]]])
/home1/zy/anaconda3/envs/torch/lib/python3.6/site-packages/torch/nn/functional.py:3063: UserWarning: Default upsampling behavior when mode=bilinear is changed to align_corners=False since 0.4.0. Please specify align_corners=True if the old behavior is desired. See the documentation of nn.Upsample for details.
"See the documentation of nn.Upsample for details.".format(mode))
tensor([[[[ 3.0000, 2.2500, 0.7500, 0.0000],
[ 3.0000, 2.4375, 1.3125, 0.7500],
[ 3.0000, 2.8125, 2.4375, 2.2500],
[ 3.0000, 3.0000, 3.0000, 3.0000]],
[[-1.0000, 0.0000, 2.0000, 3.0000],
[-1.0000, -0.2500, 1.2500, 2.0000],
[-1.0000, -0.7500, -0.2500, 0.0000],
[-1.0000, -1.0000, -1.0000, -1.0000]],
[[ 3.0000, 2.0000, 0.0000, -1.0000],
[ 2.5000, 1.7500, 0.2500, -0.5000],
[ 1.5000, 1.2500, 0.7500, 0.5000],
[ 1.0000, 1.0000, 1.0000, 1.0000]]]])