googlenet

googlenet的优点:

  1. 模型又准又轻
  2. 深度加到22层,引入Inception模块,利用不同卷积核提取不同程度的信息
  3. 采用1x1 卷积进行降维以及映射处理减少了计算量,增加模型深度提高非线性表达能力
  4. GAP, 每一个通道求平均即平均池化层,不用FC,减少了参数量
  5. 在训练时有辅助分类器

下面是Googlenet代码

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


class GoogLeNet(nn.Module):
    def __init__(self, num_classes=1000, aux_logits=True, init_weights=False):
        # aux_logits是是否使用辅助分类器
        super(GoogLeNet, self).__init__()
        self.aux_logits = aux_logits

        self.conv1 = BasicConv2d(3, 64, kernel_size=7, stride=2, padding=3)
        self.maxpool1 = nn.MaxPool2d(3, stride=2, ceil_mode=True)

        self.conv2 = BasicConv2d(64, 64, kernel_size=1)
        self.conv3 = BasicConv2d(64, 192, kernel_size=3, padding=1)
        self.maxpool2 = nn.MaxPool2d(3, stride=2, ceil_mode=True)

        self.inception3a = Inception(192, 64, 96, 128, 16, 32, 32)
        self.inception3b = Inception(256, 128, 128, 192, 32, 96, 64)
        self.maxpool3 = nn.MaxPool2d(3, stride=2, ceil_mode=True)

        self.inception4a = Inception(480, 192, 96, 208, 16, 48, 64)
        self.inception4b = Inception(512, 160, 112, 224, 24, 64, 64)
        self.inception4c = Inception(512, 128, 128, 256, 24, 64, 64)
        self.inception4d = Inception(512, 112, 144, 288, 32, 64, 64)
        self.inception4e = Inception(528, 256, 160, 320, 32, 128, 128)
        self.maxpool4 = nn.MaxPool2d(3, stride=2, ceil_mode=True)

        self.inception5a = Inception(832, 256, 160, 320, 32, 128, 128)
        self.inception5b = Inception(832, 384, 192, 384, 48, 128, 128)

        if self.aux_logits:
            self.aux1 = InceptionAux(512, num_classes)
            self.aux2 = InceptionAux(528, num_classes)

        self.avgpool = nn.AdaptiveAvgPool2d((1, 1))
        # 使用自适应的好处就是,无论你输入的图片是什么样子的,你输出的图片大小都是1*1
        self.dropout = nn.Dropout(0.4)
        self.fc = nn.Linear(1024, num_classes)
        if init_weights:
            self._initialize_weights()

    def forward(self, x):
        # N x 3 x 224 x 224
        x = self.conv1(x)
        # N x 64 x 112 x 112
        x = self.maxpool1(x)
        # N x 64 x 56 x 56
        x = self.conv2(x)
        # N x 64 x 56 x 56
        x = self.conv3(x)
        # N x 192 x 56 x 56
        x = self.maxpool2(x)

        # N x 192 x 28 x 28
        x = self.inception3a(x)
        # N x 256 x 28 x 28
        x = self.inception3b(x)
        # N x 480 x 28 x 28
        x = self.maxpool3(x)
        # N x 480 x 14 x 14
        x = self.inception4a(x)
        # N x 512 x 14 x 14
        if self.training and self.aux_logits:    # eval model lose this layer
            aux1 = self.aux1(x)

        x = self.inception4b(x)
        # N x 512 x 14 x 14
        x = self.inception4c(x)
        # N x 512 x 14 x 14
        x = self.inception4d(x)
        # N x 528 x 14 x 14
        if self.training and self.aux_logits:    # eval model lose this layer
            aux2 = self.aux2(x)

        x = self.inception4e(x)
        # N x 832 x 14 x 14
        x = self.maxpool4(x)
        # N x 832 x 7 x 7
        x = self.inception5a(x)
        # N x 832 x 7 x 7
        x = self.inception5b(x)
        # N x 1024 x 7 x 7

        x = self.avgpool(x)
        # N x 1024 x 1 x 1
        x = torch.flatten(x, 1)
        # N x 1024
        x = self.dropout(x)
        x = self.fc(x)
        # N x 1000 (num_classes)
        if self.training and self.aux_logits:   # eval model lose this layer
            return x, aux2, aux1
        return x

    def _initialize_weights(self):
        for m in self.modules():
            if isinstance(m, nn.Conv2d):
                nn.init.kaiming_normal_(m.weight, mode='fan_out', nonlinearity='relu')
                if m.bias is not None:
                    nn.init.constant_(m.bias, 0)
            elif isinstance(m, nn.Linear):
                nn.init.normal_(m.weight, 0, 0.01)
                nn.init.constant_(m.bias, 0)


