深度学习每周学习总结J2(ResNet-50v2算法实战与解析 - 鸟类识别)

0. 总结

数据导入及处理部分:本次数据导入没有使用torchvision自带的数据集,需要将原始数据进行处理包括数据导入,查看数据分类情况,定义transforms,进行数据类型转换等操作。

划分数据集:划定训练集测试集后,再使用torch.utils.data中的DataLoader()分别加载上一步处理好的训练及测试数据,查看批处理维度.

模型构建部分:resnet-50v2

设置超参数:在这之前需要定义损失函数,学习率(动态学习率),以及根据学习率定义优化器(例如SGD随机梯度下降),用来在训练中更新参数,最小化损失函数。

定义训练函数:函数的传入的参数有四个,分别是设置好的DataLoader(),定义好的模型,损失函数,优化器。函数内部初始化损失准确率为0,接着开始循环,使用DataLoader()获取一个批次的数据,对这个批次的数据带入模型得到预测值,然后使用损失函数计算得到损失值。接下来就是进行反向传播以及使用优化器优化参数,梯度清零放在反向传播之前或者是使用优化器优化之后都是可以的,一般是默认放在反向传播之前。

定义测试函数:函数传入的参数相比训练函数少了优化器,只需传入设置好的DataLoader(),定义好的模型,损失函数。此外除了处理批次数据时无需再设置梯度清零、返向传播以及优化器优化参数,其余部分均和训练函数保持一致。

训练过程:定义训练次数,有几次就使用整个数据集进行几次训练,初始化四个空list分别存储每次训练及测试的准确率及损失。使用model.train()开启训练模式,调用训练函数得到准确率及损失。使用model.eval()将模型设置为评估模式,调用测试函数得到准确率及损失。接着就是将得到的训练及测试的准确率及损失存储到相应list中并合并打印出来,得到每一次整体训练后的准确率及损失。

结果可视化

模型的保存,调取及使用。在PyTorch中,通常使用 torch.save(model.state_dict(), ‘model.pth’) 保存模型的参数,使用 model.load_state_dict(torch.load(‘model.pth’)) 加载参数。

需要改进优化的地方:确保模型和数据的一致性,都存到GPU或者CPU;注意numclasses不要直接用默认的1000,需要根据实际数据集改进;实例化模型也要注意numclasses这个参数;此外注意测试模型需要用(3,224,224)3表示通道数,这和tensorflow定义的顺序是不用的(224,224,3),做代码转换时需要注意。

import torch
import torch.nn as nn
import torchvision
from torchvision import datasets,transforms
from torch.utils.data import DataLoader
import torchvision.models as models
import torch.nn.functional as F

import os,PIL,pathlib
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 # 分辨率

1. 设置GPU

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

2. 导入数据及处理部分

# 获取数据分布情况
path_dir = './data/bird_photos/'
path_dir = pathlib.Path(path_dir)

paths = list(path_dir.glob('*'))
# classNames = [str(path).split("\\")[-1] for path in paths] # ['Bananaquit', 'Black Skimmer', 'Black Throated Bushtiti', 'Cockatoo']
classNames = [path.parts[-1] for path in paths]
classNames
['Bananaquit', 'Black Skimmer', 'Black Throated Bushtiti', 'Cockatoo']
# 定义transforms 并处理数据
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],    # 其中 mean=[0.485,0.456,0.406]与std=[0.229,0.224,0.225] 从数据集中随机抽样计算得到的。
        std = [0.229,0.224,0.225]
    )
])
test_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 = train_transforms)
total_data
Dataset ImageFolder
    Number of datapoints: 565
    Root location: ./data/bird_photos/
    StandardTransform
Transform: Compose(
               Resize(size=[224, 224], interpolation=bilinear, max_size=None, antialias=True)
               RandomHorizontalFlip(p=0.5)
               ToTensor()
               Normalize(mean=[0.485, 0.456, 0.406], std=[0.229, 0.224, 0.225])
           )
total_data.class_to_idx
{'Bananaquit': 0,
 'Black Skimmer': 1,
 'Black Throated Bushtiti': 2,
 'Cockatoo': 3}

3. 划分数据集

# 划分数据集
train_size = int(len(total_data) * 0.8)
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 0x1d5973216c0>,
 <torch.utils.data.dataset.Subset at 0x1d5973201c0>)
# 定义DataLoader用于数据集的加载

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

4. 模型构建部分

