我有一个3dResNet model from PyTorch。我还注解掉了www.example.com源代码中的flatten行resnet.py,所以我的输出不应该是1D的。
下面是我的代码:
class VideoModel(nn.Module):
def __init__(self,num_channels=3):
super(VideoModel, self).__init__()
self.r2plus1d = models.video.r2plus1d_18(pretrained=True)
self.r2plus1d.fc = Identity()
for layer in self.r2plus1d.children():
layer.requires_grad_ = False
def forward(self, x):
print(x.shape)
x = self.r2plus1d(x)
print(x.shape)
return x
我的身份类的存在只是为了忽略一层:
class Identity(nn.Module):
def __init__(self):
super().__init__()
def forward(self, x):
return x
当我运行torch.randn(1, 3, 8, 112, 112)
作为输入时,得到以下输出:
torch.Size([1, 3, 8, 112, 112])
torch.Size([1, 512, 1, 1, 1])
为什么我有一个1D输出,即使我删除了fc层和平面化操作?有没有更好的方法来删除平面化操作?
1条答案
按热度按时间nle07wnf1#
原因是
AdaptiveAvgPool3d
层就在展平步骤之前。它是用参数output_size=(1,1,1)
调用的,因此将最后三个维度池化到(1,1,1)
,而不管它们的原始维度。在您的示例中,平均池之后的输出具有
(1,512,1,1,1)
形状,展平之后的输出具有(1,512)
形状,fc层之后的输出具有(1,400)
形状。因此展平操作不负责,请禁用平均池和所有后续步骤以获得所需的结果。