ResNet

# 调整已经训练好的ResNet网络

class ResNet(nn.Module):
    def __init__(self, num_classes=10):  # num_classes,此处为 二分类值为2
        super(ResNet, self).__init__()
        net = models.resnet18(pretrained=True)  # 从预训练模型加载resnet18网络参数
        net.classifier = nn.Sequential()  # 将分类层置空,下面将改变我们的分类层
        # self.features = net  # 保留resnet的特征层
        self.layer0 = nn.Sequential(
            net.conv1,
            net.bn1,
            net.relu,
            net.maxpool
        )
        self.layer1 = net.layer1
        self.layer2 = net.layer2
        self.layer3 = net.layer3
        self.layer4 = net.layer4
        self.layer5 = nn.Sequential(
            nn.Conv2d(512, 512, (3, 3), (2, 2), 1),
            nn.BatchNorm2d(512),
            nn.ReLU(True),
            nn.Conv2d(512, 512, (3, 3), (1, 1), 1),
            nn.BatchNorm2d(512),

            nn.Conv2d(512, 512, (3, 3), (1, 1), 1),
            nn.BatchNorm2d(512),
            nn.ReLU(True),
            nn.Conv2d(512, 1024, (3, 3), (1, 1), 1),
            nn.BatchNorm2d(1024),

        )
        self.avgpool = net.avgpool
        self.fc = nn.Linear(1024, 1000)

        self.classifier = nn.Sequential(  # 定义自己的分类层
            nn.Linear(1000, 64),  # 1000不能改变 ,由resnet18网络决定的,第二个参数为神经元个数可以微调
            nn.ReLU(True),
            nn.Dropout(0.5),
            nn.Linear(64, num_classes),
        )

    def forward(self, x):
        x = self.layer0(x)
        x = self.layer1(x)
        x = self.layer2(x)
        x = self.layer3(x)
        x = self.layer4(x)
        x = self.layer5(x)
        x = self.avgpool(x)

        x = x.view(x.size(0), -1)
        x = self.fc(x)
        x = self.classifier(x)
        return x


import ResNet_bySelf

# ResNet18 = ResNet_bySelf.ResNet18(3,10)

ResNet = ResNet()

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值