From a60cd58a0be30bd6181c3ea92ede3a87ea8a2925 Mon Sep 17 00:00:00 2001 From: zhanghuan <1262329256@qq.com> Date: Wed, 3 Sep 2025 09:57:11 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BC=98=E5=8C=96=E6=9B=B2=E7=BA=BF=E7=9A=84?= =?UTF-8?q?=E7=BB=98=E5=88=B6=EF=BC=8C=E9=81=BF=E5=85=8D=E4=B8=AD=E6=96=87?= =?UTF-8?q?=E4=B8=8D=E6=98=BE=E7=A4=BA=E3=80=82?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- food_classifier.py | 19 ++++++++++++------- 1 file changed, 12 insertions(+), 7 deletions(-) diff --git a/food_classifier.py b/food_classifier.py index fc62041..f7335bd 100644 --- a/food_classifier.py +++ b/food_classifier.py @@ -5,10 +5,15 @@ import torch.nn.functional as F from torch.utils.data import DataLoader from torchvision import datasets, transforms import matplotlib.pyplot as plt +import matplotlib import numpy as np from tqdm import tqdm import os +# 设置matplotlib支持中文显示 +plt.rcParams['font.sans-serif'] = ['SimHei', 'Microsoft YaHei', 'DejaVu Sans'] # 指定默认字体 +plt.rcParams['axes.unicode_minus'] = False # 解决保存图像是负号'-'显示为方块的问题 + # 设置设备 device = torch.device("cuda" if torch.cuda.is_available() else "cpu") print(f"使用设备: {device}") @@ -71,7 +76,7 @@ class FoodCNN(nn.Module): # 数据预处理 transform_train = transforms.Compose([ - transforms.Resize((32, 32)), # 调整为32x32以匹配CIFAR10结构 + transforms.Resize((32, 32)), transforms.RandomHorizontalFlip(p=0.5), transforms.RandomRotation(10), transforms.ColorJitter(brightness=0.2, contrast=0.2, saturation=0.2, hue=0.1), @@ -254,18 +259,18 @@ if __name__ == '__main__': plt.figure(figsize=(12, 4)) plt.subplot(1, 2, 1) - plt.plot(train_losses, label='训练损失') - plt.plot(val_losses, label='验证损失') - plt.title('损失曲线') + plt.plot(train_losses, label='Train Loss') + plt.plot(val_losses, label='Val Loss') + plt.title('Loss Curve') plt.xlabel('Epoch') plt.ylabel('Loss') plt.legend() plt.grid(True) plt.subplot(1, 2, 2) - plt.plot(train_accuracies, label='训练准确率') - plt.plot(val_accuracies, label='验证准确率') - plt.title('准确率曲线') + plt.plot(train_accuracies, label='Train Accuracy') + plt.plot(val_accuracies, label='Val Accuracy') + plt.title('Accuracy Curve') plt.xlabel('Epoch') plt.ylabel('Accuracy (%)') plt.legend()