深度学习camp-第J4周:ResNet与DenseNet结合探索

  • 🍨 本文为🔗365天深度学习训练营 中的学习记录博客
  • 🍖 原作者:K同学啊

本周任务:

  • 探索ResNet和DenseNet的结合可能性
  • 本周任务较难,我们在chatGPT的帮助下完成

一、网络的构建

设计一种结合 ResNet 和 DenseNet 的网络架构,目标是在性能与复杂度之间实现平衡,同时保持与 DenseNet-121 相当的训练速度,可以通过以下步骤设计一种新的网络结构,称为 ResDenseNet(暂命名)。这种网络结构结合了 ResNet 的残差连接和 DenseNet 的密集连接优点,同时对复杂度加以控制。

设计思路
残差模块与密集模块结合:

在网络的不同阶段,使用残差模块(ResBlock)来捕获浅层特征。
在每个阶段的后期引入密集模块(DenseBlock),实现高效的特征复用。
通过调整每层的通道数,避免过多的计算和内存消耗。
瓶颈设计(Bottleneck Block):

每个模块采用瓶颈层,减少计算复杂度。
通过 1x1 卷积压缩和扩展特征通道数。
混合连接方式:

引入 局部密集连接,只连接同一模块内的层,避免 DenseNet 的全连接导致的内存开销。
在模块之间使用残差连接,便于信息流通。
网络深度与宽度的平衡:

将 DenseNet 的增长率(growth rate)减少,适当减少特征图通道数增长。
模块之间引入过渡层(Transition Layer)以压缩特征图尺寸和通道数。

import torch
import torch.nn as nnclass Bottleneck(nn.Module):def __init__(self, in_channels, growth_rate):super(Bottleneck, self).__init__()self.bn1 = nn.BatchNorm2d(in_channels)self.conv1 = nn.Conv2d(in_channels, 4 * growth_rate, kernel_size=1, stride=1, bias=False)self.bn2 = nn.BatchNorm2d(4 * growth_rate)self.conv2 = nn.Conv2d(4 * growth_rate, growth_rate, kernel_size=3, stride=1, padding=1, bias=False)def forward(self, x):out = self.conv1(self.bn1(x))out = self.conv2(self.bn2(out))return torch.cat([x, out], dim=1)class DenseBlock(nn.Module):def __init__(self, num_layers, in_channels, growth_rate):super(DenseBlock, self).__init__()self.layers = nn.ModuleList()for i in range(num_layers):self.layers.append(Bottleneck(in_channels + i * growth_rate, growth_rate))# 为了残差连接,可能需要调整通道数以匹配输入输出self.residual = nn.Conv2d(in_channels, in_channels + num_layers * growth_rate, kernel_size=1, bias=False)def forward(self, x):identity = self.residual(x)  # 将输入调整为与 DenseBlock 输出通道一致for layer in self.layers:x = layer(x)  # 密集连接,逐层拼接return x + identity  # 残差连接:输入与输出相加class TransitionLayer(nn.Module):def __init__(self, in_channels, out_channels):super(TransitionLayer, self).__init__()self.bn = nn.BatchNorm2d(in_channels)self.conv = nn.Conv2d(in_channels, out_channels, kernel_size=1, stride=1, bias=False)self.pool = nn.AvgPool2d(kernel_size=2, stride=2)def forward(self, x):x = self.conv(self.bn(x))return self.pool(x)class ResDenseNet(nn.Module):def __init__(self, num_classes=1000):super(ResDenseNet, self).__init__()self.stem = nn.Sequential(nn.Conv2d(3, 64, kernel_size=7, stride=2, padding=3, bias=False),nn.BatchNorm2d(64),nn.ReLU(inplace=True),nn.MaxPool2d(kernel_size=3, stride=2, padding=1))self.stage1 = self._make_stage(64, 128, num_layers=4, growth_rate=16)self.stage2 = self._make_stage(128, 256, num_layers=4, growth_rate=16)self.stage3 = self._make_stage(256, 512, num_layers=6, growth_rate=12)self.stage4 = self._make_stage(512, 1024, num_layers=6, growth_rate=12)self.classifier = nn.Linear(1024, num_classes)def _make_stage(self, in_channels, out_channels, num_layers, growth_rate):dense_block = DenseBlock(num_layers, in_channels, growth_rate)transition = TransitionLayer(in_channels + num_layers * growth_rate, out_channels)return nn.Sequential(dense_block, transition)def forward(self, x):x = self.stem(x)x = self.stage1(x)x = self.stage2(x)x = self.stage3(x)x = self.stage4(x)x = torch.mean(x, dim=[2, 3])  # Global Average Poolingreturn self.classifier(x)device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
model =  ResDenseNet().to(device)
model

代码输出:

