J9学习打卡笔记

IInception v3算法实战

python 复制代码
import os, PIL, random, pathlib
import torch
import torch.nn as nn
import torchvision.transforms as transforms
import torchvision
from torchvision import transforms, datasets
import torch.nn.functional as F

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

device
device(type='cuda')

网络结构

python 复制代码
import torch.nn.functional as F

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

InceptionA

python 复制代码
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)

InceptionB

python 复制代码
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)

InceptionC

python 复制代码
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)

ReductionA

python 复制代码
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)

ReductionB

python 复制代码
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)

辅助分支

python 复制代码
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
python 复制代码
device = "cuda" if torch.cuda.is_available() else "cpu"
print("Using {} device".format(device))

model = InceptionV3(4).to(device)
model
Using cuda device





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)
)
python 复制代码
# 统计模型参数量以及其他指标
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
----------------------------------------------------------------
python 复制代码
# 导入数据
data_dir = r'C:\Users\11054\Desktop\kLearning\p1_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
python 复制代码
# 数据预处理
train_transforms = transforms.Compose([
    transforms.Resize([299, 299]),  # 将输入图片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([299, 299]),  # 将输入图片resize成统一尺寸
    transforms.ToTensor(),  # 将PIL Image或numpy.ndarray转换为tensor,并归一化到[0,1]之间
    transforms.Normalize(  # 标准化处理-->转换为标准正太分布(高斯分布),使模型更容易收敛
        mean=[0.485, 0.456, 0.406],
        std=[0.229, 0.224, 0.225])  # 其中 mean=[0.485,0.456,0.406]与std=[0.229,0.224,0.225] 从数据集中随机抽样计算得到的。
])

total_data = datasets.ImageFolder(data_dir, transform=train_transforms)
print(total_data.class_to_idx)

# 划分数据集
train_size = int(0.8 * len(total_data))
test_size = len(total_data) - train_size
train_dataset, test_dataset = torch.utils.data.random_split(total_data, [train_size, test_size])

batch_size = 8  #根据自己的显卡,选择合适的batch_size大小
train_dl = torch.utils.data.DataLoader(train_dataset,
                                       batch_size=batch_size,
                                       shuffle=True,
                                       num_workers=0)
test_dl = torch.utils.data.DataLoader(test_dataset,
                                      batch_size=batch_size,
                                      shuffle=True,
                                      num_workers=0)
for X, y in test_dl:
    print("Shape of X [N, C, H, W]: ", X.shape)
    print("Shape of y: ", y.shape, y.dtype)
    break
{'cloudy': 0, 'rain': 1, 'shine': 2, 'sunrise': 3}
Shape of X [N, C, H, W]:  torch.Size([8, 3, 299, 299])
Shape of y:  torch.Size([8]) torch.int64
python 复制代码
# 训练和测试函数
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


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
python 复制代码
# 正式训练
import copy

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

epochs = 50

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 = r'C:/Users/11054/Desktop/kLearning/J9_learning/best_model.pth'  # 保存的参数文件名
torch.save(model.state_dict(), PATH)

