深度学习每周学习总结J9(Inception V3 算法实战与解析 - 天气识别)

发布于:2024-12-20 ⋅ 阅读:(20) ⋅ 点赞:(0)

0. 总结

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

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

模型构建部分:Inception V3

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

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

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

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

结果可视化

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

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

关于调优(十分重要):本次将测试集准确率提升到了96.03%(随机种子设置为42)
1:使用多卡不一定比单卡效果好,需要继续调优
2:本次微调参数主要调整了两点一是初始学习率从1e-4 增大为了3e-4;其次是原来图片预处理只加入了随机水平翻转,本次加入了小角度的随机翻转,随机缩放剪裁,光照变化等,发现有更好的效果。测试集准确率有了很大的提升。从训练后的准确率图像也可以看到,训练准确率和测试准确率很接近甚至能够超过。之前没有做这个改进之前,都是训练准确率远大于测试准确率。

有个疑问是为啥必须要把图片尺寸设置为(299,299)?(244,244)会报错

关键代码示例:

import torchvision.transforms as transforms
 
# 定义猴痘识别的 transforms
train_transforms = transforms.Compose([
    transforms.Resize([299, 299]),            # 统一图片尺寸
    transforms.RandomHorizontalFlip(p=0.5),  # 随机水平翻转
    transforms.RandomRotation(degrees=15),   # 小角度随机旋转
    transforms.RandomResizedCrop(size=299, scale=(0.8, 1.2)),  # 随机缩放裁剪
    transforms.ColorJitter(brightness=0.2, contrast=0.2, saturation=0.1),  # 光照变化
    transforms.ToTensor(),                   # 转换为 Tensor 格式
    transforms.Normalize(                    # 标准化
        mean=[0.485, 0.456, 0.406],
        std=[0.229, 0.224, 0.225]
    )
])

Inception V1 简介

什么是Inception V1?

Inception V1,也被称为GoogLeNet,是Google在2014年ILSVRC比赛中提出的一种卷积神经网络(CNN)架构,并且在比赛中获得了冠军。与当时流行的VGGNet相比,Inception V1在保持相似性能的同时,显著减少了参数数量,从而提高了计算效率。

Inception Module的核心思想

Inception V1的核心是Inception Module,它通过并行的卷积操作在同一层提取不同尺度的特征。这种设计不仅增加了网络的深度,还有效地捕捉了多种特征信息。

具体来说,一个Inception Module通常包含以下几个分支:

  1. 1x1卷积分支:用于降低输入特征图的通道数,减少计算量。
  2. 1x1卷积后接3x3卷积分支:先用1x1卷积降维,再进行3x3卷积提取特征。
  3. 1x1卷积后接5x5卷积分支:类似于3x3分支,但使用更大的卷积核以捕捉更大范围的特征。
  4. 3x3最大池化后接1x1卷积分支:先进行池化操作,再用1x1卷积进行特征整合。

通过将这些分支的输出在通道维度上拼接,Inception Module能够在同一层次上整合多种尺度的信息,提升模型的表达能力。

1x1卷积的作用

1x1卷积主要用于降维,即减少特征图的通道数。这不仅降低了网络的参数量和计算量,还间接增加了网络的深度,有助于提升模型性能。例如:

  • 原始输入:100x100x128
  • 经过1x1卷积降维到32通道,再进行5x5卷积,输出仍为100x100x256
  • 参数量由原来的约8.192×10⁹降低到2.048×10⁹

辅助分类器

Inception V1还引入了辅助分类器,主要有两个作用:

  1. 缓解梯度消失:通过在中间层添加分类器,帮助梯度更好地传播。
  2. 模型融合:将中间层的输出用于分类,增强模型的泛化能力。

不过,在实际应用中,这些辅助分类器通常在训练过程中使用,推理时会被去掉。

Inception V3 简介

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:Inceptionv3在每个卷积层之后都添加了Batch Normalization(BN),这有助于网络的收敛和泛化能力。BN可以减少Internal Covariate Shift(内部协变量偏移)现象,加快训练速度,同时提高模型的鲁棒性。

4:辅助分类器:Inception v3引入了辅助分类器,可以在网络训练过程中提供额外的梯度信息,帮助网络更好地学习特征。辅助分类器位于网络的某个中间层,其输出会与主分类器的输出进行加权融合,从而得到最终的预测结果。

5:基于RMSProp的优化器:Inception v3使用了RMSProp优化器进行训练。相比于传统的随机梯度下降(SGD)方法,RMSProp可以自适应地调整学习率,使得训练过程更加稳定,收敛速度更快。

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

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


import os,PIL,pathlib
import matplotlib.pyplot as plt
import warnings

warnings.filterwarnings('ignore') # 忽略警告信息

plt.rcParams['font.sans-serif'] = ['SimHei'] # 用来正常显示中文标签
plt.rcParams['axes.unicode_minus'] = False   # 用来正常显示负号
plt.rcParams['figure.dpi'] = 100 # 分辨率

1. 设置GPU

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

2. 导入数据及处理部分

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

paths = list(path_dir.glob('*'))
# classNames = [str(path).split("\\")[-1] for path in paths] # ['Bananaquit', 'Black Skimmer', 'Black Throated Bushtiti', 'Cockatoo']
classNames = [path.parts[-1] for path in paths]
classNames
['cloudy', 'rain', 'shine', 'sunrise']
# 定义transforms 并处理数据
# train_transforms = transforms.Compose([
#     transforms.Resize([224,224]),      # 将输入图片resize成统一尺寸
#     transforms.RandomHorizontalFlip(), # 随机水平翻转
#     transforms.ToTensor(),             # 将PIL Image 或 numpy.ndarray 装换为tensor,并归一化到[0,1]之间
#     transforms.Normalize(              # 标准化处理 --> 转换为标准正太分布(高斯分布),使模型更容易收敛
#         mean = [0.485,0.456,0.406],    # 其中 mean=[0.485,0.456,0.406]与std=[0.229,0.224,0.225] 从数据集中随机抽样计算得到的。
#         std = [0.229,0.224,0.225]
#     )
# ])

# 定义猴痘识别的 transforms 并处理数据
train_transforms = transforms.Compose([
    transforms.Resize([299, 299]),            # 统一图片尺寸
    transforms.RandomHorizontalFlip(p=0.5),  # 随机水平翻转
    transforms.RandomRotation(degrees=15),   # 小角度随机旋转
    transforms.RandomResizedCrop(size=299, scale=(0.8, 1.2)),  # 随机缩放裁剪
    transforms.ColorJitter(brightness=0.2, contrast=0.2, saturation=0.1),  # 光照变化
    transforms.ToTensor(),                   # 转换为 Tensor 格式
    transforms.Normalize(                    # 标准化
        mean=[0.485, 0.456, 0.406],
        std=[0.229, 0.224, 0.225]
    )
])

test_transforms = transforms.Compose([
    transforms.Resize([299,299]),
    transforms.ToTensor(),
    transforms.Normalize(
        mean = [0.485,0.456,0.406],
        std = [0.229,0.224,0.225]
    )
])
total_data = datasets.ImageFolder('./data/weather_recognize/weather_photos/',transform = train_transforms)
total_data
Dataset ImageFolder
    Number of datapoints: 1125
    Root location: ./data/weather_recognize/weather_photos/
    StandardTransform
Transform: Compose(
               Resize(size=[299, 299], interpolation=bilinear, max_size=None, antialias=True)
               RandomHorizontalFlip(p=0.5)
               RandomRotation(degrees=[-15.0, 15.0], interpolation=nearest, expand=False, fill=0)
               RandomResizedCrop(size=(299, 299), scale=(0.8, 1.2), ratio=(0.75, 1.3333), interpolation=bilinear, antialias=True)
               ColorJitter(brightness=(0.8, 1.2), contrast=(0.8, 1.2), saturation=(0.9, 1.1), hue=None)
               ToTensor()
               Normalize(mean=[0.485, 0.456, 0.406], std=[0.229, 0.224, 0.225])
           )