ResDenseNet((stem): Sequential((0): Conv2d(3, 64, kernel_size=(7, 7), stride=(2, 2), padding=(3, 3), bias=False)(1): BatchNorm2d(64, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)(2): ReLU(inplace=True)(3): MaxPool2d(kernel_size=3, stride=2, padding=1, dilation=1, ceil_mode=False))(stage1): Sequential((0): DenseBlock((layers): ModuleList((0): Bottleneck((bn1): BatchNorm2d(64, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)(conv1): Conv2d(64, 64, kernel_size=(1, 1), stride=(1, 1), bias=False)(bn2): BatchNorm2d(64, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)(conv2): Conv2d(64, 16, kernel_size=(3, 3), stride=(1, 1), padding=(1, 1), bias=False))(1): Bottleneck((bn1): BatchNorm2d(80, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)(conv1): Conv2d(80, 64, kernel_size=(1, 1), stride=(1, 1), bias=False)(bn2): BatchNorm2d(64, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)(conv2): Conv2d(64, 16, kernel_size=(3, 3), stride=(1, 1), padding=(1, 1), bias=False))(2): Bottleneck((bn1): BatchNorm2d(96, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)(conv1): Conv2d(96, 64, kernel_size=(1, 1), stride=(1, 1), bias=False)(bn2): BatchNorm2d(64, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)(conv2): Conv2d(64, 16, kernel_size=(3, 3), stride=(1, 1), padding=(1, 1), bias=False))(3): Bottleneck((bn1): BatchNorm2d(112, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)(conv1): Conv2d(112, 64, kernel_size=(1, 1), stride=(1, 1), bias=False)(bn2): BatchNorm2d(64, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)(conv2): Conv2d(64, 16, kernel_size=(3, 3), stride=(1, 1), padding=(1, 1), bias=False)))(residual): Conv2d(64, 128, kernel_size=(1, 1), stride=(1, 1), bias=False))(1): TransitionLayer((bn): BatchNorm2d(128, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)(conv): Conv2d(128, 128, kernel_size=(1, 1), stride=(1, 1), bias=False)(pool): AvgPool2d(kernel_size=2, stride=2, padding=0)))(stage2): Sequential((0): DenseBlock((layers): ModuleList((0): Bottleneck((bn1): BatchNorm2d(128, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)(conv1): Conv2d(128, 64, kernel_size=(1, 1), stride=(1, 1), bias=False)(bn2): BatchNorm2d(64, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)(conv2): Conv2d(64, 16, kernel_size=(3, 3), stride=(1, 1), padding=(1, 1), bias=False))(1): Bottleneck((bn1): BatchNorm2d(144, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)(conv1): Conv2d(144, 64, kernel_size=(1, 1), stride=(1, 1), bias=False)(bn2): BatchNorm2d(64, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)(conv2): Conv2d(64, 16, kernel_size=(3, 3), stride=(1, 1), padding=(1, 1), bias=False))(2): Bottleneck((bn1): BatchNorm2d(160, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)(conv1): Conv2d(160, 64, kernel_size=(1, 1), stride=(1, 1), bias=False)(bn2): BatchNorm2d(64, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)(conv2): Conv2d(64, 16, kernel_size=(3, 3), stride=(1, 1), padding=(1, 1), bias=False))(3): Bottleneck((bn1): BatchNorm2d(176, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)(conv1): Conv2d(176, 64, kernel_size=(1, 1), stride=(1, 1), bias=False)(bn2): BatchNorm2d(64, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)(conv2): Conv2d(64, 16, kernel_size=(3, 3), stride=(1, 1), padding=(1, 1), bias=False)))(residual): Conv2d(128, 192, kernel_size=(1, 1), stride=(1, 1), bias=False))(1): TransitionLayer((bn): BatchNorm2d(192, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)(conv): Conv2d(192, 256, kernel_size=(1, 1), stride=(1, 1), bias=False)(pool): AvgPool2d(kernel_size=2, stride=2, padding=0)))(stage3): Sequential((0): DenseBlock((layers): ModuleList((0): Bottleneck((bn1): BatchNorm2d(256, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)(conv1): Conv2d(256, 48, kernel_size=(1, 1), stride=(1, 1), bias=False)(bn2): BatchNorm2d(48, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)(conv2): Conv2d(48, 12, kernel_size=(3, 3), stride=(1, 1), padding=(1, 1), bias=False))(1): Bottleneck((bn1): BatchNorm2d(268, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)(conv1): Conv2d(268, 48, kernel_size=(1, 1), stride=(1, 1), bias=False)(bn2): BatchNorm2d(48, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)(conv2): Conv2d(48, 12, kernel_size=(3, 3), stride=(1, 1), padding=(1, 1), bias=False))(2): Bottleneck((bn1): BatchNorm2d(280, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)(conv1): Conv2d(280, 48, kernel_size=(1, 1), stride=(1, 1), bias=False)(bn2): BatchNorm2d(48, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)(conv2): Conv2d(48, 12, kernel_size=(3, 3), stride=(1, 1), padding=(1, 1), bias=False))(3): Bottleneck((bn1): BatchNorm2d(292, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)(conv1): Conv2d(292, 48, kernel_size=(1, 1), stride=(1, 1), bias=False)(bn2): BatchNorm2d(48, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)(conv2): Conv2d(48, 12, kernel_size=(3, 3), stride=(1, 1), padding=(1, 1), bias=False))(4): Bottleneck((bn1): BatchNorm2d(304, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)(conv1): Conv2d(304, 48, kernel_size=(1, 1), stride=(1, 1), bias=False)(bn2): BatchNorm2d(48, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)(conv2): Conv2d(48, 12, kernel_size=(3, 3), stride=(1, 1), padding=(1, 1), bias=False))(5): Bottleneck((bn1): BatchNorm2d(316, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)(conv1): Conv2d(316, 48, kernel_size=(1, 1), stride=(1, 1), bias=False)(bn2): BatchNorm2d(48, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)(conv2): Conv2d(48, 12, kernel_size=(3, 3), stride=(1, 1), padding=(1, 1), bias=False)))(residual): Conv2d(256, 328, kernel_size=(1, 1), stride=(1, 1), bias=False))(1): TransitionLayer((bn): BatchNorm2d(328, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)(conv): Conv2d(328, 512, kernel_size=(1, 1), stride=(1, 1), bias=False)(pool): AvgPool2d(kernel_size=2, stride=2, padding=0)))(stage4): Sequential((0): DenseBlock((layers): ModuleList((0): Bottleneck((bn1): BatchNorm2d(512, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)(conv1): Conv2d(512, 48, kernel_size=(1, 1), stride=(1, 1), bias=False)(bn2): BatchNorm2d(48, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)(conv2): Conv2d(48, 12, kernel_size=(3, 3), stride=(1, 1), padding=(1, 1), bias=False))(1): Bottleneck((bn1): BatchNorm2d(524, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)(conv1): Conv2d(524, 48, kernel_size=(1, 1), stride=(1, 1), bias=False)(bn2): BatchNorm2d(48, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)(conv2): Conv2d(48, 12, kernel_size=(3, 3), stride=(1, 1), padding=(1, 1), bias=False))(2): Bottleneck((bn1): BatchNorm2d(536, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)(conv1): Conv2d(536, 48, kernel_size=(1, 1), stride=(1, 1), bias=False)(bn2): BatchNorm2d(48, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)(conv2): Conv2d(48, 12, kernel_size=(3, 3), stride=(1, 1), padding=(1, 1), bias=False))(3): Bottleneck((bn1): BatchNorm2d(548, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)(conv1): Conv2d(548, 48, kernel_size=(1, 1), stride=(1, 1), bias=False)(bn2): BatchNorm2d(48, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)(conv2): Conv2d(48, 12, kernel_size=(3, 3), stride=(1, 1), padding=(1, 1), bias=False))(4): Bottleneck((bn1): BatchNorm2d(560, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)(conv1): Conv2d(560, 48, kernel_size=(1, 1), stride=(1, 1), bias=False)(bn2): BatchNorm2d(48, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)(conv2): Conv2d(48, 12, kernel_size=(3, 3), stride=(1, 1), padding=(1, 1), bias=False))(5): Bottleneck((bn1): BatchNorm2d(572, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)(conv1): Conv2d(572, 48, kernel_size=(1, 1), stride=(1, 1), bias=False)(bn2): BatchNorm2d(48, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)(conv2): Conv2d(48, 12, kernel_size=(3, 3), stride=(1, 1), padding=(1, 1), bias=False)))(residual): Conv2d(512, 584, kernel_size=(1, 1), stride=(1, 1), bias=False))(1): TransitionLayer((bn): BatchNorm2d(584, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)(conv): Conv2d(584, 1024, kernel_size=(1, 1), stride=(1, 1), bias=False)(pool): AvgPool2d(kernel_size=2, stride=2, padding=0)))(classifier): Linear(in_features=1024, out_features=1000, bias=True)
)