class Inception(nn.Module):
    def __init__(self, in_channels, ch1x1, ch3x3red, ch3x3, ch5x5red, ch5x5, pool_proj):
        super(Inception, self).__init__()

        self.branch1 = BasicConv2d(in_channels, ch1x1, kernel_size=1)

        self.branch2 = nn.Sequential(
            BasicConv2d(in_channels, ch3x3red, kernel_size=1),
            BasicConv2d(ch3x3red, ch3x3, kernel_size=3, padding=1)   # 保证输出大小等于输入大小
        )

        self.branch3 = nn.Sequential(
            BasicConv2d(in_channels, ch5x5red, kernel_size=1),
            BasicConv2d(ch5x5red, ch5x5, kernel_size=5, padding=2)   # 保证输出大小等于输入大小
        )

        self.branch4 = nn.Sequential(
            nn.MaxPool2d(kernel_size=3, stride=1, padding=1),
            BasicConv2d(in_channels, pool_proj, kernel_size=1)
        )

    def forward(self, x):
        branch1 = self.branch1(x)
        branch2 = self.branch2(x)
        branch3 = self.branch3(x)
        branch4 = self.branch4(x)

        outputs = [branch1, branch2, branch3, branch4]
        return torch.cat(outputs, 1)
    # torch.cat 按照dim = 1即channel这个维度进行拼接,算是叠在一起吧
    # 为什么dim = 1是channel,因为输入神经网路时,维度是batch、channel、长、宽


class InceptionAux(nn.Module):
    # 定义辅助分类器
    def __init__(self, in_channels, num_classes):
        super(InceptionAux, self).__init__()
        self.averagePool = nn.AvgPool2d(kernel_size=5, stride=3)
        self.conv = BasicConv2d(in_channels, 128, kernel_size=1)  # output[batch, 128, 4, 4]

        self.fc1 = nn.Linear(128, 1024)
        self.fc2 = nn.Linear(1024, num_classes)

    def forward(self, x):
        # aux1: N x 512 x 14 x 14, aux2: N x 528 x 14 x 14
        x = self.averagePool(x)
        # aux1: N x 512 x 4 x 4, aux2: N x 528 x 4 x 4
        x = self.conv(x)
        # N x 128 x 4 x 4
        x = torch.flatten(x, 1)
        x = F.dropout(x, 0.5, training=self.training)
        # N x 2048,self.training是__init__固有的东西,意思是如果是model.tran()的模式时,此时自动赋予True,model.evil(),此时自动赋予Fales。
        x = F.relu(self.fc1(x), inplace=True)
        x = F.dropout(x, 0.5, training=self.training)
        # N x 1024,原文中时0.7比较好,但是实验证明是0.5比较好
        x = self.fc2(x)
        # N x num_classes
        return x


class BasicConv2d(nn.Module):
    def __init__(self, in_channels, out_channels, **kwargs):
        super(BasicConv2d, self).__init__()
        self.conv = nn.Conv2d(in_channels, out_channels, **kwargs)
        self.relu = nn.ReLU(inplace=True)

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

下面是其对应main函数代码


import torch

import torch.nn as nn
import creatdataset

from accurary import learning_curve
from alexnet import AlexNet
from googlenet import GoogLeNet
from resnext import Resnext
from test import test
from train import train
from vgg import VGG, vgg


def load_dataset(batch_size):


    root = r"C:\Users\Jia\PycharmProjects\pythonProject\resnet_dataset"
    train_set = creatdataset.MyDataset(root, mode="train")
    train_iter = torch.utils.data.DataLoader(train_set, batch_size=batch_size, shuffle=True)

    val_set = creatdataset.MyDataset(root, mode="val")
    val_iter = torch.utils.data.DataLoader(val_set, batch_size=batch_size, shuffle=False, num_workers=0)

    test_set = creatdataset.MyDataset(root, mode="test")
    test_iter = torch.utils.data.DataLoader(test_set, batch_size=batch_size, shuffle=False, num_workers=0)
    return train_iter, val_iter, test_iter


import torch.optim as optim
BATCH_SIZE = 128  # 批大小
NUM_EPOCHS = 12  # Epoch大小
NUM_CLASSES = 3  # 分类的个数
LEARNING_RATE = 0.01  # 梯度下降学习率
MOMENTUM = 0.9  # 冲量大小
WEIGHT_DECAY = 0.0005  # 权重衰减系数
NUM_PRINT = 1
DEVICE = "cuda" if torch.cuda.is_available() else "cpu"  # GPU or CPU运行