3. 划分数据集

# 设置随机种子
torch.manual_seed(42)

# 划分数据集
train_size = int(len(total_data) * 0.8)
test_size = len(total_data) - train_size

train_dataset,test_dataset = torch.utils.data.random_split(total_data,[train_size,test_size])
train_dataset,test_dataset
(<torch.utils.data.dataset.Subset at 0x29e06793df0>,
 <torch.utils.data.dataset.Subset at 0x29e06793dc0>)
# 定义DataLoader用于数据集的加载

batch_size = 32 # 如使用多显卡,请确保 batch_size 是显卡数量的倍数。

train_dl = torch.utils.data.DataLoader(
    train_dataset,
    batch_size = batch_size,
    shuffle = True,
    num_workers = 1
)
test_dl = torch.utils.data.DataLoader(
    test_dataset,
    batch_size = batch_size,
    shuffle = True,
    num_workers = 1
)

# 观察数据维度
for X,y in test_dl:
    print("Shape of X [N,C,H,W]: ",X.shape)
    print("Shape of y: ", y.shape,y.dtype)
    break

Shape of X [N,C,H,W]:  torch.Size([32, 3, 299, 299])
Shape of y:  torch.Size([32]) torch.int64

4. 模型构建部分

import torch
import torch.nn as nn
import torch.nn.functional as F

class InceptionA(nn.Module):

    def __init__(self, in_channels, pool_features):
        super(InceptionA, self).__init__()
        self.branch1x1 = BasicConv2d(in_channels, 64, kernel_size=1) # 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)
    
    
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_7x7
        self.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)
    
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)
    
    

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)
    
    
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)
    
    
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.01
        self.fc = nn.Linear(768, num_classes)
        self.fc.stddev = 0.001

    def forward(self, x):
        # 17 x 17 x 768
        x = F.avg_pool2d(x, kernel_size=5, stride=3)
        # 5 x 5 x 768
        x = self.conv0(x)
        # 5 x 5 x 128
        x = self.conv1(x)
        # 1 x 1 x 768
        x = x.view(x.size(0), -1)
        # 768
        x = self.fc(x)
        # 1000
        return x
    
    
class 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)

    def forward(self, x):
        x = self.conv(x)
        x = self.bn(x)
        return F.relu(x, inplace=True)

class InceptionV3(nn.Module):
    def __init__(self, num_classes=1000, aux_logits=False, transform_input=False):
        super(InceptionV3, self).__init__()
        self.aux_logits = aux_logits
        self.transform_input = transform_input
        self.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: # 1
            x = x.clone()
            x[:, 0] = x[:, 0] * (0.229 / 0.5) + (0.485 - 0.5) / 0.5
            x[:, 1] = x[:, 1] * (0.224 / 0.5) + (0.456 - 0.5) / 0.5
            x[:, 2] = x[:, 2] * (0.225 / 0.5) + (0.406 - 0.5) / 0.5
        # 299 x 299 x 3
        x = self.Conv2d_1a_3x3(x)
        # 149 x 149 x 32
        x = self.Conv2d_2a_3x3(x)
        # 147 x 147 x 32
        x = self.Conv2d_2b_3x3(x)
        # 147 x 147 x 64
        x = F.max_pool2d(x, kernel_size=3, stride=2)
        # 73 x 73 x 64
        x = self.Conv2d_3b_1x1(x)
        # 73 x 73 x 80
        x = self.Conv2d_4a_3x3(x)
        # 71 x 71 x 192
        x = F.max_pool2d(x, kernel_size=3, stride=2)
        # 35 x 35 x 192
        x = self.Mixed_5b(x)
        # 35 x 35 x 256
        x = self.Mixed_5c(x)
        # 35 x 35 x 288
        x = self.Mixed_5d(x)
        # 35 x 35 x 288
        x = self.Mixed_6a(x)
        # 17 x 17 x 768
        x = self.Mixed_6b(x)
        # 17 x 17 x 768
        x = self.Mixed_6c(x)
        # 17 x 17 x 768
        x = self.Mixed_6d(x)
        # 17 x 17 x 768
        x = self.Mixed_6e(x)
        # 17 x 17 x 768
        if self.training and self.aux_logits:
            aux = self.AuxLogits(x)
        # 17 x 17 x 768
        x = self.Mixed_7a(x)
        # 8 x 8 x 1280
        x = self.Mixed_7b(x)
        # 8 x 8 x 2048
        x = self.Mixed_7c(x)
        # 8 x 8 x 2048
        x = F.avg_pool2d(x, kernel_size=8)
        # 1 x 1 x 2048
        x = F.dropout(x, training=self.training)
        # 1 x 1 x 2048
        x = x.view(x.size(0), -1)
        # 2048
        x = self.fc(x)
        # 1000 (num_classes)
        if self.training and self.aux_logits:
            return x, aux
        return x
model = InceptionV3(num_classes=len(classNames)).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)
  )
  (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)
  )
  (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)
  )
  (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)
  )
  (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)
  )
  (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)
    )
    (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)
    )
    (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)
    )
    (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)
    )
    (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)
    )
    (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)
    )
    (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)
    )
  )
  (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)
    )
    (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)
    )
    (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)
    )
    (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)
    )
    (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)
    )
    (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)
    )
    (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)
    )
  )
  (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)
    )
    (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)
    )
    (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)
    )
    (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)
    )
    (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)
    )
    (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)
    )
    (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)
    )
  )
  (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)
    )
    (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)
    )
    (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)
    )
    (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)
    )
  )
  (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)
    )
    (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)
    )
    (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)
    )
    (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)
    )
    (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)
    )
    (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)
    )
    (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)
    )
    (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)
    )
    (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)
    )
    (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)
    )
  )
  (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)
    )
    (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)
    )
    (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)
    )
    (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)
    )
    (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)
    )
    (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)
    )
    (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)
    )
    (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)
    )
    (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)
    )
    (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)
    )
  )
  (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)
    )
    (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)
    )
    (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)
    )
    (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)
    )
    (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)
    )
    (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)
    )
    (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)
    )
    (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)
    )
    (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)
    )
    (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)
    )
  )
  (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)
    )
    (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)
    )
    (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)
    )
    (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)
    )
    (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)
    )
    (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)
    )
    (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)
    )
    (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)
    )
    (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)
    )
    (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)
    )
  )
  (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)
    )
    (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)
    )
    (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)
    )
    (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)
    )
    (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)
    )
    (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)
    )
  )
  (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)
    )
    (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)
    )
    (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)
    )
    (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)
    )
    (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)
    )
    (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)
    )
    (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)
    )
    (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)
    )
    (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)
    )
  )
  (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)
    )
    (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)
    )
    (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)
    )
    (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)
    )
    (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)
    )
    (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)
    )
    (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)
    )
    (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)
    )
    (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)
    )
  )
  (fc): Linear(in_features=2048, out_features=4, bias=True)
)
# 查看模型详情
import torchsummary as summary
summary.summary(model,(3,299,299))
----------------------------------------------------------------
        Layer (type)               Output Shape         Param #
