提交__init__文件,和网络文件
This commit is contained in:
+1
-2
@@ -1,4 +1,3 @@
|
||||
/dataset/
|
||||
/demo/cifar_net103.pth
|
||||
/demo/data/
|
||||
/.idea/
|
||||
/model/
|
||||
|
||||
@@ -0,0 +1,7 @@
|
||||
"""
|
||||
食物分类网络模型包
|
||||
"""
|
||||
|
||||
from .food_net import FoodCNN, create_food_cnn
|
||||
|
||||
__all__ = ['FoodCNN', 'create_food_cnn']
|
||||
@@ -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}")
|
||||
@@ -0,0 +1,5 @@
|
||||
"""
|
||||
配置包
|
||||
"""
|
||||
|
||||
from .settings import *
|
||||
Reference in New Issue
Block a user