第J9周:Inception v3算法实战与解析(pytorch版)

>- **🍨 本文为[🔗365天深度学习训练营]中的学习记录博客**
>- **🍖 原作者:[K同学啊]**

📌本周任务:📌

  1. 了解并学习InceptionV3相对与InceptionV1有哪些改进的地方

  2. 使用Inception完成天气识别

🏡 我的环境:

  • 语言环境:Python3.8
  • 编译器:Jupyter Notebook
  • 深度学习环境:Pytorch
    • torch==2.3.1+cu118
    • torchvision==0.18.1+cu118

一、理论基础

Inception v3论文:Rethinking the Inception Architecture for Computer Vision

Inception v3由谷歌研究员Christian Szegedy等人在2015年的论文《Rethinking the Inception Architecture for Computer Vision》中提出。Inception v3是Inception网络系列的第三个版本,它在ImageNet图像识别竞赛中取得了优异成绩,尤其是在大规模图像识别任务中表现出色。

Inception v3的主要特点如下:

  1. 更深的网络结构:Inception v3比之前的Inception网络结构更深,包含了48层卷积层。这使得网络可以提取更多层次的特征,从而在图像识别任务上取得更好的效果。
  2. 使用Factorized Convolutions:Inception v3采用了Factorized Convolutions(分解卷积),将较大的卷积核分解为多个较小的卷积核。这种方法可以降低网络的参数数量,减少计算复杂度,同时保持良好的性能。
  3. 使用Batch Normalization:Inception v3在每个卷积层之后都添加了Batch Normalization(BN),这有助于网络的收敛和泛化能力。BN可以减少Internal Covariate Shift(内部协变量偏移)现象,加快训练速度,同时提高模型的鲁棒性。
  4. 辅助分类器:Inception v3引入了辅助分类器,可以在网络训练过程中提供额外的梯度信息,帮助网络更好地学习特征。辅助分类器位于网络的某个中间层,其输出会与主分类器的输出进行加权融合,从而得到最终的预测结果。
  5. 基于RMSProp的优化器:Inception v3使用了RMSProp优化器进行训练。相比于传统的随机梯度下降(SGD)方法,RMSProp可以自适应地调整学习率,使得训练过程更加稳定,收敛速度更快。

Inception v3在图像分类、物体检测和图像分割等计算机视觉任务中均取得了显著的效果。然而,由于其较大的网络结构和计算复杂度,Inception v3在实际应用中可能需要较高的硬件要求。

相对于Inception v1的Inception Module结构,Inception v3中做出了如下改动:

●将 5×5 的卷积分解为两个 3×3 的卷积运算以提升计算速度。尽管这有点违反直觉,但一个 5×5 的卷积在计算成本上是一个 3×3 卷积的 2.78 倍。所以叠加两个 3×3 卷积实际上在性能上会有所提升,如下图所示:

●此外,作者将 n×n 的卷积核尺寸分解为 1×n 和 n×1 两个卷积。例如,一个 3×3 的卷积等价于首先执行一个 1×3 的卷积再执行一个 3×1 的卷积。他们还发现这种方法在成本上要比单个 3×3 的卷积降低 33%,这一结构如下图所示:

此处如果 n=3,则与上一张图像一致。最左侧的 5x5 卷积可被表示为两个 3x3 卷积,它们又可以被表示为 1x3 和 3x1 卷积。
模块中的滤波器组被扩展(即变得更宽而不是更深),以解决表征性瓶颈。如果该模块没有被拓展宽度,而是变得更深,那么维度会过多减少,造成信息损失。如下图所示:

最后实现的inception v3网络是上图结构图如下:

二、前期准备

1. 设置GPU

如果设备上支持GPU就使用GPU,否则使用CPU

import warnings
warnings.filterwarnings("ignore") #忽略警告信息import torch
device=torch.device("cuda" if torch.cuda.is_available() else "cpu")
device

运行结果:

device(type='cuda')

2. 导入数据

import pathlib
data_dir=r'D:\THE MNIST DATABASE\weather_photos'
data_dir=pathlib.Path(data_dir)img_count=len(list(data_dir.glob('*/*')))
print("图片总数为:",img_count)

运行结果:

图片总数为: 1125

3. 查看数据集分类

data_paths=list(data_dir.glob('*'))
classNames=[str(path).split('\\')[3] for path in data_paths]
classNames

运行结果:

['cloudy', 'rain', 'shine', 'sunrise']

4. 随机查看图片

随机抽取数据集中的10张图片进行查看

import PIL,random
import matplotlib.pyplot as plt
from PIL import Image
plt.rcParams['font.sans-serif']=['SimHei'] #用来正常显示中文标签
plt.rcParams['axes.unicode_minus']=False #用来正常显示负号data_paths2=list(data_dir.glob('*/*'))
plt.figure(figsize=(20,8))
#plt.suptitle("OreoCC的案例",fontsize=15)
for i in range(10):plt.subplot(2,5,i+1)plt.axis("off")image=random.choice(data_paths2) #随机选择一个图片plt.title(image.parts[-2],fontsize=20) #通过glob对象取出他的文件夹名称,即分类名plt.imshow(Image.open(str(image))) #显示图片

运行结果:

5. 图片预处理

import torchvision.transforms as transforms
from torchvision import transforms,datasetstrain_transforms=transforms.Compose([transforms.Resize([224,224]), #将图片统一尺寸transforms.RandomHorizontalFlip(), #将图片随机水平翻转transforms.RandomRotation(0.2), #将图片按照0.2的弧度值随机旋转transforms.ToTensor(), #将图片转换为tensortransforms.Normalize(  #标准化处理->转换为正态分布,使模型更容易收敛mean=[0.485,0.456,0.406],std=[0.229,0.224,0.225])
])total_data=datasets.ImageFolder(r'D:\THE MNIST DATABASE\weather_photos',transform=train_transforms
)
total_data

运行结果:

Dataset ImageFolderNumber of datapoints: 1125Root location: D:\THE MNIST DATABASE\weather_photosStandardTransform
Transform: Compose(Resize(size=[224, 224], interpolation=bilinear, max_size=None, antialias=True)RandomHorizontalFlip(p=0.5)RandomRotation(degrees=[-0.2, 0.2], interpolation=nearest, expand=False, fill=0)ToTensor()Normalize(mean=[0.485, 0.456, 0.406], std=[0.229, 0.224, 0.225]))

将数据集分类情况进行映射输出:

total_data.class_to_idx

运行结果:

{'cloudy': 0, 'rain': 1, 'shine': 2, 'sunrise': 3}

6. 划分数据集

train_size=int(0.8*len(total_data))
test_size=len(total_data)-train_sizetrain_dataset,test_dataset=torch.utils.data.random_split(total_data,[train_size,test_size]
)
train_dataset,test_dataset

运行结果:

(<torch.utils.data.dataset.Subset at 0x1f39bf80a90>,<torch.utils.data.dataset.Subset at 0x1f3bc462210>)

查看训练集和测试集的数据数量:

train_size,test_size

运行结果:

(900, 225)

7. 加载数据集

batch_size=8
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 train_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

三、手动搭建网络模型

1. BasicConv2d模块

import torch.nn as nn
import torch.nn.functional as Fclass BasicConv2d(nn.Module):def __init__(self,in_channels,out_channels,**kwargs):super(BasicConv2d,self).__init__()self.conv=nn.Conv2d(in_channels,out_channels,bias=False,**kwargs)self.bn=nn.BatchNorm2d(out_channels,eps=0.001)self.relu=nn.ReLU(inplace=True)def forward(self,x):x=self.conv(x)x=self.bn(x)x=self.relu(x)return x

2. Inception-A

class InceptionA(nn.Module):def __init__(self,in_channels,pool_features):super(InceptionA,self).__init__()self.branch1x1=BasicConv2d(in_channels,64,kernel_size=1)self.branch5x5_1=BasicConv2d(in_channels,48,kernel_size=1)self.branch5x5_2=BasicConv2d(48,64,kernel_size=5,padding=2)self.branch3x3dbl_1=BasicConv2d(in_channels,64,kernel_size=1)self.branch3x3dbl_2=BasicConv2d(64,96,kernel_size=3,padding=1)self.branch3x3dbl_3=BasicConv2d(96,96,kernel_size=3,padding=1)self.branch_pool=BasicConv2d(in_channels,pool_features,kernel_size=1)def forward(self,x):branch1x1=self.branch1x1(x)branch5x5=self.branch5x5_1(x)branch5x5=self.branch5x5_2(branch5x5)branch3x3dbl=self.branch3x3dbl_1(x)branch3x3dbl=self.branch3x3dbl_2(branch3x3dbl)branch3x3dbl=self.branch3x3dbl_3(branch3x3dbl)branch_pool=F.avg_pool2d(x,kernel_size=3,stride=1,padding=1)branch_pool=self.branch_pool(branch_pool)outputs=[branch1x1,branch5x5,branch3x3dbl,branch_pool]return torch.cat(outputs,1)

InceptionA模块包含四个分支,每个分支使用不同的卷积核大小和参数。其中branch1x1、branch5x5_2、branch3x3dbl_3使用较小的卷积核,可以减少参数数量和计算量,提高网络效率。branch_pool使用平均池化的方式进行特征提取和降维。最后,将四个分支的结果在通道维度上进行拼接,输出InceptionA的结果。 

3. Inception-B

class InceptionB(nn.Module):def __init__(self,in_channels,channels_7x7):super(InceptionB,self).__init__()self.branch1x1=BasicConv2d(in_channels,192,kernel_size=1)c7=channels_7x7self.branch7x7_1=BasicConv2d(in_channels,c7,kernel_size=1)self.branch7x7_2=BasicConv2d(c7,c7,kernel_size=(1,7),padding=(0,3))self.branch7x7_3=BasicConv2d(c7,192,kernel_size=(7,1),padding=(3,0))self.branch7x7dbl_1=BasicConv2d(in_channels,c7,kernel_size=1)self.branch7x7dbl_2=BasicConv2d(c7,c7,kernel_size=(7,1),padding=(3,0))self.branch7x7dbl_3=BasicConv2d(c7,c7,kernel_size=(1,7),padding=(0,3))self.branch7x7dbl_4=BasicConv2d(c7,c7,kernel_size=(7,1),padding=(3,0))self.branch7x7dbl_5=BasicConv2d(c7,192,kernel_size=(1,7),padding=(0,3))self.branch_pool=BasicConv2d(in_channels,192,kernel_size=1)def forward(self,x):branch1x1=self.branch1x1(x)branch7x7=self.branch7x7_1(x)branch7x7=self.branch7x7_2(branch7x7)branch7x7=self.branch7x7_3(branch7x7)branch7x7dbl=self.branch7x7dbl_1(x)branch7x7dbl=self.branch7x7dbl_2(branch7x7dbl)branch7x7dbl=self.branch7x7dbl_3(branch7x7dbl)branch7x7dbl=self.branch7x7dbl_4(branch7x7dbl)branch7x7dbl=self.branch7x7dbl_5(branch7x7dbl)branch_pool=F.avg_pool2d(x,kernel_size=3,stride=1,padding=1)branch_pool=self.branch_pool(branch_pool)outputs=[branch1x1,branch7x7,branch7x7dbl,branch_pool]return torch.cat(outputs,1)