================================================================
            Conv2d-1         [-1, 32, 149, 149]             864
       BatchNorm2d-2         [-1, 32, 149, 149]              64
       BasicConv2d-3         [-1, 32, 149, 149]               0
            Conv2d-4         [-1, 32, 147, 147]           9,216
       BatchNorm2d-5         [-1, 32, 147, 147]              64
       BasicConv2d-6         [-1, 32, 147, 147]               0
            Conv2d-7         [-1, 64, 147, 147]          18,432
       BatchNorm2d-8         [-1, 64, 147, 147]             128
       BasicConv2d-9         [-1, 64, 147, 147]               0
           Conv2d-10           [-1, 80, 73, 73]           5,120
      BatchNorm2d-11           [-1, 80, 73, 73]             160
      BasicConv2d-12           [-1, 80, 73, 73]               0
           Conv2d-13          [-1, 192, 71, 71]         138,240
      BatchNorm2d-14          [-1, 192, 71, 71]             384
      BasicConv2d-15          [-1, 192, 71, 71]               0
           Conv2d-16           [-1, 64, 35, 35]          12,288
      BatchNorm2d-17           [-1, 64, 35, 35]             128
      BasicConv2d-18           [-1, 64, 35, 35]               0
           Conv2d-19           [-1, 48, 35, 35]           9,216
      BatchNorm2d-20           [-1, 48, 35, 35]              96
      BasicConv2d-21           [-1, 48, 35, 35]               0
           Conv2d-22           [-1, 64, 35, 35]          76,800
      BatchNorm2d-23           [-1, 64, 35, 35]             128
      BasicConv2d-24           [-1, 64, 35, 35]               0
           Conv2d-25           [-1, 64, 35, 35]          12,288
      BatchNorm2d-26           [-1, 64, 35, 35]             128
      BasicConv2d-27           [-1, 64, 35, 35]               0
           Conv2d-28           [-1, 96, 35, 35]          55,296
      BatchNorm2d-29           [-1, 96, 35, 35]             192
      BasicConv2d-30           [-1, 96, 35, 35]               0
           Conv2d-31           [-1, 96, 35, 35]          82,944
      BatchNorm2d-32           [-1, 96, 35, 35]             192
      BasicConv2d-33           [-1, 96, 35, 35]               0
           Conv2d-34           [-1, 32, 35, 35]           6,144
      BatchNorm2d-35           [-1, 32, 35, 35]              64
      BasicConv2d-36           [-1, 32, 35, 35]               0
       InceptionA-37          [-1, 256, 35, 35]               0
           Conv2d-38           [-1, 64, 35, 35]          16,384
      BatchNorm2d-39           [-1, 64, 35, 35]             128
      BasicConv2d-40           [-1, 64, 35, 35]               0
           Conv2d-41           [-1, 48, 35, 35]          12,288
      BatchNorm2d-42           [-1, 48, 35, 35]              96
      BasicConv2d-43           [-1, 48, 35, 35]               0
           Conv2d-44           [-1, 64, 35, 35]          76,800
      BatchNorm2d-45           [-1, 64, 35, 35]             128
      BasicConv2d-46           [-1, 64, 35, 35]               0
           Conv2d-47           [-1, 64, 35, 35]          16,384
      BatchNorm2d-48           [-1, 64, 35, 35]             128
      BasicConv2d-49           [-1, 64, 35, 35]               0
           Conv2d-50           [-1, 96, 35, 35]          55,296
      BatchNorm2d-51           [-1, 96, 35, 35]             192
      BasicConv2d-52           [-1, 96, 35, 35]               0
           Conv2d-53           [-1, 96, 35, 35]          82,944
      BatchNorm2d-54           [-1, 96, 35, 35]             192
      BasicConv2d-55           [-1, 96, 35, 35]               0
           Conv2d-56           [-1, 64, 35, 35]          16,384
      BatchNorm2d-57           [-1, 64, 35, 35]             128
      BasicConv2d-58           [-1, 64, 35, 35]               0
       InceptionA-59          [-1, 288, 35, 35]               0
           Conv2d-60           [-1, 64, 35, 35]          18,432
      BatchNorm2d-61           [-1, 64, 35, 35]             128
      BasicConv2d-62           [-1, 64, 35, 35]               0
           Conv2d-63           [-1, 48, 35, 35]          13,824
      BatchNorm2d-64           [-1, 48, 35, 35]              96
      BasicConv2d-65           [-1, 48, 35, 35]               0
           Conv2d-66           [-1, 64, 35, 35]          76,800
      BatchNorm2d-67           [-1, 64, 35, 35]             128
      BasicConv2d-68           [-1, 64, 35, 35]               0
           Conv2d-69           [-1, 64, 35, 35]          18,432
      BatchNorm2d-70           [-1, 64, 35, 35]             128
      BasicConv2d-71           [-1, 64, 35, 35]               0
           Conv2d-72           [-1, 96, 35, 35]          55,296
      BatchNorm2d-73           [-1, 96, 35, 35]             192
      BasicConv2d-74           [-1, 96, 35, 35]               0
           Conv2d-75           [-1, 96, 35, 35]          82,944
      BatchNorm2d-76           [-1, 96, 35, 35]             192
      BasicConv2d-77           [-1, 96, 35, 35]               0
           Conv2d-78           [-1, 64, 35, 35]          18,432
      BatchNorm2d-79           [-1, 64, 35, 35]             128
      BasicConv2d-80           [-1, 64, 35, 35]               0
       InceptionA-81          [-1, 288, 35, 35]               0
           Conv2d-82          [-1, 384, 17, 17]         995,328
      BatchNorm2d-83          [-1, 384, 17, 17]             768
      BasicConv2d-84          [-1, 384, 17, 17]               0
           Conv2d-85           [-1, 64, 35, 35]          18,432
      BatchNorm2d-86           [-1, 64, 35, 35]             128
      BasicConv2d-87           [-1, 64, 35, 35]               0
           Conv2d-88           [-1, 96, 35, 35]          55,296
      BatchNorm2d-89           [-1, 96, 35, 35]             192
      BasicConv2d-90           [-1, 96, 35, 35]               0
           Conv2d-91           [-1, 96, 17, 17]          82,944
      BatchNorm2d-92           [-1, 96, 17, 17]             192
      BasicConv2d-93           [-1, 96, 17, 17]               0
       ReductionA-94          [-1, 768, 17, 17]               0
           Conv2d-95          [-1, 192, 17, 17]         147,456
      BatchNorm2d-96          [-1, 192, 17, 17]             384
      BasicConv2d-97          [-1, 192, 17, 17]               0
           Conv2d-98          [-1, 128, 17, 17]          98,304
      BatchNorm2d-99          [-1, 128, 17, 17]             256
     BasicConv2d-100          [-1, 128, 17, 17]               0
          Conv2d-101          [-1, 128, 17, 17]         114,688
     BatchNorm2d-102          [-1, 128, 17, 17]             256
     BasicConv2d-103          [-1, 128, 17, 17]               0
          Conv2d-104          [-1, 192, 17, 17]         172,032
     BatchNorm2d-105          [-1, 192, 17, 17]             384
     BasicConv2d-106          [-1, 192, 17, 17]               0
          Conv2d-107          [-1, 128, 17, 17]          98,304
     BatchNorm2d-108          [-1, 128, 17, 17]             256
     BasicConv2d-109          [-1, 128, 17, 17]               0
          Conv2d-110          [-1, 128, 17, 17]         114,688
     BatchNorm2d-111          [-1, 128, 17, 17]             256
     BasicConv2d-112          [-1, 128, 17, 17]               0
          Conv2d-113          [-1, 128, 17, 17]         114,688
     BatchNorm2d-114          [-1, 128, 17, 17]             256
     BasicConv2d-115          [-1, 128, 17, 17]               0
          Conv2d-116          [-1, 128, 17, 17]         114,688
     BatchNorm2d-117          [-1, 128, 17, 17]             256
     BasicConv2d-118          [-1, 128, 17, 17]               0
          Conv2d-119          [-1, 192, 17, 17]         172,032
     BatchNorm2d-120          [-1, 192, 17, 17]             384
     BasicConv2d-121          [-1, 192, 17, 17]               0
          Conv2d-122          [-1, 192, 17, 17]         147,456
     BatchNorm2d-123          [-1, 192, 17, 17]             384
     BasicConv2d-124          [-1, 192, 17, 17]               0
      InceptionB-125          [-1, 768, 17, 17]               0
          Conv2d-126          [-1, 192, 17, 17]         147,456
     BatchNorm2d-127          [-1, 192, 17, 17]             384
     BasicConv2d-128          [-1, 192, 17, 17]               0
          Conv2d-129          [-1, 160, 17, 17]         122,880
     BatchNorm2d-130          [-1, 160, 17, 17]             320
     BasicConv2d-131          [-1, 160, 17, 17]               0
          Conv2d-132          [-1, 160, 17, 17]         179,200
     BatchNorm2d-133          [-1, 160, 17, 17]             320
     BasicConv2d-134          [-1, 160, 17, 17]               0
          Conv2d-135          [-1, 192, 17, 17]         215,040
     BatchNorm2d-136          [-1, 192, 17, 17]             384
     BasicConv2d-137          [-1, 192, 17, 17]               0
          Conv2d-138          [-1, 160, 17, 17]         122,880
     BatchNorm2d-139          [-1, 160, 17, 17]             320
     BasicConv2d-140          [-1, 160, 17, 17]               0
          Conv2d-141          [-1, 160, 17, 17]         179,200
     BatchNorm2d-142          [-1, 160, 17, 17]             320
     BasicConv2d-143          [-1, 160, 17, 17]               0
          Conv2d-144          [-1, 160, 17, 17]         179,200
     BatchNorm2d-145          [-1, 160, 17, 17]             320
     BasicConv2d-146          [-1, 160, 17, 17]               0
          Conv2d-147          [-1, 160, 17, 17]         179,200
     BatchNorm2d-148          [-1, 160, 17, 17]             320
     BasicConv2d-149          [-1, 160, 17, 17]               0
          Conv2d-150          [-1, 192, 17, 17]         215,040
     BatchNorm2d-151          [-1, 192, 17, 17]             384
     BasicConv2d-152          [-1, 192, 17, 17]               0
          Conv2d-153          [-1, 192, 17, 17]         147,456
     BatchNorm2d-154          [-1, 192, 17, 17]             384
     BasicConv2d-155          [-1, 192, 17, 17]               0
      InceptionB-156          [-1, 768, 17, 17]               0
          Conv2d-157          [-1, 192, 17, 17]         147,456
     BatchNorm2d-158          [-1, 192, 17, 17]             384
     BasicConv2d-159          [-1, 192, 17, 17]               0
          Conv2d-160          [-1, 160, 17, 17]         122,880
     BatchNorm2d-161          [-1, 160, 17, 17]             320
     BasicConv2d-162          [-1, 160, 17, 17]               0
          Conv2d-163          [-1, 160, 17, 17]         179,200
     BatchNorm2d-164          [-1, 160, 17, 17]             320
     BasicConv2d-165          [-1, 160, 17, 17]               0
          Conv2d-166          [-1, 192, 17, 17]         215,040
     BatchNorm2d-167          [-1, 192, 17, 17]             384
     BasicConv2d-168          [-1, 192, 17, 17]               0
          Conv2d-169          [-1, 160, 17, 17]         122,880
     BatchNorm2d-170          [-1, 160, 17, 17]             320
     BasicConv2d-171          [-1, 160, 17, 17]               0
          Conv2d-172          [-1, 160, 17, 17]         179,200
     BatchNorm2d-173          [-1, 160, 17, 17]             320
     BasicConv2d-174          [-1, 160, 17, 17]               0
          Conv2d-175          [-1, 160, 17, 17]         179,200
     BatchNorm2d-176          [-1, 160, 17, 17]             320
     BasicConv2d-177          [-1, 160, 17, 17]               0
          Conv2d-178          [-1, 160, 17, 17]         179,200
     BatchNorm2d-179          [-1, 160, 17, 17]             320
     BasicConv2d-180          [-1, 160, 17, 17]               0
          Conv2d-181          [-1, 192, 17, 17]         215,040
     BatchNorm2d-182          [-1, 192, 17, 17]             384
     BasicConv2d-183          [-1, 192, 17, 17]               0
          Conv2d-184          [-1, 192, 17, 17]         147,456
     BatchNorm2d-185          [-1, 192, 17, 17]             384
     BasicConv2d-186          [-1, 192, 17, 17]               0
      InceptionB-187          [-1, 768, 17, 17]               0
          Conv2d-188          [-1, 192, 17, 17]         147,456
     BatchNorm2d-189          [-1, 192, 17, 17]             384
     BasicConv2d-190          [-1, 192, 17, 17]               0
          Conv2d-191          [-1, 192, 17, 17]         147,456
     BatchNorm2d-192          [-1, 192, 17, 17]             384
     BasicConv2d-193          [-1, 192, 17, 17]               0
          Conv2d-194          [-1, 192, 17, 17]         258,048
     BatchNorm2d-195          [-1, 192, 17, 17]             384
     BasicConv2d-196          [-1, 192, 17, 17]               0
          Conv2d-197          [-1, 192, 17, 17]         258,048
     BatchNorm2d-198          [-1, 192, 17, 17]             384
     BasicConv2d-199          [-1, 192, 17, 17]               0
          Conv2d-200          [-1, 192, 17, 17]         147,456
     BatchNorm2d-201          [-1, 192, 17, 17]             384
     BasicConv2d-202          [-1, 192, 17, 17]               0
          Conv2d-203          [-1, 192, 17, 17]         258,048
     BatchNorm2d-204          [-1, 192, 17, 17]             384
     BasicConv2d-205          [-1, 192, 17, 17]               0
          Conv2d-206          [-1, 192, 17, 17]         258,048
     BatchNorm2d-207          [-1, 192, 17, 17]             384
     BasicConv2d-208          [-1, 192, 17, 17]               0
          Conv2d-209          [-1, 192, 17, 17]         258,048
     BatchNorm2d-210          [-1, 192, 17, 17]             384
     BasicConv2d-211          [-1, 192, 17, 17]               0
          Conv2d-212          [-1, 192, 17, 17]         258,048
     BatchNorm2d-213          [-1, 192, 17, 17]             384
     BasicConv2d-214          [-1, 192, 17, 17]               0
          Conv2d-215          [-1, 192, 17, 17]         147,456
     BatchNorm2d-216          [-1, 192, 17, 17]             384
     BasicConv2d-217          [-1, 192, 17, 17]               0
      InceptionB-218          [-1, 768, 17, 17]               0
          Conv2d-219          [-1, 192, 17, 17]         147,456
     BatchNorm2d-220          [-1, 192, 17, 17]             384
     BasicConv2d-221          [-1, 192, 17, 17]               0
          Conv2d-222            [-1, 320, 8, 8]         552,960
     BatchNorm2d-223            [-1, 320, 8, 8]             640
     BasicConv2d-224            [-1, 320, 8, 8]               0
          Conv2d-225          [-1, 192, 17, 17]         147,456
     BatchNorm2d-226          [-1, 192, 17, 17]             384
     BasicConv2d-227          [-1, 192, 17, 17]               0
          Conv2d-228          [-1, 192, 17, 17]         258,048
     BatchNorm2d-229          [-1, 192, 17, 17]             384
     BasicConv2d-230          [-1, 192, 17, 17]               0
          Conv2d-231          [-1, 192, 17, 17]         258,048
     BatchNorm2d-232          [-1, 192, 17, 17]             384
     BasicConv2d-233          [-1, 192, 17, 17]               0
          Conv2d-234            [-1, 192, 8, 8]         331,776
     BatchNorm2d-235            [-1, 192, 8, 8]             384
     BasicConv2d-236            [-1, 192, 8, 8]               0
      ReductionB-237           [-1, 1280, 8, 8]               0
          Conv2d-238            [-1, 320, 8, 8]         409,600
     BatchNorm2d-239            [-1, 320, 8, 8]             640
     BasicConv2d-240            [-1, 320, 8, 8]               0
          Conv2d-241            [-1, 384, 8, 8]         491,520
     BatchNorm2d-242            [-1, 384, 8, 8]             768
     BasicConv2d-243            [-1, 384, 8, 8]               0
          Conv2d-244            [-1, 384, 8, 8]         442,368
     BatchNorm2d-245            [-1, 384, 8, 8]             768
     BasicConv2d-246            [-1, 384, 8, 8]               0
          Conv2d-247            [-1, 384, 8, 8]         442,368
     BatchNorm2d-248            [-1, 384, 8, 8]             768
     BasicConv2d-249            [-1, 384, 8, 8]               0
          Conv2d-250            [-1, 448, 8, 8]         573,440
     BatchNorm2d-251            [-1, 448, 8, 8]             896
     BasicConv2d-252            [-1, 448, 8, 8]               0
          Conv2d-253            [-1, 384, 8, 8]       1,548,288
     BatchNorm2d-254            [-1, 384, 8, 8]             768
     BasicConv2d-255            [-1, 384, 8, 8]               0
          Conv2d-256            [-1, 384, 8, 8]         442,368
     BatchNorm2d-257            [-1, 384, 8, 8]             768
     BasicConv2d-258            [-1, 384, 8, 8]               0
          Conv2d-259            [-1, 384, 8, 8]         442,368
     BatchNorm2d-260            [-1, 384, 8, 8]             768
     BasicConv2d-261            [-1, 384, 8, 8]               0
          Conv2d-262            [-1, 192, 8, 8]         245,760
     BatchNorm2d-263            [-1, 192, 8, 8]             384
     BasicConv2d-264            [-1, 192, 8, 8]               0
      InceptionC-265           [-1, 2048, 8, 8]               0
          Conv2d-266            [-1, 320, 8, 8]         655,360
     BatchNorm2d-267            [-1, 320, 8, 8]             640
     BasicConv2d-268            [-1, 320, 8, 8]               0
          Conv2d-269            [-1, 384, 8, 8]         786,432
     BatchNorm2d-270            [-1, 384, 8, 8]             768
     BasicConv2d-271            [-1, 384, 8, 8]               0
          Conv2d-272            [-1, 384, 8, 8]         442,368
     BatchNorm2d-273            [-1, 384, 8, 8]             768
     BasicConv2d-274            [-1, 384, 8, 8]               0
          Conv2d-275            [-1, 384, 8, 8]         442,368
     BatchNorm2d-276            [-1, 384, 8, 8]             768
     BasicConv2d-277            [-1, 384, 8, 8]               0
          Conv2d-278            [-1, 448, 8, 8]         917,504
     BatchNorm2d-279            [-1, 448, 8, 8]             896
     BasicConv2d-280            [-1, 448, 8, 8]               0
          Conv2d-281            [-1, 384, 8, 8]       1,548,288
     BatchNorm2d-282            [-1, 384, 8, 8]             768
     BasicConv2d-283            [-1, 384, 8, 8]               0
          Conv2d-284            [-1, 384, 8, 8]         442,368
     BatchNorm2d-285            [-1, 384, 8, 8]             768
     BasicConv2d-286            [-1, 384, 8, 8]               0
          Conv2d-287            [-1, 384, 8, 8]         442,368
     BatchNorm2d-288            [-1, 384, 8, 8]             768
     BasicConv2d-289            [-1, 384, 8, 8]               0
          Conv2d-290            [-1, 192, 8, 8]         393,216
     BatchNorm2d-291            [-1, 192, 8, 8]             384
     BasicConv2d-292            [-1, 192, 8, 8]               0
      InceptionC-293           [-1, 2048, 8, 8]               0
          Linear-294                    [-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): 224.12