import torch
import torch.nn as nn
import torchvision.models as models
import torchvision.transforms as transforms

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

        self.conv_shortcut = conv_shortcut
        self.stride = stride

        self.preact_bn = nn.BatchNorm2d(in_channels)
        self.preact_relu = nn.ReLU(inplace=True)

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

        self.conv1 = nn.Conv2d(in_channels, filters, kernel_size=1, stride=1, bias=False)
        self.bn1 = nn.BatchNorm2d(filters)
        self.relu1 = nn.ReLU(inplace=True)

        self.conv2 = nn.Conv2d(filters, filters, kernel_size=3, stride=stride, padding=1, bias=False)
        self.bn2 = nn.BatchNorm2d(filters)
        self.relu2 = nn.ReLU(inplace=True)

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

    def forward(self, x):
        preact = self.preact_bn(x)
        preact = self.preact_relu(preact)

        if self.conv_shortcut:
            shortcut = self.shortcut(preact)
        else:
            shortcut = x if self.stride == 1 else self.shortcut(x)

        x = self.conv1(preact)
        x = self.bn1(x)
        x = self.relu1(x)

        x = self.conv2(x)
        x = self.bn2(x)
        x = self.relu2(x)

        x = self.conv3(x)
        x += shortcut

        return x

class Stack2(nn.Module):
    def __init__(self, in_channels, filters, blocks, stride1=2):
        super(Stack2, self).__init__()
        self.blocks = nn.ModuleList()
        self.blocks.append(Block2(in_channels, filters, stride=stride1, conv_shortcut=True))
        for _ in range(1, blocks - 1):
            self.blocks.append(Block2(4 * filters, filters))
        self.blocks.append(Block2(4 * filters, filters, stride=1))

    def forward(self, x):
        for block in self.blocks:
            x = block(x)
        return x

class ResNet50V2(nn.Module):
    def __init__(self, num_classes=1000):
        super(ResNet50V2, self).__init__()
        self.conv1_pad = nn.ZeroPad2d(padding=(3, 3, 3, 3))
        self.conv1 = nn.Conv2d(3, 64, kernel_size=7, stride=2, bias=False)
        self.conv1_bn = nn.BatchNorm2d(64)
        self.conv1_relu = nn.ReLU(inplace=True)

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

        self.stack1 = Stack2(64, 64, 3)
        self.stack2 = Stack2(256, 128, 4)
        self.stack3 = Stack2(512, 256, 6)
        self.stack4 = Stack2(1024, 512, 3)

        self.post_bn = nn.BatchNorm2d(2048)
        self.post_relu = nn.ReLU(inplace=True)

        self.avgpool = nn.AdaptiveAvgPool2d((1, 1))
        self.fc = nn.Linear(2048, num_classes)

    def forward(self, x):
        x = self.conv1_pad(x)
        x = self.conv1(x)
        x = self.conv1_bn(x)
        x = self.conv1_relu(x)

        x = self.pool1_pad(x)
        x = self.pool1(x)

        x = self.stack1(x)
        x = self.stack2(x)
        x = self.stack3(x)
        x = self.stack4(x)

        x = self.post_bn(x)
        x = self.post_relu(x)

        x = self.avgpool(x)
        x = torch.flatten(x, 1)
        x = self.fc(x)
        return x