def main():
    net = GoogLeNet(num_classes=3, aux_logits=True, init_weights=True)
    net = net.to(DEVICE)

    train_iter, val_iter, test_iter = load_dataset(BATCH_SIZE)  # 导入训练集和测试集

    criterion = nn.CrossEntropyLoss()  # 交叉熵损失函数损失计算器
    # 优化器
    optimizer = optim.SGD(
        net.parameters(),
        # 构建好神经网络后,网络的参数都保存在parameters()函数当中
        lr=LEARNING_RATE,
        momentum=MOMENTUM,
        weight_decay=WEIGHT_DECAY,
        nesterov=True
        # Nesterov动量梯度下降

    )
    # 调整学习率 (step_size:每训练step_size个epoch,更新一次参数; gamma:更新lr的乘法因子)
    lr_scheduler = optim.lr_scheduler.StepLR(optimizer, step_size=5, gamma=0.1)

    record_train, record_val = train(net, train_iter, criterion, optimizer, \
          NUM_EPOCHS, DEVICE, NUM_PRINT, lr_scheduler, val_iter)

    learning_curve(record_train, record_val) # 画出准确率曲线


    if test_iter is not None:  # 判断验证集是否为空 (注意这里将调用test函数)
            test(net, test_iter, criterion, DEVICE)



main()

下面是其对应train的代码

# 训练模型
import time

import torch

def train(net, train_iter, criterion, optimizer, num_epochs, device, num_print, lr_scheduler=None, val_iter=None):
    net.train()
    record_train = list()
    # 记录每一Epoch下训练集的准确率
    # List() 方法用于将元组转换为列表。
    record_val = list()

    for epoch in range(num_epochs):
        print("========== epoch: [{}/{}] ==========".format(epoch + 1, num_epochs))
        total, correct, train_loss = 0, 0, 0
        start = time.time()

        for i, (X, y) in enumerate(train_iter):
            # enumerate就是枚举的意思,把元素一个个列举出来,第一个是什么,第二个是什么,所以他返回的是元素以及对应的索引。
            device = torch.device("cuda:0" if torch.cuda.is_available() else "cpu")
            X, y = X.to(device), y.to(device)
            optimizer.zero_grad()  # 梯度归零
            logits, aux_logits2, aux_logits1 = net(X)
            loss0 =  criterion(logits, y.to(device))
            loss1 =  criterion(aux_logits1,y.to(device))
            loss2 =  criterion(aux_logits2, y.to(device))
            loss = loss0 + loss1 * 0.3 + loss2 * 0.3
            loss.backward()  # 反向传播
            optimizer.step()  # 更新参数

            train_loss += loss.item()
            total += y.size(0)
            correct += (logits.argmax(dim=1) == y).sum().item() # 累积预测正确的样本数
            # output.argmax(dim=1) 返回指定维度最大值的序号,dim=1,把dim=1这个维度的,变成这个维度的最大值的index
            # 即 dim=0是取每一列最大值的下标,dim=1是取每一行最大值的下标
            train_acc = 100.0 * correct / total

            if (i + 1) % num_print == 0:
                print("step: [{}/{}], train_loss: {:.3f} | train_acc: {:6.3f}% | lr: {:.6f}" \
                    .format(i + 1, len(train_iter), train_loss / (i + 1), \
                            train_acc, get_cur_lr(optimizer)))


        if lr_scheduler is not None:
            # 调整梯度下降算法的学习率
            lr_scheduler.step()
        # 输出训练的时间
        print("--- cost time: {:.4f}s ---".format(time.time() - start))

        if val_iter is not None:  # 判断验证集是否为空 (注意这里将调用val函数)
            record_val.append(val(net, val_iter, criterion, device)) # 每训练一个Epoch模型,使用验证集进行验证模型的准确度
        record_train.append(train_acc)
        # append() 方法用于在列表末尾追加新的对象。
    # 返回每一个Epoch下测试集和训练集的准确率
    torch.save(net.state_dict(),"googlenet1.pth")
    return record_train, record_val

# 验证模型
def val(net, val_iter, criterion, device):
    total, correct = 0, 0

    net.eval()# 验证模式

    with torch.no_grad():
        print("*************** val ***************")
        for X, y in val_iter:
            X, y = X.to(device), y.to(device)# CPU or GPU运行

            output = net(X)  # 计算输出
            val_loss = criterion(output, y)  # 计算损失
            total += y.size(0)  # 计算测试集总样本数
            correct += (output.argmax(dim=1) == y).sum().item()


            val_acc = 100.0 * correct / total  # 测试集准确率

           # 输出验证集的损失
    print("val_loss: {:.3f} | val_acc: {:6.3f}%" \
              .format(val_loss.item(),val_acc))
    print("************************************\n")

    # 训练模式 (因为这里是因为每经过一个Epoch就使用测试集一次,使用验证集后,进入下一个Epoch前将模型重新置于训练模式)
    net.train()

    return val_acc

# 返回学习率lr的函数
def get_cur_lr(optimizer):
    for param_group in optimizer.param_groups:
        return param_group['lr']

你可能感兴趣的:(深度学习,pytorch,python,计算机视觉)