InceptionB模块包含四个分支,其中branch7x7_2和branch7x7_3使用不同大小的卷积核进行多次卷积,可以提高特征的表达能力。branch7x7dbl_2、branch7x7dbl_3、branch7x7dbl_4、branch7x7dbl_5也类似地使用多个不同大小的卷积核进行多次卷积,提高了特征的表达能力,并且较好地保留了空间尺寸。branch_pool仍然使用平均池化的方式进行特征提取和降维。最后,将四个分支的结果在通道维度上进行拼接,输出InceptionB的结果。

4. Inception-C

class InceptionC(nn.Module):def __init__(self,in_channels):super(InceptionC,self).__init__()self.branch1x1=BasicConv2d(in_channels,320,kernel_size=1)self.branch3x3_1=BasicConv2d(in_channels,384,kernel_size=1)self.branch3x3_2a=BasicConv2d(384,384,kernel_size=(1,3),padding=(0,1))self.branch3x3_2b=BasicConv2d(384,384,kernel_size=(3,1),padding=(1,0))self.branch3x3dbl_1=BasicConv2d(in_channels,448,kernel_size=1)self.branch3x3dbl_2=BasicConv2d(448,384,kernel_size=3,padding=1)self.branch3x3dbl_3a=BasicConv2d(384,384,kernel_size=(1,3),padding=(0,1))self.branch3x3dbl_3b=BasicConv2d(384,384,kernel_size=(3,1),padding=(1,0))self.branch_pool=BasicConv2d(in_channels,192,kernel_size=1)def forward(self,x):branch1x1=self.branch1x1(x)branch3x3=self.branch3x3_1(x)branch3x3=[self.branch3x3_2a(branch3x3),self.branch3x3_2b(branch3x3),]branch3x3=torch.cat(branch3x3,1)branch3x3dbl=self.branch3x3dbl_1(x)branch3x3dbl=self.branch3x3dbl_2(branch3x3dbl)branch3x3dbl=[self.branch3x3dbl_3a(branch3x3dbl),self.branch3x3dbl_3b(branch3x3dbl),]branch3x3dbl=torch.cat(branch3x3dbl,1)branch_pool=F.avg_pool2d(x,kernel_size=3,stride=1,padding=1)branch_pool=self.branch_pool(branch_pool)outputs=[branch1x1,branch3x3,branch3x3dbl,branch_pool]return torch.cat(outputs,1)

InceptionC模块同样包含三个分支,其中branch3x3_2b和branch3x3dbl_2使用大小为3x3的卷积核,并且padding=1,dilation=1,可以一定程度上扩大感受野。这两个分支多次卷积,可以提高特征的表达能力。branch_pool仍然使用平均池化的方式进行特征提取和降维。最后,将三个分支的结果在通道维度上进行拼接,输出InceptionB的结果。

5、Reduction-A

class ReductionA(nn.Module):def __init__(self,in_channels):super(ReductionA,self).__init__()self.branch3x3=BasicConv2d(in_channels,384,kernel_size=3,stride=2)self.branch3x3dbl_1=BasicConv2d(in_channels,64,kernel_size=1)self.branch3x3dbl_2=BasicConv2d(64,96,kernel_size=3,padding=1)self.branch3x3dbl_3=BasicConv2d(96,96,kernel_size=3,stride=2)def forward(self,x):branch3x3=self.branch3x3(x)branch3x3dbl=self.branch3x3dbl_1(x)branch3x3dbl=self.branch3x3dbl_2(branch3x3dbl)branch3x3dbl=self.branch3x3dbl_3(branch3x3dbl)branch_pool=F.max_pool2d(x,kernel_size=3,stride=2)outputs=[branch3x3,branch3x3dbl,branch_pool]return torch.cat(outputs,1)

ReductionA模块包含三个分支,其中branch3x3_1使用3x3的卷积核进行卷积,通过stride=2来降维,同时提取特征。branch3x3_2a、branch3x3_2b、branch3x3_2c使用多层卷积对特征进行提取和表达,同时通过stride=2来降维和压缩特征,减少计算量。branch_pool使用max pooling的方式进行特征提取和降维,与其他模块类似。最后,将三个分支的结果在通道维度上进行拼接,输出ReductionA的结果。

6、Reduction-B

class ReductionB(nn.Module):def __init__(self,in_channels):super(ReductionB,self).__init__()self.branch3x3_1=BasicConv2d(in_channels,192,kernel_size=1)self.branch3x3_2=BasicConv2d(192,320,kernel_size=3,stride=2)self.branch7x7x3_1=BasicConv2d(in_channels,192,kernel_size=1)self.branch7x7x3_2=BasicConv2d(192,192,kernel_size=(1,7),padding=(0,3))self.branch7x7x3_3=BasicConv2d(192,192,kernel_size=(7,1),padding=(3,0))self.branch7x7x3_4=BasicConv2d(192,192,kernel_size=3,stride=2)def forward(self,x):branch3x3=self.branch3x3_1(x)branch3x3=self.branch3x3_2(branch3x3)branch7x7x3=self.branch7x7x3_1(x)branch7x7x3=self.branch7x7x3_2(branch7x7x3)branch7x7x3=self.branch7x7x3_3(branch7x7x3)branch7x7x3=self.branch7x7x3_4(branch7x7x3)branch_pool=F.max_pool2d(x,kernel_size=3,stride=2)outputs=[branch3x3,branch7x7x3,branch_pool]return torch.cat(outputs,1)

 7、辅助分支

class InceptionAux(nn.Module):   def __init__(self,in_channels,num_classes):super(InceptionAux,self).__init__()self.conv0=BasicConv2d(in_channels,128,kernel_size=1)self.conv1=BasicConv2d(128,768,kernel_size=5)self.conv1.stddev=0.01self.fc=nn.Linear(768,num_classes)self.fc.stddev=0.001def forward(self,x):#17x17x768x=F.avg_pool2d(x,kernel_size=5,stride=3)# 5x5x768x=self.conv0(x)# 5x5x128x=self.conv1(x)# 1x1x768x=x.view(x.size(0),-1)# 768x=self.fc(x)# 1000return x

8、InceptionV3实现

class InceptionV3(nn.Module):def __init__(self,num_classes=1000,aux_logits=False,transform_input=False):super(InceptionV3,self).__init__()self.aux_logits=aux_logitsself.transform_input=transform_inputself.Conv2d_1a_3x3=BasicConv2d(3,32,kernel_size=3,stride=2)self.Conv2d_2a_3x3=BasicConv2d(32,32,kernel_size=3)self.Conv2d_2b_3x3=BasicConv2d(32,64,kernel_size=3,padding=1)self.Conv2d_3b_1x1=BasicConv2d(64,80,kernel_size=1)self.Conv2d_4a_3x3=BasicConv2d(80,192,kernel_size=3)self.Mixed_5b=InceptionA(192,pool_features=32)self.Mixed_5c=InceptionA(256,pool_features=64)self.Mixed_5d=InceptionA(288,pool_features=64)self.Mixed_6a=ReductionA(288)self.Mixed_6b=InceptionB(768,channels_7x7=128)self.Mixed_6c=InceptionB(768,channels_7x7=160)self.Mixed_6d=InceptionB(768,channels_7x7=160)self.Mixed_6e=InceptionB(768,channels_7x7=192)if aux_logits:self.AuxLogits=InceptionAux(768,num_classes)self.Mixed_7a=ReductionB(768)self.Mixed_7b=InceptionC(1280)self.Mixed_7c=InceptionC(2048)self.fc=nn.Linear(2048,num_classes)def forward(self,x):if self.transform_input:x=x.clone()x[:,0]=x[:,0]*(0.229/0.5)+(0.485-0.5)/0.5x[:,1]=x[:,1]*(0.224/0.5)+(0.456-0.5)/0.5x[:,2]=x[:,2]*(0.225/0.5)+(0.406-0.5)/0.5# 229x229x3x=self.Conv2d_1a_3x3(x)# 149x149x32x=self.Conv2d_2a_3x3(x)# 147x147x32x=self.Conv2d_2b_3x3(x)# 147x147x64x=F.max_pool2d(x,kernel_size=3,stride=2)# 73x73x64x=self.Conv2d_3b_1x1(x)# 73x73x80x=self.Conv2d_4a_3x3(x)# 71x71x192x=F.max_pool2d(x,kernel_size=3,stride=2)# 35x35x192x=self.Mixed_5b(x)# 35x35x256x=self.Mixed_5c(x)# 35x35x288x=self.Mixed_5d(x)# 35x35x288x=self.Mixed_6a(x)# 17x17x768x=self.Mixed_6b(x)# 17x17x768x=self.Mixed_6c(x)# 17x17x768x=self.Mixed_6d(x)# 17x17x768x=self.Mixed_6e(x)# 17x17x768if self.training and self.aux_logits:aux=self.AuxLogits(x)# 17x17x768x=self.Mixed_7a(x)# 8x8x1280x=self.Mixed_7b(x)# 8x8x2048x=self.Mixed_7c(x)# 8x8x2048x=F.avg_pool2d(x,kernel_size=5)# 1x1x2048x=F.dropout(x,training=self.training)# 1x1x2048x=x.view(x.size(0),-1)# 2048x=self.fc(x)# 1000(num_classes)if self.training and self.aux_logits:return x,auxreturn x
model=InceptionV3(num_classes=4).to(device)
model

运行结果:

InceptionV3((Conv2d_1a_3x3): BasicConv2d((conv): Conv2d(3, 32, kernel_size=(3, 3), stride=(2, 2), bias=False)(bn): BatchNorm2d(32, eps=0.001, momentum=0.1, affine=True, track_running_stats=True)(relu): ReLU(inplace=True))(Conv2d_2a_3x3): BasicConv2d((conv): Conv2d(32, 32, kernel_size=(3, 3), stride=(1, 1), bias=False)(bn): BatchNorm2d(32, eps=0.001, momentum=0.1, affine=True, track_running_stats=True)(relu): ReLU(inplace=True))(Conv2d_2b_3x3): BasicConv2d((conv): Conv2d(32, 64, kernel_size=(3, 3), stride=(1, 1), padding=(1, 1), bias=False)(bn): BatchNorm2d(64, eps=0.001, momentum=0.1, affine=True, track_running_stats=True)(relu): ReLU(inplace=True))(Conv2d_3b_1x1): BasicConv2d((conv): Conv2d(64, 80, kernel_size=(1, 1), stride=(1, 1), bias=False)(bn): BatchNorm2d(80, eps=0.001, momentum=0.1, affine=True, track_running_stats=True)(relu): ReLU(inplace=True))(Conv2d_4a_3x3): BasicConv2d((conv): Conv2d(80, 192, kernel_size=(3, 3), stride=(1, 1), bias=False)(bn): BatchNorm2d(192, eps=0.001, momentum=0.1, affine=True, track_running_stats=True)(relu): ReLU(inplace=True))(Mixed_5b): InceptionA((branch1x1): BasicConv2d((conv): Conv2d(192, 64, kernel_size=(1, 1), stride=(1, 1), bias=False)(bn): BatchNorm2d(64, eps=0.001, momentum=0.1, affine=True, track_running_stats=True)(relu): ReLU(inplace=True))(branch5x5_1): BasicConv2d((conv): Conv2d(192, 48, kernel_size=(1, 1), stride=(1, 1), bias=False)(bn): BatchNorm2d(48, eps=0.001, momentum=0.1, affine=True, track_running_stats=True)(relu): ReLU(inplace=True))(branch5x5_2): BasicConv2d((conv): Conv2d(48, 64, kernel_size=(5, 5), stride=(1, 1), padding=(2, 2), bias=False)(bn): BatchNorm2d(64, eps=0.001, momentum=0.1, affine=True, track_running_stats=True)(relu): ReLU(inplace=True))(branch3x3dbl_1): BasicConv2d((conv): Conv2d(192, 64, kernel_size=(1, 1), stride=(1, 1), bias=False)(bn): BatchNorm2d(64, eps=0.001, momentum=0.1, affine=True, track_running_stats=True)(relu): ReLU(inplace=True))(branch3x3dbl_2): BasicConv2d((conv): Conv2d(64, 96, kernel_size=(3, 3), stride=(1, 1), padding=(1, 1), bias=False)(bn): BatchNorm2d(96, eps=0.001, momentum=0.1, affine=True, track_running_stats=True)(relu): ReLU(inplace=True))(branch3x3dbl_3): BasicConv2d((conv): Conv2d(96, 96, kernel_size=(3, 3), stride=(1, 1), padding=(1, 1), bias=False)(bn): BatchNorm2d(96, eps=0.001, momentum=0.1, affine=True, track_running_stats=True)(relu): ReLU(inplace=True))(branch_pool): BasicConv2d((conv): Conv2d(192, 32, kernel_size=(1, 1), stride=(1, 1), bias=False)(bn): BatchNorm2d(32, eps=0.001, momentum=0.1, affine=True, track_running_stats=True)(relu): ReLU(inplace=True)))(Mixed_5c): InceptionA((branch1x1): BasicConv2d((conv): Conv2d(256, 64, kernel_size=(1, 1), stride=(1, 1), bias=False)(bn): BatchNorm2d(64, eps=0.001, momentum=0.1, affine=True, track_running_stats=True)(relu): ReLU(inplace=True))(branch5x5_1): BasicConv2d((conv): Conv2d(256, 48, kernel_size=(1, 1), stride=(1, 1), bias=False)(bn): BatchNorm2d(48, eps=0.001, momentum=0.1, affine=True, track_running_stats=True)(relu): ReLU(inplace=True))(branch5x5_2): BasicConv2d((conv): Conv2d(48, 64, kernel_size=(5, 5), stride=(1, 1), padding=(2, 2), bias=False)(bn): BatchNorm2d(64, eps=0.001, momentum=0.1, affine=True, track_running_stats=True)(relu): ReLU(inplace=True))(branch3x3dbl_1): BasicConv2d((conv): Conv2d(256, 64, kernel_size=(1, 1), stride=(1, 1), bias=False)(bn): BatchNorm2d(64, eps=0.001, momentum=0.1, affine=True, track_running_stats=True)(relu): ReLU(inplace=True))(branch3x3dbl_2): BasicConv2d((conv): Conv2d(64, 96, kernel_size=(3, 3), stride=(1, 1), padding=(1, 1), bias=False)(bn): BatchNorm2d(96, eps=0.001, momentum=0.1, affine=True, track_running_stats=True)(relu): ReLU(inplace=True))(branch3x3dbl_3): BasicConv2d((conv): Conv2d(96, 96, kernel_size=(3, 3), stride=(1, 1), padding=(1, 1), bias=False)(bn): BatchNorm2d(96, eps=0.001, momentum=0.1, affine=True, track_running_stats=True)(relu): ReLU(inplace=True))(branch_pool): BasicConv2d((conv): Conv2d(256, 64, kernel_size=(1, 1), stride=(1, 1), bias=False)(bn): BatchNorm2d(64, eps=0.001, momentum=0.1, affine=True, track_running_stats=True)(relu): ReLU(inplace=True)))(Mixed_5d): InceptionA((branch1x1): BasicConv2d((conv): Conv2d(288, 64, kernel_size=(1, 1), stride=(1, 1), bias=False)(bn): BatchNorm2d(64, eps=0.001, momentum=0.1, affine=True, track_running_stats=True)(relu): ReLU(inplace=True))(branch5x5_1): BasicConv2d((conv): Conv2d(288, 48, kernel_size=(1, 1), stride=(1, 1), bias=False)(bn): BatchNorm2d(48, eps=0.001, momentum=0.1, affine=True, track_running_stats=True)(relu): ReLU(inplace=True))(branch5x5_2): BasicConv2d((conv): Conv2d(48, 64, kernel_size=(5, 5), stride=(1, 1), padding=(2, 2), bias=False)(bn): BatchNorm2d(64, eps=0.001, momentum=0.1, affine=True, track_running_stats=True)(relu): ReLU(inplace=True))(branch3x3dbl_1): BasicConv2d((conv): Conv2d(288, 64, kernel_size=(1, 1), stride=(1, 1), bias=False)(bn): BatchNorm2d(64, eps=0.001, momentum=0.1, affine=True, track_running_stats=True)(relu): ReLU(inplace=True))(branch3x3dbl_2): BasicConv2d((conv): Conv2d(64, 96, kernel_size=(3, 3), stride=(1, 1), padding=(1, 1), bias=False)(bn): BatchNorm2d(96, eps=0.001, momentum=0.1, affine=True, track_running_stats=True)(relu): ReLU(inplace=True))(branch3x3dbl_3): BasicConv2d((conv): Conv2d(96, 96, kernel_size=(3, 3), stride=(1, 1), padding=(1, 1), bias=False)(bn): BatchNorm2d(96, eps=0.001, momentum=0.1, affine=True, track_running_stats=True)(relu): ReLU(inplace=True))(branch_pool): BasicConv2d((conv): Conv2d(288, 64, kernel_size=(1, 1), stride=(1, 1), bias=False)(bn): BatchNorm2d(64, eps=0.001, momentum=0.1, affine=True, track_running_stats=True)(relu): ReLU(inplace=True)))(Mixed_6a): ReductionA((branch3x3): BasicConv2d((conv): Conv2d(288, 384, kernel_size=(3, 3), stride=(2, 2), bias=False)(bn): BatchNorm2d(384, eps=0.001, momentum=0.1, affine=True, track_running_stats=True)(relu): ReLU(inplace=True))(branch3x3dbl_1): BasicConv2d((conv): Conv2d(288, 64, kernel_size=(1, 1), stride=(1, 1), bias=False)(bn): BatchNorm2d(64, eps=0.001, momentum=0.1, affine=True, track_running_stats=True)(relu): ReLU(inplace=True))(branch3x3dbl_2): BasicConv2d((conv): Conv2d(64, 96, kernel_size=(3, 3), stride=(1, 1), padding=(1, 1), bias=False)(bn): BatchNorm2d(96, eps=0.001, momentum=0.1, affine=True, track_running_stats=True)(relu): ReLU(inplace=True))(branch3x3dbl_3): BasicConv2d((conv): Conv2d(96, 96, kernel_size=(3, 3), stride=(2, 2), bias=False)(bn): BatchNorm2d(96, eps=0.001, momentum=0.1, affine=True, track_running_stats=True)(relu): ReLU(inplace=True)))(Mixed_6b): InceptionB((branch1x1): BasicConv2d((conv): Conv2d(768, 192, kernel_size=(1, 1), stride=(1, 1), bias=False)(bn): BatchNorm2d(192, eps=0.001, momentum=0.1, affine=True, track_running_stats=True)(relu): ReLU(inplace=True))(branch7x7_1): BasicConv2d((conv): Conv2d(768, 128, kernel_size=(1, 1), stride=(1, 1), bias=False)(bn): BatchNorm2d(128, eps=0.001, momentum=0.1, affine=True, track_running_stats=True)(relu): ReLU(inplace=True))(branch7x7_2): BasicConv2d((conv): Conv2d(128, 128, kernel_size=(1, 7), stride=(1, 1), padding=(0, 3), bias=False)(bn): BatchNorm2d(128, eps=0.001, momentum=0.1, affine=True, track_running_stats=True)(relu): ReLU(inplace=True))(branch7x7_3): BasicConv2d((conv): Conv2d(128, 192, kernel_size=(7, 1), stride=(1, 1), padding=(3, 0), bias=False)(bn): BatchNorm2d(192, eps=0.001, momentum=0.1, affine=True, track_running_stats=True)(relu): ReLU(inplace=True))(branch7x7dbl_1): BasicConv2d((conv): Conv2d(768, 128, kernel_size=(1, 1), stride=(1, 1), bias=False)(bn): BatchNorm2d(128, eps=0.001, momentum=0.1, affine=True, track_running_stats=True)(relu): ReLU(inplace=True))(branch7x7dbl_2): BasicConv2d((conv): Conv2d(128, 128, kernel_size=(7, 1), stride=(1, 1), padding=(3, 0), bias=False)(bn): BatchNorm2d(128, eps=0.001, momentum=0.1, affine=True, track_running_stats=True)(relu): ReLU(inplace=True))(branch7x7dbl_3): BasicConv2d((conv): Conv2d(128, 128, kernel_size=(1, 7), stride=(1, 1), padding=(0, 3), bias=False)(bn): BatchNorm2d(128, eps=0.001, momentum=0.1, affine=True, track_running_stats=True)(relu): ReLU(inplace=True))(branch7x7dbl_4): BasicConv2d((conv): Conv2d(128, 128, kernel_size=(7, 1), stride=(1, 1), padding=(3, 0), bias=False)(bn): BatchNorm2d(128, eps=0.001, momentum=0.1, affine=True, track_running_stats=True)(relu): ReLU(inplace=True))(branch7x7dbl_5): BasicConv2d((conv): Conv2d(128, 192, kernel_size=(1, 7), stride=(1, 1), padding=(0, 3), bias=False)(bn): BatchNorm2d(192, eps=0.001, momentum=0.1, affine=True, track_running_stats=True)(relu): ReLU(inplace=True))(branch_pool): BasicConv2d((conv): Conv2d(768, 192, kernel_size=(1, 1), stride=(1, 1), bias=False)(bn): BatchNorm2d(192, eps=0.001, momentum=0.1, affine=True, track_running_stats=True)(relu): ReLU(inplace=True)))(Mixed_6c): InceptionB((branch1x1): BasicConv2d((conv): Conv2d(768, 192, kernel_size=(1, 1), stride=(1, 1), bias=False)(bn): BatchNorm2d(192, eps=0.001, momentum=0.1, affine=True, track_running_stats=True)(relu): ReLU(inplace=True))(branch7x7_1): BasicConv2d((conv): Conv2d(768, 160, kernel_size=(1, 1), stride=(1, 1), bias=False)(bn): BatchNorm2d(160, eps=0.001, momentum=0.1, affine=True, track_running_stats=True)(relu): ReLU(inplace=True))(branch7x7_2): BasicConv2d((conv): Conv2d(160, 160, kernel_size=(1, 7), stride=(1, 1), padding=(0, 3), bias=False)(bn): BatchNorm2d(160, eps=0.001, momentum=0.1, affine=True, track_running_stats=True)(relu): ReLU(inplace=True))(branch7x7_3): BasicConv2d((conv): Conv2d(160, 192, kernel_size=(7, 1), stride=(1, 1), padding=(3, 0), bias=False)(bn): BatchNorm2d(192, eps=0.001, momentum=0.1, affine=True, track_running_stats=True)(relu): ReLU(inplace=True))(branch7x7dbl_1): BasicConv2d((conv): Conv2d(768, 160, kernel_size=(1, 1), stride=(1, 1), bias=False)(bn): BatchNorm2d(160, eps=0.001, momentum=0.1, affine=True, track_running_stats=True)(relu): ReLU(inplace=True))(branch7x7dbl_2): BasicConv2d((conv): Conv2d(160, 160, kernel_size=(7, 1), stride=(1, 1), padding=(3, 0), bias=False)(bn): BatchNorm2d(160, eps=0.001, momentum=0.1, affine=True, track_running_stats=True)(relu): ReLU(inplace=True))(branch7x7dbl_3): BasicConv2d((conv): Conv2d(160, 160, kernel_size=(1, 7), stride=(1, 1), padding=(0, 3), bias=False)(bn): BatchNorm2d(160, eps=0.001, momentum=0.1, affine=True, track_running_stats=True)(relu): ReLU(inplace=True))(branch7x7dbl_4): BasicConv2d((conv): Conv2d(160, 160, kernel_size=(7, 1), stride=(1, 1), padding=(3, 0), bias=False)(bn): BatchNorm2d(160, eps=0.001, momentum=0.1, affine=True, track_running_stats=True)(relu): ReLU(inplace=True))(branch7x7dbl_5): BasicConv2d((conv): Conv2d(160, 192, kernel_size=(1, 7), stride=(1, 1), padding=(0, 3), bias=False)(bn): BatchNorm2d(192, eps=0.001, momentum=0.1, affine=True, track_running_stats=True)(relu): ReLU(inplace=True))(branch_pool): BasicConv2d((conv): Conv2d(768, 192, kernel_size=(1, 1), stride=(1, 1), bias=False)(bn): BatchNorm2d(192, eps=0.001, momentum=0.1, affine=True, track_running_stats=True)(relu): ReLU(inplace=True)))(Mixed_6d): InceptionB((branch1x1): BasicConv2d((conv): Conv2d(768, 192, kernel_size=(1, 1), stride=(1, 1), bias=False)(bn): BatchNorm2d(192, eps=0.001, momentum=0.1, affine=True, track_running_stats=True)(relu): ReLU(inplace=True))(branch7x7_1): BasicConv2d((conv): Conv2d(768, 160, kernel_size=(1, 1), stride=(1, 1), bias=False)(bn): BatchNorm2d(160, eps=0.001, momentum=0.1, affine=True, track_running_stats=True)(relu): ReLU(inplace=True))(branch7x7_2): BasicConv2d((conv): Conv2d(160, 160, kernel_size=(1, 7), stride=(1, 1), padding=(0, 3), bias=False)(bn): BatchNorm2d(160, eps=0.001, momentum=0.1, affine=True, track_running_stats=True)(relu): ReLU(inplace=True))(branch7x7_3): BasicConv2d((conv): Conv2d(160, 192, kernel_size=(7, 1), stride=(1, 1), padding=(3, 0), bias=False)(bn): BatchNorm2d(192, eps=0.001, momentum=0.1, affine=True, track_running_stats=True)(relu): ReLU(inplace=True))(branch7x7dbl_1): BasicConv2d((conv): Conv2d(768, 160, kernel_size=(1, 1), stride=(1, 1), bias=False)(bn): BatchNorm2d(160, eps=0.001, momentum=0.1, affine=True, track_running_stats=True)(relu): ReLU(inplace=True))(branch7x7dbl_2): BasicConv2d((conv): Conv2d(160, 160, kernel_size=(7, 1), stride=(1, 1), padding=(3, 0), bias=False)(bn): BatchNorm2d(160, eps=0.001, momentum=0.1, affine=True, track_running_stats=True)(relu): ReLU(inplace=True))(branch7x7dbl_3): BasicConv2d((conv): Conv2d(160, 160, kernel_size=(1, 7), stride=(1, 1), padding=(0, 3), bias=False)(bn): BatchNorm2d(160, eps=0.001, momentum=0.1, affine=True, track_running_stats=True)(relu): ReLU(inplace=True))(branch7x7dbl_4): BasicConv2d((conv): Conv2d(160, 160, kernel_size=(7, 1), stride=(1, 1), padding=(3, 0), bias=False)(bn): BatchNorm2d(160, eps=0.001, momentum=0.1, affine=True, track_running_stats=True)(relu): ReLU(inplace=True))(branch7x7dbl_5): BasicConv2d((conv): Conv2d(160, 192, kernel_size=(1, 7), stride=(1, 1), padding=(0, 3), bias=False)(bn): BatchNorm2d(192, eps=0.001, momentum=0.1, affine=True, track_running_stats=True)(relu): ReLU(inplace=True))(branch_pool): BasicConv2d((conv): Conv2d(768, 192, kernel_size=(1, 1), stride=(1, 1), bias=False)(bn): BatchNorm2d(192, eps=0.001, momentum=0.1, affine=True, track_running_stats=True)(relu): ReLU(inplace=True)))(Mixed_6e): InceptionB((branch1x1): BasicConv2d((conv): Conv2d(768, 192, kernel_size=(1, 1), stride=(1, 1), bias=False)(bn): BatchNorm2d(192, eps=0.001, momentum=0.1, affine=True, track_running_stats=True)(relu): ReLU(inplace=True))(branch7x7_1): BasicConv2d((conv): Conv2d(768, 192, kernel_size=(1, 1), stride=(1, 1), bias=False)(bn): BatchNorm2d(192, eps=0.001, momentum=0.1, affine=True, track_running_stats=True)(relu): ReLU(inplace=True))(branch7x7_2): BasicConv2d((conv): Conv2d(192, 192, kernel_size=(1, 7), stride=(1, 1), padding=(0, 3), bias=False)(bn): BatchNorm2d(192, eps=0.001, momentum=0.1, affine=True, track_running_stats=True)(relu): ReLU(inplace=True))(branch7x7_3): BasicConv2d((conv): Conv2d(192, 192, kernel_size=(7, 1), stride=(1, 1), padding=(3, 0), bias=False)(bn): BatchNorm2d(192, eps=0.001, momentum=0.1, affine=True, track_running_stats=True)(relu): ReLU(inplace=True))(branch7x7dbl_1): BasicConv2d((conv): Conv2d(768, 192, kernel_size=(1, 1), stride=(1, 1), bias=False)(bn): BatchNorm2d(192, eps=0.001, momentum=0.1, affine=True, track_running_stats=True)(relu): ReLU(inplace=True))(branch7x7dbl_2): BasicConv2d((conv): Conv2d(192, 192, kernel_size=(7, 1), stride=(1, 1), padding=(3, 0), bias=False)(bn): BatchNorm2d(192, eps=0.001, momentum=0.1, affine=True, track_running_stats=True)(relu): ReLU(inplace=True))(branch7x7dbl_3): BasicConv2d((conv): Conv2d(192, 192, kernel_size=(1, 7), stride=(1, 1), padding=(0, 3), bias=False)(bn): BatchNorm2d(192, eps=0.001, momentum=0.1, affine=True, track_running_stats=True)(relu): ReLU(inplace=True))(branch7x7dbl_4): BasicConv2d((conv): Conv2d(192, 192, kernel_size=(7, 1), stride=(1, 1), padding=(3, 0), bias=False)(bn): BatchNorm2d(192, eps=0.001, momentum=0.1, affine=True, track_running_stats=True)(relu): ReLU(inplace=True))(branch7x7dbl_5): BasicConv2d((conv): Conv2d(192, 192, kernel_size=(1, 7), stride=(1, 1), padding=(0, 3), bias=False)(bn): BatchNorm2d(192, eps=0.001, momentum=0.1, affine=True, track_running_stats=True)(relu): ReLU(inplace=True))(branch_pool): BasicConv2d((conv): Conv2d(768, 192, kernel_size=(1, 1), stride=(1, 1), bias=False)(bn): BatchNorm2d(192, eps=0.001, momentum=0.1, affine=True, track_running_stats=True)(relu): ReLU(inplace=True)))(Mixed_7a): ReductionB((branch3x3_1): BasicConv2d((conv): Conv2d(768, 192, kernel_size=(1, 1), stride=(1, 1), bias=False)(bn): BatchNorm2d(192, eps=0.001, momentum=0.1, affine=True, track_running_stats=True)(relu): ReLU(inplace=True))(branch3x3_2): BasicConv2d((conv): Conv2d(192, 320, kernel_size=(3, 3), stride=(2, 2), bias=False)(bn): BatchNorm2d(320, eps=0.001, momentum=0.1, affine=True, track_running_stats=True)(relu): ReLU(inplace=True))(branch7x7x3_1): BasicConv2d((conv): Conv2d(768, 192, kernel_size=(1, 1), stride=(1, 1), bias=False)(bn): BatchNorm2d(192, eps=0.001, momentum=0.1, affine=True, track_running_stats=True)(relu): ReLU(inplace=True))(branch7x7x3_2): BasicConv2d((conv): Conv2d(192, 192, kernel_size=(1, 7), stride=(1, 1), padding=(0, 3), bias=False)(bn): BatchNorm2d(192, eps=0.001, momentum=0.1, affine=True, track_running_stats=True)(relu): ReLU(inplace=True))(branch7x7x3_3): BasicConv2d((conv): Conv2d(192, 192, kernel_size=(7, 1), stride=(1, 1), padding=(3, 0), bias=False)(bn): BatchNorm2d(192, eps=0.001, momentum=0.1, affine=True, track_running_stats=True)(relu): ReLU(inplace=True))(branch7x7x3_4): BasicConv2d((conv): Conv2d(192, 192, kernel_size=(3, 3), stride=(2, 2), bias=False)(bn): BatchNorm2d(192, eps=0.001, momentum=0.1, affine=True, track_running_stats=True)(relu): ReLU(inplace=True)))(Mixed_7b): InceptionC((branch1x1): BasicConv2d((conv): Conv2d(1280, 320, kernel_size=(1, 1), stride=(1, 1), bias=False)(bn): BatchNorm2d(320, eps=0.001, momentum=0.1, affine=True, track_running_stats=True)(relu): ReLU(inplace=True))(branch3x3_1): BasicConv2d((conv): Conv2d(1280, 384, kernel_size=(1, 1), stride=(1, 1), bias=False)(bn): BatchNorm2d(384, eps=0.001, momentum=0.1, affine=True, track_running_stats=True)(relu): ReLU(inplace=True))(branch3x3_2a): BasicConv2d((conv): Conv2d(384, 384, kernel_size=(1, 3), stride=(1, 1), padding=(0, 1), bias=False)(bn): BatchNorm2d(384, eps=0.001, momentum=0.1, affine=True, track_running_stats=True)(relu): ReLU(inplace=True))(branch3x3_2b): BasicConv2d((conv): Conv2d(384, 384, kernel_size=(3, 1), stride=(1, 1), padding=(1, 0), bias=False)(bn): BatchNorm2d(384, eps=0.001, momentum=0.1, affine=True, track_running_stats=True)(relu): ReLU(inplace=True))(branch3x3dbl_1): BasicConv2d((conv): Conv2d(1280, 448, kernel_size=(1, 1), stride=(1, 1), bias=False)(bn): BatchNorm2d(448, eps=0.001, momentum=0.1, affine=True, track_running_stats=True)(relu): ReLU(inplace=True))(branch3x3dbl_2): BasicConv2d((conv): Conv2d(448, 384, kernel_size=(3, 3), stride=(1, 1), padding=(1, 1), bias=False)(bn): BatchNorm2d(384, eps=0.001, momentum=0.1, affine=True, track_running_stats=True)(relu): ReLU(inplace=True))(branch3x3dbl_3a): BasicConv2d((conv): Conv2d(384, 384, kernel_size=(1, 3), stride=(1, 1), padding=(0, 1), bias=False)(bn): BatchNorm2d(384, eps=0.001, momentum=0.1, affine=True, track_running_stats=True)(relu): ReLU(inplace=True))(branch3x3dbl_3b): BasicConv2d((conv): Conv2d(384, 384, kernel_size=(3, 1), stride=(1, 1), padding=(1, 0), bias=False)(bn): BatchNorm2d(384, eps=0.001, momentum=0.1, affine=True, track_running_stats=True)(relu): ReLU(inplace=True))(branch_pool): BasicConv2d((conv): Conv2d(1280, 192, kernel_size=(1, 1), stride=(1, 1), bias=False)(bn): BatchNorm2d(192, eps=0.001, momentum=0.1, affine=True, track_running_stats=True)(relu): ReLU(inplace=True)))(Mixed_7c): InceptionC((branch1x1): BasicConv2d((conv): Conv2d(2048, 320, kernel_size=(1, 1), stride=(1, 1), bias=False)(bn): BatchNorm2d(320, eps=0.001, momentum=0.1, affine=True, track_running_stats=True)(relu): ReLU(inplace=True))(branch3x3_1): BasicConv2d((conv): Conv2d(2048, 384, kernel_size=(1, 1), stride=(1, 1), bias=False)(bn): BatchNorm2d(384, eps=0.001, momentum=0.1, affine=True, track_running_stats=True)(relu): ReLU(inplace=True))(branch3x3_2a): BasicConv2d((conv): Conv2d(384, 384, kernel_size=(1, 3), stride=(1, 1), padding=(0, 1), bias=False)(bn): BatchNorm2d(384, eps=0.001, momentum=0.1, affine=True, track_running_stats=True)(relu): ReLU(inplace=True))(branch3x3_2b): BasicConv2d((conv): Conv2d(384, 384, kernel_size=(3, 1), stride=(1, 1), padding=(1, 0), bias=False)(bn): BatchNorm2d(384, eps=0.001, momentum=0.1, affine=True, track_running_stats=True)(relu): ReLU(inplace=True))(branch3x3dbl_1): BasicConv2d((conv): Conv2d(2048, 448, kernel_size=(1, 1), stride=(1, 1), bias=False)(bn): BatchNorm2d(448, eps=0.001, momentum=0.1, affine=True, track_running_stats=True)(relu): ReLU(inplace=True))(branch3x3dbl_2): BasicConv2d((conv): Conv2d(448, 384, kernel_size=(3, 3), stride=(1, 1), padding=(1, 1), bias=False)(bn): BatchNorm2d(384, eps=0.001, momentum=0.1, affine=True, track_running_stats=True)(relu): ReLU(inplace=True))(branch3x3dbl_3a): BasicConv2d((conv): Conv2d(384, 384, kernel_size=(1, 3), stride=(1, 1), padding=(0, 1), bias=False)(bn): BatchNorm2d(384, eps=0.001, momentum=0.1, affine=True, track_running_stats=True)(relu): ReLU(inplace=True))(branch3x3dbl_3b): BasicConv2d((conv): Conv2d(384, 384, kernel_size=(3, 1), stride=(1, 1), padding=(1, 0), bias=False)(bn): BatchNorm2d(384, eps=0.001, momentum=0.1, affine=True, track_running_stats=True)(relu): ReLU(inplace=True))(branch_pool): BasicConv2d((conv): Conv2d(2048, 192, kernel_size=(1, 1), stride=(1, 1), bias=False)(bn): BatchNorm2d(192, eps=0.001, momentum=0.1, affine=True, track_running_stats=True)(relu): ReLU(inplace=True)))(fc): Linear(in_features=2048, out_features=4, bias=True)
)

