深度学习Day-26:Inception-V3算法实战与解析

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

要求:

  1. 了解并学习InceptionV3相对于InceptionV1改进了那些地方(重点)
  2. 使用InceptionV3完成天气识别案例

一、 基础配置

  • 语言环境:Python3.8
  • 编译器选择:Pycharm
  • 深度学习环境:
    • torch==1.12.1+cu113
    • torchvision==0.13.1+cu113

二、 前期准备 

1.设置GPU

import pathlib
import torch
import torch.nn as nn
from torchvision import transforms, datasets

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

print(device)

2. 导入数据

本项目所采用的数据集未收录于公开数据中,故需要自己在文件目录中导入相应数据集合,并设置对应文件目录,以供后续学习过程中使用。

运行下述代码:

data_dir = './data/'
data_dir = pathlib.Path(data_dir)

data_paths = list(data_dir.glob('*'))
classeNames = [str(path).split("\\")[1] for path in data_paths]
print(classeNames)

image_count = len(list(data_dir.glob('*/*')))
print("图片总数为:", image_count)

得到如下输出:

['cloudy', 'rain', 'shine', 'sunrise']
图片总数为: 1125

接下来,我们通过transforms.Compose对整个数据集进行预处理:

train_transforms = transforms.Compose([
    transforms.Resize([224, 224]),  # 将输入图片resize成统一尺寸
    # transforms.RandomHorizontalFlip(), # 随机水平翻转
    transforms.ToTensor(),  # 将PIL Image或numpy.ndarray转换为tensor,并归一化到[0,1]之间
    transforms.Normalize(  # 标准化处理-->转换为标准正太分布(高斯分布),使模型更容易收敛
        mean=[0.485, 0.456, 0.406],
        std=[0.229, 0.224, 0.225])  # 其中 mean=[0.485,0.456,0.406]与std=[0.229,0.224,0.225] 从数据集中随机抽样计算得到的。
])

test_transform = transforms.Compose([
    transforms.Resize([224, 224]),  # 将输入图片resize成统一尺寸
    transforms.ToTensor(),  # 将PIL Image或numpy.ndarray转换为tensor,并归一化到[0,1]之间
    transforms.Normalize(  # 标准化处理-->转换为标准正太分布(高斯分布),使模型更容易收敛
        mean=[0.485, 0.456, 0.406],
        std=[0.229, 0.224, 0.225])  # 其中 mean=[0.485,0.456,0.406]与std=[0.229,0.224,0.225] 从数据集中随机抽样计算得到的。
])

total_data = datasets.ImageFolder("./data/", transform=train_transforms)
print(total_data.class_to_idx)

得到如下输出:

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

3. 划分数据集

 此处数据集需要做按比例划分的操作:

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

接下来,根据划分得到的训练集和验证集对数据集进行包装:

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

并通过:

for X, y in test_dl:
    print("Shape of X [N, C, H, W]: ", X.shape)
    print("Shape of y: ", y.shape, y.dtype)
    break

输出测试数据集的数据分布情况:

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

4.搭建模型

1.模型搭建


class BasicConv2d(nn.Module):
    def __init__(self, in_channel, out_channel, **kwargs):
        super(BasicConv2d, self).__init__()
        self.conv = nn.Conv2d(in_channel, out_channel, bias=False, **kwargs)
        self.norm = nn.BatchNorm2d(out_channel, eps=0.001)
        self.relu = nn.ReLU(inplace=True)

    def forward(self, x):
        x = self.conv(x)
        x = self.norm(x)
        x = self.relu(x)
        return x


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


import torch.nn.functional as F


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=5)
        # 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

2.查看模型信息

# 统计模型参数量以及其他指标
import torchsummary

# 调用并将模型转移到GPU中
model = InceptionV3().to(device)

# 显示网络结构
torchsummary.summary(model, (3, 299, 299))
print(model)

得到如下输出:

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

三、 训练模型 

1. 编写训练函数

def train(dataloader, model, loss_fn, optimizer):
    size = len(dataloader.dataset)  # 训练集的大小
    num_batches = len(dataloader)  # 批次数目, (size/batch_size,向上取整)

    train_loss, train_acc = 0, 0  # 初始化训练损失和正确率

    for X, y in dataloader:  # 获取图片及其标签
        X, y = X.to(device), y.to(device)

        # 计算预测误差
        pred = model(X)  # 网络输出
        loss = loss_fn(pred, y)  # 计算网络输出和真实值之间的差距,targets为真实值,计算二者差值即为损失

        # 反向传播
        optimizer.zero_grad()  # grad属性归零
        loss.backward()  # 反向传播
        optimizer.step()  # 每一步自动更新

        # 记录acc与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

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)

            # 计算loss
            target_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 /= size
    test_loss /= num_batches

    return test_acc, test_loss