# Now, instantiate and use the model
model = ResNet50V2(num_classes=len(classNames))
model.to(device)
ResNet50V2(
  (conv1_pad): ZeroPad2d((3, 3, 3, 3))
  (conv1): Conv2d(3, 64, kernel_size=(7, 7), stride=(2, 2), bias=False)
  (conv1_bn): BatchNorm2d(64, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)
  (conv1_relu): ReLU(inplace=True)
  (pool1_pad): ZeroPad2d((1, 1, 1, 1))
  (pool1): MaxPool2d(kernel_size=3, stride=2, padding=0, dilation=1, ceil_mode=False)
  (stack1): Stack2(
    (blocks): ModuleList(
      (0): Block2(
        (preact_bn): BatchNorm2d(64, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)
        (preact_relu): ReLU(inplace=True)
        (shortcut): Conv2d(64, 256, kernel_size=(1, 1), stride=(2, 2))
        (conv1): Conv2d(64, 64, kernel_size=(1, 1), stride=(1, 1), bias=False)
        (bn1): BatchNorm2d(64, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)
        (relu1): ReLU(inplace=True)
        (conv2): Conv2d(64, 64, kernel_size=(3, 3), stride=(2, 2), padding=(1, 1), bias=False)
        (bn2): BatchNorm2d(64, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)
        (relu2): ReLU(inplace=True)
        (conv3): Conv2d(64, 256, kernel_size=(1, 1), stride=(1, 1))
      )
      (1-2): 2 x Block2(
        (preact_bn): BatchNorm2d(256, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)
        (preact_relu): ReLU(inplace=True)
        (conv1): Conv2d(256, 64, kernel_size=(1, 1), stride=(1, 1), bias=False)
        (bn1): BatchNorm2d(64, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)
        (relu1): ReLU(inplace=True)
        (conv2): Conv2d(64, 64, kernel_size=(3, 3), stride=(1, 1), padding=(1, 1), bias=False)
        (bn2): BatchNorm2d(64, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)
        (relu2): ReLU(inplace=True)
        (conv3): Conv2d(64, 256, kernel_size=(1, 1), stride=(1, 1))
      )
    )
  )
  (stack2): Stack2(
    (blocks): ModuleList(
      (0): Block2(
        (preact_bn): BatchNorm2d(256, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)
        (preact_relu): ReLU(inplace=True)
        (shortcut): Conv2d(256, 512, kernel_size=(1, 1), stride=(2, 2))
        (conv1): Conv2d(256, 128, kernel_size=(1, 1), stride=(1, 1), bias=False)
        (bn1): BatchNorm2d(128, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)
        (relu1): ReLU(inplace=True)
        (conv2): Conv2d(128, 128, kernel_size=(3, 3), stride=(2, 2), padding=(1, 1), bias=False)
        (bn2): BatchNorm2d(128, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)
        (relu2): ReLU(inplace=True)
        (conv3): Conv2d(128, 512, kernel_size=(1, 1), stride=(1, 1))
      )
      (1-3): 3 x Block2(
        (preact_bn): BatchNorm2d(512, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)
        (preact_relu): ReLU(inplace=True)
        (conv1): Conv2d(512, 128, kernel_size=(1, 1), stride=(1, 1), bias=False)
        (bn1): BatchNorm2d(128, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)
        (relu1): ReLU(inplace=True)
        (conv2): Conv2d(128, 128, kernel_size=(3, 3), stride=(1, 1), padding=(1, 1), bias=False)
        (bn2): BatchNorm2d(128, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)
        (relu2): ReLU(inplace=True)
        (conv3): Conv2d(128, 512, kernel_size=(1, 1), stride=(1, 1))
      )
    )
  )
  (stack3): Stack2(
    (blocks): ModuleList(
      (0): Block2(
        (preact_bn): BatchNorm2d(512, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)
        (preact_relu): ReLU(inplace=True)
        (shortcut): Conv2d(512, 1024, kernel_size=(1, 1), stride=(2, 2))
        (conv1): Conv2d(512, 256, kernel_size=(1, 1), stride=(1, 1), bias=False)
        (bn1): BatchNorm2d(256, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)
        (relu1): ReLU(inplace=True)
        (conv2): Conv2d(256, 256, kernel_size=(3, 3), stride=(2, 2), padding=(1, 1), bias=False)
        (bn2): BatchNorm2d(256, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)
        (relu2): ReLU(inplace=True)
        (conv3): Conv2d(256, 1024, kernel_size=(1, 1), stride=(1, 1))
      )
      (1-5): 5 x Block2(
        (preact_bn): BatchNorm2d(1024, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)
        (preact_relu): ReLU(inplace=True)
        (conv1): Conv2d(1024, 256, kernel_size=(1, 1), stride=(1, 1), bias=False)
        (bn1): BatchNorm2d(256, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)
        (relu1): ReLU(inplace=True)
        (conv2): Conv2d(256, 256, kernel_size=(3, 3), stride=(1, 1), padding=(1, 1), bias=False)
        (bn2): BatchNorm2d(256, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)
        (relu2): ReLU(inplace=True)
        (conv3): Conv2d(256, 1024, kernel_size=(1, 1), stride=(1, 1))
      )
    )
  )
  (stack4): Stack2(
    (blocks): ModuleList(
      (0): Block2(
        (preact_bn): BatchNorm2d(1024, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)
        (preact_relu): ReLU(inplace=True)
        (shortcut): Conv2d(1024, 2048, kernel_size=(1, 1), stride=(2, 2))
        (conv1): Conv2d(1024, 512, kernel_size=(1, 1), stride=(1, 1), bias=False)
        (bn1): BatchNorm2d(512, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)
        (relu1): ReLU(inplace=True)
        (conv2): Conv2d(512, 512, kernel_size=(3, 3), stride=(2, 2), padding=(1, 1), bias=False)
        (bn2): BatchNorm2d(512, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)
        (relu2): ReLU(inplace=True)
        (conv3): Conv2d(512, 2048, kernel_size=(1, 1), stride=(1, 1))
      )
      (1-2): 2 x Block2(
        (preact_bn): BatchNorm2d(2048, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)
        (preact_relu): ReLU(inplace=True)
        (conv1): Conv2d(2048, 512, kernel_size=(1, 1), stride=(1, 1), bias=False)
        (bn1): BatchNorm2d(512, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)
        (relu1): ReLU(inplace=True)
        (conv2): Conv2d(512, 512, kernel_size=(3, 3), stride=(1, 1), padding=(1, 1), bias=False)
        (bn2): BatchNorm2d(512, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)
        (relu2): ReLU(inplace=True)
        (conv3): Conv2d(512, 2048, kernel_size=(1, 1), stride=(1, 1))
      )
    )
  )
  (post_bn): BatchNorm2d(2048, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)
  (post_relu): ReLU(inplace=True)
  (avgpool): AdaptiveAvgPool2d(output_size=(1, 1))
  (fc): Linear(in_features=2048, out_features=4, bias=True)
)
# 查看模型详情
import torchsummary as summary
summary.summary(model,(3,224,224))
----------------------------------------------------------------
        Layer (type)               Output Shape         Param #
