cannot assign ‘list‘ as child module ‘backbone‘ (torch.nn.Module or None expected)

1.问题描述

本来想把Googlenet作为主干网络,去掉最后三层,但发生了报错:cannot assign ‘list’ as child module ‘backbone’ (torch.nn.Module or None expected)
代码如下:

self.backbone = models.googlenet(pretrained=True)
self.backbone = list(self.backbone.children())[:-3]
self.backbone = nn.Sequential(*self.backbone, nn.Conv2d(1024, 512, kernel_size=1))

2.解决办法

backbone = models.googlenet(pretrained=True)
self.backbone = list(backbone.children())[:-3]
self.backbone = nn.Sequential(*self.backbone, nn.Conv2d(1024, 512, kernel_size=1))

修改成这样就好了,不知道为啥!!

你可能感兴趣的:(小问题记录,深度学习,人工智能)