Params size (MB): 83.14
Estimated Total Size (MB): 308.28
----------------------------------------------------------------

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

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

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

# 调用官方接口示例
loss_fn = nn.CrossEntropyLoss()

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

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

6. 训练函数

# 训练函数
def train(dataloader,model,loss_fn,optimizer):
    size = len(dataloader.dataset) # 训练集大小
    num_batches = len(dataloader) # 批次数目
    
    train_loss,train_acc = 0,0
    
    for X,y in dataloader:
        X,y = X.to(device),y.to(device)
        
        # 计算预测误差
        pred = model(X)
        loss = loss_fn(pred,y)
        
        # 反向传播
        optimizer.zero_grad()
        loss.backward()
        optimizer.step()
        
        # 记录acc与loss
        train_acc += (pred.argmax(1)==y).type(torch.float).sum().item()
        train_loss += loss.item()
        
    train_acc /= size
    train_loss /= num_batches
    
    return train_acc,train_loss

7. 测试函数

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

8. 正式训练

import copy

epochs = 60

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

best_acc = 0.0

# 检查 GPU 可用性并打印设备信息
if torch.cuda.is_available():
    for i in range(torch.cuda.device_count()):
        print(f"GPU {i}: {torch.cuda.get_device_name(i)}")
        print(f"Initial Memory Allocated: {torch.cuda.memory_allocated(i)/1024**2:.2f} MB")
        print(f"Initial Memory Cached: {torch.cuda.memory_reserved(i)/1024**2:.2f} MB")