print('Done')
Epoch: 1, Train_acc:73.0%, Train_loss:0.729, Test_acc:89.3%, Test_loss:0.296, Lr:1.00E-04
Epoch: 2, Train_acc:80.9%, Train_loss:0.540, Test_acc:89.8%, Test_loss:0.336, Lr:1.00E-04
Epoch: 3, Train_acc:82.0%, Train_loss:0.506, Test_acc:85.3%, Test_loss:0.486, Lr:1.00E-04
Epoch: 4, Train_acc:83.8%, Train_loss:0.443, Test_acc:87.1%, Test_loss:0.352, Lr:1.00E-04
Epoch: 5, Train_acc:85.0%, Train_loss:0.458, Test_acc:87.1%, Test_loss:0.317, Lr:1.00E-04
Epoch: 6, Train_acc:85.8%, Train_loss:0.421, Test_acc:92.0%, Test_loss:0.303, Lr:1.00E-04
Epoch: 7, Train_acc:86.7%, Train_loss:0.393, Test_acc:88.4%, Test_loss:0.335, Lr:1.00E-04
Epoch: 8, Train_acc:89.1%, Train_loss:0.314, Test_acc:94.7%, Test_loss:0.186, Lr:1.00E-04
Epoch: 9, Train_acc:88.3%, Train_loss:0.344, Test_acc:90.7%, Test_loss:0.233, Lr:1.00E-04
Epoch:10, Train_acc:88.4%, Train_loss:0.337, Test_acc:80.9%, Test_loss:0.435, Lr:1.00E-04
Epoch:11, Train_acc:89.3%, Train_loss:0.342, Test_acc:94.7%, Test_loss:0.200, Lr:1.00E-04
Epoch:12, Train_acc:90.1%, Train_loss:0.298, Test_acc:92.9%, Test_loss:0.221, Lr:1.00E-04
Epoch:13, Train_acc:89.0%, Train_loss:0.344, Test_acc:87.6%, Test_loss:0.325, Lr:1.00E-04
Epoch:14, Train_acc:90.1%, Train_loss:0.288, Test_acc:93.3%, Test_loss:0.219, Lr:1.00E-04
Epoch:15, Train_acc:91.0%, Train_loss:0.260, Test_acc:91.1%, Test_loss:0.238, Lr:1.00E-04
Epoch:16, Train_acc:89.9%, Train_loss:0.296, Test_acc:96.9%, Test_loss:0.128, Lr:1.00E-04
Epoch:17, Train_acc:90.6%, Train_loss:0.310, Test_acc:95.1%, Test_loss:0.167, Lr:1.00E-04
Epoch:18, Train_acc:89.7%, Train_loss:0.288, Test_acc:92.9%, Test_loss:0.207, Lr:1.00E-04
Epoch:19, Train_acc:92.7%, Train_loss:0.225, Test_acc:95.6%, Test_loss:0.153, Lr:1.00E-04
Epoch:20, Train_acc:92.8%, Train_loss:0.210, Test_acc:92.0%, Test_loss:0.340, Lr:1.00E-04
Epoch:21, Train_acc:92.9%, Train_loss:0.210, Test_acc:94.2%, Test_loss:0.163, Lr:1.00E-04
Epoch:22, Train_acc:92.1%, Train_loss:0.258, Test_acc:92.9%, Test_loss:0.215, Lr:1.00E-04
Epoch:23, Train_acc:91.3%, Train_loss:0.270, Test_acc:92.9%, Test_loss:0.210, Lr:1.00E-04
Epoch:24, Train_acc:92.2%, Train_loss:0.251, Test_acc:92.0%, Test_loss:0.289, Lr:1.00E-04
Epoch:25, Train_acc:93.6%, Train_loss:0.194, Test_acc:94.7%, Test_loss:0.135, Lr:1.00E-04
Epoch:26, Train_acc:95.7%, Train_loss:0.156, Test_acc:93.3%, Test_loss:0.248, Lr:1.00E-04
Epoch:27, Train_acc:94.6%, Train_loss:0.178, Test_acc:87.1%, Test_loss:0.395, Lr:1.00E-04
Epoch:28, Train_acc:93.2%, Train_loss:0.224, Test_acc:95.6%, Test_loss:0.136, Lr:1.00E-04
Epoch:29, Train_acc:94.4%, Train_loss:0.161, Test_acc:94.7%, Test_loss:0.177, Lr:1.00E-04
Epoch:30, Train_acc:94.6%, Train_loss:0.157, Test_acc:94.7%, Test_loss:0.162, Lr:1.00E-04
Epoch:31, Train_acc:92.2%, Train_loss:0.219, Test_acc:94.7%, Test_loss:0.261, Lr:1.00E-04
Epoch:32, Train_acc:94.2%, Train_loss:0.199, Test_acc:95.6%, Test_loss:0.154, Lr:1.00E-04
Epoch:33, Train_acc:94.1%, Train_loss:0.194, Test_acc:96.9%, Test_loss:0.104, Lr:1.00E-04
Epoch:34, Train_acc:95.3%, Train_loss:0.132, Test_acc:92.9%, Test_loss:0.201, Lr:1.00E-04
Epoch:35, Train_acc:95.9%, Train_loss:0.119, Test_acc:94.2%, Test_loss:0.148, Lr:1.00E-04
Epoch:36, Train_acc:94.6%, Train_loss:0.144, Test_acc:95.6%, Test_loss:0.129, Lr:1.00E-04
Epoch:37, Train_acc:97.0%, Train_loss:0.107, Test_acc:93.3%, Test_loss:0.194, Lr:1.00E-04
Epoch:38, Train_acc:96.9%, Train_loss:0.109, Test_acc:95.6%, Test_loss:0.135, Lr:1.00E-04
Epoch:39, Train_acc:95.1%, Train_loss:0.155, Test_acc:93.8%, Test_loss:0.212, Lr:1.00E-04
Epoch:40, Train_acc:94.3%, Train_loss:0.155, Test_acc:96.9%, Test_loss:0.087, Lr:1.00E-04
Epoch:41, Train_acc:94.7%, Train_loss:0.164, Test_acc:96.0%, Test_loss:0.129, Lr:1.00E-04
Epoch:42, Train_acc:94.2%, Train_loss:0.165, Test_acc:96.9%, Test_loss:0.097, Lr:1.00E-04
Epoch:43, Train_acc:96.6%, Train_loss:0.115, Test_acc:95.6%, Test_loss:0.171, Lr:1.00E-04
Epoch:44, Train_acc:96.8%, Train_loss:0.108, Test_acc:95.6%, Test_loss:0.142, Lr:1.00E-04
Epoch:45, Train_acc:95.0%, Train_loss:0.151, Test_acc:97.3%, Test_loss:0.116, Lr:1.00E-04
Epoch:46, Train_acc:94.6%, Train_loss:0.152, Test_acc:95.1%, Test_loss:0.134, Lr:1.00E-04
Epoch:47, Train_acc:96.7%, Train_loss:0.105, Test_acc:94.7%, Test_loss:0.175, Lr:1.00E-04
Epoch:48, Train_acc:97.1%, Train_loss:0.094, Test_acc:94.7%, Test_loss:0.168, Lr:1.00E-04
Epoch:49, Train_acc:96.6%, Train_loss:0.108, Test_acc:95.6%, Test_loss:0.161, Lr:1.00E-04
Epoch:50, Train_acc:96.4%, Train_loss:0.097, Test_acc:96.0%, Test_loss:0.146, Lr:1.00E-04
Done
python 复制代码
# 结果可视化
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()