代码输入:

import torchsummary as summary
summary.summary(model, (3, 224, 224))

代码输出:

----------------------------------------------------------------Layer (type)               Output Shape         Param #
================================================================Conv2d-1         [-1, 64, 112, 112]           9,408BatchNorm2d-2         [-1, 64, 112, 112]             128ReLU-3         [-1, 64, 112, 112]               0MaxPool2d-4           [-1, 64, 56, 56]               0Conv2d-5          [-1, 128, 56, 56]           8,192BatchNorm2d-6           [-1, 64, 56, 56]             128Conv2d-7           [-1, 64, 56, 56]           4,096BatchNorm2d-8           [-1, 64, 56, 56]             128Conv2d-9           [-1, 16, 56, 56]           9,216Bottleneck-10           [-1, 80, 56, 56]               0BatchNorm2d-11           [-1, 80, 56, 56]             160Conv2d-12           [-1, 64, 56, 56]           5,120BatchNorm2d-13           [-1, 64, 56, 56]             128Conv2d-14           [-1, 16, 56, 56]           9,216Bottleneck-15           [-1, 96, 56, 56]               0BatchNorm2d-16           [-1, 96, 56, 56]             192Conv2d-17           [-1, 64, 56, 56]           6,144BatchNorm2d-18           [-1, 64, 56, 56]             128Conv2d-19           [-1, 16, 56, 56]           9,216Bottleneck-20          [-1, 112, 56, 56]               0BatchNorm2d-21          [-1, 112, 56, 56]             224Conv2d-22           [-1, 64, 56, 56]           7,168BatchNorm2d-23           [-1, 64, 56, 56]             128Conv2d-24           [-1, 16, 56, 56]           9,216Bottleneck-25          [-1, 128, 56, 56]               0DenseBlock-26          [-1, 128, 56, 56]               0BatchNorm2d-27          [-1, 128, 56, 56]             256Conv2d-28          [-1, 128, 56, 56]          16,384AvgPool2d-29          [-1, 128, 28, 28]               0TransitionLayer-30          [-1, 128, 28, 28]               0Conv2d-31          [-1, 192, 28, 28]          24,576BatchNorm2d-32          [-1, 128, 28, 28]             256Conv2d-33           [-1, 64, 28, 28]           8,192BatchNorm2d-34           [-1, 64, 28, 28]             128Conv2d-35           [-1, 16, 28, 28]           9,216Bottleneck-36          [-1, 144, 28, 28]               0BatchNorm2d-37          [-1, 144, 28, 28]             288Conv2d-38           [-1, 64, 28, 28]           9,216BatchNorm2d-39           [-1, 64, 28, 28]             128Conv2d-40           [-1, 16, 28, 28]           9,216Bottleneck-41          [-1, 160, 28, 28]               0BatchNorm2d-42          [-1, 160, 28, 28]             320Conv2d-43           [-1, 64, 28, 28]          10,240BatchNorm2d-44           [-1, 64, 28, 28]             128Conv2d-45           [-1, 16, 28, 28]           9,216Bottleneck-46          [-1, 176, 28, 28]               0BatchNorm2d-47          [-1, 176, 28, 28]             352Conv2d-48           [-1, 64, 28, 28]          11,264BatchNorm2d-49           [-1, 64, 28, 28]             128Conv2d-50           [-1, 16, 28, 28]           9,216Bottleneck-51          [-1, 192, 28, 28]               0DenseBlock-52          [-1, 192, 28, 28]               0BatchNorm2d-53          [-1, 192, 28, 28]             384Conv2d-54          [-1, 256, 28, 28]          49,152AvgPool2d-55          [-1, 256, 14, 14]               0TransitionLayer-56          [-1, 256, 14, 14]               0Conv2d-57          [-1, 328, 14, 14]          83,968BatchNorm2d-58          [-1, 256, 14, 14]             512Conv2d-59           [-1, 48, 14, 14]          12,288BatchNorm2d-60           [-1, 48, 14, 14]              96Conv2d-61           [-1, 12, 14, 14]           5,184Bottleneck-62          [-1, 268, 14, 14]               0BatchNorm2d-63          [-1, 268, 14, 14]             536Conv2d-64           [-1, 48, 14, 14]          12,864BatchNorm2d-65           [-1, 48, 14, 14]              96Conv2d-66           [-1, 12, 14, 14]           5,184Bottleneck-67          [-1, 280, 14, 14]               0BatchNorm2d-68          [-1, 280, 14, 14]             560Conv2d-69           [-1, 48, 14, 14]          13,440BatchNorm2d-70           [-1, 48, 14, 14]              96Conv2d-71           [-1, 12, 14, 14]           5,184Bottleneck-72          [-1, 292, 14, 14]               0BatchNorm2d-73          [-1, 292, 14, 14]             584Conv2d-74           [-1, 48, 14, 14]          14,016BatchNorm2d-75           [-1, 48, 14, 14]              96Conv2d-76           [-1, 12, 14, 14]           5,184Bottleneck-77          [-1, 304, 14, 14]               0BatchNorm2d-78          [-1, 304, 14, 14]             608Conv2d-79           [-1, 48, 14, 14]          14,592BatchNorm2d-80           [-1, 48, 14, 14]              96Conv2d-81           [-1, 12, 14, 14]           5,184Bottleneck-82          [-1, 316, 14, 14]               0BatchNorm2d-83          [-1, 316, 14, 14]             632Conv2d-84           [-1, 48, 14, 14]          15,168BatchNorm2d-85           [-1, 48, 14, 14]              96Conv2d-86           [-1, 12, 14, 14]           5,184Bottleneck-87          [-1, 328, 14, 14]               0DenseBlock-88          [-1, 328, 14, 14]               0BatchNorm2d-89          [-1, 328, 14, 14]             656Conv2d-90          [-1, 512, 14, 14]         167,936AvgPool2d-91            [-1, 512, 7, 7]               0TransitionLayer-92            [-1, 512, 7, 7]               0Conv2d-93            [-1, 584, 7, 7]         299,008BatchNorm2d-94            [-1, 512, 7, 7]           1,024Conv2d-95             [-1, 48, 7, 7]          24,576BatchNorm2d-96             [-1, 48, 7, 7]              96Conv2d-97             [-1, 12, 7, 7]           5,184Bottleneck-98            [-1, 524, 7, 7]               0BatchNorm2d-99            [-1, 524, 7, 7]           1,048Conv2d-100             [-1, 48, 7, 7]          25,152BatchNorm2d-101             [-1, 48, 7, 7]              96Conv2d-102             [-1, 12, 7, 7]           5,184Bottleneck-103            [-1, 536, 7, 7]               0BatchNorm2d-104            [-1, 536, 7, 7]           1,072Conv2d-105             [-1, 48, 7, 7]          25,728BatchNorm2d-106             [-1, 48, 7, 7]              96Conv2d-107             [-1, 12, 7, 7]           5,184Bottleneck-108            [-1, 548, 7, 7]               0BatchNorm2d-109            [-1, 548, 7, 7]           1,096Conv2d-110             [-1, 48, 7, 7]          26,304BatchNorm2d-111             [-1, 48, 7, 7]              96Conv2d-112             [-1, 12, 7, 7]           5,184Bottleneck-113            [-1, 560, 7, 7]               0BatchNorm2d-114            [-1, 560, 7, 7]           1,120Conv2d-115             [-1, 48, 7, 7]          26,880BatchNorm2d-116             [-1, 48, 7, 7]              96Conv2d-117             [-1, 12, 7, 7]           5,184Bottleneck-118            [-1, 572, 7, 7]               0BatchNorm2d-119            [-1, 572, 7, 7]           1,144Conv2d-120             [-1, 48, 7, 7]          27,456BatchNorm2d-121             [-1, 48, 7, 7]              96Conv2d-122             [-1, 12, 7, 7]           5,184Bottleneck-123            [-1, 584, 7, 7]               0DenseBlock-124            [-1, 584, 7, 7]               0BatchNorm2d-125            [-1, 584, 7, 7]           1,168Conv2d-126           [-1, 1024, 7, 7]         598,016AvgPool2d-127           [-1, 1024, 3, 3]               0TransitionLayer-128           [-1, 1024, 3, 3]               0Linear-129                 [-1, 1000]       1,025,000
================================================================
Total params: 2,734,104
Trainable params: 2,734,104
Non-trainable params: 0
----------------------------------------------------------------
Input size (MB): 0.57
Forward/backward pass size (MB): 95.40
Params size (MB): 10.43
Estimated Total Size (MB): 106.41
----------------------------------------------------------------

