J8学习打卡笔记

news/2024/12/20 19:55:20/
  • 🍨 本文为🔗365天深度学习训练营 中的学习记录博客
  • 🍖 原作者:K同学啊

Inception v1算法实战与解析

  • 导入数据
  • 数据预处理
  • 划分数据集
  • 搭建模型
  • 训练模型
  • 正式训练
  • 结果可视化
  • 详细网络结构图
  • 个人总结

import os, PIL, random, pathlib
import torch
import torch.nn as nn
import torchvision.transforms as transforms
import torchvision
from torchvision import transforms, datasets
import torch.nn.functional as Fdevice = torch.device("cuda" if torch.cuda.is_available() else "cpu")device
device(type='cuda')

导入数据


data_dir = r'C:\Users\11054\Desktop\kLearning\p4_learning\data'
data_dir = pathlib.Path(data_dir)data_paths = list(data_dir.glob('*'))
classeNames = [str(path).split("\\")[-1] for path in data_paths]
print(classeNames)image_count = len(list(data_dir.glob('*/*')))
print("图片总数为:", image_count)
['Monkeypox', 'Others']
图片总数为: 2142

数据预处理


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] 从数据集中随机抽样计算得到的。
])test_transform = transforms.Compose([transforms.Resize([224, 224]),  # 将输入图片resize成统一尺寸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(data_dir, transform=train_transforms)
print(total_data.class_to_idx)
{'Monkeypox': 0, 'Others': 1}

划分数据集


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])batch_size = 8        #根据自己的显卡,选择合适的batch_size大小
train_dl = torch.utils.data.DataLoader(train_dataset,batch_size=batch_size,shuffle=True,num_workers=0)
test_dl = torch.utils.data.DataLoader(test_dataset,batch_size=batch_size,shuffle=True,num_workers=0)
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([8, 3, 224, 224])
Shape of y:  torch.Size([8]) torch.int64

搭建模型