================================================================
         ZeroPad2d-1          [-1, 3, 230, 230]               0
            Conv2d-2         [-1, 64, 112, 112]           9,408
       BatchNorm2d-3         [-1, 64, 112, 112]             128
              ReLU-4         [-1, 64, 112, 112]               0
         ZeroPad2d-5         [-1, 64, 114, 114]               0
         MaxPool2d-6           [-1, 64, 56, 56]               0
       BatchNorm2d-7           [-1, 64, 56, 56]             128
              ReLU-8           [-1, 64, 56, 56]               0
            Conv2d-9          [-1, 256, 28, 28]          16,640
           Conv2d-10           [-1, 64, 56, 56]           4,096
      BatchNorm2d-11           [-1, 64, 56, 56]             128
             ReLU-12           [-1, 64, 56, 56]               0
           Conv2d-13           [-1, 64, 28, 28]          36,864
      BatchNorm2d-14           [-1, 64, 28, 28]             128
             ReLU-15           [-1, 64, 28, 28]               0
           Conv2d-16          [-1, 256, 28, 28]          16,640
           Block2-17          [-1, 256, 28, 28]               0
      BatchNorm2d-18          [-1, 256, 28, 28]             512
             ReLU-19          [-1, 256, 28, 28]               0
           Conv2d-20           [-1, 64, 28, 28]          16,384
      BatchNorm2d-21           [-1, 64, 28, 28]             128
             ReLU-22           [-1, 64, 28, 28]               0
           Conv2d-23           [-1, 64, 28, 28]          36,864
      BatchNorm2d-24           [-1, 64, 28, 28]             128
             ReLU-25           [-1, 64, 28, 28]               0
           Conv2d-26          [-1, 256, 28, 28]          16,640
           Block2-27          [-1, 256, 28, 28]               0
      BatchNorm2d-28          [-1, 256, 28, 28]             512
             ReLU-29          [-1, 256, 28, 28]               0
           Conv2d-30           [-1, 64, 28, 28]          16,384
      BatchNorm2d-31           [-1, 64, 28, 28]             128
             ReLU-32           [-1, 64, 28, 28]               0
           Conv2d-33           [-1, 64, 28, 28]          36,864
      BatchNorm2d-34           [-1, 64, 28, 28]             128
             ReLU-35           [-1, 64, 28, 28]               0
           Conv2d-36          [-1, 256, 28, 28]          16,640
           Block2-37          [-1, 256, 28, 28]               0
           Stack2-38          [-1, 256, 28, 28]               0
      BatchNorm2d-39          [-1, 256, 28, 28]             512
             ReLU-40          [-1, 256, 28, 28]               0
           Conv2d-41          [-1, 512, 14, 14]         131,584
           Conv2d-42          [-1, 128, 28, 28]          32,768
      BatchNorm2d-43          [-1, 128, 28, 28]             256
             ReLU-44          [-1, 128, 28, 28]               0
           Conv2d-45          [-1, 128, 14, 14]         147,456
      BatchNorm2d-46          [-1, 128, 14, 14]             256
             ReLU-47          [-1, 128, 14, 14]               0
           Conv2d-48          [-1, 512, 14, 14]          66,048
           Block2-49          [-1, 512, 14, 14]               0
      BatchNorm2d-50          [-1, 512, 14, 14]           1,024
             ReLU-51          [-1, 512, 14, 14]               0
           Conv2d-52          [-1, 128, 14, 14]          65,536
      BatchNorm2d-53          [-1, 128, 14, 14]             256
             ReLU-54          [-1, 128, 14, 14]               0
           Conv2d-55          [-1, 128, 14, 14]         147,456
      BatchNorm2d-56          [-1, 128, 14, 14]             256
             ReLU-57          [-1, 128, 14, 14]               0
           Conv2d-58          [-1, 512, 14, 14]          66,048
           Block2-59          [-1, 512, 14, 14]               0
      BatchNorm2d-60          [-1, 512, 14, 14]           1,024
             ReLU-61          [-1, 512, 14, 14]               0
           Conv2d-62          [-1, 128, 14, 14]          65,536
      BatchNorm2d-63          [-1, 128, 14, 14]             256
             ReLU-64          [-1, 128, 14, 14]               0
           Conv2d-65          [-1, 128, 14, 14]         147,456
      BatchNorm2d-66          [-1, 128, 14, 14]             256
             ReLU-67          [-1, 128, 14, 14]               0
           Conv2d-68          [-1, 512, 14, 14]          66,048
           Block2-69          [-1, 512, 14, 14]               0
      BatchNorm2d-70          [-1, 512, 14, 14]           1,024
             ReLU-71          [-1, 512, 14, 14]               0
           Conv2d-72          [-1, 128, 14, 14]          65,536
      BatchNorm2d-73          [-1, 128, 14, 14]             256
             ReLU-74          [-1, 128, 14, 14]               0
           Conv2d-75          [-1, 128, 14, 14]         147,456
      BatchNorm2d-76          [-1, 128, 14, 14]             256
             ReLU-77          [-1, 128, 14, 14]               0
           Conv2d-78          [-1, 512, 14, 14]          66,048
           Block2-79          [-1, 512, 14, 14]               0
           Stack2-80          [-1, 512, 14, 14]               0
      BatchNorm2d-81          [-1, 512, 14, 14]           1,024
             ReLU-82          [-1, 512, 14, 14]               0
           Conv2d-83           [-1, 1024, 7, 7]         525,312
           Conv2d-84          [-1, 256, 14, 14]         131,072
      BatchNorm2d-85          [-1, 256, 14, 14]             512
             ReLU-86          [-1, 256, 14, 14]               0
           Conv2d-87            [-1, 256, 7, 7]         589,824
      BatchNorm2d-88            [-1, 256, 7, 7]             512
             ReLU-89            [-1, 256, 7, 7]               0
           Conv2d-90           [-1, 1024, 7, 7]         263,168
           Block2-91           [-1, 1024, 7, 7]               0
      BatchNorm2d-92           [-1, 1024, 7, 7]           2,048
             ReLU-93           [-1, 1024, 7, 7]               0
           Conv2d-94            [-1, 256, 7, 7]         262,144
      BatchNorm2d-95            [-1, 256, 7, 7]             512
             ReLU-96            [-1, 256, 7, 7]               0
           Conv2d-97            [-1, 256, 7, 7]         589,824
      BatchNorm2d-98            [-1, 256, 7, 7]             512
             ReLU-99            [-1, 256, 7, 7]               0
          Conv2d-100           [-1, 1024, 7, 7]         263,168
          Block2-101           [-1, 1024, 7, 7]               0
     BatchNorm2d-102           [-1, 1024, 7, 7]           2,048
            ReLU-103           [-1, 1024, 7, 7]               0
          Conv2d-104            [-1, 256, 7, 7]         262,144
     BatchNorm2d-105            [-1, 256, 7, 7]             512
            ReLU-106            [-1, 256, 7, 7]               0
          Conv2d-107            [-1, 256, 7, 7]         589,824
     BatchNorm2d-108            [-1, 256, 7, 7]             512
            ReLU-109            [-1, 256, 7, 7]               0
          Conv2d-110           [-1, 1024, 7, 7]         263,168
          Block2-111           [-1, 1024, 7, 7]               0
     BatchNorm2d-112           [-1, 1024, 7, 7]           2,048
            ReLU-113           [-1, 1024, 7, 7]               0
          Conv2d-114            [-1, 256, 7, 7]         262,144
     BatchNorm2d-115            [-1, 256, 7, 7]             512
            ReLU-116            [-1, 256, 7, 7]               0
          Conv2d-117            [-1, 256, 7, 7]         589,824
     BatchNorm2d-118            [-1, 256, 7, 7]             512
            ReLU-119            [-1, 256, 7, 7]               0
          Conv2d-120           [-1, 1024, 7, 7]         263,168
          Block2-121           [-1, 1024, 7, 7]               0
     BatchNorm2d-122           [-1, 1024, 7, 7]           2,048
            ReLU-123           [-1, 1024, 7, 7]               0
          Conv2d-124            [-1, 256, 7, 7]         262,144
     BatchNorm2d-125            [-1, 256, 7, 7]             512
            ReLU-126            [-1, 256, 7, 7]               0
          Conv2d-127            [-1, 256, 7, 7]         589,824
     BatchNorm2d-128            [-1, 256, 7, 7]             512
            ReLU-129            [-1, 256, 7, 7]               0
          Conv2d-130           [-1, 1024, 7, 7]         263,168
          Block2-131           [-1, 1024, 7, 7]               0
     BatchNorm2d-132           [-1, 1024, 7, 7]           2,048
            ReLU-133           [-1, 1024, 7, 7]               0
          Conv2d-134            [-1, 256, 7, 7]         262,144
     BatchNorm2d-135            [-1, 256, 7, 7]             512
            ReLU-136            [-1, 256, 7, 7]               0
          Conv2d-137            [-1, 256, 7, 7]         589,824
     BatchNorm2d-138            [-1, 256, 7, 7]             512
            ReLU-139            [-1, 256, 7, 7]               0
          Conv2d-140           [-1, 1024, 7, 7]         263,168
          Block2-141           [-1, 1024, 7, 7]               0
          Stack2-142           [-1, 1024, 7, 7]               0
     BatchNorm2d-143           [-1, 1024, 7, 7]           2,048
            ReLU-144           [-1, 1024, 7, 7]               0
          Conv2d-145           [-1, 2048, 4, 4]       2,099,200
          Conv2d-146            [-1, 512, 7, 7]         524,288
     BatchNorm2d-147            [-1, 512, 7, 7]           1,024
            ReLU-148            [-1, 512, 7, 7]               0
          Conv2d-149            [-1, 512, 4, 4]       2,359,296
     BatchNorm2d-150            [-1, 512, 4, 4]           1,024
            ReLU-151            [-1, 512, 4, 4]               0
          Conv2d-152           [-1, 2048, 4, 4]       1,050,624
          Block2-153           [-1, 2048, 4, 4]               0
     BatchNorm2d-154           [-1, 2048, 4, 4]           4,096
            ReLU-155           [-1, 2048, 4, 4]               0
          Conv2d-156            [-1, 512, 4, 4]       1,048,576
     BatchNorm2d-157            [-1, 512, 4, 4]           1,024
            ReLU-158            [-1, 512, 4, 4]               0
          Conv2d-159            [-1, 512, 4, 4]       2,359,296
     BatchNorm2d-160            [-1, 512, 4, 4]           1,024
            ReLU-161            [-1, 512, 4, 4]               0
          Conv2d-162           [-1, 2048, 4, 4]       1,050,624
          Block2-163           [-1, 2048, 4, 4]               0
     BatchNorm2d-164           [-1, 2048, 4, 4]           4,096
            ReLU-165           [-1, 2048, 4, 4]               0
          Conv2d-166            [-1, 512, 4, 4]       1,048,576
     BatchNorm2d-167            [-1, 512, 4, 4]           1,024
            ReLU-168            [-1, 512, 4, 4]               0
          Conv2d-169            [-1, 512, 4, 4]       2,359,296
     BatchNorm2d-170            [-1, 512, 4, 4]           1,024
            ReLU-171            [-1, 512, 4, 4]               0
          Conv2d-172           [-1, 2048, 4, 4]       1,050,624
          Block2-173           [-1, 2048, 4, 4]               0
          Stack2-174           [-1, 2048, 4, 4]               0
     BatchNorm2d-175           [-1, 2048, 4, 4]           4,096
            ReLU-176           [-1, 2048, 4, 4]               0
AdaptiveAvgPool2d-177           [-1, 2048, 1, 1]               0
          Linear-178                    [-1, 4]           8,196
================================================================
Total params: 23,527,620
Trainable params: 23,527,620
Non-trainable params: 0
----------------------------------------------------------------
Input size (MB): 0.57
Forward/backward pass size (MB): 101.68
Params size (MB): 89.75
Estimated Total Size (MB): 192.01
----------------------------------------------------------------

5. 设置超参数:定义损失函数,学习率,以及根据学习率定义优化器等

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

# learn_rate = 1e-3 # 初始学习率
# def adjust_learning_rate(optimizer,epoch,start_lr):
#     # 每两个epoch 衰减到原来的0.98
#     lr = start_lr * (0.92 ** (epoch//2))
#     for param_group in optimizer.param_groups:
#         param_group['lr'] = lr
        
# optimizer = torch.optim.Adam(model.parameters(),lr=learn_rate)
# 调用官方接口示例
loss_fn = nn.CrossEntropyLoss()

learn_rate = 1e-4
lambda1 = lambda epoch:(0.92**(epoch//2))

optimizer = torch.optim.Adam(model.parameters(),lr = learn_rate)
scheduler = torch.optim.lr_scheduler.LambdaLR(optimizer,lr_lambda=lambda1) # 选定调整方法

6. 训练函数

# 训练函数
def train(dataloader,model,loss_fn,optimizer):
    size = len(dataloader.dataset) # 训练集大小
    num_batches = len(dataloader) # 批次数目
    
    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)
        
        # 反向传播
        optimizer.zero_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

7. 测试函数

# 测试函数
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)
            
            # 计算loss
            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

8. 正式训练

import copy

epochs = 40

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

best_acc = 0.0

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

print('Done')
Epoch: 1,Train_acc:48.0%,Train_loss:1.221,Test_acc:19.5%,Test_loss:1.435,Lr:1.00E-04
Epoch: 2,Train_acc:72.1%,Train_loss:0.746,Test_acc:36.3%,Test_loss:1.754,Lr:9.20E-05
Epoch: 3,Train_acc:85.2%,Train_loss:0.453,Test_acc:74.3%,Test_loss:0.690,Lr:9.20E-05
Epoch: 4,Train_acc:90.9%,Train_loss:0.288,Test_acc:71.7%,Test_loss:1.046,Lr:8.46E-05
Epoch: 5,Train_acc:93.1%,Train_loss:0.236,Test_acc:73.5%,Test_loss:1.107,Lr:8.46E-05
Epoch: 6,Train_acc:94.5%,Train_loss:0.162,Test_acc:72.6%,Test_loss:0.840,Lr:7.79E-05
Epoch: 7,Train_acc:96.5%,Train_loss:0.268,Test_acc:76.1%,Test_loss:0.703,Lr:7.79E-05
Epoch: 8,Train_acc:94.2%,Train_loss:0.234,Test_acc:78.8%,Test_loss:0.803,Lr:7.16E-05
Epoch: 9,Train_acc:94.5%,Train_loss:0.163,Test_acc:69.0%,Test_loss:1.384,Lr:7.16E-05
Epoch:10,Train_acc:94.9%,Train_loss:0.172,Test_acc:78.8%,Test_loss:0.606,Lr:6.59E-05
Epoch:11,Train_acc:96.7%,Train_loss:0.125,Test_acc:76.1%,Test_loss:0.757,Lr:6.59E-05
Epoch:12,Train_acc:97.6%,Train_loss:0.074,Test_acc:85.8%,Test_loss:0.452,Lr:6.06E-05
Epoch:13,Train_acc:97.8%,Train_loss:0.087,Test_acc:81.4%,Test_loss:0.592,Lr:6.06E-05
Epoch:14,Train_acc:98.0%,Train_loss:0.089,Test_acc:80.5%,Test_loss:0.617,Lr:5.58E-05
Epoch:15,Train_acc:95.4%,Train_loss:0.133,Test_acc:71.7%,Test_loss:1.433,Lr:5.58E-05
Epoch:16,Train_acc:97.6%,Train_loss:0.074,Test_acc:77.0%,Test_loss:0.772,Lr:5.13E-05
Epoch:17,Train_acc:98.5%,Train_loss:0.101,Test_acc:80.5%,Test_loss:0.843,Lr:5.13E-05
Epoch:18,Train_acc:97.8%,Train_loss:0.072,Test_acc:69.9%,Test_loss:1.233,Lr:4.72E-05
Epoch:19,Train_acc:98.5%,Train_loss:0.079,Test_acc:81.4%,Test_loss:0.866,Lr:4.72E-05
Epoch:20,Train_acc:97.6%,Train_loss:0.070,Test_acc:79.6%,Test_loss:0.767,Lr:4.34E-05
Epoch:21,Train_acc:98.0%,Train_loss:0.356,Test_acc:78.8%,Test_loss:0.836,Lr:4.34E-05
Epoch:22,Train_acc:96.2%,Train_loss:0.126,Test_acc:78.8%,Test_loss:0.697,Lr:4.00E-05
Epoch:23,Train_acc:99.1%,Train_loss:0.071,Test_acc:78.8%,Test_loss:0.655,Lr:4.00E-05
Epoch:24,Train_acc:97.8%,Train_loss:0.068,Test_acc:84.1%,Test_loss:0.527,Lr:3.68E-05
Epoch:25,Train_acc:98.2%,Train_loss:0.115,Test_acc:77.0%,Test_loss:0.790,Lr:3.68E-05
Epoch:26,Train_acc:98.0%,Train_loss:0.138,Test_acc:80.5%,Test_loss:0.657,Lr:3.38E-05
Epoch:27,Train_acc:98.0%,Train_loss:0.154,Test_acc:83.2%,Test_loss:0.536,Lr:3.38E-05
Epoch:28,Train_acc:98.9%,Train_loss:0.046,Test_acc:80.5%,Test_loss:0.576,Lr:3.11E-05
Epoch:29,Train_acc:98.7%,Train_loss:0.086,Test_acc:81.4%,Test_loss:0.569,Lr:3.11E-05
Epoch:30,Train_acc:99.8%,Train_loss:0.039,Test_acc:77.9%,Test_loss:0.906,Lr:2.86E-05
Epoch:31,Train_acc:99.1%,Train_loss:0.041,Test_acc:83.2%,Test_loss:0.521,Lr:2.86E-05
Epoch:32,Train_acc:99.3%,Train_loss:0.026,Test_acc:84.1%,Test_loss:0.510,Lr:2.63E-05
Epoch:33,Train_acc:99.8%,Train_loss:0.028,Test_acc:79.6%,Test_loss:0.566,Lr:2.63E-05
Epoch:34,Train_acc:99.8%,Train_loss:0.026,Test_acc:81.4%,Test_loss:0.553,Lr:2.42E-05
Epoch:35,Train_acc:98.5%,Train_loss:0.159,Test_acc:77.9%,Test_loss:0.684,Lr:2.42E-05
Epoch:36,Train_acc:99.3%,Train_loss:0.048,Test_acc:81.4%,Test_loss:0.591,Lr:2.23E-05
Epoch:37,Train_acc:99.3%,Train_loss:0.064,Test_acc:83.2%,Test_loss:0.509,Lr:2.23E-05
Epoch:38,Train_acc:99.1%,Train_loss:0.131,Test_acc:86.7%,Test_loss:0.597,Lr:2.05E-05
Epoch:39,Train_acc:99.1%,Train_loss:0.045,Test_acc:83.2%,Test_loss:0.652,Lr:2.05E-05
Epoch:40,Train_acc:99.8%,Train_loss:0.083,Test_acc:80.5%,Test_loss:0.627,Lr:1.89E-05
Done

9. 结果可视化

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 = 'Test Accuracy')
plt.plot(epochs_range,test_loss,label = 'Test Loss')
plt.legend(loc = 'lower right')
plt.title('Training and validation Loss')
plt.show()

在这里插入图片描述

10. 模型的保存

# 自定义模型保存
# 状态字典保存
torch.save(model.state_dict(),'./模型参数/J2_resnet50v2_model_state_dict.pth') # 仅保存状态字典

# 加载状态字典到模型
best_model = ResNet50V2(num_classes=len(classNames)).to(device) # 定义官方vgg16模型用来加载参数

best_model.load_state_dict(torch.load('./模型参数/J2_resnet50v2_model_state_dict.pth')) # 加载状态字典到模型
<All keys matched successfully>

11. 使用训练好的模型进行预测

# 指定路径图片预测
from PIL import Image
import torchvision.transforms as transforms

classes = list(total_data.class_to_idx) # 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)
    print(output) # 观察模型预测结果的输出数据
    
    _,pred = torch.max(output,1)
    pred_class = classes[pred]
    print(f'预测结果是:{pred_class}')
# 预测训练集中的某张照片
predict_one_image(image_path='./data/bird_photos/Bananaquit/007.jpg',
                 model = model,
                 transform = test_transforms,
                 classes = classes
                 )
tensor([[ 8.8948, -4.9875,  1.8381, -6.7715]], device='cuda:0',
       grad_fn=<AddmmBackward0>)
预测结果是:Bananaquit
classes
['Bananaquit', 'Black Skimmer', 'Black Throated Bushtiti', 'Cockatoo']

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值