接下来我们简单阅读我们构建的网络:

  1. 首先我们构建Bottleneck,bottleneck的主要目的是构建denseblock的组成部分,通过两次归一化层以及两次卷积构成
  2. 随后我们构建Denseblock,并且使用残差连接
  3. 构建transition层进行池化,最终能够全连接
  4. 整体网络构建如下:
Input (224x224x3)||   Conv2d (7x7, stride=2)|   BatchNorm2d|   ReLU|   MaxPool2d (3x3, stride=2)v
Stem Layer (64 channels)|v
Stage 1: DenseBlock + TransitionLayer (64 -> 128 channels)|  v
Stage 2: DenseBlock + TransitionLayer (128 -> 256 channels)|v
Stage 3: DenseBlock + TransitionLayer (256 -> 512 channels)|v
Stage 4: DenseBlock + TransitionLayer (512 -> 1024 channels)|v
Global Average Pooling (1024x1x1)|v
Fully Connected Layer (1024 -> num_classes)|v
Output (num_classes)

二、对上周的乳腺癌识别

import pathlib
data_dir = './data/J3-1-data'
data_dir = pathlib.Path(data_dir)data_path = list(data_dir.glob('*'))
classNames = [path.name for path in data_path]
print(classNames)

代码输出:

['0', '1']
from torch.utils.data import DataLoader
from torchvision import datasets, transformstrain_transforms = transforms.Compose([transforms.Resize([224, 224]),transforms.ToTensor(),transforms.Normalize([0.485, 0.456, 0.406], [0.229, 0.224, 0.225])
])total_data = datasets.ImageFolder(data_dir, transform=train_transforms)
total_data