else:
    print("No GPU available. Using CPU.")

# 多显卡设置 当前使用的是使用 PyTorch 自带的 DataParallel,后续如有需要可以设置为DistributedDataParallel,这是更加高效的方式
# 且多卡不一定比单卡效果就好,需要调整优化
# if torch.cuda.device_count() > 1:
#     print(f"Using {torch.cuda.device_count()} GPUs")
#     model = nn.DataParallel(model)
# model = model.to('cuda')

for epoch in range(epochs):
    
    # 更新学习率——使用自定义学习率时使用
    # adjust_learning_rate(optimizer,epoch,learn_rate)
    
    model.train()
    epoch_train_acc,epoch_train_loss = train(train_dl,model,loss_fn,optimizer)
    scheduler.step() # 更新学习率——调用官方动态学习率时使用
    
    model.eval()
    epoch_test_acc,epoch_test_loss = test(test_dl,model,loss_fn)
    
    # 保存最佳模型到 best_model
    if epoch_test_acc > best_acc:
        best_acc = epoch_test_acc
        best_model = copy.deepcopy(model)
    
    train_acc.append(epoch_train_acc)
    train_loss.append(epoch_train_loss)
    test_acc.append(epoch_test_acc)
    test_loss.append(epoch_test_loss)
    
    # 获取当前学习率
    lr = optimizer.state_dict()['param_groups'][0]['lr']
    
    template = ('Epoch:{:2d},Train_acc:{:.1f}%,Train_loss:{:.3f},Test_acc:{:.1f}%,Test_loss:{:.3f},Lr:{:.2E}')
    print(template.format(epoch+1,epoch_train_acc*100,epoch_train_loss,epoch_test_acc*100,epoch_test_loss,lr))

    # 实时监控 GPU 状态
    if torch.cuda.is_available():
        for i in range(torch.cuda.device_count()):
            print(f"GPU {i} Usage:")
            print(f"  Memory Allocated: {torch.cuda.memory_allocated(i)/1024**2:.2f} MB")
            print(f"  Memory Cached: {torch.cuda.memory_reserved(i)/1024**2:.2f} MB")
            print(f"  Max Memory Allocated: {torch.cuda.max_memory_allocated(i)/1024**2:.2f} MB")
            print(f"  Max Memory Cached: {torch.cuda.max_memory_reserved(i)/1024**2:.2f} MB")

print('Done','best_acc: ',best_acc)
GPU 0: NVIDIA GeForce RTX 4070 Laptop GPU
Initial Memory Allocated: 92.00 MB
Initial Memory Cached: 524.00 MB
Epoch: 1,Train_acc:75.0%,Train_loss:0.664,Test_acc:35.6%,Test_loss:3.178,Lr:3.00E-04
GPU 0 Usage:
  Memory Allocated: 442.28 MB
  Memory Cached: 4330.00 MB
  Max Memory Allocated: 3518.43 MB
  Max Memory Cached: 4330.00 MB