9. 查看模型详情

import torchsummary as summary
summary.summary(model,(3,299,299))

运行结果:

----------------------------------------------------------------Layer (type)               Output Shape         Param #
================================================================Conv2d-1         [-1, 32, 149, 149]             864BatchNorm2d-2         [-1, 32, 149, 149]              64ReLU-3         [-1, 32, 149, 149]               0BasicConv2d-4         [-1, 32, 149, 149]               0Conv2d-5         [-1, 32, 147, 147]           9,216BatchNorm2d-6         [-1, 32, 147, 147]              64ReLU-7         [-1, 32, 147, 147]               0BasicConv2d-8         [-1, 32, 147, 147]               0Conv2d-9         [-1, 64, 147, 147]          18,432BatchNorm2d-10         [-1, 64, 147, 147]             128ReLU-11         [-1, 64, 147, 147]               0BasicConv2d-12         [-1, 64, 147, 147]               0Conv2d-13           [-1, 80, 73, 73]           5,120BatchNorm2d-14           [-1, 80, 73, 73]             160ReLU-15           [-1, 80, 73, 73]               0BasicConv2d-16           [-1, 80, 73, 73]               0Conv2d-17          [-1, 192, 71, 71]         138,240BatchNorm2d-18          [-1, 192, 71, 71]             384ReLU-19          [-1, 192, 71, 71]               0BasicConv2d-20          [-1, 192, 71, 71]               0Conv2d-21           [-1, 64, 35, 35]          12,288BatchNorm2d-22           [-1, 64, 35, 35]             128ReLU-23           [-1, 64, 35, 35]               0BasicConv2d-24           [-1, 64, 35, 35]               0Conv2d-25           [-1, 48, 35, 35]           9,216BatchNorm2d-26           [-1, 48, 35, 35]              96ReLU-27           [-1, 48, 35, 35]               0BasicConv2d-28           [-1, 48, 35, 35]               0Conv2d-29           [-1, 64, 35, 35]          76,800BatchNorm2d-30           [-1, 64, 35, 35]             128ReLU-31           [-1, 64, 35, 35]               0BasicConv2d-32           [-1, 64, 35, 35]               0Conv2d-33           [-1, 64, 35, 35]          12,288BatchNorm2d-34           [-1, 64, 35, 35]             128ReLU-35           [-1, 64, 35, 35]               0BasicConv2d-36           [-1, 64, 35, 35]               0Conv2d-37           [-1, 96, 35, 35]          55,296BatchNorm2d-38           [-1, 96, 35, 35]             192ReLU-39           [-1, 96, 35, 35]               0BasicConv2d-40           [-1, 96, 35, 35]               0Conv2d-41           [-1, 96, 35, 35]          82,944BatchNorm2d-42           [-1, 96, 35, 35]             192ReLU-43           [-1, 96, 35, 35]               0BasicConv2d-44           [-1, 96, 35, 35]               0Conv2d-45           [-1, 32, 35, 35]           6,144BatchNorm2d-46           [-1, 32, 35, 35]              64ReLU-47           [-1, 32, 35, 35]               0BasicConv2d-48           [-1, 32, 35, 35]               0InceptionA-49          [-1, 256, 35, 35]               0Conv2d-50           [-1, 64, 35, 35]          16,384BatchNorm2d-51           [-1, 64, 35, 35]             128ReLU-52           [-1, 64, 35, 35]               0BasicConv2d-53           [-1, 64, 35, 35]               0Conv2d-54           [-1, 48, 35, 35]          12,288BatchNorm2d-55           [-1, 48, 35, 35]              96ReLU-56           [-1, 48, 35, 35]               0BasicConv2d-57           [-1, 48, 35, 35]               0Conv2d-58           [-1, 64, 35, 35]          76,800BatchNorm2d-59           [-1, 64, 35, 35]             128ReLU-60           [-1, 64, 35, 35]               0BasicConv2d-61           [-1, 64, 35, 35]               0Conv2d-62           [-1, 64, 35, 35]          16,384BatchNorm2d-63           [-1, 64, 35, 35]             128ReLU-64           [-1, 64, 35, 35]               0BasicConv2d-65           [-1, 64, 35, 35]               0Conv2d-66           [-1, 96, 35, 35]          55,296BatchNorm2d-67           [-1, 96, 35, 35]             192ReLU-68           [-1, 96, 35, 35]               0BasicConv2d-69           [-1, 96, 35, 35]               0Conv2d-70           [-1, 96, 35, 35]          82,944BatchNorm2d-71           [-1, 96, 35, 35]             192ReLU-72           [-1, 96, 35, 35]               0BasicConv2d-73           [-1, 96, 35, 35]               0Conv2d-74           [-1, 64, 35, 35]          16,384BatchNorm2d-75           [-1, 64, 35, 35]             128ReLU-76           [-1, 64, 35, 35]               0BasicConv2d-77           [-1, 64, 35, 35]               0InceptionA-78          [-1, 288, 35, 35]               0Conv2d-79           [-1, 64, 35, 35]          18,432BatchNorm2d-80           [-1, 64, 35, 35]             128ReLU-81           [-1, 64, 35, 35]               0BasicConv2d-82           [-1, 64, 35, 35]               0Conv2d-83           [-1, 48, 35, 35]          13,824BatchNorm2d-84           [-1, 48, 35, 35]              96ReLU-85           [-1, 48, 35, 35]               0BasicConv2d-86           [-1, 48, 35, 35]               0Conv2d-87           [-1, 64, 35, 35]          76,800BatchNorm2d-88           [-1, 64, 35, 35]             128ReLU-89           [-1, 64, 35, 35]               0BasicConv2d-90           [-1, 64, 35, 35]               0Conv2d-91           [-1, 64, 35, 35]          18,432BatchNorm2d-92           [-1, 64, 35, 35]             128ReLU-93           [-1, 64, 35, 35]               0BasicConv2d-94           [-1, 64, 35, 35]               0Conv2d-95           [-1, 96, 35, 35]          55,296BatchNorm2d-96           [-1, 96, 35, 35]             192ReLU-97           [-1, 96, 35, 35]               0BasicConv2d-98           [-1, 96, 35, 35]               0Conv2d-99           [-1, 96, 35, 35]          82,944BatchNorm2d-100           [-1, 96, 35, 35]             192ReLU-101           [-1, 96, 35, 35]               0BasicConv2d-102           [-1, 96, 35, 35]               0Conv2d-103           [-1, 64, 35, 35]          18,432BatchNorm2d-104           [-1, 64, 35, 35]             128ReLU-105           [-1, 64, 35, 35]               0BasicConv2d-106           [-1, 64, 35, 35]               0InceptionA-107          [-1, 288, 35, 35]               0Conv2d-108          [-1, 384, 17, 17]         995,328BatchNorm2d-109          [-1, 384, 17, 17]             768ReLU-110          [-1, 384, 17, 17]               0BasicConv2d-111          [-1, 384, 17, 17]               0Conv2d-112           [-1, 64, 35, 35]          18,432BatchNorm2d-113           [-1, 64, 35, 35]             128ReLU-114           [-1, 64, 35, 35]               0BasicConv2d-115           [-1, 64, 35, 35]               0Conv2d-116           [-1, 96, 35, 35]          55,296BatchNorm2d-117           [-1, 96, 35, 35]             192ReLU-118           [-1, 96, 35, 35]               0BasicConv2d-119           [-1, 96, 35, 35]               0Conv2d-120           [-1, 96, 17, 17]          82,944BatchNorm2d-121           [-1, 96, 17, 17]             192ReLU-122           [-1, 96, 17, 17]               0BasicConv2d-123           [-1, 96, 17, 17]               0ReductionA-124          [-1, 768, 17, 17]               0Conv2d-125          [-1, 192, 17, 17]         147,456BatchNorm2d-126          [-1, 192, 17, 17]             384ReLU-127          [-1, 192, 17, 17]               0BasicConv2d-128          [-1, 192, 17, 17]               0Conv2d-129          [-1, 128, 17, 17]          98,304BatchNorm2d-130          [-1, 128, 17, 17]             256ReLU-131          [-1, 128, 17, 17]               0BasicConv2d-132          [-1, 128, 17, 17]               0Conv2d-133          [-1, 128, 17, 17]         114,688BatchNorm2d-134          [-1, 128, 17, 17]             256ReLU-135          [-1, 128, 17, 17]               0BasicConv2d-136          [-1, 128, 17, 17]               0Conv2d-137          [-1, 192, 17, 17]         172,032BatchNorm2d-138          [-1, 192, 17, 17]             384ReLU-139          [-1, 192, 17, 17]               0BasicConv2d-140          [-1, 192, 17, 17]               0Conv2d-141          [-1, 128, 17, 17]          98,304BatchNorm2d-142          [-1, 128, 17, 17]             256ReLU-143          [-1, 128, 17, 17]               0BasicConv2d-144          [-1, 128, 17, 17]               0Conv2d-145          [-1, 128, 17, 17]         114,688BatchNorm2d-146          [-1, 128, 17, 17]             256ReLU-147          [-1, 128, 17, 17]               0BasicConv2d-148          [-1, 128, 17, 17]               0Conv2d-149          [-1, 128, 17, 17]         114,688BatchNorm2d-150          [-1, 128, 17, 17]             256ReLU-151          [-1, 128, 17, 17]               0BasicConv2d-152          [-1, 128, 17, 17]               0Conv2d-153          [-1, 128, 17, 17]         114,688BatchNorm2d-154          [-1, 128, 17, 17]             256ReLU-155          [-1, 128, 17, 17]               0BasicConv2d-156          [-1, 128, 17, 17]               0Conv2d-157          [-1, 192, 17, 17]         172,032BatchNorm2d-158          [-1, 192, 17, 17]             384ReLU-159          [-1, 192, 17, 17]               0BasicConv2d-160          [-1, 192, 17, 17]               0Conv2d-161          [-1, 192, 17, 17]         147,456BatchNorm2d-162          [-1, 192, 17, 17]             384ReLU-163          [-1, 192, 17, 17]               0BasicConv2d-164          [-1, 192, 17, 17]               0InceptionB-165          [-1, 768, 17, 17]               0Conv2d-166          [-1, 192, 17, 17]         147,456BatchNorm2d-167          [-1, 192, 17, 17]             384ReLU-168          [-1, 192, 17, 17]               0BasicConv2d-169          [-1, 192, 17, 17]               0Conv2d-170          [-1, 160, 17, 17]         122,880BatchNorm2d-171          [-1, 160, 17, 17]             320ReLU-172          [-1, 160, 17, 17]               0BasicConv2d-173          [-1, 160, 17, 17]               0Conv2d-174          [-1, 160, 17, 17]         179,200BatchNorm2d-175          [-1, 160, 17, 17]             320ReLU-176          [-1, 160, 17, 17]               0BasicConv2d-177          [-1, 160, 17, 17]               0Conv2d-178          [-1, 192, 17, 17]         215,040BatchNorm2d-179          [-1, 192, 17, 17]             384ReLU-180          [-1, 192, 17, 17]               0BasicConv2d-181          [-1, 192, 17, 17]               0Conv2d-182          [-1, 160, 17, 17]         122,880BatchNorm2d-183          [-1, 160, 17, 17]             320ReLU-184          [-1, 160, 17, 17]               0BasicConv2d-185          [-1, 160, 17, 17]               0Conv2d-186          [-1, 160, 17, 17]         179,200BatchNorm2d-187          [-1, 160, 17, 17]             320ReLU-188          [-1, 160, 17, 17]               0BasicConv2d-189          [-1, 160, 17, 17]               0Conv2d-190          [-1, 160, 17, 17]         179,200BatchNorm2d-191          [-1, 160, 17, 17]             320ReLU-192          [-1, 160, 17, 17]               0BasicConv2d-193          [-1, 160, 17, 17]               0Conv2d-194          [-1, 160, 17, 17]         179,200BatchNorm2d-195          [-1, 160, 17, 17]             320ReLU-196          [-1, 160, 17, 17]               0BasicConv2d-197          [-1, 160, 17, 17]               0Conv2d-198          [-1, 192, 17, 17]         215,040BatchNorm2d-199          [-1, 192, 17, 17]             384ReLU-200          [-1, 192, 17, 17]               0BasicConv2d-201          [-1, 192, 17, 17]               0Conv2d-202          [-1, 192, 17, 17]         147,456BatchNorm2d-203          [-1, 192, 17, 17]             384ReLU-204          [-1, 192, 17, 17]               0BasicConv2d-205          [-1, 192, 17, 17]               0InceptionB-206          [-1, 768, 17, 17]               0Conv2d-207          [-1, 192, 17, 17]         147,456BatchNorm2d-208          [-1, 192, 17, 17]             384ReLU-209          [-1, 192, 17, 17]               0BasicConv2d-210          [-1, 192, 17, 17]               0Conv2d-211          [-1, 160, 17, 17]         122,880BatchNorm2d-212          [-1, 160, 17, 17]             320ReLU-213          [-1, 160, 17, 17]               0BasicConv2d-214          [-1, 160, 17, 17]               0Conv2d-215          [-1, 160, 17, 17]         179,200BatchNorm2d-216          [-1, 160, 17, 17]             320ReLU-217          [-1, 160, 17, 17]               0BasicConv2d-218          [-1, 160, 17, 17]               0Conv2d-219          [-1, 192, 17, 17]         215,040BatchNorm2d-220          [-1, 192, 17, 17]             384ReLU-221          [-1, 192, 17, 17]               0BasicConv2d-222          [-1, 192, 17, 17]               0Conv2d-223          [-1, 160, 17, 17]         122,880BatchNorm2d-224          [-1, 160, 17, 17]             320ReLU-225          [-1, 160, 17, 17]               0BasicConv2d-226          [-1, 160, 17, 17]               0Conv2d-227          [-1, 160, 17, 17]         179,200BatchNorm2d-228          [-1, 160, 17, 17]             320ReLU-229          [-1, 160, 17, 17]               0BasicConv2d-230          [-1, 160, 17, 17]               0Conv2d-231          [-1, 160, 17, 17]         179,200BatchNorm2d-232          [-1, 160, 17, 17]             320ReLU-233          [-1, 160, 17, 17]               0BasicConv2d-234          [-1, 160, 17, 17]               0Conv2d-235          [-1, 160, 17, 17]         179,200BatchNorm2d-236          [-1, 160, 17, 17]             320ReLU-237          [-1, 160, 17, 17]               0BasicConv2d-238          [-1, 160, 17, 17]               0Conv2d-239          [-1, 192, 17, 17]         215,040BatchNorm2d-240          [-1, 192, 17, 17]             384ReLU-241          [-1, 192, 17, 17]               0BasicConv2d-242          [-1, 192, 17, 17]               0Conv2d-243          [-1, 192, 17, 17]         147,456BatchNorm2d-244          [-1, 192, 17, 17]             384ReLU-245          [-1, 192, 17, 17]               0BasicConv2d-246          [-1, 192, 17, 17]               0InceptionB-247          [-1, 768, 17, 17]               0Conv2d-248          [-1, 192, 17, 17]         147,456BatchNorm2d-249          [-1, 192, 17, 17]             384ReLU-250          [-1, 192, 17, 17]               0BasicConv2d-251          [-1, 192, 17, 17]               0Conv2d-252          [-1, 192, 17, 17]         147,456BatchNorm2d-253          [-1, 192, 17, 17]             384ReLU-254          [-1, 192, 17, 17]               0BasicConv2d-255          [-1, 192, 17, 17]               0Conv2d-256          [-1, 192, 17, 17]         258,048BatchNorm2d-257          [-1, 192, 17, 17]             384ReLU-258          [-1, 192, 17, 17]               0BasicConv2d-259          [-1, 192, 17, 17]               0Conv2d-260          [-1, 192, 17, 17]         258,048BatchNorm2d-261          [-1, 192, 17, 17]             384ReLU-262          [-1, 192, 17, 17]               0BasicConv2d-263          [-1, 192, 17, 17]               0Conv2d-264          [-1, 192, 17, 17]         147,456BatchNorm2d-265          [-1, 192, 17, 17]             384ReLU-266          [-1, 192, 17, 17]               0BasicConv2d-267          [-1, 192, 17, 17]               0Conv2d-268          [-1, 192, 17, 17]         258,048BatchNorm2d-269          [-1, 192, 17, 17]             384ReLU-270          [-1, 192, 17, 17]               0BasicConv2d-271          [-1, 192, 17, 17]               0Conv2d-272          [-1, 192, 17, 17]         258,048BatchNorm2d-273          [-1, 192, 17, 17]             384ReLU-274          [-1, 192, 17, 17]               0BasicConv2d-275          [-1, 192, 17, 17]               0Conv2d-276          [-1, 192, 17, 17]         258,048BatchNorm2d-277          [-1, 192, 17, 17]             384ReLU-278          [-1, 192, 17, 17]               0BasicConv2d-279          [-1, 192, 17, 17]               0Conv2d-280          [-1, 192, 17, 17]         258,048BatchNorm2d-281          [-1, 192, 17, 17]             384ReLU-282          [-1, 192, 17, 17]               0BasicConv2d-283          [-1, 192, 17, 17]               0Conv2d-284          [-1, 192, 17, 17]         147,456BatchNorm2d-285          [-1, 192, 17, 17]             384ReLU-286          [-1, 192, 17, 17]               0BasicConv2d-287          [-1, 192, 17, 17]               0InceptionB-288          [-1, 768, 17, 17]               0Conv2d-289          [-1, 192, 17, 17]         147,456BatchNorm2d-290          [-1, 192, 17, 17]             384ReLU-291          [-1, 192, 17, 17]               0BasicConv2d-292          [-1, 192, 17, 17]               0Conv2d-293            [-1, 320, 8, 8]         552,960BatchNorm2d-294            [-1, 320, 8, 8]             640ReLU-295            [-1, 320, 8, 8]               0BasicConv2d-296            [-1, 320, 8, 8]               0Conv2d-297          [-1, 192, 17, 17]         147,456BatchNorm2d-298          [-1, 192, 17, 17]             384ReLU-299          [-1, 192, 17, 17]               0BasicConv2d-300          [-1, 192, 17, 17]               0Conv2d-301          [-1, 192, 17, 17]         258,048BatchNorm2d-302          [-1, 192, 17, 17]             384ReLU-303          [-1, 192, 17, 17]               0BasicConv2d-304          [-1, 192, 17, 17]               0Conv2d-305          [-1, 192, 17, 17]         258,048BatchNorm2d-306          [-1, 192, 17, 17]             384ReLU-307          [-1, 192, 17, 17]               0BasicConv2d-308          [-1, 192, 17, 17]               0Conv2d-309            [-1, 192, 8, 8]         331,776BatchNorm2d-310            [-1, 192, 8, 8]             384ReLU-311            [-1, 192, 8, 8]               0BasicConv2d-312            [-1, 192, 8, 8]               0ReductionB-313           [-1, 1280, 8, 8]               0Conv2d-314            [-1, 320, 8, 8]         409,600BatchNorm2d-315            [-1, 320, 8, 8]             640ReLU-316            [-1, 320, 8, 8]               0BasicConv2d-317            [-1, 320, 8, 8]               0Conv2d-318            [-1, 384, 8, 8]         491,520BatchNorm2d-319            [-1, 384, 8, 8]             768ReLU-320            [-1, 384, 8, 8]               0BasicConv2d-321            [-1, 384, 8, 8]               0Conv2d-322            [-1, 384, 8, 8]         442,368BatchNorm2d-323            [-1, 384, 8, 8]             768ReLU-324            [-1, 384, 8, 8]               0BasicConv2d-325            [-1, 384, 8, 8]               0Conv2d-326            [-1, 384, 8, 8]         442,368BatchNorm2d-327            [-1, 384, 8, 8]             768ReLU-328            [-1, 384, 8, 8]               0BasicConv2d-329            [-1, 384, 8, 8]               0Conv2d-330            [-1, 448, 8, 8]         573,440BatchNorm2d-331            [-1, 448, 8, 8]             896ReLU-332            [-1, 448, 8, 8]               0BasicConv2d-333            [-1, 448, 8, 8]               0Conv2d-334            [-1, 384, 8, 8]       1,548,288BatchNorm2d-335            [-1, 384, 8, 8]             768ReLU-336            [-1, 384, 8, 8]               0BasicConv2d-337            [-1, 384, 8, 8]               0Conv2d-338            [-1, 384, 8, 8]         442,368BatchNorm2d-339            [-1, 384, 8, 8]             768ReLU-340            [-1, 384, 8, 8]               0BasicConv2d-341            [-1, 384, 8, 8]               0Conv2d-342            [-1, 384, 8, 8]         442,368BatchNorm2d-343            [-1, 384, 8, 8]             768ReLU-344            [-1, 384, 8, 8]               0BasicConv2d-345            [-1, 384, 8, 8]               0Conv2d-346            [-1, 192, 8, 8]         245,760BatchNorm2d-347            [-1, 192, 8, 8]             384ReLU-348            [-1, 192, 8, 8]               0BasicConv2d-349            [-1, 192, 8, 8]               0InceptionC-350           [-1, 2048, 8, 8]               0Conv2d-351            [-1, 320, 8, 8]         655,360BatchNorm2d-352            [-1, 320, 8, 8]             640ReLU-353            [-1, 320, 8, 8]               0BasicConv2d-354            [-1, 320, 8, 8]               0Conv2d-355            [-1, 384, 8, 8]         786,432BatchNorm2d-356            [-1, 384, 8, 8]             768ReLU-357            [-1, 384, 8, 8]               0BasicConv2d-358            [-1, 384, 8, 8]               0Conv2d-359            [-1, 384, 8, 8]         442,368BatchNorm2d-360            [-1, 384, 8, 8]             768ReLU-361            [-1, 384, 8, 8]               0BasicConv2d-362            [-1, 384, 8, 8]               0Conv2d-363            [-1, 384, 8, 8]         442,368BatchNorm2d-364            [-1, 384, 8, 8]             768ReLU-365            [-1, 384, 8, 8]               0BasicConv2d-366            [-1, 384, 8, 8]               0Conv2d-367            [-1, 448, 8, 8]         917,504BatchNorm2d-368            [-1, 448, 8, 8]             896ReLU-369            [-1, 448, 8, 8]               0BasicConv2d-370            [-1, 448, 8, 8]               0Conv2d-371            [-1, 384, 8, 8]       1,548,288BatchNorm2d-372            [-1, 384, 8, 8]             768ReLU-373            [-1, 384, 8, 8]               0BasicConv2d-374            [-1, 384, 8, 8]               0Conv2d-375            [-1, 384, 8, 8]         442,368BatchNorm2d-376            [-1, 384, 8, 8]             768ReLU-377            [-1, 384, 8, 8]               0BasicConv2d-378            [-1, 384, 8, 8]               0Conv2d-379            [-1, 384, 8, 8]         442,368BatchNorm2d-380            [-1, 384, 8, 8]             768ReLU-381            [-1, 384, 8, 8]               0BasicConv2d-382            [-1, 384, 8, 8]               0Conv2d-383            [-1, 192, 8, 8]         393,216BatchNorm2d-384            [-1, 192, 8, 8]             384ReLU-385            [-1, 192, 8, 8]               0BasicConv2d-386            [-1, 192, 8, 8]               0InceptionC-387           [-1, 2048, 8, 8]               0Linear-388                    [-1, 4]           8,196
================================================================
Total params: 21,793,764
Trainable params: 21,793,764
Non-trainable params: 0
----------------------------------------------------------------
Input size (MB): 1.02
Forward/backward pass size (MB): 292.53
Params size (MB): 83.14
Estimated Total Size (MB): 376.69
----------------------------------------------------------------

