解决PaddlePaddle飞桨在迁移学习使用预训练模型时更改num_classes参数出现警告

发布时间 2023-05-10 00:35:17作者: belhomme

当我们使用 PaddlePaddle 进行迁移学习的时候,直接导入模型虽然是可以的,但是总是会有个警告

如直接用官方的 resnet101 并加载预训练模型的话

model = paddle.vision.models.resnet101(pretrained=True, num_classes=2)

会提示这些信息:

model = paddle.vision.models.resnet101(pretrained=True, num_classes=2)
W0508 14:42:41.530314  1313 device_context.cc:447] Please NOTE: device: 0, GPU Compute Capability: 7.0, Driver API Version: 11.2, Runtime API Version: 10.1
W0508 14:42:41.535259  1313 device_context.cc:465] device: 0, cuDNN Version: 7.6.
/opt/conda/envs/python35-paddle120-env/lib/python3.7/site-packages/paddle/fluid/dygraph/layers.py:1441: UserWarning: Skip loading for fc.weight. fc.weight receives a shape [2048, 1000], but the expected shape is [2048, 2].
  warnings.warn(("Skip loading for {}. ".format(key) + str(err)))
/opt/conda/envs/python35-paddle120-env/lib/python3.7/site-packages/paddle/fluid/dygraph/layers.py:1441: UserWarning: Skip loading for fc.bias. fc.bias receives a shape [1000], but the expected shape is [2].
  warnings.warn(("Skip loading for {}. ".format(key) + str(err)))

虽然说最后的训练效果还是会很不错,但是这个警告信息看着却是很难受,原因是因为我们自己分类的数据集 num_classes 只有 2 ,和预训练模型的 num_classes 并不匹配,因此我们要进行以下操作,实现既加载了预训练模型,又能更好的训练自己的数据

class ResNet(nn.Layer):
    def __init__(self):
        super(ResNeXt, self).__init__()
        # 加载预训练模型
        self.layer = paddle.vision.models.resnet101(pretrained=True)
        self.fc = nn.Sequential(
            nn.Dropout(0.5),
            nn.Linear(1000, 100),
            nn.Dropout(0.5),
            nn.Linear(100, 2),
        )

    def forward(self, inputs):
        outputs = self.layer(inputs)
        outputs = self.fc(outputs)

        return outputs

model = ResNet()

经过此番操作之后,输出内容就没有警告了

W0510 00:19:38.900521  7391 gpu_context.cc:244] Please NOTE: device: 0, GPU Compute Capability: 7.0, Driver API Version: 11.2, Runtime API Version: 10.1
W0510 00:19:38.904939  7391 gpu_context.cc:272] device: 0, cuDNN Version: 7.6.