from PIL import Image

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


def predict_one_image(image_path, model, transform, classes):
    test_img = Image.open(image_path).convert('RGB')
    plt.imshow(test_img)  # 展示预测的图片

    test_img = transform(test_img)
    img = test_img.to(device).unsqueeze(0)

    model.eval()
    output = model(img)

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

# 预测训练集中的某张照片
predict_one_image(image_path=r'C:\Users\11054\Desktop\kLearning\p1_data\rain\rain1.jpg',
                  model=model,
                  transform=train_transforms,
                  classes=classes)
['cloudy', 'rain', 'shine', 'sunrise']
{'cloudy': 0, 'rain': 1, 'shine': 2, 'sunrise': 3}
预测结果是:rain





![在这里插入图片描述](https://i-blog.csdnimg.cn/direct/11694d2134d1499681fda128ac09c7e5.png#pic_center)

个人总结

  • Inception v3 的核心思想是使用Inception模块,在同一层中并行使用不同大小的卷积核(如 1x1、3x3、5x5),以捕捉不同尺度的特征。
    通过模块化设计,网络可以在不同层次上提取多尺度的特征,从而提高模型的泛化能力
  • Inception v3 将输入尺寸增大到 299 * 299,优点如下:
    捕捉更多细节:较大的输入尺寸可以保留更多的图像细节,尤其是在高分辨率图像上。
    更深的特征提取:较大的输入尺寸允许卷积层在更大的感受野内提取特征,从而提高模型的表达能力。
    推理时间的权衡:虽然增大了输入尺寸会稍微增加计算量,但 Inception v3 通过优化网络结构(如使用更小的卷积核和分解卷积操作),整体推理时间仍然可以控制在合理范围内。
  • InceptionA
    InceptionA模块是Inception V3网络中第一个主要的Inception模块,它主要用于在网络的早期阶段提取大量的低级特征。InceptionA模块包含几个并行的卷积路径,这些路径的输出在深度维度上进行拼接。具体结构如下:

1x1卷积:用于降低通道数,减少计算量。

5x5卷积:通过前一个1x1卷积降维后,进行5x5卷积,提取更复杂的特征。

3x3卷积(两次):通过1x1卷积降维后,进行两次3x3卷积,等效于一次5x5卷积,但计算量更小。

3x3最大池化:进行最大池化操作,然后通过1x1卷积恢复通道数。

  • InceptionB
    InceptionB模块出现在网络的中段,用于提取中级特征。它进一步增加了网络的深度和复杂性,同时保持了计算效率。具体结构如下:

1x1卷积:用于降维和减少计算量。

1x7卷积:通过前一个1x1卷积降维后,进行1x7卷积,捕捉水平方向的特征。

7x1卷积:通过前一个1x1卷积降维后,进行7x1卷积,捕捉垂直方向的特征。

3x3最大池化:进行最大池化操作,然后通过1x1卷积恢复通道数。

  • InceptionC
    InceptionC模块出现在网络的后段,用于提取高级特征。它更加复杂,使用了更多非对称的卷积核组合。具体结构如下:

1x1卷积:用于降维和减少计算量。

1x3和3x1卷积:通过前一个1x1卷积降维后,进行1x3和3x1卷积,分别捕捉不同方向的特征。

3x3最大池化:进行最大池化操作,然后通过1x1卷积恢复通道数。

  • ReductionA
    ReductionA模块用于在网络的中段减少特征图的尺寸,同时增加通道数。它通过不同的路径组合来实现这一点,具体结构如下:

3x3最大池化:直接进行最大池化操作,减少特征图尺寸。

3x3卷积:通过1x1卷积降维后,进行3x3卷积,减少特征图尺寸。

两个3x3卷积(两次3x3卷积):通过1x1卷积降维后,进行两次3x3卷积,等效于一次5x5卷积,减少特征图尺寸。

  • ReductionB
    ReductionB模块用于在网络的后段进一步减少特征图的尺寸,同时增加通道数。它的结构更加复杂,具体如下:

3x3最大池化:直接进行最大池化操作,减少特征图尺寸。

3x3卷积:通过1x1卷积降维后,进行3x3卷积,减少特征图尺寸。

两个1x7和7x1卷积(两次1x7和7x1卷积):通过1x1卷积降维后,进行两次1x7和7x1卷积,捕捉不同方向的特征,然后通过3x3卷积减少特征图尺寸。

  • 辅助分支
    辅助分支是Inception V3网络中的一个特殊设计,用于在网络的中间层增加额外的监督信号,从而帮助网络更快地收敛和提高模型的鲁棒性。辅助分支的具体结构如下:

平均池化:在中间层的特征图上进行平均池化操作,减少特征图的尺寸。

1x1卷积:降维,减少通道数。

全连接层:将降维后的特征图展平,通过全连接层生成辅助分类器的输出。

辅助分类器:用于计算中间层的损失,这个损失在训练过程中会被加到最终的损失函数中。

相关推荐
B1nna1 小时前
Redis学习(三)缓存
redis·学习·缓存
_im.m.z1 小时前
【设计模式学习笔记】1. 设计模式概述
笔记·学习·设计模式
胡西风_foxww2 小时前
【ES6复习笔记】迭代器(10)
前端·笔记·迭代器·es6·iterator
左漫在成长4 小时前
王佩丰24节Excel学习笔记——第十九讲:Indirect函数
笔记·学习·excel
纪伊路上盛名在4 小时前
Max AI prompt1
笔记·学习·学习方法
Suwg2095 小时前
【MySQL】踩坑笔记——保存带有换行符等特殊字符的数据,需要进行转义保存
数据库·笔记·mysql
2401_857610035 小时前
中文学习系统:成本效益分析与系统优化
java·数据库·学习·架构
条哥的高频放大器5 小时前
μC/OS-Ⅱ源码学习(8)---同步与延时
学习
悲且狂5 小时前
文件路径与Resource接口详解
java·学习
胡西风_foxww5 小时前
【ES6复习笔记】对象方法扩展(17)
前端·笔记·es6·对象·方法·扩展·对象方法扩展