三、 训练模型

1. 编写训练函数

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

2. 编写测试函数

测试函数和训练函数大致相同,但是由于不进行梯度下降对网络权重进行更新,所以不需要传入优化器

#测试函数
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

3. 正式训练

import copy
opt=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):model.train()epoch_train_acc,epoch_train_loss=train(train_dl,model,loss_fn,opt)model.eval()epoch_test_acc,epoch_test_loss=test(test_dl,model,loss_fn)#保存最佳模型到J9_modelif epoch_test_acc>best_acc:best_acc=epoch_test_accJ9_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=opt.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'D:\THE MNIST DATABASE\J-series\J9_model.pth'
torch.save(model.state_dict(),PATH)

运行结果:

Epoch: 1,Train_acc:77.2%,Train_loss:0.634,Test_acc:89.3%,Test_loss:0.443,Lr:1.00E-04
Epoch: 2,Train_acc:78.3%,Train_loss:0.596,Test_acc:88.9%,Test_loss:0.346,Lr:1.00E-04
Epoch: 3,Train_acc:82.3%,Train_loss:0.477,Test_acc:68.9%,Test_loss:1.772,Lr:1.00E-04
Epoch: 4,Train_acc:83.1%,Train_loss:0.510,Test_acc:88.0%,Test_loss:0.358,Lr:1.00E-04
Epoch: 5,Train_acc:84.8%,Train_loss:0.443,Test_acc:91.1%,Test_loss:0.213,Lr:1.00E-04
Epoch: 6,Train_acc:83.9%,Train_loss:0.432,Test_acc:88.4%,Test_loss:0.301,Lr:1.00E-04
Epoch: 7,Train_acc:87.8%,Train_loss:0.368,Test_acc:88.4%,Test_loss:0.300,Lr:1.00E-04
Epoch: 8,Train_acc:87.9%,Train_loss:0.371,Test_acc:90.2%,Test_loss:0.358,Lr:1.00E-04
Epoch: 9,Train_acc:88.6%,Train_loss:0.361,Test_acc:92.9%,Test_loss:0.183,Lr:1.00E-04
Epoch:10,Train_acc:88.9%,Train_loss:0.310,Test_acc:91.6%,Test_loss:0.277,Lr:1.00E-04
Epoch:11,Train_acc:89.8%,Train_loss:0.306,Test_acc:94.2%,Test_loss:0.174,Lr:1.00E-04
Epoch:12,Train_acc:88.0%,Train_loss:0.357,Test_acc:92.0%,Test_loss:0.301,Lr:1.00E-04
Epoch:13,Train_acc:92.1%,Train_loss:0.246,Test_acc:92.9%,Test_loss:0.266,Lr:1.00E-04
Epoch:14,Train_acc:91.2%,Train_loss:0.269,Test_acc:94.2%,Test_loss:0.163,Lr:1.00E-04
Epoch:15,Train_acc:90.8%,Train_loss:0.294,Test_acc:87.1%,Test_loss:0.331,Lr:1.00E-04
Epoch:16,Train_acc:91.4%,Train_loss:0.251,Test_acc:93.8%,Test_loss:0.177,Lr:1.00E-04
Epoch:17,Train_acc:92.9%,Train_loss:0.214,Test_acc:90.7%,Test_loss:0.276,Lr:1.00E-04
Epoch:18,Train_acc:88.9%,Train_loss:0.315,Test_acc:92.9%,Test_loss:0.215,Lr:1.00E-04
Epoch:19,Train_acc:92.2%,Train_loss:0.231,Test_acc:91.6%,Test_loss:0.251,Lr:1.00E-04
Epoch:20,Train_acc:92.9%,Train_loss:0.228,Test_acc:91.1%,Test_loss:0.229,Lr:1.00E-04
Epoch:21,Train_acc:93.0%,Train_loss:0.211,Test_acc:90.7%,Test_loss:0.245,Lr:1.00E-04
Epoch:22,Train_acc:93.2%,Train_loss:0.235,Test_acc:94.2%,Test_loss:0.150,Lr:1.00E-04
Epoch:23,Train_acc:91.6%,Train_loss:0.259,Test_acc:83.1%,Test_loss:0.375,Lr:1.00E-04
Epoch:24,Train_acc:93.2%,Train_loss:0.209,Test_acc:93.3%,Test_loss:0.219,Lr:1.00E-04
Epoch:25,Train_acc:93.2%,Train_loss:0.183,Test_acc:94.2%,Test_loss:0.181,Lr:1.00E-04
Epoch:26,Train_acc:91.4%,Train_loss:0.242,Test_acc:92.9%,Test_loss:0.249,Lr:1.00E-04
Epoch:27,Train_acc:92.0%,Train_loss:0.220,Test_acc:93.3%,Test_loss:0.234,Lr:1.00E-04
Epoch:28,Train_acc:94.0%,Train_loss:0.195,Test_acc:94.7%,Test_loss:0.141,Lr:1.00E-04
Epoch:29,Train_acc:93.1%,Train_loss:0.189,Test_acc:95.1%,Test_loss:0.191,Lr:1.00E-04
Epoch:30,Train_acc:93.2%,Train_loss:0.226,Test_acc:95.6%,Test_loss:0.204,Lr:1.00E-04
Epoch:31,Train_acc:94.3%,Train_loss:0.147,Test_acc:93.8%,Test_loss:0.219,Lr:1.00E-04
Epoch:32,Train_acc:95.4%,Train_loss:0.150,Test_acc:93.3%,Test_loss:0.225,Lr:1.00E-04
Epoch:33,Train_acc:94.9%,Train_loss:0.165,Test_acc:95.6%,Test_loss:0.169,Lr:1.00E-04
Epoch:34,Train_acc:95.4%,Train_loss:0.150,Test_acc:94.7%,Test_loss:0.193,Lr:1.00E-04
Epoch:35,Train_acc:95.7%,Train_loss:0.100,Test_acc:93.8%,Test_loss:0.145,Lr:1.00E-04
Epoch:36,Train_acc:94.4%,Train_loss:0.198,Test_acc:92.9%,Test_loss:0.167,Lr:1.00E-04
Epoch:37,Train_acc:95.3%,Train_loss:0.163,Test_acc:94.7%,Test_loss:0.110,Lr:1.00E-04
Epoch:38,Train_acc:96.1%,Train_loss:0.120,Test_acc:93.3%,Test_loss:0.177,Lr:1.00E-04
Epoch:39,Train_acc:94.6%,Train_loss:0.197,Test_acc:94.2%,Test_loss:0.196,Lr:1.00E-04
Epoch:40,Train_acc:95.4%,Train_loss:0.132,Test_acc:96.0%,Test_loss:0.117,Lr:1.00E-04
Epoch:41,Train_acc:96.6%,Train_loss:0.115,Test_acc:96.9%,Test_loss:0.116,Lr:1.00E-04
Epoch:42,Train_acc:96.1%,Train_loss:0.113,Test_acc:95.6%,Test_loss:0.119,Lr:1.00E-04
Epoch:43,Train_acc:97.1%,Train_loss:0.103,Test_acc:93.3%,Test_loss:0.218,Lr:1.00E-04
Epoch:44,Train_acc:94.9%,Train_loss:0.168,Test_acc:89.3%,Test_loss:0.251,Lr:1.00E-04
Epoch:45,Train_acc:97.3%,Train_loss:0.094,Test_acc:93.3%,Test_loss:0.180,Lr:1.00E-04
Epoch:46,Train_acc:97.4%,Train_loss:0.086,Test_acc:94.7%,Test_loss:0.210,Lr:1.00E-04
Epoch:47,Train_acc:95.3%,Train_loss:0.125,Test_acc:95.1%,Test_loss:0.200,Lr:1.00E-04
Epoch:48,Train_acc:95.9%,Train_loss:0.131,Test_acc:94.7%,Test_loss:0.159,Lr:1.00E-04
Epoch:49,Train_acc:95.0%,Train_loss:0.147,Test_acc:93.8%,Test_loss:0.218,Lr:1.00E-04
Epoch:50,Train_acc:97.6%,Train_loss:0.076,Test_acc:95.6%,Test_loss:0.172,Lr:1.00E-04