代码输出:

Dataset ImageFolderNumber of datapoints: 13403Root location: data\J3-1-dataStandardTransform
Transform: Compose(Resize(size=[224, 224], interpolation=bilinear, max_size=None, antialias=True)ToTensor()Normalize(mean=[0.485, 0.456, 0.406], std=[0.229, 0.224, 0.225]))
train_size = int(0.7 * len(total_data)) 
remain_size  = len(total_data) - train_size  
train_dataset, remain_dataset = torch.utils.data.random_split(total_data, [train_size, remain_size])
test_size = int(0.6 * len(remain_dataset))
validate_size = len(remain_dataset) - test_size
test_dataset, validate_dataset = torch.utils.data.random_split(remain_dataset, [test_size, validate_size]) #随机分配数据
train_dataset, test_dataset, validate_dataset

代码输出:

(<torch.utils.data.dataset.Subset at 0x2138402dbb0>,<torch.utils.data.dataset.Subset at 0x21383feb590>,<torch.utils.data.dataset.Subset at 0x21383ece690>)
batch_size = 32train_dl = DataLoader(train_dataset, batch_size=batch_size,shuffle=True)test_dl = DataLoader(test_dataset,batch_size = batch_size,shuffle = True
)validate_dl = DataLoader(validate_dataset,batch_size = batch_size,shuffle = False
)for x, y in validate_dl:print("shape of x [N, C, H, W]:", x.shape)print("shape of y:", y.shape, y.dtype)break

代码输出:

shape of x [N, C, H, W]: torch.Size([32, 3, 224, 224])
shape of y: torch.Size([32]) torch.int64
def train(dataloader, model, loss_fn, optimizer):size = len(dataloader.dataset)num_batches = len(dataloader)train_loss, train_acc = 0, 0for x, y in dataloader:x, y = x.to(device), y.to(device)pred = model(x)loss = loss_fn(pred, y)#backwardoptimizer.zero_grad()loss.backward()optimizer.step()train_acc += (pred.argmax(1) == y).type(torch.float).sum().item()train_loss += loss.item()train_acc /= sizetrain_loss /= num_batchesreturn train_acc, train_lossdef test(dataloader, model, loss_fn):size = len(dataloader.dataset)num_batches = len(dataloader)test_loss, test_acc = 0, 0for x, y in dataloader:x, y = x.to(device), y.to(device)pred = model(x)loss = loss_fn(pred, y)test_acc += (pred.argmax(1) == y).type(torch.float).sum().item()test_loss += loss.item()test_acc /= sizetest_loss /= num_batchesreturn test_acc, test_loss

训练:

