🏡 我的环境:
- 语言环境: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),分别用
fcX
与predictions
表示 - 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,warningswarnings.filterwarnings("ignore")
device = torch.device("cuda" if torch.cuda.is_available() else "cpu")