Epoch: 2,Train_acc:85.4%,Train_loss:0.473,Test_acc:75.6%,Test_loss:1.198,Lr:2.76E-04
GPU 0 Usage:
  Memory Allocated: 441.65 MB
  Memory Cached: 4366.00 MB
  Max Memory Allocated: 3601.60 MB
  Max Memory Cached: 4366.00 MB
Epoch: 3,Train_acc:84.9%,Train_loss:0.458,Test_acc:87.6%,Test_loss:0.368,Lr:2.76E-04
GPU 0 Usage:
  Memory Allocated: 442.17 MB
  Memory Cached: 4366.00 MB
  Max Memory Allocated: 3602.83 MB
  Max Memory Cached: 4366.00 MB
Epoch: 4,Train_acc:86.0%,Train_loss:0.446,Test_acc:87.1%,Test_loss:0.414,Lr:2.54E-04
GPU 0 Usage:
  Memory Allocated: 443.47 MB
  Memory Cached: 4368.00 MB
  Max Memory Allocated: 3602.83 MB
  Max Memory Cached: 4368.00 MB
Epoch: 5,Train_acc:89.0%,Train_loss:0.383,Test_acc:89.3%,Test_loss:0.277,Lr:2.54E-04
GPU 0 Usage:
  Memory Allocated: 440.66 MB
  Memory Cached: 4368.00 MB
  Max Memory Allocated: 3602.83 MB
  Max Memory Cached: 4368.00 MB
Epoch: 6,Train_acc:87.6%,Train_loss:0.382,Test_acc:88.4%,Test_loss:0.378,Lr:2.34E-04
GPU 0 Usage:
  Memory Allocated: 438.65 MB
  Memory Cached: 4368.00 MB
  Max Memory Allocated: 3602.83 MB
  Max Memory Cached: 4368.00 MB
Epoch: 7,Train_acc:87.6%,Train_loss:0.389,Test_acc:87.6%,Test_loss:0.297,Lr:2.34E-04
GPU 0 Usage:
  Memory Allocated: 440.96 MB
  Memory Cached: 4368.00 MB
  Max Memory Allocated: 3602.83 MB
  Max Memory Cached: 4368.00 MB
Epoch: 8,Train_acc:89.2%,Train_loss:0.325,Test_acc:88.4%,Test_loss:0.267,Lr:2.15E-04
GPU 0 Usage:
  Memory Allocated: 441.04 MB
  Memory Cached: 4368.00 MB
  Max Memory Allocated: 3602.83 MB
  Max Memory Cached: 4368.00 MB
Epoch: 9,Train_acc:89.1%,Train_loss:0.341,Test_acc:87.6%,Test_loss:0.309,Lr:2.15E-04
GPU 0 Usage:
  Memory Allocated: 441.02 MB
  Memory Cached: 4368.00 MB
  Max Memory Allocated: 3602.83 MB
  Max Memory Cached: 4368.00 MB
Epoch:10,Train_acc:88.7%,Train_loss:0.340,Test_acc:92.4%,Test_loss:0.233,Lr:1.98E-04
GPU 0 Usage:
  Memory Allocated: 439.24 MB
  Memory Cached: 4368.00 MB
  Max Memory Allocated: 3602.83 MB
  Max Memory Cached: 4368.00 MB
Epoch:11,Train_acc:89.9%,Train_loss:0.307,Test_acc:90.2%,Test_loss:0.254,Lr:1.98E-04
GPU 0 Usage:
  Memory Allocated: 440.40 MB
  Memory Cached: 4368.00 MB
  Max Memory Allocated: 3602.83 MB
  Max Memory Cached: 4368.00 MB
Epoch:12,Train_acc:89.0%,Train_loss:0.322,Test_acc:91.6%,Test_loss:0.250,Lr:1.82E-04
GPU 0 Usage:
  Memory Allocated: 442.53 MB
  Memory Cached: 4368.00 MB
  Max Memory Allocated: 3602.83 MB
  Max Memory Cached: 4368.00 MB
Epoch:13,Train_acc:92.7%,Train_loss:0.242,Test_acc:92.0%,Test_loss:0.223,Lr:1.82E-04
GPU 0 Usage:
  Memory Allocated: 441.67 MB
  Memory Cached: 4368.00 MB
  Max Memory Allocated: 3602.83 MB
  Max Memory Cached: 4368.00 MB
Epoch:14,Train_acc:91.1%,Train_loss:0.311,Test_acc:91.1%,Test_loss:0.224,Lr:1.67E-04
GPU 0 Usage:
  Memory Allocated: 440.82 MB
  Memory Cached: 4368.00 MB
  Max Memory Allocated: 3602.83 MB
  Max Memory Cached: 4368.00 MB
Epoch:15,Train_acc:90.1%,Train_loss:0.300,Test_acc:93.3%,Test_loss:0.196,Lr:1.67E-04
GPU 0 Usage:
  Memory Allocated: 442.16 MB
  Memory Cached: 4368.00 MB
  Max Memory Allocated: 3602.83 MB
  Max Memory Cached: 4368.00 MB
Epoch:16,Train_acc:89.1%,Train_loss:0.334,Test_acc:89.3%,Test_loss:0.333,Lr:1.54E-04
GPU 0 Usage:
  Memory Allocated: 440.50 MB
  Memory Cached: 4368.00 MB
  Max Memory Allocated: 3602.83 MB
  Max Memory Cached: 4368.00 MB
Epoch:17,Train_acc:89.7%,Train_loss:0.335,Test_acc:89.8%,Test_loss:0.232,Lr:1.54E-04
GPU 0 Usage:
  Memory Allocated: 441.43 MB
  Memory Cached: 4368.00 MB
  Max Memory Allocated: 3602.83 MB
  Max Memory Cached: 4368.00 MB
Epoch:18,Train_acc:93.1%,Train_loss:0.260,Test_acc:90.7%,Test_loss:0.260,Lr:1.42E-04
GPU 0 Usage:
  Memory Allocated: 441.24 MB
  Memory Cached: 4368.00 MB
  Max Memory Allocated: 3602.83 MB
  Max Memory Cached: 4368.00 MB
Epoch:19,Train_acc:91.3%,Train_loss:0.231,Test_acc:92.0%,Test_loss:0.179,Lr:1.42E-04
GPU 0 Usage:
  Memory Allocated: 441.50 MB
  Memory Cached: 4368.00 MB
  Max Memory Allocated: 3602.83 MB
  Max Memory Cached: 4368.00 MB
Epoch:20,Train_acc:90.4%,Train_loss:0.238,Test_acc:92.4%,Test_loss:0.656,Lr:1.30E-04
GPU 0 Usage:
  Memory Allocated: 440.64 MB
  Memory Cached: 4368.00 MB
  Max Memory Allocated: 3602.83 MB
  Max Memory Cached: 4368.00 MB
Epoch:21,Train_acc:93.3%,Train_loss:0.195,Test_acc:92.9%,Test_loss:0.189,Lr:1.30E-04
GPU 0 Usage:
  Memory Allocated: 441.06 MB
  Memory Cached: 4368.00 MB
  Max Memory Allocated: 3602.83 MB
  Max Memory Cached: 4368.00 MB
Epoch:22,Train_acc:93.7%,Train_loss:0.261,Test_acc:91.1%,Test_loss:0.206,Lr:1.20E-04
GPU 0 Usage:
  Memory Allocated: 441.77 MB
  Memory Cached: 4368.00 MB
  Max Memory Allocated: 3602.83 MB
  Max Memory Cached: 4368.00 MB