3.正式训练

import copy

optimizer = torch.optim.Adam(model.parameters(), lr=1e-4)
loss_fn = nn.CrossEntropyLoss()  # 创建损失函数

epochs = 10

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

best_acc = 0  # 设置一个最佳准确率,作为最佳模型的判别指标

for epoch in range(epochs):
    # 更新学习率(使用自定义学习率时使用)
    # adjust_learning_rate(optimizer, epoch, learn_rate)

    model.train()
    epoch_train_acc, epoch_train_loss = train(train_dl, model, loss_fn, optimizer)
    # scheduler.step() # 更新学习率(调用官方动态学习率接口时使用)

    model.eval()
    epoch_test_acc, epoch_test_loss = test(test_dl, model, loss_fn)

    # 保存最佳模型到 best_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))

# 保存最佳模型到文件中
PATH = './best_model.pth'  # 保存的参数文件名
torch.save(model.state_dict(), PATH)

print('Done')

得到如下输出:

Epoch: 1, Train_acc:65.9%, Train_loss:2.683, Test_acc:28.9%, Test_loss:7.532, Lr:1.00E-04
Epoch: 2, Train_acc:86.3%, Train_loss:0.529, Test_acc:90.2%, Test_loss:0.350, Lr:1.00E-04
Epoch: 3, Train_acc:87.2%, Train_loss:0.531, Test_acc:90.7%, Test_loss:0.313, Lr:1.00E-04
Epoch: 4, Train_acc:91.0%, Train_loss:0.354, Test_acc:93.8%, Test_loss:0.210, Lr:1.00E-04
Epoch: 5, Train_acc:90.3%, Train_loss:0.373, Test_acc:92.4%, Test_loss:0.563, Lr:1.00E-04
Epoch: 6, Train_acc:92.2%, Train_loss:0.236, Test_acc:91.1%, Test_loss:0.206, Lr:1.00E-04
Epoch: 7, Train_acc:93.7%, Train_loss:0.174, Test_acc:91.6%, Test_loss:0.178, Lr:1.00E-04
Epoch: 8, Train_acc:95.0%, Train_loss:0.166, Test_acc:92.9%, Test_loss:0.210, Lr:1.00E-04
Epoch: 9, Train_acc:95.0%, Train_loss:0.192, Test_acc:92.9%, Test_loss:0.219, Lr:1.00E-04
Epoch:10, Train_acc:94.4%, Train_loss:0.183, Test_acc:92.9%, Test_loss:0.210, Lr:1.00E-04
Done

四、 结果可视化

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'] = 100  # 分辨率

epochs_range = range(epochs)

plt.figure(figsize=(12, 3))
plt.subplot(1, 2, 1)

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

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

得到的可视化结果:

 2. 指定图片进行预测

首先,先定义出一个用于预测的函数:

from PIL import Image

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)

    _, pred = torch.max(output, 1)
    pred_class = classes[pred]
    print(f'预测结果是:{pred_class}')

接着调用函数对指定图片进行预测:

# 预测训练集中的某张照片
predict_one_image(image_path='./data/cloudy/cloudy16.jpg',
                  model=model,
                  transform=train_transforms,
                  classes=classes)

得到如下结果:

预测结果是:cloudy

五、网络介绍

1.简介

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网络是上图结构图如下:

2.总结 

        Inception v3是一种深度卷积神经网络,其主要特点包括更深的网络结构、使用Factorized Convolutions、添加Batch Normalization、引入辅助分类器以及使用基于RMSProp的优化器进行训练。相对于Inception v1的Inception Module结构,Inception v3在卷积操作上做出了改动,使用两个3x3的卷积代替一个5x5的卷积以提高计算速度,并将n x n的卷积核尺寸分解为1 x n和n x 1两个卷积。此外,滤波器组也被扩展以解决表征性瓶颈。在计算机视觉任务中,Inception v3在图像分类、物体检测和图像分割等方面均表现优异。然而,由于其较大的网络结构和计算复杂度,Inception v3在实际应用中可能需要较高的硬件要求。

  • 8
    点赞
  • 10
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值