import copy
from torch.optim.lr_scheduler import ReduceLROnPlateauopt = torch.optim.Adam(model.parameters(), lr= 1e-4)
scheduler = ReduceLROnPlateau(opt, mode='min', factor=0.1, patience=5, verbose=True) # 当指标(如损失)连续 5 次没有改善时,将学习率乘以 0.1
loss_fn = nn.CrossEntropyLoss() # 交叉熵epochs = 32train_loss = []
train_acc  = []
test_loss  = []
test_acc   = []best_acc = 0    # 设置一个最佳准确率,作为最佳模型的判别指标for epoch in range(epochs):model.train()epoch_train_acc, epoch_train_loss = train(train_dl, model, loss_fn, opt)model.eval()epoch_test_acc, epoch_test_loss = test(test_dl, model, loss_fn)scheduler.step(epoch_test_loss)if epoch_test_acc > best_acc:best_acc = epoch_test_accbest_model = copy.deepcopy(model)train_acc.append(epoch_train_acc)train_loss.append(epoch_train_loss)test_acc.append(epoch_test_acc)test_loss.append(epoch_test_loss)# 获取当前的学习率lr = opt.state_dict()['param_groups'][0]['lr']template = ('Epoch:{:2d}, Train_acc:{:.1f}%, Train_loss:{:.3f}, Test_acc:{:.1f}%, Test_loss:{:.3f}, Lr:{:.2E}')print(template.format(epoch+1, epoch_train_acc*100, epoch_train_loss, epoch_test_acc*100, epoch_test_loss, lr))# 保存最佳模型到文件中
PATH = './best_model.pth'  # 保存的参数文件名
torch.save(best_model.state_dict(), PATH)print('Done')

代码输出:

Epoch: 1, Train_acc:80.7%, Train_loss:0.892, Test_acc:71.2%, Test_loss:1.992, Lr:1.00E-04
Epoch: 2, Train_acc:82.5%, Train_loss:0.409, Test_acc:83.9%, Test_loss:0.393, Lr:1.00E-04
Epoch: 3, Train_acc:83.4%, Train_loss:0.395, Test_acc:82.8%, Test_loss:0.443, Lr:1.00E-04
Epoch: 4, Train_acc:83.8%, Train_loss:0.380, Test_acc:84.1%, Test_loss:0.378, Lr:1.00E-04
Epoch: 5, Train_acc:84.2%, Train_loss:0.375, Test_acc:54.6%, Test_loss:1.337, Lr:1.00E-04
Epoch: 6, Train_acc:84.2%, Train_loss:0.378, Test_acc:84.7%, Test_loss:0.354, Lr:1.00E-04
Epoch: 7, Train_acc:84.7%, Train_loss:0.368, Test_acc:64.4%, Test_loss:0.696, Lr:1.00E-04
Epoch: 8, Train_acc:84.9%, Train_loss:0.360, Test_acc:84.7%, Test_loss:0.493, Lr:1.00E-04
Epoch: 9, Train_acc:85.1%, Train_loss:0.362, Test_acc:73.7%, Test_loss:0.506, Lr:1.00E-04
Epoch:10, Train_acc:85.2%, Train_loss:0.350, Test_acc:77.3%, Test_loss:0.791, Lr:1.00E-04
Epoch:11, Train_acc:85.5%, Train_loss:0.352, Test_acc:53.7%, Test_loss:2.223, Lr:1.00E-04
Epoch:12, Train_acc:85.6%, Train_loss:0.351, Test_acc:84.5%, Test_loss:0.438, Lr:1.00E-05
Epoch:13, Train_acc:86.7%, Train_loss:0.321, Test_acc:87.4%, Test_loss:0.295, Lr:1.00E-05
Epoch:14, Train_acc:86.5%, Train_loss:0.314, Test_acc:87.3%, Test_loss:0.296, Lr:1.00E-05
Epoch:15, Train_acc:87.2%, Train_loss:0.310, Test_acc:87.1%, Test_loss:0.320, Lr:1.00E-05
Epoch:16, Train_acc:87.6%, Train_loss:0.307, Test_acc:87.2%, Test_loss:0.297, Lr:1.00E-05
Epoch:17, Train_acc:87.4%, Train_loss:0.309, Test_acc:88.2%, Test_loss:0.289, Lr:1.00E-05
Epoch:18, Train_acc:87.0%, Train_loss:0.310, Test_acc:87.6%, Test_loss:0.293, Lr:1.00E-05
Epoch:19, Train_acc:87.1%, Train_loss:0.305, Test_acc:88.3%, Test_loss:0.281, Lr:1.00E-05
Epoch:20, Train_acc:87.6%, Train_loss:0.298, Test_acc:87.6%, Test_loss:0.299, Lr:1.00E-05
Epoch:21, Train_acc:87.5%, Train_loss:0.299, Test_acc:87.9%, Test_loss:0.289, Lr:1.00E-05
Epoch:22, Train_acc:87.5%, Train_loss:0.299, Test_acc:88.3%, Test_loss:0.292, Lr:1.00E-05
Epoch:23, Train_acc:88.0%, Train_loss:0.296, Test_acc:86.4%, Test_loss:0.347, Lr:1.00E-05
Epoch:24, Train_acc:87.7%, Train_loss:0.299, Test_acc:88.1%, Test_loss:0.286, Lr:1.00E-05
Epoch:25, Train_acc:87.8%, Train_loss:0.294, Test_acc:86.4%, Test_loss:0.327, Lr:1.00E-06
Epoch:26, Train_acc:87.9%, Train_loss:0.290, Test_acc:87.5%, Test_loss:0.291, Lr:1.00E-06
Epoch:27, Train_acc:88.2%, Train_loss:0.286, Test_acc:88.9%, Test_loss:0.272, Lr:1.00E-06
Epoch:28, Train_acc:88.1%, Train_loss:0.287, Test_acc:88.6%, Test_loss:0.277, Lr:1.00E-06
Epoch:29, Train_acc:88.2%, Train_loss:0.286, Test_acc:89.4%, Test_loss:0.269, Lr:1.00E-06
Epoch:30, Train_acc:88.1%, Train_loss:0.285, Test_acc:89.1%, Test_loss:0.271, Lr:1.00E-06
Epoch:31, Train_acc:88.1%, Train_loss:0.288, Test_acc:88.9%, Test_loss:0.274, Lr:1.00E-06
Epoch:32, Train_acc:87.9%, Train_loss:0.291, Test_acc:89.1%, Test_loss:0.275, Lr:1.00E-06
Done