四、 结果可视化

1. Loss与Accuracy图

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

运行结果:

2. 指定图片进行预测  

from PIL import Imageclasses=list(total_data.class_to_idx)def predict_one_image(image_path,model,transform,classes):test_img=Image.open(image_path).convert('RGB')plt.imshow(test_img)   #展示预测的图片test_img=transform(test_img)img=test_img.to(device).unsqueeze(0)model.eval()output=model(img)_,pred=torch.max(output,1)pred_class=classes[pred]print(f'预测结果是:{pred_class}')

预测图片:

#预测训练集中的某张照片
predict_one_image(image_path=r'D:\THE MNIST DATABASE\weather_photos\shine\shine15.jpg',model=model,transform=train_transforms,classes=classes)

运行结果:

预测结果是:shine

五、心得体会  

在本周的项目训练中,手动搭建了InceptionV3模型,加深了对该模型的理解。

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

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

相关文章

什么是 AWS PrivateLink

您可以使用 Amazon VPC 定义虚拟私有云&#xff08;VPC&#xff09;&#xff0c;这是一个逻辑隔离虚拟网络。您可以在 VPC 中启动 AWS 资源。您可以允许 VPC 中的资源连接到该 VPC 外部的资源。例如&#xff0c;向 VPC 添加互联网网关以允许访问互联网&#xff0c;或添加 VPN 连…

