Commit e8b72718 by 前钰

Upload New File

parent 57eabcc2
import torch.nn as nn
import torch.nn as nn
# 定义一个简单的卷积神经网络用于猫狗分类任务
class SimpleCNN(nn.Module):
def __init__(self):
super(SimpleCNN, self).__init__()
# 特征提取模块:通过三层卷积 + 激活 + 池化提取图像的空间特征
self.features = nn.Sequential(
# 第1个卷积层:输入3通道(RGB),输出16通道,卷积核大小为3x3,padding=1保持输出尺寸
nn.Conv2d(3, 16, kernel_size=3, padding=1),
nn.ReLU(), # ReLU激活函数,引入非线性
nn.MaxPool2d(2), # 最大池化,核大小2x2,缩小一半尺寸 (224 → 112)
# 第2个卷积层:输入16通道,输出32通道
nn.Conv2d(16, 32, kernel_size=3, padding=1),
nn.ReLU(),
nn.MaxPool2d(2), # 池化后尺寸 112 → 56
# 第3个卷积层:输入32通道,输出64通道
nn.Conv2d(32, 64, kernel_size=3, padding=1),
nn.ReLU(),
nn.MaxPool2d(2) # 池化后尺寸 56 → 28
)
# 分类器模块:全连接层将提取的特征进行分类
self.classifier = nn.Sequential(
nn.Linear(64 * 28 * 28, 128), # 输入为展平后的特征张量,输出128维
nn.ReLU(), # 非线性激活
nn.Dropout(0.5), # Dropout防止过拟合,随机丢弃50%神经元
nn.Linear(128, 2) # 输出层:2个神经元表示猫(0)或狗(1)
)
def forward(self, x):
x = self.features(x) # 先通过卷积提取特征
x = x.view(x.size(0), -1) # 将多维特征图展平为1维向量(batch_size, features)
x = self.classifier(x) # 再送入全连接层进行分类
return x
class BetterCNN(nn.Module):
def __init__(self):
super(BetterCNN, self).__init__()
# 特征提取部分,包含3个卷积块(Block)
self.features = nn.Sequential(
# Block 1:
# 输入通道3(RGB图像),输出通道32,卷积核大小3x3,padding=1保持尺寸不变
nn.Conv2d(3, 32, kernel_size=3, padding=1),
# 批归一化,加快训练速度并提高稳定性,减少内部协变量偏移
nn.BatchNorm2d(32),
# ReLU激活函数,引入非线性,帮助模型拟合复杂函数
nn.ReLU(),
# 最大池化,核大小2x2,步长2,将特征图尺寸缩小一半,减小计算量
# 输入224x224,输出112x112
nn.MaxPool2d(2),
# Block 2:
# 输入32通道,输出64通道,同样3x3卷积,padding=1
nn.Conv2d(32, 64, kernel_size=3, padding=1),
nn.BatchNorm2d(64), # 批归一化层
nn.ReLU(),
# 池化后尺寸112x112 -> 56x56
nn.MaxPool2d(2),
# Block 3:
# 输入64通道,输出128通道,继续提取更深层特征
nn.Conv2d(64, 128, kernel_size=3, padding=1),
nn.BatchNorm2d(128),
nn.ReLU(),
# 池化后尺寸56x56 -> 28x28
nn.MaxPool2d(2),
)
# 分类器部分,将卷积提取的特征转化为最终分类结果
self.classifier = nn.Sequential(
# 自适应平均池化,将每个通道的特征图缩小为1x1
# 输出张量形状为 (batch_size, 128, 1, 1)
nn.AdaptiveAvgPool2d((1, 1)),
# 展平层,将特征图展平为一维向量,形状变为 (batch_size, 128)
nn.Flatten(),
# 全连接层,将128维特征映射到64维,进一步抽象特征
nn.Linear(128, 64),
nn.ReLU(),
# Dropout随机丢弃50%的神经元,防止过拟合
nn.Dropout(0.5),
# 最终全连接层,将64维特征映射到2个类别(猫或狗)
nn.Linear(64, 2)
)
def forward(self, x):
# 输入x通过特征提取模块,输出卷积特征
x = self.features(x)
# 再通过分类器模块,输出最终的类别得分
x = self.classifier(x)
return x
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment