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

news2024/11/23 9:01:23
  • 🍨 本文为🔗365天深度学习训练营 中的学习记录博客
  • 🍖 原作者:K同学啊 | 接辅导、项目定制

目录

      • 0. 总结
      • 1. 设置GPU
      • 2. 导入数据及处理部分
      • 3. 划分数据集
      • 4. 模型构建部分
      • 5. 设置超参数:定义损失函数,学习率,以及根据学习率定义优化器等
      • 6. 训练函数
      • 7. 测试函数
      • 8. 正式训练
      • 9. 结果可视化
      • 10. 模型的保存
      • 11. 使用训练好的模型进行预测

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']

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.coloradmin.cn/o/2203554.html

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈,一经查实,立即删除!

相关文章

Web开发:总结常见的批处理脚本(.bat)

一、一键复制多个文件 echo off setlocalset source01.pngcopy "%source%" "a.png" copy "%source%" "b.png" copy "%source%" "c.png"endlocal说明&#xff1a; 将上述代码复制到一个新的文本文件中。将文件保…

4.STM32-中断

STM32-中断 需求&#xff1a;红灯每两秒进行闪烁&#xff0c;按键key1控制绿灯亮灭 简单的程序代码无法满足要求 如何让STM32既能执行HAL_DELAY这种耗时的任务&#xff0c;同时又能快速响应按键按下这种突发情况呢 设置中断步骤 1.接入中断 将KEY1输入模式由原先的GPIO_In…

React学习02 -事件处理、生命周期和diffing算法

文章目录 react事件处理非受控组件受控组件高阶函数函数柯里化 生命周期引出生命周期旧版生命周期新版生命周期 Diffing算法 react事件处理 1.react通过onXXX属性指定事件处理函数&#xff0c; a.react使用的是自定义事件&#xff0c;将原生js事件方法重写并改为小驼峰写法&am…

大数据新视界 --大数据大厂之大数据驱动下的物流供应链优化:实时追踪与智能调配

&#x1f496;&#x1f496;&#x1f496;亲爱的朋友们&#xff0c;热烈欢迎你们来到 青云交的博客&#xff01;能与你们在此邂逅&#xff0c;我满心欢喜&#xff0c;深感无比荣幸。在这个瞬息万变的时代&#xff0c;我们每个人都在苦苦追寻一处能让心灵安然栖息的港湾。而 我的…

前端自定义指令控制权限(后端Spring Security)

1. 新建 directives/auth.ts &#xfeff; &#xfeff; //导入自定义指令 import auth from /directives/auth// 注册全局自定义指令 v-auth app.directive(auth, auth);&#xfeff;1.1完整的authDirective.ts import { wmsStore } from "/store/pinia"// 判断用…

dmdfm5安装部署

dmdfm5安装部署 1 环境说明2 命令行安装dmfdm52.1 创建 dmdba 用户2.2 命令行安装 dmdfm2.3 配置自启动脚本服务2.4 web端 访问 dmdfm 3 安装过程错误记录4 更多达梦数据库学习使用列表 1 环境说明 cpu x86OS 麒麟v10(sp2)dmfdm5 版本 : dmdfm_V5.0.1.1_rev157137_x86_linux_6…

计算机网络803-(4)网络层

目录 1.虚电路服务 虚电路是逻辑连接 2.数据报服务 3.虚电路服务与数据报服务的对比 二.虚拟互连网络-IP网 1.网络通信问题 2.中间设备 3.网络互连使用路由器 三.分类的 IP 地址 1. IP 地址及其表示方法 2.IP 地址的编址方法 3.分类 IP 地址 &#xff08;1&#x…

双通讯直流电能计量装置功能介绍

DJSF1352系列电子式直流电能表是为满足现代直流电力计量需求而设计的高性能设备。其主要特点包括液晶显示和RS485通讯功能&#xff0c;方便与微机进行数据交互&#xff0c;适用于充电桩、蓄电池、太阳能电池板等多种直流信号设备的电量监测。该产品由测量单元、数据处理单元、通…

python数学运算库numpy的使用

数组 numpy创建数组的方法 可以用np.array()将一个列表作为参数 import numpy as npd1 np.array(range(1,7))print(d1) # 输出数据 print(d1.size) # 输出元素个数 print(d1.ndim) # 输出数组维度 print(d1.shape) # 输出数组形状&#xff08;长宽高&#xff09; 可以…