class inception_block(nn.Module):def __init__(self, in_channels, ch1x1, ch3x3red, ch3x3, ch5x5red, ch5x5, pool_proj):super(inception_block, self).__init__()# 1x1 conv branchself.branch1 = nn.Sequential(nn.Conv2d(in_channels, ch1x1, kernel_size=1),nn.BatchNorm2d(ch1x1),nn.ReLU(inplace=True))# 1x1 conv -> 3x3 conv branchself.branch2 = nn.Sequential(nn.Conv2d(in_channels, ch3x3red, kernel_size=1),nn.BatchNorm2d(ch3x3red),nn.ReLU(inplace=True),nn.Conv2d(ch3x3red, ch3x3, kernel_size=3, padding=1),nn.BatchNorm2d(ch3x3),nn.ReLU(inplace=True))# 1x1 conv -> 5x5 conv branchself.branch3 = nn.Sequential(nn.Conv2d(in_channels, ch5x5red, kernel_size=1),nn.BatchNorm2d(ch5x5red),nn.ReLU(inplace=True),nn.Conv2d(ch5x5red, ch5x5, kernel_size=5, padding=2),nn.BatchNorm2d(ch5x5),nn.ReLU(inplace=True))# 3x3 max pooling -> 1x1 conv branchself.branch4 = nn.Sequential(nn.MaxPool2d(kernel_size=3, stride=1, padding=1),nn.Conv2d(in_channels, pool_proj, kernel_size=1),nn.BatchNorm2d(pool_proj),nn.ReLU(inplace=True))def forward(self, x):# Compute forward pass through all branches and concatenate the output feature mapsbranch1_output = self.branch1(x)branch2_output = self.branch2(x)branch3_output = self.branch3(x)branch4_output = self.branch4(x)outputs = [branch1_output, branch2_output, branch3_output, branch4_output]return torch.cat(outputs, 1)
class InceptionV1(nn.Module):def __init__(self, num_classes=1000):super(InceptionV1, self).__init__()self.conv1 = nn.Conv2d(3, 64, kernel_size=7, stride=2, padding=3)self.maxpool1 = nn.MaxPool2d(kernel_size=3, stride=2, padding=1)self.conv2 = nn.Conv2d(64, 64, kernel_size=1, stride=1, padding=0)self.conv3 = nn.Conv2d(64, 192, kernel_size=3, stride=1, padding=1)self.maxpool2 = nn.MaxPool2d(kernel_size=3, stride=2, padding=1)self.inception3a = inception_block(192, 64, 96, 128, 16, 32, 32)self.inception3b = inception_block(256, 128, 128, 192, 32, 96, 64)self.maxpool3    = nn.MaxPool2d(kernel_size=3, stride=2, padding=1)self.inception4a = inception_block(480, 192, 96, 208, 16, 48, 64)self.inception4b = inception_block(512, 160, 112, 224, 24, 64, 64)self.inception4c = inception_block(512, 128, 128, 256, 24, 64, 64)self.inception4d = inception_block(512, 112, 144, 288, 32, 64, 64)self.inception4e = inception_block(528, 256, 160, 320, 32, 128, 128)self.maxpool4    = nn.MaxPool2d(kernel_size=3, stride=2, padding=1)self.inception5a = inception_block(832, 256, 160, 320, 32, 128, 128)self.inception5b=nn.Sequential(inception_block(832, 384, 192, 384, 48, 128, 128),nn.AvgPool2d(kernel_size=7,stride=1,padding=0),nn.Dropout(0.4))# 全连接网络层,用于分类self.classifier = nn.Sequential(nn.Linear(in_features=1024, out_features=1024),nn.ReLU(),nn.Linear(in_features=1024, out_features=num_classes),nn.Softmax(dim=1))def forward(self, x):x = self.conv1(x)x = F.relu(x)x = self.maxpool1(x)x = self.conv2(x)x = F.relu(x)x = self.conv3(x)x = F.relu(x)x = self.maxpool2(x)x = self.inception3a(x)x = self.inception3b(x)x = self.maxpool3(x)x = self.inception4a(x)x = self.inception4b(x)x = self.inception4c(x)x = self.inception4d(x)x = self.inception4e(x)x = self.maxpool4(x)x = self.inception5a(x)x = self.inception5b(x)x = torch.flatten(x, start_dim=1)x = self.classifier(x)return x
# 统计模型参数量以及其他指标
import torchsummary# 调用并将模型转移到GPU中
model = InceptionV1(num_classes=2).to(device)# 显示网络结构
torchsummary.summary(model, (3, 224, 224))
print(model)
----------------------------------------------------------------Layer (type)               Output Shape         Param #
================================================================Conv2d-1         [-1, 64, 112, 112]           9,472MaxPool2d-2           [-1, 64, 56, 56]               0Conv2d-3           [-1, 64, 56, 56]           4,160Conv2d-4          [-1, 192, 56, 56]         110,784MaxPool2d-5          [-1, 192, 28, 28]               0Conv2d-6           [-1, 64, 28, 28]          12,352BatchNorm2d-7           [-1, 64, 28, 28]             128ReLU-8           [-1, 64, 28, 28]               0Conv2d-9           [-1, 96, 28, 28]          18,528BatchNorm2d-10           [-1, 96, 28, 28]             192ReLU-11           [-1, 96, 28, 28]               0Conv2d-12          [-1, 128, 28, 28]         110,720BatchNorm2d-13          [-1, 128, 28, 28]             256ReLU-14          [-1, 128, 28, 28]               0Conv2d-15           [-1, 16, 28, 28]           3,088BatchNorm2d-16           [-1, 16, 28, 28]              32ReLU-17           [-1, 16, 28, 28]               0Conv2d-18           [-1, 32, 28, 28]          12,832BatchNorm2d-19           [-1, 32, 28, 28]              64ReLU-20           [-1, 32, 28, 28]               0MaxPool2d-21          [-1, 192, 28, 28]               0Conv2d-22           [-1, 32, 28, 28]           6,176BatchNorm2d-23           [-1, 32, 28, 28]              64ReLU-24           [-1, 32, 28, 28]               0inception_block-25          [-1, 256, 28, 28]               0Conv2d-26          [-1, 128, 28, 28]          32,896BatchNorm2d-27          [-1, 128, 28, 28]             256ReLU-28          [-1, 128, 28, 28]               0Conv2d-29          [-1, 128, 28, 28]          32,896BatchNorm2d-30          [-1, 128, 28, 28]             256ReLU-31          [-1, 128, 28, 28]               0Conv2d-32          [-1, 192, 28, 28]         221,376BatchNorm2d-33          [-1, 192, 28, 28]             384ReLU-34          [-1, 192, 28, 28]               0Conv2d-35           [-1, 32, 28, 28]           8,224BatchNorm2d-36           [-1, 32, 28, 28]              64ReLU-37           [-1, 32, 28, 28]               0Conv2d-38           [-1, 96, 28, 28]          76,896BatchNorm2d-39           [-1, 96, 28, 28]             192ReLU-40           [-1, 96, 28, 28]               0MaxPool2d-41          [-1, 256, 28, 28]               0Conv2d-42           [-1, 64, 28, 28]          16,448BatchNorm2d-43           [-1, 64, 28, 28]             128ReLU-44           [-1, 64, 28, 28]               0inception_block-45          [-1, 480, 28, 28]               0MaxPool2d-46          [-1, 480, 14, 14]               0Conv2d-47          [-1, 192, 14, 14]          92,352BatchNorm2d-48          [-1, 192, 14, 14]             384ReLU-49          [-1, 192, 14, 14]               0Conv2d-50           [-1, 96, 14, 14]          46,176BatchNorm2d-51           [-1, 96, 14, 14]             192ReLU-52           [-1, 96, 14, 14]               0Conv2d-53          [-1, 208, 14, 14]         179,920BatchNorm2d-54          [-1, 208, 14, 14]             416ReLU-55          [-1, 208, 14, 14]               0Conv2d-56           [-1, 16, 14, 14]           7,696BatchNorm2d-57           [-1, 16, 14, 14]              32ReLU-58           [-1, 16, 14, 14]               0Conv2d-59           [-1, 48, 14, 14]          19,248BatchNorm2d-60           [-1, 48, 14, 14]              96ReLU-61           [-1, 48, 14, 14]               0MaxPool2d-62          [-1, 480, 14, 14]               0Conv2d-63           [-1, 64, 14, 14]          30,784BatchNorm2d-64           [-1, 64, 14, 14]             128ReLU-65           [-1, 64, 14, 14]               0inception_block-66          [-1, 512, 14, 14]               0Conv2d-67          [-1, 160, 14, 14]          82,080BatchNorm2d-68          [-1, 160, 14, 14]             320ReLU-69          [-1, 160, 14, 14]               0Conv2d-70          [-1, 112, 14, 14]          57,456BatchNorm2d-71          [-1, 112, 14, 14]             224ReLU-72          [-1, 112, 14, 14]               0Conv2d-73          [-1, 224, 14, 14]         226,016BatchNorm2d-74          [-1, 224, 14, 14]             448ReLU-75          [-1, 224, 14, 14]               0Conv2d-76           [-1, 24, 14, 14]          12,312BatchNorm2d-77           [-1, 24, 14, 14]              48ReLU-78           [-1, 24, 14, 14]               0Conv2d-79           [-1, 64, 14, 14]          38,464BatchNorm2d-80           [-1, 64, 14, 14]             128ReLU-81           [-1, 64, 14, 14]               0MaxPool2d-82          [-1, 512, 14, 14]               0Conv2d-83           [-1, 64, 14, 14]          32,832BatchNorm2d-84           [-1, 64, 14, 14]             128ReLU-85           [-1, 64, 14, 14]               0inception_block-86          [-1, 512, 14, 14]               0Conv2d-87          [-1, 128, 14, 14]          65,664BatchNorm2d-88          [-1, 128, 14, 14]             256ReLU-89          [-1, 128, 14, 14]               0Conv2d-90          [-1, 128, 14, 14]          65,664BatchNorm2d-91          [-1, 128, 14, 14]             256ReLU-92          [-1, 128, 14, 14]               0Conv2d-93          [-1, 256, 14, 14]         295,168BatchNorm2d-94          [-1, 256, 14, 14]             512ReLU-95          [-1, 256, 14, 14]               0Conv2d-96           [-1, 24, 14, 14]          12,312BatchNorm2d-97           [-1, 24, 14, 14]              48ReLU-98           [-1, 24, 14, 14]               0Conv2d-99           [-1, 64, 14, 14]          38,464BatchNorm2d-100           [-1, 64, 14, 14]             128ReLU-101           [-1, 64, 14, 14]               0MaxPool2d-102          [-1, 512, 14, 14]               0Conv2d-103           [-1, 64, 14, 14]          32,832BatchNorm2d-104           [-1, 64, 14, 14]             128ReLU-105           [-1, 64, 14, 14]               0inception_block-106          [-1, 512, 14, 14]               0Conv2d-107          [-1, 112, 14, 14]          57,456BatchNorm2d-108          [-1, 112, 14, 14]             224ReLU-109          [-1, 112, 14, 14]               0Conv2d-110          [-1, 144, 14, 14]          73,872BatchNorm2d-111          [-1, 144, 14, 14]             288ReLU-112          [-1, 144, 14, 14]               0Conv2d-113          [-1, 288, 14, 14]         373,536BatchNorm2d-114          [-1, 288, 14, 14]             576ReLU-115          [-1, 288, 14, 14]               0Conv2d-116           [-1, 32, 14, 14]          16,416BatchNorm2d-117           [-1, 32, 14, 14]              64ReLU-118           [-1, 32, 14, 14]               0Conv2d-119           [-1, 64, 14, 14]          51,264BatchNorm2d-120           [-1, 64, 14, 14]             128ReLU-121           [-1, 64, 14, 14]               0MaxPool2d-122          [-1, 512, 14, 14]               0Conv2d-123           [-1, 64, 14, 14]          32,832BatchNorm2d-124           [-1, 64, 14, 14]             128ReLU-125           [-1, 64, 14, 14]               0inception_block-126          [-1, 528, 14, 14]               0Conv2d-127          [-1, 256, 14, 14]         135,424BatchNorm2d-128          [-1, 256, 14, 14]             512ReLU-129          [-1, 256, 14, 14]               0Conv2d-130          [-1, 160, 14, 14]          84,640BatchNorm2d-131          [-1, 160, 14, 14]             320ReLU-132          [-1, 160, 14, 14]               0Conv2d-133          [-1, 320, 14, 14]         461,120BatchNorm2d-134          [-1, 320, 14, 14]             640ReLU-135          [-1, 320, 14, 14]               0Conv2d-136           [-1, 32, 14, 14]          16,928BatchNorm2d-137           [-1, 32, 14, 14]              64ReLU-138           [-1, 32, 14, 14]               0Conv2d-139          [-1, 128, 14, 14]         102,528BatchNorm2d-140          [-1, 128, 14, 14]             256ReLU-141          [-1, 128, 14, 14]               0MaxPool2d-142          [-1, 528, 14, 14]               0Conv2d-143          [-1, 128, 14, 14]          67,712BatchNorm2d-144          [-1, 128, 14, 14]             256ReLU-145          [-1, 128, 14, 14]               0inception_block-146          [-1, 832, 14, 14]               0MaxPool2d-147            [-1, 832, 7, 7]               0Conv2d-148            [-1, 256, 7, 7]         213,248BatchNorm2d-149            [-1, 256, 7, 7]             512ReLU-150            [-1, 256, 7, 7]               0Conv2d-151            [-1, 160, 7, 7]         133,280BatchNorm2d-152            [-1, 160, 7, 7]             320ReLU-153            [-1, 160, 7, 7]               0Conv2d-154            [-1, 320, 7, 7]         461,120BatchNorm2d-155            [-1, 320, 7, 7]             640ReLU-156            [-1, 320, 7, 7]               0Conv2d-157             [-1, 32, 7, 7]          26,656BatchNorm2d-158             [-1, 32, 7, 7]              64ReLU-159             [-1, 32, 7, 7]               0Conv2d-160            [-1, 128, 7, 7]         102,528BatchNorm2d-161            [-1, 128, 7, 7]             256ReLU-162            [-1, 128, 7, 7]               0MaxPool2d-163            [-1, 832, 7, 7]               0Conv2d-164            [-1, 128, 7, 7]         106,624BatchNorm2d-165            [-1, 128, 7, 7]             256ReLU-166            [-1, 128, 7, 7]               0inception_block-167            [-1, 832, 7, 7]               0Conv2d-168            [-1, 384, 7, 7]         319,872BatchNorm2d-169            [-1, 384, 7, 7]             768ReLU-170            [-1, 384, 7, 7]               0Conv2d-171            [-1, 192, 7, 7]         159,936BatchNorm2d-172            [-1, 192, 7, 7]             384ReLU-173            [-1, 192, 7, 7]               0Conv2d-174            [-1, 384, 7, 7]         663,936BatchNorm2d-175            [-1, 384, 7, 7]             768ReLU-176            [-1, 384, 7, 7]               0Conv2d-177             [-1, 48, 7, 7]          39,984BatchNorm2d-178             [-1, 48, 7, 7]              96ReLU-179             [-1, 48, 7, 7]               0Conv2d-180            [-1, 128, 7, 7]         153,728BatchNorm2d-181            [-1, 128, 7, 7]             256ReLU-182            [-1, 128, 7, 7]               0MaxPool2d-183            [-1, 832, 7, 7]               0Conv2d-184            [-1, 128, 7, 7]         106,624BatchNorm2d-185            [-1, 128, 7, 7]             256ReLU-186            [-1, 128, 7, 7]               0inception_block-187           [-1, 1024, 7, 7]               0AvgPool2d-188           [-1, 1024, 1, 1]               0Dropout-189           [-1, 1024, 1, 1]               0Linear-190                 [-1, 1024]       1,049,600ReLU-191                 [-1, 1024]               0Linear-192                    [-1, 2]           2,050Softmax-193                    [-1, 2]               0
================================================================
Total params: 7,039,122
Trainable params: 7,039,122
Non-trainable params: 0
----------------------------------------------------------------
Input size (MB): 0.57
Forward/backward pass size (MB): 69.61
Params size (MB): 26.85
Estimated Total Size (MB): 97.04
----------------------------------------------------------------
InceptionV1((conv1): Conv2d(3, 64, kernel_size=(7, 7), stride=(2, 2), padding=(3, 3))(maxpool1): MaxPool2d(kernel_size=3, stride=2, padding=1, dilation=1, ceil_mode=False)(conv2): Conv2d(64, 64, kernel_size=(1, 1), stride=(1, 1))(conv3): Conv2d(64, 192, kernel_size=(3, 3), stride=(1, 1), padding=(1, 1))(maxpool2): MaxPool2d(kernel_size=3, stride=2, padding=1, dilation=1, ceil_mode=False)(inception3a): inception_block((branch1): Sequential((0): Conv2d(192, 64, kernel_size=(1, 1), stride=(1, 1))(1): BatchNorm2d(64, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)(2): ReLU(inplace=True))(branch2): Sequential((0): Conv2d(192, 96, kernel_size=(1, 1), stride=(1, 1))(1): BatchNorm2d(96, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)(2): ReLU(inplace=True)(3): Conv2d(96, 128, kernel_size=(3, 3), stride=(1, 1), padding=(1, 1))(4): BatchNorm2d(128, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)(5): ReLU(inplace=True))(branch3): Sequential((0): Conv2d(192, 16, kernel_size=(1, 1), stride=(1, 1))(1): BatchNorm2d(16, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)(2): ReLU(inplace=True)(3): Conv2d(16, 32, kernel_size=(5, 5), stride=(1, 1), padding=(2, 2))(4): BatchNorm2d(32, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)(5): ReLU(inplace=True))(branch4): Sequential((0): MaxPool2d(kernel_size=3, stride=1, padding=1, dilation=1, ceil_mode=False)(1): Conv2d(192, 32, kernel_size=(1, 1), stride=(1, 1))(2): BatchNorm2d(32, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)(3): ReLU(inplace=True)))(inception3b): inception_block((branch1): Sequential((0): Conv2d(256, 128, kernel_size=(1, 1), stride=(1, 1))(1): BatchNorm2d(128, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)(2): ReLU(inplace=True))(branch2): Sequential((0): Conv2d(256, 128, kernel_size=(1, 1), stride=(1, 1))(1): BatchNorm2d(128, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)(2): ReLU(inplace=True)(3): Conv2d(128, 192, kernel_size=(3, 3), stride=(1, 1), padding=(1, 1))(4): BatchNorm2d(192, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)(5): ReLU(inplace=True))(branch3): Sequential((0): Conv2d(256, 32, kernel_size=(1, 1), stride=(1, 1))(1): BatchNorm2d(32, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)(2): ReLU(inplace=True)(3): Conv2d(32, 96, kernel_size=(5, 5), stride=(1, 1), padding=(2, 2))(4): BatchNorm2d(96, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)(5): ReLU(inplace=True))(branch4): Sequential((0): MaxPool2d(kernel_size=3, stride=1, padding=1, dilation=1, ceil_mode=False)(1): Conv2d(256, 64, kernel_size=(1, 1), stride=(1, 1))(2): BatchNorm2d(64, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)(3): ReLU(inplace=True)))(maxpool3): MaxPool2d(kernel_size=3, stride=2, padding=1, dilation=1, ceil_mode=False)(inception4a): inception_block((branch1): Sequential((0): Conv2d(480, 192, kernel_size=(1, 1), stride=(1, 1))(1): BatchNorm2d(192, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)(2): ReLU(inplace=True))(branch2): Sequential((0): Conv2d(480, 96, kernel_size=(1, 1), stride=(1, 1))(1): BatchNorm2d(96, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)(2): ReLU(inplace=True)(3): Conv2d(96, 208, kernel_size=(3, 3), stride=(1, 1), padding=(1, 1))(4): BatchNorm2d(208, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)(5): ReLU(inplace=True))(branch3): Sequential((0): Conv2d(480, 16, kernel_size=(1, 1), stride=(1, 1))(1): BatchNorm2d(16, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)(2): ReLU(inplace=True)(3): Conv2d(16, 48, kernel_size=(5, 5), stride=(1, 1), padding=(2, 2))(4): BatchNorm2d(48, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)(5): ReLU(inplace=True))(branch4): Sequential((0): MaxPool2d(kernel_size=3, stride=1, padding=1, dilation=1, ceil_mode=False)(1): Conv2d(480, 64, kernel_size=(1, 1), stride=(1, 1))(2): BatchNorm2d(64, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)(3): ReLU(inplace=True)))(inception4b): inception_block((branch1): Sequential((0): Conv2d(512, 160, kernel_size=(1, 1), stride=(1, 1))(1): BatchNorm2d(160, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)(2): ReLU(inplace=True))(branch2): Sequential((0): Conv2d(512, 112, kernel_size=(1, 1), stride=(1, 1))(1): BatchNorm2d(112, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)(2): ReLU(inplace=True)(3): Conv2d(112, 224, kernel_size=(3, 3), stride=(1, 1), padding=(1, 1))(4): BatchNorm2d(224, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)(5): ReLU(inplace=True))(branch3): Sequential((0): Conv2d(512, 24, kernel_size=(1, 1), stride=(1, 1))(1): BatchNorm2d(24, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)(2): ReLU(inplace=True)(3): Conv2d(24, 64, kernel_size=(5, 5), stride=(1, 1), padding=(2, 2))(4): BatchNorm2d(64, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)(5): ReLU(inplace=True))(branch4): Sequential((0): MaxPool2d(kernel_size=3, stride=1, padding=1, dilation=1, ceil_mode=False)(1): Conv2d(512, 64, kernel_size=(1, 1), stride=(1, 1))(2): BatchNorm2d(64, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)(3): ReLU(inplace=True)))(inception4c): inception_block((branch1): Sequential((0): Conv2d(512, 128, kernel_size=(1, 1), stride=(1, 1))(1): BatchNorm2d(128, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)(2): ReLU(inplace=True))(branch2): Sequential((0): Conv2d(512, 128, kernel_size=(1, 1), stride=(1, 1))(1): BatchNorm2d(128, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)(2): ReLU(inplace=True)(3): Conv2d(128, 256, kernel_size=(3, 3), stride=(1, 1), padding=(1, 1))(4): BatchNorm2d(256, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)(5): ReLU(inplace=True))(branch3): Sequential((0): Conv2d(512, 24, kernel_size=(1, 1), stride=(1, 1))(1): BatchNorm2d(24, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)(2): ReLU(inplace=True)(3): Conv2d(24, 64, kernel_size=(5, 5), stride=(1, 1), padding=(2, 2))(4): BatchNorm2d(64, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)(5): ReLU(inplace=True))(branch4): Sequential((0): MaxPool2d(kernel_size=3, stride=1, padding=1, dilation=1, ceil_mode=False)(1): Conv2d(512, 64, kernel_size=(1, 1), stride=(1, 1))(2): BatchNorm2d(64, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)(3): ReLU(inplace=True)))(inception4d): inception_block((branch1): Sequential((0): Conv2d(512, 112, kernel_size=(1, 1), stride=(1, 1))(1): BatchNorm2d(112, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)(2): ReLU(inplace=True))(branch2): Sequential((0): Conv2d(512, 144, kernel_size=(1, 1), stride=(1, 1))(1): BatchNorm2d(144, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)(2): ReLU(inplace=True)(3): Conv2d(144, 288, kernel_size=(3, 3), stride=(1, 1), padding=(1, 1))(4): BatchNorm2d(288, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)(5): ReLU(inplace=True))(branch3): Sequential((0): Conv2d(512, 32, kernel_size=(1, 1), stride=(1, 1))(1): BatchNorm2d(32, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)(2): ReLU(inplace=True)(3): Conv2d(32, 64, kernel_size=(5, 5), stride=(1, 1), padding=(2, 2))(4): BatchNorm2d(64, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)(5): ReLU(inplace=True))(branch4): Sequential((0): MaxPool2d(kernel_size=3, stride=1, padding=1, dilation=1, ceil_mode=False)(1): Conv2d(512, 64, kernel_size=(1, 1), stride=(1, 1))(2): BatchNorm2d(64, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)(3): ReLU(inplace=True)))(inception4e): inception_block((branch1): Sequential((0): Conv2d(528, 256, kernel_size=(1, 1), stride=(1, 1))(1): BatchNorm2d(256, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)(2): ReLU(inplace=True))(branch2): Sequential((0): Conv2d(528, 160, kernel_size=(1, 1), stride=(1, 1))(1): BatchNorm2d(160, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)(2): ReLU(inplace=True)(3): Conv2d(160, 320, kernel_size=(3, 3), stride=(1, 1), padding=(1, 1))(4): BatchNorm2d(320, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)(5): ReLU(inplace=True))(branch3): Sequential((0): Conv2d(528, 32, kernel_size=(1, 1), stride=(1, 1))(1): BatchNorm2d(32, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)(2): ReLU(inplace=True)(3): Conv2d(32, 128, kernel_size=(5, 5), stride=(1, 1), padding=(2, 2))(4): BatchNorm2d(128, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)(5): ReLU(inplace=True))(branch4): Sequential((0): MaxPool2d(kernel_size=3, stride=1, padding=1, dilation=1, ceil_mode=False)(1): Conv2d(528, 128, kernel_size=(1, 1), stride=(1, 1))(2): BatchNorm2d(128, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)(3): ReLU(inplace=True)))(maxpool4): MaxPool2d(kernel_size=3, stride=2, padding=1, dilation=1, ceil_mode=False)(inception5a): inception_block((branch1): Sequential((0): Conv2d(832, 256, kernel_size=(1, 1), stride=(1, 1))(1): BatchNorm2d(256, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)(2): ReLU(inplace=True))(branch2): Sequential((0): Conv2d(832, 160, kernel_size=(1, 1), stride=(1, 1))(1): BatchNorm2d(160, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)(2): ReLU(inplace=True)(3): Conv2d(160, 320, kernel_size=(3, 3), stride=(1, 1), padding=(1, 1))(4): BatchNorm2d(320, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)(5): ReLU(inplace=True))(branch3): Sequential((0): Conv2d(832, 32, kernel_size=(1, 1), stride=(1, 1))(1): BatchNorm2d(32, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)(2): ReLU(inplace=True)(3): Conv2d(32, 128, kernel_size=(5, 5), stride=(1, 1), padding=(2, 2))(4): BatchNorm2d(128, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)(5): ReLU(inplace=True))(branch4): Sequential((0): MaxPool2d(kernel_size=3, stride=1, padding=1, dilation=1, ceil_mode=False)(1): Conv2d(832, 128, kernel_size=(1, 1), stride=(1, 1))(2): BatchNorm2d(128, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)(3): ReLU(inplace=True)))(inception5b): Sequential((0): inception_block((branch1): Sequential((0): Conv2d(832, 384, kernel_size=(1, 1), stride=(1, 1))(1): BatchNorm2d(384, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)(2): ReLU(inplace=True))(branch2): Sequential((0): Conv2d(832, 192, kernel_size=(1, 1), stride=(1, 1))(1): BatchNorm2d(192, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)(2): ReLU(inplace=True)(3): Conv2d(192, 384, kernel_size=(3, 3), stride=(1, 1), padding=(1, 1))(4): BatchNorm2d(384, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)(5): ReLU(inplace=True))(branch3): Sequential((0): Conv2d(832, 48, kernel_size=(1, 1), stride=(1, 1))(1): BatchNorm2d(48, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)(2): ReLU(inplace=True)(3): Conv2d(48, 128, kernel_size=(5, 5), stride=(1, 1), padding=(2, 2))(4): BatchNorm2d(128, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)(5): ReLU(inplace=True))(branch4): Sequential((0): MaxPool2d(kernel_size=3, stride=1, padding=1, dilation=1, ceil_mode=False)(1): Conv2d(832, 128, kernel_size=(1, 1), stride=(1, 1))(2): BatchNorm2d(128, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)(3): ReLU(inplace=True)))(1): AvgPool2d(kernel_size=7, stride=1, padding=0)(2): Dropout(p=0.4, inplace=False))(classifier): Sequential((0): Linear(in_features=1024, out_features=1024, bias=True)(1): ReLU()(2): Linear(in_features=1024, out_features=2, bias=True)(3): Softmax(dim=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与losstrain_acc += (pred.argmax(1) == y).type(torch.float).sum().item()train_loss += loss.item()train_acc /= sizetrain_loss /= num_batchesreturn train_acc, train_loss
def test(dataloader, model, loss_fn):size = len(dataloader.dataset)  # 测试集的大小num_batches = len(dataloader)  # 批次数目test_loss, test_acc = 0, 0# 当不进行训练时,停止梯度更新,节省计算内存消耗with torch.no_grad():for imgs, target in dataloader:imgs, target = imgs.to(device), target.to(device)# 计算losstarget_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 /= sizetest_loss /= num_batchesreturn test_acc, test_loss

正式训练


import copyoptimizer = torch.optim.Adam(model.parameters(), lr=1e-4)
loss_fn = nn.CrossEntropyLoss()  # 创建损失函数epochs = 50train_loss = []
train_acc = []
test_loss = []
test_acc = []best_acc = 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_modelif epoch_test_acc > best_acc:best_acc = epoch_test_accbest_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 = r'C:/Users/11054/Desktop/kLearning/J8_learning/best_model.pth'  # 保存的参数文件名
torch.save(model.state_dict(), PATH)print('Done')
Epoch: 1, Train_acc:62.8%, Train_loss:0.650, Test_acc:66.9%, Test_loss:0.622, Lr:1.00E-04
Epoch: 2, Train_acc:65.6%, Train_loss:0.635, Test_acc:66.2%, Test_loss:0.612, Lr:1.00E-04
Epoch: 3, Train_acc:67.7%, Train_loss:0.612, Test_acc:68.5%, Test_loss:0.621, Lr:1.00E-04
Epoch: 4, Train_acc:71.7%, Train_loss:0.582, Test_acc:73.0%, Test_loss:0.576, Lr:1.00E-04
Epoch: 5, Train_acc:72.1%, Train_loss:0.575, Test_acc:74.4%, Test_loss:0.562, Lr:1.00E-04
Epoch: 6, Train_acc:74.2%, Train_loss:0.556, Test_acc:75.1%, Test_loss:0.548, Lr:1.00E-04
Epoch: 7, Train_acc:75.8%, Train_loss:0.549, Test_acc:78.1%, Test_loss:0.517, Lr:1.00E-04
Epoch: 8, Train_acc:76.9%, Train_loss:0.531, Test_acc:79.5%, Test_loss:0.510, Lr:1.00E-04
Epoch: 9, Train_acc:81.2%, Train_loss:0.498, Test_acc:83.7%, Test_loss:0.478, Lr:1.00E-04
Epoch:10, Train_acc:81.1%, Train_loss:0.497, Test_acc:82.3%, Test_loss:0.486, Lr:1.00E-04
Epoch:11, Train_acc:81.7%, Train_loss:0.490, Test_acc:83.0%, Test_loss:0.476, Lr:1.00E-04
Epoch:12, Train_acc:83.9%, Train_loss:0.472, Test_acc:85.5%, Test_loss:0.454, Lr:1.00E-04
Epoch:13, Train_acc:83.7%, Train_loss:0.474, Test_acc:83.9%, Test_loss:0.467, Lr:1.00E-04
Epoch:14, Train_acc:84.4%, Train_loss:0.462, Test_acc:86.0%, Test_loss:0.444, Lr:1.00E-04
Epoch:15, Train_acc:86.2%, Train_loss:0.446, Test_acc:80.7%, Test_loss:0.490, Lr:1.00E-04
Epoch:16, Train_acc:85.9%, Train_loss:0.449, Test_acc:86.0%, Test_loss:0.445, Lr:1.00E-04
Epoch:17, Train_acc:86.6%, Train_loss:0.444, Test_acc:80.9%, Test_loss:0.501, Lr:1.00E-04
Epoch:18, Train_acc:86.6%, Train_loss:0.446, Test_acc:83.4%, Test_loss:0.468, Lr:1.00E-04
Epoch:19, Train_acc:89.1%, Train_loss:0.417, Test_acc:85.8%, Test_loss:0.453, Lr:1.00E-04
Epoch:20, Train_acc:88.2%, Train_loss:0.425, Test_acc:90.4%, Test_loss:0.404, Lr:1.00E-04
Epoch:21, Train_acc:90.4%, Train_loss:0.407, Test_acc:87.9%, Test_loss:0.428, Lr:1.00E-04
Epoch:22, Train_acc:90.3%, Train_loss:0.411, Test_acc:89.0%, Test_loss:0.422, Lr:1.00E-04
Epoch:23, Train_acc:89.5%, Train_loss:0.415, Test_acc:85.3%, Test_loss:0.449, Lr:1.00E-04
Epoch:24, Train_acc:89.8%, Train_loss:0.412, Test_acc:89.0%, Test_loss:0.416, Lr:1.00E-04
Epoch:25, Train_acc:88.5%, Train_loss:0.428, Test_acc:90.2%, Test_loss:0.411, Lr:1.00E-04
Epoch:26, Train_acc:90.4%, Train_loss:0.406, Test_acc:89.5%, Test_loss:0.413, Lr:1.00E-04
Epoch:27, Train_acc:91.9%, Train_loss:0.395, Test_acc:89.3%, Test_loss:0.418, Lr:1.00E-04
Epoch:28, Train_acc:92.9%, Train_loss:0.381, Test_acc:91.6%, Test_loss:0.388, Lr:1.00E-04
Epoch:29, Train_acc:92.9%, Train_loss:0.383, Test_acc:90.0%, Test_loss:0.409, Lr:1.00E-04
Epoch:30, Train_acc:91.5%, Train_loss:0.397, Test_acc:89.0%, Test_loss:0.420, Lr:1.00E-04
Epoch:31, Train_acc:91.9%, Train_loss:0.392, Test_acc:91.6%, Test_loss:0.396, Lr:1.00E-04
Epoch:32, Train_acc:89.2%, Train_loss:0.421, Test_acc:89.7%, Test_loss:0.411, Lr:1.00E-04
Epoch:33, Train_acc:92.3%, Train_loss:0.392, Test_acc:90.0%, Test_loss:0.409, Lr:1.00E-04
Epoch:34, Train_acc:92.2%, Train_loss:0.386, Test_acc:92.3%, Test_loss:0.387, Lr:1.00E-04
Epoch:35, Train_acc:92.2%, Train_loss:0.393, Test_acc:92.5%, Test_loss:0.387, Lr:1.00E-04
Epoch:36, Train_acc:95.0%, Train_loss:0.362, Test_acc:91.8%, Test_loss:0.395, Lr:1.00E-04
Epoch:37, Train_acc:93.3%, Train_loss:0.383, Test_acc:90.7%, Test_loss:0.409, Lr:1.00E-04
Epoch:38, Train_acc:93.8%, Train_loss:0.378, Test_acc:91.6%, Test_loss:0.399, Lr:1.00E-04
Epoch:39, Train_acc:93.3%, Train_loss:0.384, Test_acc:91.4%, Test_loss:0.392, Lr:1.00E-04
Epoch:40, Train_acc:94.5%, Train_loss:0.371, Test_acc:90.4%, Test_loss:0.405, Lr:1.00E-04
Epoch:41, Train_acc:95.6%, Train_loss:0.360, Test_acc:91.8%, Test_loss:0.397, Lr:1.00E-04
Epoch:42, Train_acc:91.2%, Train_loss:0.401, Test_acc:85.1%, Test_loss:0.450, Lr:1.00E-04
Epoch:43, Train_acc:92.2%, Train_loss:0.391, Test_acc:88.3%, Test_loss:0.425, Lr:1.00E-04
Epoch:44, Train_acc:93.9%, Train_loss:0.375, Test_acc:89.5%, Test_loss:0.413, Lr:1.00E-04
Epoch:45, Train_acc:95.4%, Train_loss:0.359, Test_acc:93.2%, Test_loss:0.381, Lr:1.00E-04
Epoch:46, Train_acc:93.5%, Train_loss:0.381, Test_acc:91.6%, Test_loss:0.395, Lr:1.00E-04
Epoch:47, Train_acc:95.7%, Train_loss:0.354, Test_acc:92.8%, Test_loss:0.382, Lr:1.00E-04
Epoch:48, Train_acc:95.9%, Train_loss:0.356, Test_acc:93.7%, Test_loss:0.373, Lr:1.00E-04
Epoch:49, Train_acc:95.9%, Train_loss:0.354, Test_acc:94.4%, Test_loss:0.367, Lr:1.00E-04
Epoch:50, Train_acc:95.1%, Train_loss:0.362, Test_acc:92.3%, Test_loss:0.391, Lr:1.00E-04
Done

结果可视化


import matplotlib.pyplot as plt
# 隐藏警告
import warningswarnings.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()from PIL import Imageclasses = list(total_data.class_to_idx)
print(classes)
print(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=r'C:\Users\11054\Desktop\kLearning\p4_learning\data\Monkeypox\M01_01_02.jpg',model=model,transform=train_transforms,classes=classes)

在这里插入图片描述

['Monkeypox', 'Others']
{'Monkeypox': 0, 'Others': 1}
预测结果是:Monkeypox

在这里插入图片描述

详细网络结构图

在这里插入图片描述

个人总结

  1. 主要特点和创新点(Inception模块)
  • 其设计理念是,在同一层网络中使用多种不同尺寸的卷积核(如1x1, 3x3, 5x5等)和池化层,然后将它们的输出拼接在一起。这种设计允许网络在同一空间维度上捕获多尺度特征,从而提高了网络的表达能力。
  • 1x1卷积核的使用不仅减少了计算量,还起到了降维的作用,帮助减少模型的参数数量和计算复杂度。
  1. 辅助分类器:
    Inception v1在网络的中间层添加了两个辅助分类器。这些分类器通过添加额外的损失函数来帮助训练时的梯度传播,防止梯度消失问题,特别是在深层网络中。在测试时,这些辅助分类器的输出会被忽略。
  2. 参数效率:
  • 通过使用1x1卷积核和特殊的模块设计,Inception v1在保持高性能的同时,有效地减少了模型的参数数量,这使得网络更加高效,能够更好地推广到更大的数据集上。
    在这里插入图片描述
    一个典型的Inception模块包括以下几个部分:
    1x1卷积层:用于降维和减少计算量。
    3x3卷积层:用于捕获局部细节特征。
    5x5卷积层:用于捕获更大范围的特征。
    3x3最大池化层:用于捕获空间信息。
    拼接层:将所有上述卷积层和池化层的输出在通道维度上拼接在一起。

http://www.ppmy.cn/news/1556714.html

相关文章

航电系统组成架构详解!

一、航电系统的组成架构 航电系统通常由多个子系统组成,这些子系统协同工作,确保飞机的正常飞行和高效管理。以下是一些关键的子系统: 人机接口: 显示系统:包括平显(HUD)、头盔显示器&#x…

从源码分析swift GCD_DispatchGroup

前言: 最近在写需求的时候用到了DispatchGroup,一直没有深入去学习,既然遇到了那么就总结下吧。。。。 基本介绍: 任务组(DispatchGroup) DispatchGroup 可以将多个任务组合在一起并且监听它们的完成状态。…

samout llm解码 幻觉更低更稳定

这段代码定义了一个简单的对话生成系统,包括模型加载、词汇表加载、以及基于给定提示生成文本的功能。下面是对代码的解析: load_model_and_voc(device"cpu"): 该函数用于加载预训练的模型和词汇表(vocabulary)。它首先…

C++ Learning 友元•内部类

友元 在 C 中,友元是一个非常特殊的概念,它使得某些非成员函数或其他类能够访问类的私有成员和保护成员。通常,私有成员和保护成员只能在类的成员函数内部访问,但通过将函数或类声明为友元,能够绕过这个访问限制。 友元…

linux cpu 管理

视频教程:ubuntu cpu 管理_哔哩哔哩_bilibili 概述 平均负载,CPU 使用率,CPU上下文 1 平均负载 #查看命令: rootzyb:~# uptime 18:21:47 up 1:09, 2 users, load average: 0.00, 0.00, 0.00 依次则是过去 1 分钟、5 分钟、1…

在Visual Studio Code (VSCode) 中将终端输出重定向到一个文本文件中

在Visual Studio Code (VSCode) 中将终端输出重定向到一个文本文件中 在Visual Studio Code (VSCode) 中,你可以通过以下步骤将终端输出重定向到一个文本文件中: 1. 方法一 使用重定向符号 在终端中运行命令时,可以使用重定向符号 > 或 >> 将输出保存到文件中。 …

Android运行低版本项目可能遇到的问题

Android运行低版本项目可能遇到的问题 低版本项目总是遇到各种问题的,耐心点 一、gradle-xxx.xxx.xxx.zip一直下载不下来 在gradle-wrapper.properties可以试下 distributionBaseGRADLE_USER_HOME distributionPathwrapper/dists zipStoreBaseGRADLE_USER_HOME …

C/C++圣诞树

系列文章 序号直达链接1C/C爱心代码2C/C跳动的爱心3C/C李峋同款跳动的爱心代码4C/C满屏飘字表白代码5C/C大雪纷飞代码6C/C烟花代码7C/C黑客帝国同款字母雨8C/C樱花树代码9C/C奥特曼代码10C/C精美圣诞树11C/C俄罗斯方块12C/C贪吃蛇13C/C孤单又灿烂的神-鬼怪14C/C闪烁的爱心15C…