>- **🍨 本文为[🔗365天深度学习训练营](https://mp.weixin.qq.com/s/0dvHCaOoFnW8SCp3JpzKxg) 中的学习记录博客**
>- **🍖 原作者:[K同学啊](https://mtyjkh.blog.csdn.net/)**

打卡~

555环境:

  • 语言环境:Python
  • 编译器:Jupyter Lab
  • 深度学习环境:Pytorch 
    • torch
    • torchvision

# 一、前期准备
# 1.1 设置环境

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("cpu")
device

# 1.2 导入数据

import os,PIL,random,pathlib

data_dir = '/Users/corawoo/Desktop/data6'
data_dir = pathlib.Path(data_dir)

data_paths  = list(data_dir.glob('*'))
classeNames = []
for path in data_paths:
    try:
        classeName = str(path).split("\\")[1]
        classeNames.append(classeName)
    except IndexError:
        # 处理索引超出范围的情况,可以选择跳过当前路径或进行其他处理
        pass
classeNames
train_transforms = transforms.Compose([
    transforms.Resize([224, 224]),  # 将输入图片resize成统一尺寸
    # transforms.RandomHorizontalFlip(), # 随机水平翻转
    transforms.ToTensor(),          # 将PIL Image或numpy.ndarray转换为tensor,并归一化到[0,1]之间
    transforms.Normalize(           # 标准化处理-->转换为标准正太分布(高斯分布),使模型更容易收敛
        mean=[0.485, 0.456, 0.406], 
        std=[0.229, 0.224, 0.225])  # 其中 mean=[0.485,0.456,0.406]与std=[0.229,0.224,0.225] 从数据集中随机抽样计算得到的。
])

total_data = datasets.ImageFolder("/Users/corawoo/Desktop/data6",transform=train_transforms)
total_data
Dataset ImageFolder
    Number of datapoints: 1800
    Root location: /Users/corawoo/Desktop/data6
    StandardTransform
Transform: Compose(
               Resize(size=[224, 224], interpolation=bilinear, max_size=None, antialias=True)
               ToTensor()
               Normalize(mean=[0.485, 0.456, 0.406], std=[0.229, 0.224, 0.225])
           )
total_data.class_to_idx
{'Angelina Jolie': 0,
 'Brad Pitt': 1,
 'Denzel Washington': 2,
 'Hugh Jackman': 3,
 'Jennifer Lawrence': 4,
 'Johnny Depp': 5,
 'Kate Winslet': 6,
 'Leonardo DiCaprio': 7,
 'Megan Fox': 8,
 'Natalie Portman': 9,
 'Nicole Kidman': 10,
 'Robert Downey Jr': 11,
 'Sandra Bullock': 12,
 'Scarlett Johansson': 13,
 'Tom Cruise': 14,
 'Tom Hanks': 15,
 'Will Smith': 16}

# 1.3 划分数据集


train_size = int(0.8 * len(total_data))
test_size  = len(total_data) - train_size
train_dataset, test_dataset = torch.utils.data.random_split(total_data, [train_size, test_size])
train_dataset, test_dataset
(<torch.utils.data.dataset.Subset at 0x159542f10>,
 <torch.utils.data.dataset.Subset at 0x1595d3c40>)
batch_size = 32

train_dl = torch.utils.data.DataLoader(train_dataset,
                                           batch_size=batch_size,
                                           shuffle=True,
                                           num_workers=1)
test_dl = torch.utils.data.DataLoader(test_dataset,
                                          batch_size=batch_size,
                                          shuffle=True,
                                          num_workers=1)
for X, y in test_dl:
    print("Shape of X [N, C, H, W]: ", X.shape)
    print("Shape of y: ", y.shape, y.dtype)
    break
Shape of X [N, C, H, W]:  torch.Size([32, 3, 224, 224])
Shape of y:  torch.Size([32]) torch.int64

# 二、调用官方的vgg16模型
VGG-16(Visual Geometry Group-16)是由牛津大学视觉几何组(Visual Geometry Group)提出的一种深度卷积神经网络架构,用于图像分类和对象识别任务。VGG-16在2014年被提出,是VGG系列中的一种。VGG-16之所以备受关注,是因为它在ImageNet图像识别竞赛中取得了很好的成绩,展示了其在大规模图像识别任务中的有效性。

以下是VGG-16的主要特点:

1. 深度:VGG-16由16个卷积层和3个全连接层组成,因此具有相对较深的网络结构。这种深度有助于网络学习到更加抽象和复杂的特征。 
2. 卷积层的设计:VGG-16的卷积层全部采用3x3的卷积核和步长为1的卷积操作,同时在卷积层之后都接有ReLU激活函数。这种设计的好处在于,通过堆叠多个较小的卷积核,可以提高网络的非线性建模能力,同时减少了参数数量,从而降低了过拟合的风险。 
3. 池化层:在卷积层之后,VGG-16使用最大池化层来减少特征图的空间尺寸,帮助提取更加显著的特征并减少计算量。 
4. 全连接层:VGG-16在卷积层之后接有3个全连接层,最后一个全连接层输出与类别数相对应的向量,用于进行分类。 

VGG-16结构说明:

● 13个卷积层(Convolutional Layer),分别用blockX_convX表示;
● 3个全连接层(Fully connected Layer),用classifier表示;
● 5个池化层(Pool layer)。

VGG-16包含了16个隐藏层(13个卷积层和3个全连接层),故称为VGG-16

# 2.1 模型建立

from torchvision.models import vgg16

device = "cuda" if torch.cuda.is_available() else "cpu"
print("Using {} device".format(device))
    
# 加载预训练模型,并且对模型进行微调
model = vgg16(pretrained = True).to(device) # 加载预训练的vgg16模型

for param in model.parameters():
    param.requires_grad = False # 冻结模型的参数,这样子在训练的时候只训练最后一层的参数

# 修改classifier模块的第6层(即:(6): Linear(in_features=4096, out_features=2, bias=True))
# 注意查看我们下方打印出来的模型
model.classifier._modules['6'] = nn.Linear(4096,len(classeNames)) # 修改vgg16模型中最后一层全连接层,输出目标类别个数
model.to(device)  
model
Using cpu device
Downloading: "https://download.pytorch.org/models/vgg16-397923af.pth" to /Users/corawoo/.cache/torch/hub/checkpoints/vgg16-397923af.pth
100%|█████████████████████████████████████████| 528M/528M [35:10<00:00, 262kB/s]
VGG(
  (features): Sequential(
    (0): Conv2d(3, 64, kernel_size=(3, 3), stride=(1, 1), padding=(1, 1))
    (1): ReLU(inplace=True)
    (2): Conv2d(64, 64, kernel_size=(3, 3), stride=(1, 1), padding=(1, 1))
    (3): ReLU(inplace=True)
    (4): MaxPool2d(kernel_size=2, stride=2, padding=0, dilation=1, ceil_mode=False)
    (5): Conv2d(64, 128, kernel_size=(3, 3), stride=(1, 1), padding=(1, 1))
    (6): ReLU(inplace=True)
    (7): Conv2d(128, 128, kernel_size=(3, 3), stride=(1, 1), padding=(1, 1))
    (8): ReLU(inplace=True)
    (9): MaxPool2d(kernel_size=2, stride=2, padding=0, dilation=1, ceil_mode=False)
    (10): Conv2d(128, 256, kernel_size=(3, 3), stride=(1, 1), padding=(1, 1))
    (11): ReLU(inplace=True)
    (12): Conv2d(256, 256, kernel_size=(3, 3), stride=(1, 1), padding=(1, 1))
    (13): ReLU(inplace=True)
    (14): Conv2d(256, 256, kernel_size=(3, 3), stride=(1, 1), padding=(1, 1))
    (15): ReLU(inplace=True)
    (16): MaxPool2d(kernel_size=2, stride=2, padding=0, dilation=1, ceil_mode=False)
    (17): Conv2d(256, 512, kernel_size=(3, 3), stride=(1, 1), padding=(1, 1))
    (18): ReLU(inplace=True)
    (19): Conv2d(512, 512, kernel_size=(3, 3), stride=(1, 1), padding=(1, 1))
    (20): ReLU(inplace=True)
    (21): Conv2d(512, 512, kernel_size=(3, 3), stride=(1, 1), padding=(1, 1))
    (22): ReLU(inplace=True)
    (23): MaxPool2d(kernel_size=2, stride=2, padding=0, dilation=1, ceil_mode=False)
    (24): Conv2d(512, 512, kernel_size=(3, 3), stride=(1, 1), padding=(1, 1))
    (25): ReLU(inplace=True)
    (26): Conv2d(512, 512, kernel_size=(3, 3), stride=(1, 1), padding=(1, 1))
    (27): ReLU(inplace=True)
    (28): Conv2d(512, 512, kernel_size=(3, 3), stride=(1, 1), padding=(1, 1))
    (29): ReLU(inplace=True)
    (30): MaxPool2d(kernel_size=2, stride=2, padding=0, dilation=1, ceil_mode=False)
  )
  (avgpool): AdaptiveAvgPool2d(output_size=(7, 7))
  (classifier): Sequential(
    (0): Linear(in_features=25088, out_features=4096, bias=True)
    (1): ReLU(inplace=True)
    (2): Dropout(p=0.5, inplace=False)
    (3): Linear(in_features=4096, out_features=4096, bias=True)
    (4): ReLU(inplace=True)
    (5): Dropout(p=0.5, inplace=False)
    (6): Linear(in_features=4096, out_features=0, bias=True)
  )
)

# 三、训练模型
# 3.1 编写训练函数

# 训练循环
def train(dataloader, model, loss_fn, optimizer):
    size = len(dataloader.dataset)  # 训练集的大小
    num_batches = len(dataloader)   # 批次数目, (size/batch_size,向上取整)

    train_loss, train_acc = 0, 0  # 初始化训练损失和正确率
    
    for X, y in dataloader:  # 获取图片及其标签
        X, y = X.to(device), y.to(device)
        
        # 计算预测误差
        pred = model(X)          # 网络输出
        loss = loss_fn(pred, y)  # 计算网络输出和真实值之间的差距,targets为真实值,计算二者差值即为损失
        
        # 反向传播
        optimizer.zero_grad()  # grad属性归零
        loss.backward()        # 反向传播
        optimizer.step()       # 每一步自动更新
        
        # 记录acc与loss
        train_acc  += (pred.argmax(1) == y).type(torch.float).sum().item()
        train_loss += loss.item()
            
    train_acc  /= size
    train_loss /= num_batches

    return train_acc, train_loss

# 3.2 编写测试函数

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

# 3.3 设置动态学习率

def adjust_learning_rate(optimizer, epoch, start_lr):
    # 每 2 个 epoch 衰减到原来的 0.98
    lr = start_lr * (0.92 ** (epoch // 2))
    for param_group in optimizer.param_groups:
        param_group['lr'] = lr

learn_rate = 1e-4  # 初始学习率
optimizer = torch.optim.SGD(model.parameters(), lr=learn_rate)

    # 打印训练数据的标签值

    for batch in train_dl:
        X, y = batch
        print("Labels:", y)
Labels: tensor([16,  0,  1, 10,  7,  4,  6,  8,  8,  6,  2, 16, 11, 11, 11,  5, 14, 11,
         5,  6,  7,  9,  7, 10,  3, 15, 13,  0,  6, 16, 13, 16])
Labels: tensor([ 3,  4,  0,  3, 14, 11, 14,  4, 15,  6,  1,  9, 16, 13,  0,  9,  0,  5,
        13,  3,  7,  3,  5,  3, 13,  5, 14,  8,  5, 12, 11,  2])
Labels: tensor([ 7, 14, 14,  6, 16,  9,  2, 14, 13,  2,  3, 11, 11,  1,  8, 11,  8,  6,
         9,  3,  0, 15, 13,  5, 12, 10, 13,  5, 14, 13,  1, 11])
Labels: tensor([16,  5,  1, 16,  5,  5,  0,  9, 16, 13,  2, 14,  6, 14,  7,  7,  2, 15,
         6, 11,  0,  4, 11,  3,  4,  6,  1, 12,  6,  0, 13,  8])
Labels: tensor([ 9, 14, 13,  9,  8,  8, 12, 16,  7,  3, 10,  2, 14,  6,  6,  3,  3,  6,
        13,  0, 13, 11, 14,  7,  5,  7,  3,  7,  7, 10,  5,  9])
Labels: tensor([ 8,  1,  2, 10, 16,  6,  7,  8,  1,  5,  4, 13,  8,  8,  7,  2,  2,  5,
         2,  5,  2,  4, 13,  5,  2, 12, 15, 16, 16,  9,  5,  6])
Labels: tensor([13, 14, 15,  1, 13,  4,  3, 13,  3,  9, 13, 10,  8,  7, 12,  7,  2,  0,
         8,  6,  2, 11,  1, 15,  5,  1,  1,  9,  7,  0, 15,  3])
Labels: tensor([ 1,  1,  5,  2, 13, 12, 14, 13, 12,  9, 13,  6,  9,  9,  7,  3,  4,  6,
         7,  7, 10,  9, 16, 12, 11, 16, 14,  3,  2,  4, 14,  2])
Labels: tensor([ 9,  2,  4,  9, 14,  5, 15, 14,  9, 13,  2, 15,  9,  8,  6,  4, 15, 10,
        15, 16,  0, 12,  8, 13,  0,  3,  4, 13, 10,  5,  5, 13])
Labels: tensor([15,  0,  1,  3, 12,  0, 10, 13, 10, 11,  7,  3,  2,  3,  6,  1, 16, 12,
        10, 14,  0, 15, 11,  4,  7,  2, 14, 12, 10,  4, 16,  7])
Labels: tensor([15,  1,  4, 15,  5,  1, 11,  0, 10,  9, 11, 14,  4,  4,  4,  4, 14, 15,
        11,  1,  4,  8, 15, 13,  7,  1, 14,  3,  4, 15, 14, 16])
Labels: tensor([ 2,  8,  5, 10,  7, 13, 13,  1,  2,  5,  8, 15, 12, 14,  0, 13,  2,  2,
        14, 12,  2,  7, 13,  4, 10,  5,  1, 10,  6,  9,  4, 10])
Labels: tensor([ 8, 14,  3, 10,  3, 13,  8, 15,  7,  0, 13,  0,  9, 15, 15,  1, 15,  6,
        13,  1,  9, 11,  2,  4,  1, 16,  2,  1,  7,  0,  4,  7])
Labels: tensor([10, 14,  9, 10,  6,  4, 13, 10,  7, 14,  6,  0,  8, 12,  4,  9,  4, 16,
         4, 16, 13,  8,  5, 10,  6, 11, 15,  1,  5,  3, 15,  6])
Labels: tensor([10,  9, 15, 13, 14,  4, 16,  9,  6, 16, 12,  9,  9,  9, 11,  1, 13,  4,
        13,  3, 15, 12, 16, 13,  8, 14, 13,  8, 13,  1,  0, 10])
Labels: tensor([ 1, 13,  0, 12,  7,  3,  2,  7,  8,  7,  3, 16,  0, 12, 10,  2, 15,  9,
        12,  8, 11, 13,  2, 13, 11, 11,  8, 16, 13,  5,  0,  5])
Labels: tensor([15, 15, 13, 14, 13, 13, 13,  5,  3, 12, 13,  3, 14,  1,  0,  8,  4, 12,
         0,  6, 13,  9,  9,  3,  3,  4, 15, 16,  2, 12,  7, 14])
Labels: tensor([16,  6, 12,  9, 11,  3,  2,  8, 13, 10,  1,  7, 15,  4,  7,  9,  6,  6,
        14,  3,  1,  6, 10,  6, 10, 15, 16, 14, 11,  7,  7,  4])
Labels: tensor([12,  4,  4,  8, 13,  6,  5,  3, 12,  7,  3,  1,  8,  8,  4, 15, 15,  4,
        14,  7, 12, 15,  3, 11, 10, 12, 16,  5, 10,  9,  3, 11])
Labels: tensor([ 4, 15,  7, 15, 13, 13,  2,  6, 13,  9, 11, 14,  4,  5,  6,  2,  3,  2,
         0,  6,  4, 15, 12, 10,  9, 10,  9, 13, 16, 11,  6, 16])
Labels: tensor([13,  6,  2, 11, 10,  3,  9, 10, 15,  5,  9,  9,  6,  6,  2, 13,  8, 15,
         5, 16,  9,  5,  3,  4, 16,  4, 11, 11, 14, 15,  3,  7])
Labels: tensor([14,  0, 14,  5,  6, 16,  1, 16, 13, 12,  4,  6,  9,  4, 14, 15,  2, 13,
         0, 13,  7, 13, 13,  6, 15,  9,  9,  2,  8,  0,  9, 13])
Labels: tensor([14, 10, 10,  2,  0,  8,  5, 15, 10,  3,  3, 13, 16, 11, 13,  6,  0, 16,
        12, 13, 12,  8, 15,  0, 13, 12,  2,  5,  2, 14,  6,  1])
Labels: tensor([ 7,  5, 15, 13,  5,  1,  2, 15, 14,  6, 12,  4, 16, 13,  8, 10,  1,  8,
        16,  4,  6,  8, 11, 12, 16,  8,  0,  7,  3,  4,  4, 16])
Labels: tensor([12,  9,  6, 14, 13, 16,  7, 16, 10, 16, 13,  9,  1,  7, 16,  6,  3,  9,
        12,  4,  1,  0,  1,  7, 10, 11,  0, 11,  2, 10, 13,  6])
Labels: tensor([11, 15, 10,  7,  3,  2, 13,  7,  1, 13,  0,  7,  0, 10, 13, 13,  9, 13,
        10, 14, 13,  8,  6, 10, 13,  1,  3,  5,  9,  2,  8, 10])
Labels: tensor([ 8,  2, 14,  0,  0,  4, 15,  8, 16, 13,  9,  3,  8, 12, 14,  3, 15, 11,
        12,  4,  8, 13, 11,  8,  9,  3, 14, 13,  4, 12,  5,  8])
Labels: tensor([12, 12, 13,  5, 10, 11,  1,  4,  2, 13, 13, 14,  3,  1,  1,  6,  2,  7,
         0, 12,  9, 12, 16,  7,  6,  4, 13,  3,  8, 13,  9,  7])
Labels: tensor([13, 13,  5, 13,  9,  2,  5,  6,  7, 14, 12,  6, 13,  1,  2, 13,  6,  1,
        13, 15,  2, 13, 10,  5, 13, 15,  1,  5,  2,  1, 16, 13])
Labels: tensor([ 1, 13,  4, 15, 13, 14,  3, 16,  5,  8,  7, 15,  7,  8,  4, 13,  5,  4,
        16,  4,  0, 10, 12,  2, 12,  7, 11,  3,  4, 13, 13, 15])
Labels: tensor([ 9, 12, 11,  3,  6,  0,  9,  5, 11,  9,  6,  4,  4, 16,  5,  7,  1, 13,
        16,  3, 16, 14,  8, 16, 13,  5,  6,  8, 10,  0, 10,  2])
Labels: tensor([12,  0,  9, 16,  5,  9,  7,  3,  8, 13, 13,  6,  1, 10,  1, 10, 11,  1,
         5,  9,  3, 10,  0, 14,  2, 12,  8,  6,  5,  9, 15,  2])
Labels: tensor([ 0, 10, 11,  9,  2, 16,  0, 15,  9,  1, 11,  3,  0,  2, 10, 12,  9, 10,
        10,  5,  4, 13, 15,  1,  7, 16,  8,  5,  9, 11,  3,  5])
Labels: tensor([15,  3,  0, 10,  8,  8, 12, 15, 16,  8,  3,  6,  7, 14,  3, 12, 10,  0,
        12, 14, 15, 13, 12,  0, 13, 13,  9, 13,  3, 11,  7,  9])
Labels: tensor([12,  9, 10, 13,  2,  1,  8,  0, 12,  7,  6, 16, 16,  8, 12,  7, 15,  7,
         3,  1, 13, 13, 12,  9,  0,  4,  8,  6,  5,  9,  7,  3])
Labels: tensor([13, 10,  3,  4, 14, 14, 16,  2, 11, 11, 13,  2,  5,  9,  8, 14,  8,  0,
        13,  2,  9, 16,  1, 13,  3,  9,  4,  7,  5,  7, 14,  8])
Labels: tensor([13, 13,  4, 10,  2, 16, 16, 15, 13,  5, 16,  9,  7,  7,  0, 13,  5, 16,
        16, 14, 13, 12,  2,  1,  0, 16,  7, 16,  9, 11, 12, 14])
Labels: tensor([16, 11, 14,  5,  7,  5,  1,  6, 15, 15,  4, 11, 12,  1,  6, 13,  4,  6,
         2, 10, 13,  2,  3,  8, 13, 12,  0,  9,  5, 10,  9, 14])
Labels: tensor([13,  1, 14, 12, 12,  8,  5,  9, 12,  4,  9,  3,  4, 13, 14, 14,  9,  8,
         0,  9,  2, 11,  7, 13,  4, 13,  1, 11,  1,  0, 11,  9])
Labels: tensor([11,  0,  2,  2, 11, 16,  6,  6,  1, 10, 11,  4, 12,  1,  2,  3, 12,  0,
         2, 10, 14,  1, 10,  6,  1,  1,  8, 13,  6,  0,  3,  5])
Labels: tensor([16,  7, 15,  2,  5, 14, 13,  9,  5,  7, 14, 11,  2,  5,  0,  6, 12,  0,
        10,  9,  4, 13, 13, 14,  0, 12, 14, 15,  6, 14,  3, 14])
Labels: tensor([ 8, 11, 11,  9,  5,  7, 15, 14, 11,  6,  5,  7, 15,  2, 12,  1,  2, 11,
        14,  2,  4, 16,  1, 11, 11,  4, 15, 10, 12, 13,  6,  1])
Labels: tensor([13,  2, 13,  3,  6,  0, 13,  1,  7, 10, 12, 16,  6, 12,  3, 11,  5,  1,
        13, 10, 12,  9, 15, 13,  3,  3,  4,  9, 13,  9,  3,  8])
Labels: tensor([ 8, 15,  1, 13, 12, 10, 16,  0,  0,  5, 11,  3,  4, 13, 11, 14,  0, 13,
         5,  7, 12, 10, 15,  2,  0, 13, 15,  8,  8, 15,  2,  8])
Labels: tensor([ 5,  5,  1,  0, 15,  6,  5,  1,  8, 16, 11,  6, 13,  5, 11,  8,  4, 12,
         0,  3,  8, 14, 14,  5,  3, 13, 13, 13,  6, 12, 15, 15])

# 3.4 正式训练

import copy

loss_fn    = nn.CrossEntropyLoss()  # 创建损失函数
epochs     = 40

train_loss = []
train_acc  = []
test_loss  = []
test_acc   = []

best_acc = 0    # 设置一个最佳准确率,作为最佳模型的判别指标

# 定义学习率调度器
scheduler = torch.optim.lr_scheduler.StepLR(optimizer, step_size=10, gamma=0.5) 

for epoch in range(epochs):
    # 更新学习率(使用自定义学习率时使用)
    # adjust_learning_rate(optimizer, epoch, learn_rate)
    
    model.train()
    epoch_train_acc, epoch_train_loss = train(train_dl, model, loss_fn, optimizer)
    scheduler.step()  # 更新学习率(调用官方动态学习率接口时使用)
    
    model.eval()
    epoch_test_acc, epoch_test_loss = test(test_dl, model, loss_fn)
    
    # 保存最佳模型到 best_model
    if epoch_test_acc > best_acc:
        best_acc   = epoch_test_acc
        best_model = copy.deepcopy(model)
    
    train_acc.append(epoch_train_acc)
    train_loss.append(epoch_train_loss)
    test_acc.append(epoch_test_acc)
    test_loss.append(epoch_test_loss)
    
    # 获取当前的学习率
    lr = optimizer.state_dict()['param_groups'][0]['lr']
    
    template = ('Epoch:{:2d}, Train_acc:{:.1f}%, Train_loss:{:.3f}, Test_acc:{:.1f}%, Test_loss:{:.3f}, Lr:{:.2E}')
    print(template.format(epoch+1, epoch_train_acc*100, epoch_train_loss, 
                          epoch_test_acc*100, epoch_test_loss, lr))
    
# 保存最佳模型到文件中
PATH = '/Users/corawoo/Desktop/data_vgg16'  # 保存的参数文件名
torch.save(model.state_dict(), PATH)

print('Done')
Epoch: 1, Train_acc:7.4%, Train_loss:2.895, Test_acc:15.0%, Test_loss:2.774, Lr:1.00E-04
Epoch: 2, Train_acc:8.3%, Train_loss:2.873, Test_acc:15.3%, Test_loss:2.757, Lr:1.00E-04
Epoch: 3, Train_acc:11.4%, Train_loss:2.822, Test_acc:16.4%, Test_loss:2.730, Lr:1.00E-04
Epoch: 4, Train_acc:12.5%, Train_loss:2.789, Test_acc:15.6%, Test_loss:2.705, Lr:1.00E-04
Epoch: 5, Train_acc:13.5%, Train_loss:2.757, Test_acc:16.7%, Test_loss:2.685, Lr:1.00E-04
Epoch: 6, Train_acc:12.2%, Train_loss:2.738, Test_acc:17.8%, Test_loss:2.661, Lr:1.00E-04
Epoch: 7, Train_acc:14.8%, Train_loss:2.711, Test_acc:17.8%, Test_loss:2.645, Lr:1.00E-04
Epoch: 8, Train_acc:13.6%, Train_loss:2.703, Test_acc:18.1%, Test_loss:2.620, Lr:1.00E-04
Epoch: 9, Train_acc:14.6%, Train_loss:2.686, Test_acc:18.3%, Test_loss:2.620, Lr:1.00E-04
Epoch:10, Train_acc:14.0%, Train_loss:2.664, Test_acc:18.6%, Test_loss:2.599, Lr:5.00E-05
Epoch:11, Train_acc:15.2%, Train_loss:2.655, Test_acc:18.6%, Test_loss:2.587, Lr:5.00E-05
Epoch:12, Train_acc:15.2%, Train_loss:2.648, Test_acc:18.1%, Test_loss:2.585, Lr:5.00E-05
Epoch:13, Train_acc:14.3%, Train_loss:2.640, Test_acc:18.3%, Test_loss:2.582, Lr:5.00E-05
Epoch:14, Train_acc:16.0%, Train_loss:2.624, Test_acc:18.9%, Test_loss:2.560, Lr:5.00E-05
Epoch:15, Train_acc:16.2%, Train_loss:2.620, Test_acc:19.2%, Test_loss:2.555, Lr:5.00E-05
Epoch:16, Train_acc:16.5%, Train_loss:2.609, Test_acc:19.4%, Test_loss:2.550, Lr:5.00E-05
Epoch:17, Train_acc:15.7%, Train_loss:2.603, Test_acc:19.2%, Test_loss:2.523, Lr:5.00E-05
Epoch:18, Train_acc:17.2%, Train_loss:2.580, Test_acc:19.2%, Test_loss:2.528, Lr:5.00E-05
Epoch:19, Train_acc:17.2%, Train_loss:2.590, Test_acc:19.4%, Test_loss:2.529, Lr:5.00E-05
Epoch:20, Train_acc:18.3%, Train_loss:2.556, Test_acc:19.4%, Test_loss:2.510, Lr:2.50E-05
Epoch:21, Train_acc:16.4%, Train_loss:2.573, Test_acc:19.4%, Test_loss:2.520, Lr:2.50E-05
Epoch:22, Train_acc:17.6%, Train_loss:2.575, Test_acc:19.4%, Test_loss:2.521, Lr:2.50E-05
Epoch:23, Train_acc:17.6%, Train_loss:2.563, Test_acc:20.0%, Test_loss:2.513, Lr:2.50E-05
Epoch:24, Train_acc:15.8%, Train_loss:2.559, Test_acc:20.0%, Test_loss:2.490, Lr:2.50E-05
Epoch:25, Train_acc:18.7%, Train_loss:2.544, Test_acc:20.0%, Test_loss:2.515, Lr:2.50E-05
Epoch:26, Train_acc:19.7%, Train_loss:2.532, Test_acc:19.7%, Test_loss:2.502, Lr:2.50E-05
Epoch:27, Train_acc:17.7%, Train_loss:2.543, Test_acc:20.3%, Test_loss:2.497, Lr:2.50E-05
Epoch:28, Train_acc:17.9%, Train_loss:2.544, Test_acc:20.3%, Test_loss:2.503, Lr:2.50E-05
Epoch:29, Train_acc:18.1%, Train_loss:2.550, Test_acc:20.3%, Test_loss:2.487, Lr:2.50E-05
Epoch:30, Train_acc:18.7%, Train_loss:2.540, Test_acc:20.3%, Test_loss:2.490, Lr:1.25E-05
Epoch:31, Train_acc:16.8%, Train_loss:2.538, Test_acc:20.3%, Test_loss:2.491, Lr:1.25E-05
Epoch:32, Train_acc:17.4%, Train_loss:2.536, Test_acc:20.3%, Test_loss:2.492, Lr:1.25E-05
Epoch:33, Train_acc:17.4%, Train_loss:2.553, Test_acc:20.0%, Test_loss:2.484, Lr:1.25E-05
Epoch:34, Train_acc:18.7%, Train_loss:2.541, Test_acc:20.3%, Test_loss:2.458, Lr:1.25E-05
Epoch:35, Train_acc:20.1%, Train_loss:2.527, Test_acc:20.3%, Test_loss:2.493, Lr:1.25E-05
Epoch:36, Train_acc:18.7%, Train_loss:2.518, Test_acc:20.3%, Test_loss:2.494, Lr:1.25E-05
Epoch:37, Train_acc:17.4%, Train_loss:2.532, Test_acc:20.3%, Test_loss:2.477, Lr:1.25E-05
Epoch:38, Train_acc:17.4%, Train_loss:2.532, Test_acc:20.3%, Test_loss:2.475, Lr:1.25E-05
Epoch:39, Train_acc:19.0%, Train_loss:2.506, Test_acc:20.3%, Test_loss:2.481, Lr:1.25E-05
Epoch:40, Train_acc:18.3%, Train_loss:2.519, Test_acc:20.3%, Test_loss:2.465, Lr:6.25E-06
Done

# 四、结果可视化
# 4.1 loss和accuracy图

import matplotlib.pyplot as plt
#隐藏警告
import warnings
warnings.filterwarnings("ignore")               #忽略警告信息
plt.rcParams['font.sans-serif']    = ['SimHei'] # 用来正常显示中文标签
plt.rcParams['axes.unicode_minus'] = False      # 用来正常显示负号
plt.rcParams['figure.dpi']         = 100        #分辨率

epochs_range = range(epochs)

plt.figure(figsize=(12, 3))
plt.subplot(1, 2, 1)

plt.plot(epochs_range, train_acc, label='Training Accuracy')
plt.plot(epochs_range, test_acc, label='Test Accuracy')
plt.legend(loc='lower right')
plt.title('Training and Validation Accuracy')

plt.subplot(1, 2, 2)
plt.plot(epochs_range, train_loss, label='Training Loss')
plt.plot(epochs_range, test_loss, label='Test Loss')
plt.legend(loc='upper right')
plt.title('Training and Validation Loss')
plt.show()

# 4.2 指定图片进行预测

from PIL import Image 

classes = list(total_data.class_to_idx)

def predict_one_image(image_path, model, transform, classes):
    
    test_img = Image.open(image_path).convert('RGB')
    plt.imshow(test_img)  # 展示预测的图片

    test_img = transform(test_img)
    img = test_img.to(device).unsqueeze(0)
    
    model.eval()
    output = model(img)

    _,pred = torch.max(output,1)
    pred_class = classes[pred]
    print(f'预测结果是:{pred_class}')
# 预测训练集中的某张照片
predict_one_image(image_path='/Users/corawoo/Desktop/data6/Angelina Jolie/001_fe3347c0.jpg', 
                  model=model, 
                  transform=train_transforms, 
                  classes=classes)

# 4.3 模型评估

best_model.eval()
epoch_test_acc, epoch_test_loss = test(test_dl, best_model, loss_fn)
epoch_test_acc, epoch_test_loss
(0.20277777777777778, 2.4938654700915017)
# 查看是否与我们记录的最高准确率一致
epoch_test_acc
0.20277777777777778
Logo

DAMO开发者矩阵,由阿里巴巴达摩院和中国互联网协会联合发起,致力于探讨最前沿的技术趋势与应用成果,搭建高质量的交流与分享平台,推动技术创新与产业应用链接,围绕“人工智能与新型计算”构建开放共享的开发者生态。

更多推荐