鸡西市网站建设_网站建设公司_MySQL_seo优化
2026/1/1 11:13:40 网站建设 项目流程

指令

一、日志核心信息解读

指标数值关键结论
Batch [0] Loss4.9721初始损失符合 208 类分类任务预期(理论值≈5.33)
Batch [10] Loss3.6429训练 10 个批次后 Loss 下降 1.33,模型开始学习
Batch [20] Loss2.5330训练 20 个批次后 Loss 再降 1.11,收敛趋势健康
20 批次总耗时132.96s单批次平均≈6.65s(比初始 4.34s 略慢,是 CPU 资源调度正常波动)

二、立刻能做的轻量化优化(进一步提速 + 加速收敛)

1. 冻结特征层(最核心,计算量砍 80%)

现在训练还在更新整个模型的参数,冻结特征层后单批次耗时会从≈6.65s 降至≈1~2s,代码修改如下(训练中暂停后重新运行即可):

python

运行

from torchvision import models from torchvision.models import MobileNet_V2_Weights # 加载模型+冻结特征层 model = models.mobilenet_v2(weights=MobileNet_V2_Weights.IMAGENET1K_V1) # 关键:冻结所有特征提取层,仅训练分类头 for param in model.features.parameters(): param.requires_grad = False # 修改分类头 model.classifier[1] = nn.Linear(model.last_channel, num_classes) model = model.to("cpu")
2. 调整学习率(冻结后分类头可加大学习率,收敛更快)

python

运行

# 从0.001→0.005,分类头单参数学习效率更高 optimizer = optim.Adam(model.parameters(), lr=0.005)

三、后续训练的关键观察点

  1. Loss 趋势
    • 正常情况下,本轮 Epoch 结束时 Loss 应降至 1.0~1.5 左右;
    • 若 Loss 下降变慢(如连续 50 批次 Loss 基本不变),可适当调大学习率(如 0.008)或减小批次大小(如 16)。
  2. 验证阶段
    • 第一轮 Epoch 验证准确率(Test Acc)能达到 60%+ 即为正常,后续每轮应提升 5%~10%,最终稳定在 95%+(Fruits360 数据集简单,MobileNetV2 能达到 98%+)。
  3. 耗时变化
    • 冻结特征层后,20 批次耗时会从 132s 降至≈30~40s,整轮 Epoch 耗时从 2.6 小时降至≈40 分钟,效率大幅提升。

四、小技巧:减少验证阶段耗时(可选)

若想优先完成训练,可暂时跳过完整验证,仅每 3 轮验证一次:

# ==================== 配置项 ==================== TRAIN_DIR = "./wlzcfruit/Training" TEST_DIR = "./wlzcfruit/Test" MODEL_SAVE_PATH = "wlzcfruit_mobilenetv2.pth" NUM_EPOCHS = 15 BATCH_SIZE = 32 PRINT_STEP = 10 # 每10个批次打印一次进度 # ================================================ # 1. 数据预处理(简化:先去掉Normalize,减少计算量) transform = transforms.Compose([ transforms.Resize((100, 100)), transforms.ToTensor(), # 暂时注释Normalize,先验证进度,后续加回 # transforms.Normalize(mean=[0.485, 0.456, 0.406], std=[0.229, 0.224, 0.225]) ]) # 2. 加载数据集(Win7 num_workers=0 正确) train_dataset = ImageFolder(TRAIN_DIR, transform=transform) test_dataset = ImageFolder(TEST_DIR, transform=transform) num_classes = len(train_dataset.classes) print(f"数据集加载完成 | 训练集样本数:{len(train_dataset)} | 类别数:{num_classes}")

阿雪技术观

在科技发展浪潮中,我们不妨积极投身技术共享。不满足于做受益者,更要主动担当贡献者。无论是分享代码、撰写技术博客,还是参与开源项目维护改进,每一个微小举动都可能蕴含推动技术进步的巨大能量。东方仙盟是汇聚力量的天地,我们携手在此探索硅基生命,为科技进步添砖加瓦。

Hey folks, in this wild tech - driven world, why not dive headfirst into the whole tech - sharing scene? Don't just be the one reaping all the benefits; step up and be a contributor too. Whether you're tossing out your code snippets, hammering out some tech blogs, or getting your hands dirty with maintaining and sprucing up open - source projects, every little thing you do might just end up being a massive force that pushes tech forward. And guess what? The Eastern FairyAlliance is this awesome place where we all come together. We're gonna team up and explore the whole silicon - based life thing, and in the process, we'll be fueling the growth of technology

需要专业的网站建设服务?

联系我们获取免费的网站建设咨询和方案报价,让我们帮助您实现业务目标

立即咨询