vue2 -- el-form组件动态增减表单项及表单项验证

需求 在数据录入场景(如订单信息录入)中,可根据实际情况(如商品种类增加)动态添加表单项(如商品相关信息)。包含必填项验证和数据格式验证(如邮箱、电话格式),防止错误数据提交。 效果 代码一 <template><div>

旧衣回收小程序:提高回收效率,扩大服务范围

近年来&#xff0c;旧衣回收作为一种新兴回收模式&#xff0c;逐渐走入了大众的生活中&#xff0c;在回收市场中形成了新的商业模式&#xff0c;也为大众带来了新的创业选择。 随着社会生活的快速发展&#xff0c;人们的生活水平不断提高&#xff0c;为旧衣市场发展提供了基础…

电子电气架构 --- Trace 32(劳特巴赫)多核系统的调试

我是穿拖鞋的汉子,魔都中坚持长期主义的汽车电子工程师。 老规矩,分享一段喜欢的文字,避免自己成为高知识低文化的工程师: 所有人的看法和评价都是暂时的,只有自己的经历是伴随一生的,几乎所有的担忧和畏惧,都是来源于自己的想象,只有你真的去做了,才会发现有多快乐。…

使用C++和QT开发应用程序入门以及开发实例分享

目录 1、搭建开发环境&#xff08;VS2010和QT4.8.2&#xff09; 2、创建一个QT窗口 3、在QT窗口中添加子窗口 4、QT界面布局 5、QT信号&#xff08;SIGNAL&#xff09;和槽&#xff08;SLOT&#xff09; 6、最后 C软件异常排查从入门到精通系列教程&#xff08;专栏文章列…

