Pytorch从零开始实战11

Pytorch从零开始实战——ResNet-50V2算法实战

本系列来源于365天深度学习训练营

原作者K同学

文章目录

  • Pytorch从零开始实战——ResNet-50V2算法实战
    • 环境准备
    • 数据集
    • 模型选择
    • 开始训练
    • 可视化
    • 总结

环境准备

本文基于Jupyter notebook,使用Python3.8,Pytorch2.0.1+cu118,torchvision0.15.2,需读者自行配置好环境且有一些深度学习理论基础。本次实验的目的是理解并使用ResNet-50V2模型,其他部分与上次几乎相同。
第一步,导入常用包

import torch
import torch.nn as nn
import matplotlib.pyplot as plt
import torchvision
import torchvision.transforms as transforms
import torchvision.datasets as datasets
import torch.nn.functional as F
import random
from time import time
import numpy as np
import pandas as pd
import datetime
import gc
import os
import copy
import warnings
os.environ['KMP_DUPLICATE_LIB_OK']='True'  # 用于避免jupyter环境突然关闭
torch.backends.cudnn.benchmark=True  # 用于加速GPU运算的代码

设置随机数种子

torch.manual_seed(428)
torch.cuda.manual_seed(428)
torch.cuda.manual_seed_all(428)
random.seed(428)
np.random.seed(428)

检查设备对象

device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
device, torch.cuda.device_count() # # (device(type='cuda'), 2)

数据集

本次数据集是使用鸟的图片,分别有四种类别的鸟,根据鸟的类别名称存放在不同的文件夹中。
使用pathlib查看类别

import pathlib
data_dir = './data/bird_photos/'
data_dir = pathlib.Path(data_dir) # 转成pathlib.Path对象
data_paths = list(data_dir.glob('*')) 
classNames = [str(path).split("/")[2] for path in data_paths]
classNames # ['Black Throated Bushtiti', 'Cockatoo', 'Black Skimmer', 'Bananaquit']

使用transforms对数据集进行统一处理,并且根据文件夹名映射对应标签

all_transforms = transforms.Compose([
    transforms.Resize([224, 224]),
    transforms.ToTensor(),
    transforms.Normalize(mean=[0.485, 0.456, 0.406], std=[0.229, 0.224, 0.225]) # 标准化
])

total_data = datasets.ImageFolder("./data/bird_photos/", transform=all_transforms)
total_data.class_to_idx
 # {'Bananaquit': 0,
 # 'Black Skimmer': 1,
 # 'Black Throated Bushtiti': 2,
 # 'Cockatoo': 3}

随机查看5张图片

def plotsample(data):
    fig, axs = plt.subplots(1, 5, figsize=(10, 10)) #建立子图
    for i in range(5):
        num = random.randint(0, len(data) - 1) #首先选取随机数,随机选取五次
        #抽取数据中对应的图像对象,make_grid函数可将任意格式的图像的通道数升为3,而不改变图像原始的数据
        #而展示图像用的imshow函数最常见的输入格式也是3通道
        npimg = torchvision.utils.make_grid(data[num][0]).numpy()
        nplabel = data[num][1] #提取标签 
        #将图像由(3, weight, height)转化为(weight, height, 3),并放入imshow函数中读取
        axs[i].imshow(np.transpose(npimg, (1, 2, 0))) 
        axs[i].set_title(nplabel) #给每个子图加上标签
        axs[i].axis("off") #消除每个子图的坐标轴

plotsample(total_data)

在这里插入图片描述
根据8比2划分数据集和测试集,并且利用DataLoader划分批次和随机打乱

train_size = int(0.8 * len(total_data))
test_size  = len(total_data) - train_size
train_ds, test_ds = torch.utils.data.random_split(total_data, [train_size, test_size])

batch_size = 32
train_dl = torch.utils.data.DataLoader(train_ds,
                                        batch_size=batch_size,
                                        shuffle=True,
                                      )
