把网络模型单独拎出来了,方便解耦。下一步准备把重要的配置全部拎出来。

This commit is contained in:
zhanghuan
2025-09-10 14:48:43 +08:00
parent 393d4c2a60
commit 4c2fa0e533
3 changed files with 18 additions and 171 deletions
+6 -58
View File
@@ -13,66 +13,14 @@ from typing import List, Optional, Tuple
from tkinterdnd2 import DND_FILES, TkinterDnD from tkinterdnd2 import DND_FILES, TkinterDnD
import threading import threading
import time import time
from net import create_food_cnn
# 设置customtkinter的外观 # 设置customtkinter的外观
ctk.set_appearance_mode("System") ctk.set_appearance_mode("System")
ctk.set_default_color_theme("blue") ctk.set_default_color_theme("blue")
# 定义CNN模型(与训练代码中的结构相同)
class FoodCNN(nn.Module):
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, 2) # 2分类
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
class FoodClassifierApp: class FoodClassifierApp:
def __init__(self, root): def __init__(self, root):
@@ -81,7 +29,7 @@ class FoodClassifierApp:
self.root.geometry("1400x800") self.root.geometry("1400x800")
# 食物类别(根据您的数据集) # 食物类别(根据您的数据集)
self.food_classes = ["回锅肉", "西红柿鸡蛋"] self.food_classes = ["回锅肉", "西红柿鸡蛋","麻辣小面"]
# 设备设置 # 设备设置
self.device = torch.device("cuda" if torch.cuda.is_available() else "cpu") self.device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
@@ -106,10 +54,10 @@ class FoodClassifierApp:
def load_model(self): def load_model(self):
"""加载训练好的PyTorch模型""" """加载训练好的PyTorch模型"""
try: try:
model_path = "../model/01/best_food_model.pth" model_path = "../model/02/best_food_model.pth"
if os.path.exists(model_path): if os.path.exists(model_path):
# 创建模型实例 # 创建模型实例
self.model = FoodCNN() self.model = create_food_cnn()
# 加载模型权重 # 加载模型权重
self.model.load_state_dict(torch.load(model_path, map_location=self.device)) self.model.load_state_dict(torch.load(model_path, map_location=self.device))
self.model.to(self.device) self.model.to(self.device)
+4 -57
View File
@@ -1,66 +1,13 @@
import torch import torch
import torch.nn as nn import torch.nn as nn
import torch.nn.functional as F import torch.nn.functional as F
from net import create_food_cnn
class FoodCNN(nn.Module):
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, 2) # 2分类
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
# 1. 初始化模型 # 1. 初始化模型
model = FoodCNN() model = create_food_cnn()
# 2. 加载训练好的权重 # 2. 加载训练好的权重
model.load_state_dict(torch.load("../model/01/best_food_model.pth", map_location='cpu')) model.load_state_dict(torch.load("../model/02/best_food_model.pth", map_location='cpu'))
model.eval() # 设置为推理模式 model.eval() # 设置为推理模式
# 3. 创建示例输入 (假设输入是 3x224x224 的图片) # 3. 创建示例输入 (假设输入是 3x224x224 的图片)
@@ -68,4 +15,4 @@ example_input = torch.randn(1, 3, 224, 224)
# 4. 转换为 TorchScript # 4. 转换为 TorchScript
traced_script_module = torch.jit.trace(model, example_input) traced_script_module = torch.jit.trace(model, example_input)
traced_script_module.save("../model/01/best_food_model_mobile.pt") traced_script_module.save("../model/02/best_food_model_mobile.pt")
+8 -56
View File
@@ -9,8 +9,14 @@ import matplotlib
import numpy as np import numpy as np
from tqdm import tqdm from tqdm import tqdm
import os import os
import sys
import time import time
# 添加net目录到路径
# sys.path.append(os.path.join(os.path.dirname(__file__), '..', 'net'))
# from food_net import create_food_cnn
from net import create_food_cnn
# 设置matplotlib支持中文显示 # 设置matplotlib支持中文显示
plt.rcParams['font.sans-serif'] = ['SimHei', 'Microsoft YaHei', 'DejaVu Sans'] # 指定默认字体 plt.rcParams['font.sans-serif'] = ['SimHei', 'Microsoft YaHei', 'DejaVu Sans'] # 指定默认字体
plt.rcParams['axes.unicode_minus'] = False # 解决保存图像是负号'-'显示为方块的问题 plt.rcParams['axes.unicode_minus'] = False # 解决保存图像是负号'-'显示为方块的问题
@@ -19,61 +25,7 @@ plt.rcParams['axes.unicode_minus'] = False # 解决保存图像是负号'-'显
device = torch.device("cuda" if torch.cuda.is_available() else "cpu") device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
print(f"使用设备: {device}") print(f"使用设备: {device}")
# 定义CNN模型(基于CIFAR10结构,输出层改为3分类)
class FoodCNN(nn.Module):
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) # 改为2分类
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
# 数据预处理 # 数据预处理
transform_train = transforms.Compose([ transform_train = transforms.Compose([
@@ -201,7 +153,7 @@ if __name__ == '__main__':
print(f"测试集大小: {len(test_dataset)}") print(f"测试集大小: {len(test_dataset)}")
# 创建模型 # 创建模型
model = FoodCNN().to(device) model = create_food_cnn().to(device)
print(f"模型参数数量: {sum(p.numel() for p in model.parameters() if p.requires_grad)}") print(f"模型参数数量: {sum(p.numel() for p in model.parameters() if p.requires_grad)}")
# 定义损失函数和优化器(使用与CIFAR10相同的超参数) # 定义损失函数和优化器(使用与CIFAR10相同的超参数)