第P8周:RestNet34实现X光肺炎识别
·
- 🍨 本文为🔗365天深度学习训练营中的学习记录博客
- 🍖 原作者:K同学啊


resnet34代码:
class BasicBlock(nn.Module):
expansion = 1
def __init__(self, in_channels, out_channels, stride=1, downsample=None):
super(BasicBlock, self).__init__()
# --- 第一个 3×3 卷积 ---
self.conv1 = nn.Conv2d(
in_channels, out_channels, kernel_size=3,
stride=stride, padding=1, bias=False
)
self.bn1 = nn.BatchNorm2d(out_channels)
self.relu = nn.ReLU(inplace=True)
# --- 第二个 3×3 卷积 ---
self.conv2 = nn.Conv2d(
out_channels, out_channels, kernel_size=3,
stride=1, padding=1, bias=False
)
self.bn2 = nn.BatchNorm2d(out_channels)
self.downsample = downsample
def forward(self, x):
identity = x
out = self.conv1(x)
out = self.bn1(out)
out = self.relu(out)
out = self.conv2(out)
out = self.bn2(out)
if self.downsample is not None:
identity = self.downsample(x)
out += identity
out = self.relu(out)
return out
class ResNet34(nn.Module):
"""
ResNet34 网络
参数:
num_classes: 分类数量 (默认 2, 用于 NORMAL / PNEUMONIA 二分类)
include_top: 是否包含最后的全局池化与全连接层
"""
def __init__(self, num_classes=2, include_top=True):
super(ResNet34, self).__init__()
self.include_top = include_top
# --- 初始卷积层 ---
# 输入: 224×224×3 → 输出: 112×112×64
self.conv1 = nn.Conv2d(3, 64, kernel_size=7, stride=2, padding=3, bias=False)
self.bn1 = nn.BatchNorm2d(64)
self.relu = nn.ReLU(inplace=True)
# 输出: 56×56×64
self.maxpool = nn.MaxPool2d(kernel_size=3, stride=2, padding=1)
# --- 四个残差阶段 ---
# conv2_x: 56×56, 64 → 56×56, 64 (3 个 block, stride=1)
self.layer1 = self._make_layer(64, 64, blocks=3, stride=1)
# conv3_x: 56×56, 64 → 28×28, 128 (4 个 block, 第一个 stride=2)
self.layer2 = self._make_layer(64, 128, blocks=4, stride=2)
# conv4_x: 28×28, 128 → 14×14, 256 (6 个 block, 第一个 stride=2)
self.layer3 = self._make_layer(128, 256, blocks=6, stride=2)
# conv5_x: 14×14, 256 → 7×7, 512 (3 个 block, 第一个 stride=2)
self.layer4 = self._make_layer(256, 512, blocks=3, stride=2)
if self.include_top:
# --- 全局平均池化 + 全连接 ---
self.avgpool = nn.AdaptiveAvgPool2d((1, 1))
self.fc = nn.Linear(512 * BasicBlock.expansion, num_classes)
# --- 权重初始化 ---
self._init_weights()
def _make_layer(self, in_channels, out_channels, blocks, stride):
"""
构建一个残差阶段 (包含多个 BasicBlock)。
参数:
in_channels: 输入通道数
out_channels: 输出通道数
blocks: 该阶段的 BasicBlock 数量
stride: 第一个 block 的步长
"""
downsample = None
# 当 stride != 1 或通道数变化时, shortcut 需要 1×1 卷积
if stride != 1 or in_channels != out_channels * BasicBlock.expansion:
downsample = nn.Sequential(
nn.Conv2d(
in_channels, out_channels * BasicBlock.expansion,
kernel_size=1, stride=stride, bias=False
),
nn.BatchNorm2d(out_channels * BasicBlock.expansion),
)
layers = []
# 第一个 block
layers.append(
BasicBlock(in_channels, out_channels, stride, downsample)
)
# 后续 block (in_channels = out_channels, stride=1)
for _ in range(1, blocks):
layers.append(
BasicBlock(out_channels, out_channels, stride=1)
)
return nn.Sequential(*layers)
def _init_weights(self):
"""Kaiming 初始化"""
for m in self.modules():
if isinstance(m, nn.Conv2d):
nn.init.kaiming_normal_(
m.weight, mode='fan_out', nonlinearity='relu'
)
elif isinstance(m, nn.BatchNorm2d):
nn.init.constant_(m.weight, 1)
nn.init.constant_(m.bias, 0)
def forward(self, x):
"""
前向传播。
输入: x: (B, 3, 224, 224)
输出: 分类 logits: (B, num_classes)
或特征图 (当 include_top=False): (B, 512, 7, 7)
"""
# --- 初始层 ---
x = self.conv1(x) # (B, 64, 112, 112)
x = self.bn1(x)
x = self.relu(x)
x = self.maxpool(x) # (B, 64, 56, 56)
# --- 四个残差阶段 ---
x = self.layer1(x) # (B, 64, 56, 56)
x = self.layer2(x) # (B, 128, 28, 28)
x = self.layer3(x) # (B, 256, 14, 14)
x = self.layer4(x) # (B, 512, 7, 7)
if self.include_top:
# --- 分类头 ---
x = self.avgpool(x) # (B, 512, 1, 1)
x = torch.flatten(x, 1) # (B, 512)
x = self.fc(x) # (B, num_classes)
return x
else:
return x # 返回特征图
个人总结:这一周主要就是实现了resnet34的一个模型识别x光肺炎,主要学到了一个就是残差块的构建,并且以及对于resnt模型的直接调用冻结微调等操作。后续也是借助ai写了一下resnt34的代码。基本上可以手动实现。
AtomGit 是由开放原子开源基金会联合 CSDN 等生态伙伴共同推出的新一代开源与人工智能协作平台。平台坚持“开放、中立、公益”的理念,把代码托管、模型共享、数据集托管、智能体开发体验和算力服务整合在一起,为开发者提供从开发、训练到部署的一站式体验。
更多推荐




所有评论(0)