test_dl = torch.utils.data.DataLoader(test_ds,
                                        batch_size=batch_size,
                                        shuffle=True,
                                     )

len(train_dl.dataset), len(test_dl.dataset) # (452, 113)

模型选择

ResNetV2与ResNet区别
其中(a)original 表示原始的 ResNet 的残差结构,(b)proposed 表示新的 ResNet 的残差结构。主要差别就是(a)结构先卷积后进行 BN 和激活函数计算,最后执行 addition 后再进行ReLU 计算; (b)结构先进行 BN 和激活函数计算后卷积,把 addition 后的 ReLU 计算放到了残差结构内部。
根据论文所说,改进的模型能够降低错误率。
Pytorch从零开始实战11_第1张图片
本次使用的模型整体架构如下图,借用K同学所绘制的图片,红色和灰色预激活位置有点问题。
Pytorch从零开始实战11_第2张图片
首先实现Block块,首先进行预激活层,包括标准化和ReLu激活函数,接着进行shortcut操作,如果conv_shortcut为True,会使用一个1x1卷积层进行变换,否则,如果stride为1,则进行恒等映射,否则使用1x1的最大池化。随后经过三个卷积层。在forward方法中,输入 x 经过预激活层,然后进行三个卷积操作,最后将shortcut和经过卷积的结果相加。这种结构使得梯度更容易反向传播,从而有助于训练深层网络。

class Block2(nn.Module):
    def __init__(self, in_channels, filters, kernel_size=3, stride=1, conv_shortcut=False):
        super(Block2, self).__init__()

        self.preact = nn.Sequential(
            nn.BatchNorm2d(in_channels),
            nn.ReLU()
        )

        if conv_shortcut:
            self.shortcut = nn.Conv2d(in_channels, 4 * filters, kernel_size=1, stride=stride)
        else:
            if stride == 1:
                self.shortcut = nn.Identity()
            else: 
                self.shortcut = nn.MaxPool2d(1, stride=stride)

        self.conv1 = nn.Sequential(
            nn.Conv2d(in_channels, filters, kernel_size=1, stride=1, bias=False),
            nn.BatchNorm2d(filters),
            nn.ReLU()
        )

        self.conv2 = nn.Sequential(
            nn.ZeroPad2d(padding=(1, 1, 1, 1)),
            nn.Conv2d(filters, filters, kernel_size=kernel_size, stride=stride, bias=False),
            nn.BatchNorm2d(filters),
            nn.ReLU()
        )

        self.conv3 = nn.Conv2d(filters, 4 * filters, kernel_size=1)

    def forward(self, x):
        preact = self.preact(x)
        shortcut = self.shortcut(preact)
        x = self.conv1(preact)
        x = self.conv2(x)
        x = self.conv3(x)
        out = shortcut + x
        return out

下面实现堆叠块,通过传入不同的参数去调用Block块,其中[Block2(4 * filters, filters) for i in range(0, blocks)],使用 Python 中的列表解析创建了 blocks 个残差块。这些残差块的输入通道数为 4 * filters,以匹配前一个残差块的输出通道数。

class Stack2(nn.Module):
    def __init__(self, in_channels, filters, blocks, stride1=2):
        super(Stack2, self).__init__()

        self.blocks = nn.Sequential(
            Block2(in_channels, filters, conv_shortcut=True),
            *[Block2(4 * filters, filters) for i in range(0, blocks)],
            Block2(4 * filters, filters, stride=stride1)
        )

    def forward(self, x):
        return self.blocks(x)

下面就是网络主体,按照上图实现就行了