Epoch:23,Train_acc:94.2%,Train_loss:0.195,Test_acc:92.4%,Test_loss:0.205,Lr:1.20E-04
GPU 0 Usage:
  Memory Allocated: 441.27 MB
  Memory Cached: 4368.00 MB
  Max Memory Allocated: 3602.83 MB
  Max Memory Cached: 4368.00 MB
Epoch:24,Train_acc:91.9%,Train_loss:0.236,Test_acc:93.8%,Test_loss:0.186,Lr:1.10E-04
GPU 0 Usage:
  Memory Allocated: 443.12 MB
  Memory Cached: 4368.00 MB
  Max Memory Allocated: 3602.83 MB
  Max Memory Cached: 4368.00 MB
Epoch:25,Train_acc:93.4%,Train_loss:0.309,Test_acc:94.2%,Test_loss:0.149,Lr:1.10E-04
GPU 0 Usage:
  Memory Allocated: 443.39 MB
  Memory Cached: 4368.00 MB
  Max Memory Allocated: 3602.83 MB
  Max Memory Cached: 4368.00 MB
Epoch:26,Train_acc:93.2%,Train_loss:0.177,Test_acc:92.4%,Test_loss:0.192,Lr:1.01E-04
GPU 0 Usage:
  Memory Allocated: 441.71 MB
  Memory Cached: 4368.00 MB
  Max Memory Allocated: 3604.01 MB
  Max Memory Cached: 4368.00 MB
Epoch:27,Train_acc:94.9%,Train_loss:0.222,Test_acc:92.0%,Test_loss:0.227,Lr:1.01E-04
GPU 0 Usage:
  Memory Allocated: 443.70 MB
  Memory Cached: 4368.00 MB
  Max Memory Allocated: 3604.28 MB
  Max Memory Cached: 4368.00 MB
Epoch:28,Train_acc:93.8%,Train_loss:0.200,Test_acc:94.2%,Test_loss:0.144,Lr:9.34E-05
GPU 0 Usage:
  Memory Allocated: 441.37 MB
  Memory Cached: 4368.00 MB
  Max Memory Allocated: 3604.28 MB
  Max Memory Cached: 4368.00 MB
Epoch:29,Train_acc:94.9%,Train_loss:0.158,Test_acc:92.4%,Test_loss:0.168,Lr:9.34E-05
GPU 0 Usage:
  Memory Allocated: 441.79 MB
  Memory Cached: 4368.00 MB
  Max Memory Allocated: 3604.28 MB
  Max Memory Cached: 4368.00 MB
Epoch:30,Train_acc:95.7%,Train_loss:0.151,Test_acc:93.3%,Test_loss:0.161,Lr:8.59E-05
GPU 0 Usage:
  Memory Allocated: 442.08 MB
  Memory Cached: 4368.00 MB
  Max Memory Allocated: 3604.74 MB
  Max Memory Cached: 4368.00 MB
Epoch:31,Train_acc:94.7%,Train_loss:0.137,Test_acc:94.2%,Test_loss:0.136,Lr:8.59E-05
GPU 0 Usage:
  Memory Allocated: 441.25 MB
  Memory Cached: 4368.00 MB
  Max Memory Allocated: 3604.74 MB
  Max Memory Cached: 4368.00 MB
Epoch:32,Train_acc:95.9%,Train_loss:0.133,Test_acc:94.2%,Test_loss:0.197,Lr:7.90E-05
GPU 0 Usage:
  Memory Allocated: 442.79 MB
  Memory Cached: 4368.00 MB
  Max Memory Allocated: 3604.74 MB
  Max Memory Cached: 4368.00 MB
Epoch:33,Train_acc:94.7%,Train_loss:0.144,Test_acc:94.2%,Test_loss:0.366,Lr:7.90E-05
GPU 0 Usage:
  Memory Allocated: 442.73 MB
  Memory Cached: 4368.00 MB
  Max Memory Allocated: 3604.74 MB
  Max Memory Cached: 4368.00 MB
Epoch:34,Train_acc:96.2%,Train_loss:0.109,Test_acc:92.0%,Test_loss:0.248,Lr:7.27E-05
GPU 0 Usage:
  Memory Allocated: 442.30 MB
  Memory Cached: 4368.00 MB
  Max Memory Allocated: 3604.74 MB
  Max Memory Cached: 4368.00 MB
Epoch:35,Train_acc:96.0%,Train_loss:0.138,Test_acc:92.9%,Test_loss:0.177,Lr:7.27E-05
GPU 0 Usage:
  Memory Allocated: 440.91 MB
  Memory Cached: 4368.00 MB
  Max Memory Allocated: 3604.74 MB
  Max Memory Cached: 4368.00 MB
Epoch:36,Train_acc:96.7%,Train_loss:0.179,Test_acc:92.4%,Test_loss:0.206,Lr:6.69E-05
GPU 0 Usage:
  Memory Allocated: 441.46 MB
  Memory Cached: 4368.00 MB
  Max Memory Allocated: 3604.74 MB
  Max Memory Cached: 4368.00 MB
Epoch:37,Train_acc:95.2%,Train_loss:0.148,Test_acc:91.1%,Test_loss:0.219,Lr:6.69E-05
GPU 0 Usage:
  Memory Allocated: 440.19 MB
  Memory Cached: 4368.00 MB
  Max Memory Allocated: 3604.74 MB
  Max Memory Cached: 4368.00 MB
Epoch:38,Train_acc:94.0%,Train_loss:0.162,Test_acc:93.3%,Test_loss:0.157,Lr:6.15E-05
GPU 0 Usage:
  Memory Allocated: 443.07 MB
  Memory Cached: 4368.00 MB
  Max Memory Allocated: 3604.74 MB
  Max Memory Cached: 4368.00 MB
Epoch:39,Train_acc:97.4%,Train_loss:0.117,Test_acc:92.9%,Test_loss:0.279,Lr:6.15E-05
GPU 0 Usage:
  Memory Allocated: 442.75 MB
  Memory Cached: 4368.00 MB
  Max Memory Allocated: 3604.74 MB
  Max Memory Cached: 4368.00 MB
Epoch:40,Train_acc:95.0%,Train_loss:0.203,Test_acc:92.0%,Test_loss:0.590,Lr:5.66E-05
GPU 0 Usage:
  Memory Allocated: 442.67 MB
  Memory Cached: 4368.00 MB
  Max Memory Allocated: 3604.74 MB
  Max Memory Cached: 4368.00 MB
Epoch:41,Train_acc:96.2%,Train_loss:0.127,Test_acc:93.3%,Test_loss:0.199,Lr:5.66E-05
GPU 0 Usage:
  Memory Allocated: 442.07 MB
  Memory Cached: 4368.00 MB
  Max Memory Allocated: 3604.74 MB
  Max Memory Cached: 4368.00 MB
Epoch:42,Train_acc:96.2%,Train_loss:0.110,Test_acc:96.0%,Test_loss:0.132,Lr:5.21E-05
GPU 0 Usage:
  Memory Allocated: 441.30 MB
  Memory Cached: 4368.00 MB
  Max Memory Allocated: 3604.74 MB
  Max Memory Cached: 4368.00 MB
Epoch:43,Train_acc:97.3%,Train_loss:0.132,Test_acc:94.2%,Test_loss:0.175,Lr:5.21E-05
GPU 0 Usage:
  Memory Allocated: 442.24 MB
  Memory Cached: 4368.00 MB
  Max Memory Allocated: 3604.74 MB
  Max Memory Cached: 4368.00 MB
Epoch:44,Train_acc:96.0%,Train_loss:0.111,Test_acc:93.3%,Test_loss:0.179,Lr:4.79E-05
GPU 0 Usage:
  Memory Allocated: 440.67 MB
  Memory Cached: 4368.00 MB
  Max Memory Allocated: 3604.74 MB
  Max Memory Cached: 4368.00 MB