1553B总线协议

参考地址 MIL-STD-1553B时分制指令/响应多路传输数据总线采用半双工传输方式。 1553B总线协议概述 1553B总线协议是一种军用标准&#xff0c;主要用于飞机上的设备间的信息传输。这种协议由美国军方制定&#xff0c;被广泛应用于航空、航天和军事领域的电子联网系统中。1553B…

LVI-GS:港大待开源、雷达-视觉-惯紧耦合,实时渲染

文章目录 摘要一、 介绍二、相关工作A 基于NeRF的SLAMB 基于3DGS的SLAM 三、方法A 超原语B. 3D 高斯投影C. 关键帧管理D. 基于金字塔的训练E. 高斯映射 四、实验 摘要 文章&#xff1a;github 介绍&#xff1a;介绍 3D高斯溅射&#xff08;3DGS&#xff09;在快速渲染和高保真…

2024年中国AI大模型场景应用趋势蓝皮书【附47页PPT】

目录 01 AI大模型行业应用概况 02 AI大模型行业应用现状及案例 03 AI大模型行业应用痛点及解决方案 04 AI大模型行业应用前景趋势及投资机会分析 如何学习AI大模型 &#xff1f; “最先掌握AI的人&#xff0c;将会比较晚掌握AI的人有竞争优势”。 这句话&#xff0c;放在…

基于Zynq FPGA对雷龙SD NAND的测试

一、SD NAND 特征 1.1 SD 卡简介 雷龙的 SD NAND 有很多型号&#xff0c;在测试中使用的是 CSNP4GCR01-AMW 与 CSNP32GCR01-AOW。芯片是基于 NAND FLASH 和 SD 控制器实现的 SD 卡。具有强大的坏块管理和纠错功能&#xff0c;并且在意外掉电的情况下同样能保证数据的安全。 …

Spring Boot框架下的教育导师匹配系统

第一章 绪论 1.1 选题背景 如今的信息时代&#xff0c;对信息的共享性&#xff0c;信息的流通性有着较高要求&#xff0c;尽管身边每时每刻都在产生大量信息&#xff0c;这些信息也都会在短时间内得到处理&#xff0c;并迅速传播。因为很多时候&#xff0c;管理层决策需要大量信…

鉴源实验室·加密技术在汽车系统中的应用

随着汽车技术的快速发展&#xff0c;现代汽车已经不再是简单的交通工具&#xff0c;而是融合了多种智能功能的移动终端。无论是自动驾驶、车联网&#xff08;V2X&#xff09;&#xff0c;还是车内娱乐系统&#xff0c;数据传输和存储已经成为汽车生态系统中的关键环节。然而&am…

Oracle数据库是什么?

Oracle Database&#xff0c;又名 Oracle RDBMS&#xff0c;简称 Oracle。Oracle 数据库系统是美国 Oracle 公司&#xff08;甲骨文&#xff09;提供的以分布式数据库为核心的一组软件产品&#xff0c;是目前最流行的客户/服务器&#xff08;client/server&#xff09;或B/S体系…

利用Docker Compose构建微服务架构

&#x1f493; 博客主页&#xff1a;瑕疵的CSDN主页 &#x1f4dd; Gitee主页&#xff1a;瑕疵的gitee主页 ⏩ 文章专栏&#xff1a;《热点资讯》 利用Docker Compose构建微服务架构 引言 Docker Compose 简介 安装 Docker Compose 创建项目结构 编写 Dockerfile 前端 Dockerf…

macOS15.1及以上系统bug:开发者证书无法打开,钥匙串访问无法打开一直出现图标后立马闪退

团队紧跟苹果最新系统发现bug:今日设备信息如下,希望能带给遇到这个问题的开发者一点帮助。 错误图如下: 点击证书文件后,先出现钥匙串访问图标,后立马闪退消失 中间试过很多方法,都是一样的表现,最后好在解决了,看网上也没有相关的帖子,这里直接写解决办法和导致原因…

牵手APP引领交友新风尚,多元匹配助力寻找心仪伴侣

第54次《中国互联网络发展状况统计报告》数据显示&#xff0c;截至2024年6月&#xff0c;我国网民规模近11亿人&#xff0c;互联网普及率达78.0%。网络平台的普及&#xff0c;在便捷生活的同时&#xff0c;也衍生出一系列全新的领域与服务生态。如今&#xff0c;线上交友作为一…

C#WPF使用CommunityToolkit.Mvvm库

C#WPF之快速理解MVVM模式 接上篇介绍MVVM&#xff0c;这里使用相关库介绍MVVM。 CommunityToolkit.Mvvm CommunityToolkit.Mvvm介绍 CommunityToolkit.Mvvm是Microsoft Community Toolkit的一部分&#xff0c;它是一个轻量级但功能强大的MVVM&#xff08;Model-View-ViewMod…

Pr 视频效果:ASC CDL

视频效果/颜色校正/ASC CDL Color Correction/ASC CDL ASC CDL ASC CDL效果通过对红、绿、蓝三个原色通道的独立调整&#xff0c;实现对图像色彩的精确控制。在此基础上&#xff0c;还可用于调整处理后图像的整体饱和度。 ◆ ◆ ◆ 效果选项说明 斜率 Slope、偏移 Offset和功…

【Linux】网络编程:应用层协议—HTTP协议(超详细)

目录 一、HTTP协议概念 HTTP协议的基本概念 HTTP的工作流程 二、认识URL URL 的基本结构 URLencode与URLdecode 三、认识HTTP协议 四、HTTP请求格式 1. 请求行&#xff08;Request Line&#xff09; 2. 请求报头&#xff08;Request Headers&#xff09; 3. 空行&a…

kelp protocol

道阻且长,行而不辍,未来可期 有很长一段时间我都在互联网到处拾金,but,东拼西凑的,总感觉不踏实,最近在老老实实的看官方文档 & 阅读白皮书 &看合约,挑拣一些重要的部分配上官方的证据,和过路公主or王子分享一下,愿我们早日追赶上公司里那些可望不可及大佬们。…

鸿蒙ArkTS中的布局容器组件(Scroll、List、Tabs)

1、Scroll组件 Scroll组件是一个可滚动的容器组件&#xff0c;用于在子组件的布局尺寸超过父组件尺寸时提供滚动功能。它允许在其内部容纳超过自身显示区域的内容&#xff0c;并通过滚动机制来查看全部内容。这对于显示大量信息&#xff08;如长列表、长篇文本或大型图像等&…