class ResNet50V2(nn.Module):
    def __init__(self, include_top=True, preact=True, num_classes=1000):
        super(ResNet50V2, self).__init__()

        self.conv1 = nn.Sequential(
            nn.Conv2d(3, 64, kernel_size=7, stride=2, padding=3, bias=False),
            nn.BatchNorm2d(64),
            nn.ReLU()
        )

        self.pool1 = nn.MaxPool2d(kernel_size=3, stride=2, padding=1)

        self.conv2 = Stack2(64, 64, 3)
        self.conv3 = Stack2(256, 128, 4)
        self.conv4 = Stack2(512, 256, 6)
        self.conv5 = Stack2(1024, 512, 3, stride1=1)

        self.post = nn.Sequential(
            nn.BatchNorm2d(2048),
            nn.ReLU()
        )

        self.include_top = include_top
        if include_top:
            self.avg_pool = nn.AdaptiveAvgPool2d(1)
            self.fc = nn.Linear(2048, num_classes)

    def forward(self, x):
        x = self.conv1(x)
        x = self.pool1(x)
        x = self.conv2(x)
        x = self.conv3(x)
        x = self.conv4(x)
        x = self.conv5(x)
        x = self.post(x)

        if self.include_top:
            x = self.avg_pool(x)
            x = torch.flatten(x, 1)
            x = self.fc(x)

        return x

使用summary查看网络
Pytorch从零开始实战11_第3张图片

开始训练

定义训练函数

def train(dataloader, model, loss_fn, opt):
    size = len(dataloader.dataset)
    num_batches = len(dataloader)
    train_acc, train_loss = 0, 0

    for X, y in dataloader:
        X, y = X.to(device), y.to(device)
        pred = model(X)
        loss = loss_fn(pred, y)

        opt.zero_grad()
        loss.backward()
        opt.step()

        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

定义测试函数

def test(dataloader, model, loss_fn):
    size = len(dataloader.dataset)
    num_batches = len(dataloader)
    test_acc, test_loss = 0, 0
    with torch.no_grad():
        for X, y in dataloader:
            X, y = X.to(device), y.to(device)
            pred = model(X)
            loss = loss_fn(pred, y)
    
            test_acc += (pred.argmax(1) == y).type(torch.float).sum().item()
            test_loss += loss.item()

    test_acc /= size
    test_loss /= num_batches
    return test_acc, test_loss

定义学习率、损失函数、优化算法

loss_fn = nn.CrossEntropyLoss()
learn_rate = 0.0001
opt = torch.optim.Adam(model.parameters(), lr=learn_rate)

开始训练,epoch设置为30

import time
epochs = 30
train_loss = []
train_acc = []
test_loss = []
test_acc = []

T1 = time.time()

best_acc = 0
best_model = 0

for epoch in range(epochs):

    model.train()
    epoch_train_acc, epoch_train_loss = train(train_dl, model, loss_fn, opt)
    
    model.eval() # 确保模型不会进行训练操作
    epoch_test_acc, epoch_test_loss = test(test_dl, model, loss_fn)

    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)
    
    print("epoch:%d, train_acc:%.1f%%, train_loss:%.3f, test_acc:%.1f%%, test_loss:%.3f"
          % (epoch + 1, epoch_train_acc * 100, epoch_train_loss, epoch_test_acc * 100, epoch_test_loss))

T2 = time.time()
print('程序运行时间:%s秒' % (T2 - T1))

PATH = './best_model.pth'  # 保存的参数文件名
if best_model is not None:
    torch.save(best_model.state_dict(), PATH)
    print('保存最佳模型')
print("Done")

虽然结果过拟合了,但这不是本次实验的重点,如果使用更好的参数,效果可能会不错。
Pytorch从零开始实战11_第4张图片

可视化

可视化训练过程和测试过程

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()

Pytorch从零开始实战11_第5张图片

总结

本次实验主要实现了ResNet-50V2模型,ResNet模型本身就是用来缓解梯度爆炸和梯度消失问题的,在V2结构中,激活函数ReLU应用在残差块的输出上,而不是在整个块的输入上。这使得网络能够学习到更复杂的非线性映射,并且使得原本的网络进行相对的恒等映射,提高了网络的表达能力。当然,网络可以有不同的残差连接,本文这种修改并不是一成不变适用于所有情况,而是在某些场景下可能有助于训练,提高网络性能。

你可能感兴趣的:(pytorch,人工智能,python)