From d9057edbd925cfcfddb1a0733d63d60291a393d1 Mon Sep 17 00:00:00 2001 From: zhanghuan <1262329256@qq.com> Date: Wed, 10 Sep 2025 15:35:41 +0800 Subject: [PATCH] =?UTF-8?q?=E6=8F=90=E4=BA=A4=5F=5Finit=5F=5F=E6=96=87?= =?UTF-8?q?=E4=BB=B6=EF=BC=8C=E5=92=8C=E7=BD=91=E7=BB=9C=E6=96=87=E4=BB=B6?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .gitignore | 3 +- net/__init__.py | 7 ++++ net/food_net.py | 84 ++++++++++++++++++++++++++++++++++++++++++++ settings/__init__.py | 5 +++ 4 files changed, 97 insertions(+), 2 deletions(-) create mode 100644 net/__init__.py create mode 100644 net/food_net.py create mode 100644 settings/__init__.py diff --git a/.gitignore b/.gitignore index d089d36..ba0d2b7 100644 --- a/.gitignore +++ b/.gitignore @@ -1,4 +1,3 @@ /dataset/ -/demo/cifar_net103.pth -/demo/data/ /.idea/ +/model/ diff --git a/net/__init__.py b/net/__init__.py new file mode 100644 index 0000000..fb4fe63 --- /dev/null +++ b/net/__init__.py @@ -0,0 +1,7 @@ +""" +食物分类网络模型包 +""" + +from .food_net import FoodCNN, create_food_cnn + +__all__ = ['FoodCNN', 'create_food_cnn'] \ No newline at end of file diff --git a/net/food_net.py b/net/food_net.py new file mode 100644 index 0000000..37fd284 --- /dev/null +++ b/net/food_net.py @@ -0,0 +1,84 @@ +import torch +import torch.nn as nn +import torch.nn.functional as F + + +class FoodCNN(nn.Module): + """ + 食物分类CNN模型 + 基于CIFAR10结构,适配3分类任务 + """ + def __init__(self): + super(FoodCNN, self).__init__() + # 第一个卷积块 + self.conv1 = nn.Conv2d(3, 32, 3, padding=1) + self.conv2 = nn.Conv2d(32, 32, 3, padding=1) + self.pool1 = nn.MaxPool2d(2, 2) + self.dropout1 = nn.Dropout2d(0.25) + + # 第二个卷积块 + self.conv3 = nn.Conv2d(32, 64, 3, padding=1) + self.conv4 = nn.Conv2d(64, 64, 3, padding=1) + self.pool2 = nn.MaxPool2d(2, 2) + self.dropout2 = nn.Dropout2d(0.25) + + # 第三个卷积块 + self.conv5 = nn.Conv2d(64, 128, 3, padding=1) + self.conv6 = nn.Conv2d(128, 128, 3, padding=1) + self.pool3 = nn.MaxPool2d(2, 2) + self.dropout3 = nn.Dropout2d(0.25) + + # 全连接层 + self.fc1 = nn.Linear(128 * 4 * 4, 512) + self.dropout4 = nn.Dropout(0.5) + self.fc2 = nn.Linear(512, 3) # 3分类 + + def forward(self, x): + # 第一个卷积块 + x = F.relu(self.conv1(x)) + x = F.relu(self.conv2(x)) + x = self.pool1(x) + x = self.dropout1(x) + + # 第二个卷积块 + x = F.relu(self.conv3(x)) + x = F.relu(self.conv4(x)) + x = self.pool2(x) + x = self.dropout2(x) + + # 第三个卷积块 + x = F.relu(self.conv5(x)) + x = F.relu(self.conv6(x)) + x = self.pool3(x) + x = self.dropout3(x) + + # 展平 + x = x.view(-1, 128 * 4 * 4) + + # 全连接层 + x = F.relu(self.fc1(x)) + x = self.dropout4(x) + x = self.fc2(x) + + return x + + +def create_food_cnn(): + """ + 创建食物分类CNN模型 + + Returns: + FoodCNN: 网络模型实例 + """ + return FoodCNN() + + +if __name__ == "__main__": + # 测试网络 + model = create_food_cnn() + print(f"模型参数数量: {sum(p.numel() for p in model.parameters() if p.requires_grad)}") + + # 测试前向传播 + dummy_input = torch.randn(1, 3, 32, 32) + output = model(dummy_input) + print(f"输出形状: {output.shape}") \ No newline at end of file diff --git a/settings/__init__.py b/settings/__init__.py new file mode 100644 index 0000000..388d1cc --- /dev/null +++ b/settings/__init__.py @@ -0,0 +1,5 @@ +""" +配置包 +""" + +from .settings import * \ No newline at end of file