结果上看不如上次的DenseNet121

结果可视化:

import matplotlib.pyplot as plt
epochs_range = range(epochs)plt.figure(figsize=(12, 3))
plt.subplot(1, 2, 1)plt.plot(epochs_range, train_acc, label='Training Accuracy')
plt.plot(epochs_range, test_acc, label='Test Accuracy')
plt.legend(loc='lower right')
plt.title('Training and Validation Accuracy')plt.subplot(1, 2, 2)
plt.plot(epochs_range, train_loss, label='Training Loss')
plt.plot(epochs_range, test_loss, label='Test Loss')
plt.legend(loc='upper right')
plt.title('Training and Validation Loss')
plt.show()

代码输出:
在这里插入图片描述
对验证集的准确率:

def validate(dataloader, model):model.eval()size = len(dataloader.dataset)num_batches = len(dataloader)validate_acc = 0for x, y in dataloader:x, y = x.to(device), y.to(device)pred = model(x)validate_acc += (pred.argmax(1) == y).type(torch.float).sum().item()validate_acc /= sizereturn validate_acc# 计算验证集准确率
validate_acc = validate(validate_dl, best_model)
print(f"Validation Accuracy: {validate_acc:.2%}")

代码输出:

Validation Accuracy: 89.37%

达到89.4%

三、总结

这次的结合主要是在和GPT一起完成的,主要是简单的结合,看到很多人说文献中报道过DPN结构,我待会儿也会去看看。

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/web/63009.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

「iOS」通过CoreLocation Framework深入了解MVC架构

「iOS」通过CoreLocation Framework重新了解多界面传值以及MVC架构 文章目录 「iOS」通过CoreLocation Framework重新了解多界面传值以及MVC架构前言CoreLocation了解根据需求建模设计属性方法设计协议传值Block传值KVONotification通知方式 总结参考文章 前言 在这个学期的前…

ArrayList源码分析、扩容机制面试题,数组和List的相互转换,ArrayList与LinkedList的区别

目录 1.java集合框架体系 2. 前置知识-数组 2.1 数组 2.1.1 定义&#xff1a; 2.1.2 数组如何获取其他元素的地址值&#xff1f;&#xff08;寻址公式&#xff09; 2.1.3 为什么数组索引从0开始呢&#xff1f;从1开始不行吗&#xff1f; 3. ArrayList 3.1 ArrayList和和…

【C++】- 掌握STL List类:带你探索双向链表的魅力

文章目录 前言&#xff1a;一.list的介绍及使用1. list的介绍2. list的使用2.1 list的构造2.2 list iterator的使用2.3 list capacity2.4 list element access2.5 list modifiers2.6 list的迭代器失效 二.list的模拟实现1. list的节点2. list的成员变量3.list迭代器相关问题3.1…

Docker--Docker Container(容器) 之容器实战

对docker容器的前两篇文章 Docker–Docker Container(容器) 之 操作实例 Docker–Docker Container(容器&#xff09; Mysql容器化安装 我们可以先在Docker Hub上查看对应的Mysql镜像,拉取对应的镜像&#xff1a; 拉取mysql5.7版本的镜像&#xff1a; docker pull mysql:5.7…

【汇编语言】内中断(二) —— 安装自己的中断处理程序:你也能控制0号中断

文章目录 前言1. 编程处理0号中断1.1 效果演示1.2 分析所要编写的中断处理程序1.2.1 引发中断1.2.2 中断处理程序1.2.3 中断处理程序do0应该存放的位置1.2.4 中断向量表的修改1.2.5 总结 1.3 程序框架1.4 注意事项1.5 从CPU的角度看中断处理程序1.6 一些问题的思考与解答 2. 安…

VS2019中无法跳转定义_其中之一情况

我习惯了使用VS2019看stm的代码&#xff1b; 遇到的问题&#xff0c;在导入代码后&#xff0c;发现有些函数调用不能跳转到定义&#xff1b; 问题描述步骤 1、导入代码 2、跳转&#xff0c;无法跳转 1、中文路径 2、删除.vs文件 和网上查的都没办法解决 最后发现是VS不支持 …

让 Win10 上网本 Debug 模式 QUDPSocket 信号槽 收发不丢包的方法总结

在前两篇文章里&#xff0c;我们探讨了不少UDP丢包的解决方案。经过几年的摸索测试&#xff0c;其实方法非常简单, 无需修改代码。 1. Windows 下设置UDP缓存 这个方法可以一劳永逸解决UDP的收发丢包问题&#xff0c;只要添加注册表项目并重启即可。即使用Qt的信号与槽&#…

Elasticsearch:ES|QL 中的全文搜索 - 8.17

细心的开发者如果已经阅读我前两天发布的文章 “Elastic 8.17&#xff1a;Elasticsearch logsdb 索引模式、Elastic Rerank 等”&#xff0c;你就会发现在 8.17 的发布版中&#xff0c;有一个重要的功能发布。那就是 ES|QL 开始支持全文搜索了。在今天的文章中我们来尝试一下。…

SQL和Python 哪个更容易自学?

SQL和Python不是一个物种&#xff0c;Python肯定更难学习。如果你从事数据工作&#xff0c;我建议先学SQL、有余力再学Python。因为SQL不光容易学&#xff0c;而且前期的投入产出比更大。 SQL是数据查询语言&#xff0c;场景限于数据查询和数据库的管理&#xff0c;对大部分数据…

【unity】从零开始制作平台跳跃游戏--界面的认识,添加第一个角色!

在上一篇文章中&#xff0c;我们已经完成了unity的环境配置与安装⬇️ 【Unity】环境配置与安装-CSDN博客 接下来&#xff0c;让我们开始新建一个项目吧&#xff01; 新建项目 首先进入unityHub的项目页面&#xff0c;点击“新项目”&#xff1a; 我们这个系列将会以2D平台…

怎么禁用 vscode 中点击 go 包名时自动打开浏览器跳转到 pkg.go.dev

本文引用怎么禁用 vscode 中点击 go 包名时自动打开浏览器跳转到 pkg.go.dev 在 vscode 设置项中配置 gopls 的 ui.navigation.importShortcut 为 Definition 即可。 "gopls": {"ui.navigation.importShortcut": "Definition" }ui.navigation.i…

Unity3D实现抽象类的应用场景例子

系列文章目录 unity知识点 文章目录 系列文章目录👉前言👉一、示例👉二、使用步骤👉三、抽象类和接口的区别👉3-1、抽象类👉3-2、接口类👉壁纸分享👉总结👉前言 假设我们正在制作一个游戏,游戏中有多种不同类型的角色,这些角色都有一些共同的行为(比如移…

数据仓库工具箱—读书笔记01(数据仓库、商业智能及维度建模初步)

数据仓库、商业智能及维度建模初步 记录一下读《数据仓库工具箱》时的思考&#xff0c;摘录一些书中关于维度建模比较重要的思想与大家分享&#x1f923;&#x1f923;&#x1f923; 博主在这里先把这本书"变薄"~有时间的小伙伴可以亲自再读一读&#xff0c;感受一下…

docker启动一个helloworld(公司内网服务器)

这里写目录标题 容易遇到的问题&#xff1a;1、docker连接问题 我来介绍几种启动 Docker Hello World 的方法&#xff1a; 最简单的方式&#xff1a; docker run hello-world这会自动下载并运行官方的 hello-world 镜像。 使用 Nginx 作为 Hello World&#xff1a; docker…

基于IEEE 802.1Qci的时间敏感网络(TSN)主干架构安全分析及异常检测系统设计

中文标题&#xff1a;基于IEEE 802.1Qci的时间敏感网络&#xff08;TSN&#xff09;主干架构安全分析及异常检测系统设计 英文标题&#xff1a;Security Analysis of the TSN Backbone Architecture and Anomaly Detection System Design Based on IEEE 802.1Qci 作者信息&…

怎样提升企业网络的性能?

企业网络的稳定性和高效性直接影响员工的工作效率。以下从多维度分析了一些有效策略&#xff0c;帮助公司提升网络性能&#xff0c;营造更高效的办公环境。 1. 升级网络设备 采用性能更高的网络硬件是优化网络体验的重要基础。选择支持高吞吐量、低延迟的设备&#xff08;如企业…

力扣239.滑动窗口最大值

文章目录 一、前言二、单调队列 一、前言 力扣239.滑动窗口最大值 滑动窗口最大值&#xff0c;这道题给定一个数组&#xff0c;以及一个窗口的长度&#xff0c;这个窗口会往后滑动&#xff0c;直到数组最后一个元素。 要求每个滑动窗口的中的最大值。对于这道题&#xff0c;我…

mac 安装CosyVoice (cpu版本)

CosyVoice 介绍 CosyVoice 是阿里研发的一个tts大模型 官方项目地址&#xff1a;https://github.com/FunAudioLLM/CosyVoice.git 下载项目&#xff08;非官方&#xff09; git clone --recursive https://github.com/v3ucn/CosyVoice_for_MacOs.git 进入项目 cd CosyVoic…

Maven 安装配置(详细教程)

文章目录 一、Maven 简介二、下载 Maven三、配置 Maven3.1 配置环境变量3.2 Maven 配置3.3 IDEA 配置 四、结语 一、Maven 简介 Maven 是一个基于项目对象模型&#xff08;POM&#xff09;的项目管理和自动化构建工具。它主要服务于 Java 平台&#xff0c;但也支持其他编程语言…

Scala中的泛型特质

代码如下&#xff1a; package test41 //泛型特质 object test3 { //定义一个日志//泛型特质&#xff0c;X是泛型名称&#xff0c;可以更改。trait Logger[X] {val content: Xdef show():Unit }class FileLogger extends Logger[String] {override val content: String "…