第P7周:马铃薯病害识别(VGG-16复现)

🏡 我的环境:

  • 语言环境:Python3.8
  • 编译器:Jupyter Lab
  • 深度学习环境:Pytorch
    • torch==1.12.1+cu113
    • torchvision==0.13.1+cu113
  • 手动搭建VGG-16模型

    VVG-16结构说明:

  • 13个卷积层(Convolutional Layer),分别用blockX_convX表示
  • 3个全连接层(Fully connected Layer),分别用fcXpredictions表示
  • 5个池化层(Pool layer),分别用blockX_pool表示
  • def test (dataloader, model, loss_fn):
        size        = len(dataloader.dataset)  # 测试集的大小
        num_batches = len(dataloader)          # 批次数目, (size/batch_size,向上取整)
        test_loss, test_acc = 0, 0
      
    
        # 当不进行训练时,停止梯度更新,节省计算内存消耗
        with torch.no_grad():
            for imgs, target in dataloader:
                imgs, target = imgs.to(device), target.to(device)
              
    
                # 计算loss
                target_pred = model(imgs)
                loss        = loss_fn(target_pred, target)
              
    
                test_loss += loss.item()
                test_acc  += (target_pred.argmax(1) == target).type(torch.float).sum().item()
    
        test_acc  /= size
        test_loss /= num_batches
    
        return test_acc, test_loss

    测试函数的编写。遇到问题,召回率低:原因是conv1卷积层设置的参数和目标的数据集大小不合适,尝试将pading改为2

  •    VGG16 网络特简单的卷积层和池化层结构:VGG16采用连续的3x3卷积核和2x2池化核,保持了模型结构的简洁性,图片分割时可以尝试换类型进行测试,多次较差检验,这是复杂度允许范围之内的。VGG16拥有16个加权层,比之前的AlexNet等模型更深,能够提取更丰富的特征。由于VGG16的卷积层较多,因此参数量也相对较大,训练时需要更多的计算资源和存储空间,这边建议用gpu进行编写测试:

    import torch
    import torch.nn as nn
    import torchvision.transforms as transforms
    import torchvision
    from torchvision import transforms, datasets
    import os,PIL,pathlib,warnings

    warnings.filterwarnings("ignore")            

    device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
     

  • 3
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值