pdf合并成一个文件,揭秘四款好用软件!

在这个数字化时代&#xff0c;PDF文件已成为我们工作、学习和生活中不可或缺的一部分。无论是报告、合同、还是学术论文&#xff0c;PDF以其跨平台兼容性和良好的格式保持性赢得了广泛青睐。然而&#xff0c;面对多个PDF文件需要合并成一个完整文档时&#xff0c;你是否也曾感到…

对于JS脚本加标签功能的一些小理解

在JS中加标签&#xff0c;最主要的应用场景就是结合循环代码使用。用标签标识循环或者代码块&#xff0c;以便使用break 和 continue语句来结束循环。个人觉得标签加循环的本质作用是为了增加性能&#xff0c;减少运行代码行&#xff0c;以便提速。示例如下&#xff1a; 打印输…

Leetcode.20 有效的括号

关键词&#xff1a;vector, push_back(), pop_back(), stack, push(), pop(), top() 1.题目 2.解答思路及解答 解答思路&#xff1a; 左括号需要一个相同的括号&#xff0c;如果是括号嵌套的方式&#xff0c;可以类比“回文数”那题&#xff0c;利用双下标或者双指针遍历。 …

shell 脚本批量更新本地git仓库

文章目录 一、问题概述二、解决方法三、运行效果1. windows2. centos 一、问题概述 你是否遇到这样的场景&#xff1a; 本地git仓库克隆了线上的多个项目&#xff0c;需要更新时&#xff0c;无法象svn一样&#xff0c;选中多个项目一起更新。 只能苦逼的一个个选中&#xff0c…

【解决办法】git clone报错unable to access ‘xxx‘: SSL certificate problem

git clone 是 Git 版本控制系统中的一个基本命令&#xff0c;用于从远程仓库复制一个完整的版本库到本地。这个命令不仅复制远程仓库中的所有文件&#xff0c;还复制仓库的历史记录&#xff0c;使得你可以在本地进行版本控制操作&#xff0c;如提交&#xff08;commit&#xff…

YOLO11改进|SPPF篇|引入YOLOv9提出的SPPELAN模块

目录 一、【SPPELAN】模块1.1【SPPELAN】模块介绍1.2【SPPELAN】核心代码 二、添加【SPPELAN】模块2.1STEP12.2STEP22.3STEP32.4STEP4 三、yaml文件与运行3.1yaml文件3.2运行成功截图 一、【SPPELAN】模块 1.1【SPPELAN】模块介绍 下图是【SPPELAN】的结构图&#xff0c;让我们…

AI产品经理面试100问,三天看完一周拿5个offer

Attention(重点掌握) 1.什么是 Attention?为什么要用 Attention?它有什么作用? 2.Attention的流程是什么样的? 3.普通的Attention和Transformer的Self-attention之间有什么关系? 4.什么是Self-attention? Transformer(重点掌握) 1.Transformer是什么&#xff0c;它的基…

socket编程(java)

socket编程&#xff08;java&#xff09; 简介 ​ Socket&#xff08;套接字&#xff09;是计算机网络编程中用于实现网络通信的一种机制。它提供了一种编程接口&#xff0c;允许应用程序通过网络进行数据传输&#xff0c;实现不同主机之间的通信。 ​ Socket可以看作是一种…

多态相关问题

多态 1、概念的概念 通俗来讲&#xff0c;就是多种形态。具体点就是去完成某个行为&#xff0c;当不同的对象去完成时会产生出不同的状态。 例子&#xff1a;新用户 领红包 99 老用户 领红包 2 不常用 领红包 6 符合多态条件&#xff1a; #include <iostream> using …

计算机毕业设计 医院预约挂号系统的设计与实现 Python毕业设计 Python毕业设计选题【附源码+安装调试】

博主介绍&#xff1a;✌从事软件开发10年之余&#xff0c;专注于Java技术领域、Python人工智能及数据挖掘、小程序项目开发和Android项目开发等。CSDN、掘金、华为云、InfoQ、阿里云等平台优质作者✌ &#x1f345;文末获取源码联系&#x1f345; &#x1f447;&#x1f3fb; 精…

力扣之1364.顾客的可信联系人数量

题目&#xff1a; sql建表语句&#xff1a; Create table If Not Exists Customers (customer_id int, customer_name varchar(20), email varchar(30)); Create table If Not Exists Contacts (user_id int, contact_name varchar(20), contact_email varchar(30)); Cre…