Epoch:45,Train_acc:97.8%,Train_loss:0.065,Test_acc:93.8%,Test_loss:0.175,Lr:4.79E-05
GPU 0 Usage:
  Memory Allocated: 441.61 MB
  Memory Cached: 4368.00 MB
  Max Memory Allocated: 3604.74 MB
  Max Memory Cached: 4368.00 MB
Epoch:46,Train_acc:97.7%,Train_loss:0.078,Test_acc:92.9%,Test_loss:0.188,Lr:4.41E-05
GPU 0 Usage:
  Memory Allocated: 441.65 MB
  Memory Cached: 4368.00 MB
  Max Memory Allocated: 3604.74 MB
  Max Memory Cached: 4368.00 MB
Epoch:47,Train_acc:97.3%,Train_loss:0.090,Test_acc:92.4%,Test_loss:0.183,Lr:4.41E-05
GPU 0 Usage:
  Memory Allocated: 441.28 MB
  Memory Cached: 4368.00 MB
  Max Memory Allocated: 3604.74 MB
  Max Memory Cached: 4368.00 MB
Epoch:48,Train_acc:98.1%,Train_loss:0.065,Test_acc:95.1%,Test_loss:0.110,Lr:4.06E-05
GPU 0 Usage:
  Memory Allocated: 441.88 MB
  Memory Cached: 4368.00 MB
  Max Memory Allocated: 3604.74 MB
  Max Memory Cached: 4368.00 MB
Epoch:49,Train_acc:97.8%,Train_loss:0.068,Test_acc:92.9%,Test_loss:0.181,Lr:4.06E-05
GPU 0 Usage:
  Memory Allocated: 442.35 MB
  Memory Cached: 4368.00 MB
  Max Memory Allocated: 3604.74 MB
  Max Memory Cached: 4368.00 MB
Epoch:50,Train_acc:98.7%,Train_loss:0.061,Test_acc:94.2%,Test_loss:0.153,Lr:3.73E-05
GPU 0 Usage:
  Memory Allocated: 441.16 MB
  Memory Cached: 4368.00 MB
  Max Memory Allocated: 3604.74 MB
  Max Memory Cached: 4368.00 MB
Epoch:51,Train_acc:98.3%,Train_loss:0.149,Test_acc:92.0%,Test_loss:0.185,Lr:3.73E-05
GPU 0 Usage:
  Memory Allocated: 442.35 MB
  Memory Cached: 4368.00 MB
  Max Memory Allocated: 3604.74 MB
  Max Memory Cached: 4368.00 MB
Epoch:52,Train_acc:97.8%,Train_loss:0.067,Test_acc:90.7%,Test_loss:0.237,Lr:3.43E-05
GPU 0 Usage:
  Memory Allocated: 441.16 MB
  Memory Cached: 4368.00 MB
  Max Memory Allocated: 3604.74 MB
  Max Memory Cached: 4368.00 MB
Epoch:53,Train_acc:98.3%,Train_loss:0.056,Test_acc:93.3%,Test_loss:0.158,Lr:3.43E-05
GPU 0 Usage:
  Memory Allocated: 442.35 MB
  Memory Cached: 4368.00 MB
  Max Memory Allocated: 3604.74 MB
  Max Memory Cached: 4368.00 MB
Epoch:54,Train_acc:99.0%,Train_loss:0.101,Test_acc:95.6%,Test_loss:0.151,Lr:3.16E-05
GPU 0 Usage:
  Memory Allocated: 441.16 MB
  Memory Cached: 4368.00 MB
  Max Memory Allocated: 3604.74 MB
  Max Memory Cached: 4368.00 MB
Epoch:55,Train_acc:98.4%,Train_loss:0.111,Test_acc:94.2%,Test_loss:0.155,Lr:3.16E-05
GPU 0 Usage:
  Memory Allocated: 442.35 MB
  Memory Cached: 4368.00 MB
  Max Memory Allocated: 3604.74 MB
  Max Memory Cached: 4368.00 MB
Epoch:56,Train_acc:98.1%,Train_loss:0.065,Test_acc:94.7%,Test_loss:0.133,Lr:2.91E-05
GPU 0 Usage:
  Memory Allocated: 441.16 MB
  Memory Cached: 4368.00 MB
  Max Memory Allocated: 3604.74 MB
  Max Memory Cached: 4368.00 MB
Epoch:57,Train_acc:97.0%,Train_loss:0.137,Test_acc:94.7%,Test_loss:0.141,Lr:2.91E-05
GPU 0 Usage:
  Memory Allocated: 442.35 MB
  Memory Cached: 4368.00 MB
  Max Memory Allocated: 3604.74 MB
  Max Memory Cached: 4368.00 MB
Epoch:58,Train_acc:98.6%,Train_loss:0.054,Test_acc:92.4%,Test_loss:0.171,Lr:2.67E-05
GPU 0 Usage:
  Memory Allocated: 441.16 MB
  Memory Cached: 4368.00 MB
  Max Memory Allocated: 3604.74 MB
  Max Memory Cached: 4368.00 MB
Epoch:59,Train_acc:98.7%,Train_loss:0.043,Test_acc:95.1%,Test_loss:0.176,Lr:2.67E-05
GPU 0 Usage:
  Memory Allocated: 442.35 MB
  Memory Cached: 4368.00 MB
  Max Memory Allocated: 3604.74 MB
  Max Memory Cached: 4368.00 MB
Epoch:60,Train_acc:98.7%,Train_loss:0.084,Test_acc:92.4%,Test_loss:0.211,Lr:2.46E-05
GPU 0 Usage:
  Memory Allocated: 441.16 MB
  Memory Cached: 4368.00 MB
  Max Memory Allocated: 3604.74 MB
  Max Memory Cached: 4368.00 MB
Done best_acc:  0.96

9. 结果可视化

epochs_range = range(epochs)

plt.figure(figsize = (12,3))

plt.subplot(1,2,1)
plt.plot(epochs_range,train_acc,label = 'Training Accuracy')
plt.plot(epochs_range,test_acc,label = 'Test Accuracy')
plt.legend(loc = 'lower right')
plt.title('Training and Validation Accuracy')

plt.subplot(1,2,2)
plt.plot(epochs_range,train_loss,label = 'Test Accuracy')
plt.plot(epochs_range,test_loss,label = 'Test Loss')
plt.legend(loc = 'lower right')
plt.title('Training and validation Loss')
plt.show()


在这里插入图片描述

10. 模型的保存

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

# 定义模型用来加载参数
best_model = InceptionV3(num_classes=len(classNames)).to(device)

best_model.load_state_dict(torch.load('./模型参数/J9_InceptionV3_model_state_dict.pth')) # 加载状态字典到模型

<All keys matched successfully>

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

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

classes = list(total_data.class_to_idx) # classes = list(total_data.class_to_idx)

def predict_one_image(image_path,model,transform,classes):
    
    test_img = Image.open(image_path).convert('RGB')
    # plt.imshow(test_img) # 展示待预测的图片
    
    test_img = transform(test_img)
    img = test_img.to(device).unsqueeze(0)
    
    model.eval()
    output = model(img)
    print(output) # 观察模型预测结果的输出数据
    
    _,pred = torch.max(output,1)
    pred_class = classes[pred]
    print(f'预测结果是:{pred_class}')

# 预测训练集中的某张照片
predict_one_image(image_path='./data/weather_recognize/weather_photos/sunrise/sunrise10.jpg',
                 model = model,
                 transform = test_transforms,
                 classes = classes
                 )

tensor([[-3.4108, -5.2150, -6.3457,  8.8984]], device='cuda:0',
       grad_fn=<AddmmBackward0>)
预测结果是:sunrise
classes
['cloudy', 'rain', 'shine', 'sunrise']

网站公告

今日签到

点亮在社区的每一天
去签到