ResNet网络结构,BN以及迁移学习详解

这篇具有很好参考价值的文章主要介绍了ResNet网络结构,BN以及迁移学习详解。希望对大家有所帮助。如果存在错误或未考虑完全的地方,请大家不吝赐教,您也可以点击"举报违法"按钮提交疑问。

ResNet网络结构,BN以及迁移学习详解

 网络中的亮点:

1.超深的网络结构(超过1000层)

2.提出residual(残差)模块

3.使用Batch Normalization加速训练(丢弃dropout)ResNet网络结构,BN以及迁移学习详解

左边是将卷积层和池化层进行一个简单的堆叠所搭建的网络结构

20层的训练错误率大概在1%~2%左右

56层的训练错误率大概在7%~8%

所以通过简单的卷积层和池化层的堆叠,并不是层数越深训练效果越好

随着网络层数不断地加深,梯度消失和梯度爆炸这个现象会越来越明显:

假设我们每一层的误差梯度是一个小于1的数,那么在我们的反向传播过程中,

每向前传播一次,都要乘以一个小于1的系数,当我们网络越来越深的时候,结果就越趋近于0

这样梯度就会越来越小

假设误差梯度是一个大于1的数,最后会发生梯度爆炸

通常解决梯度消失和梯度爆炸问题的方法:

标准化处理,权重初始化,BN(Batch Normalization)

退化问题:

在我们解决了梯度消失和梯度爆炸问题后,我们仍然会存在层数深的效果不如层数浅的效果的问题

提出了残差结构:

ResNet网络结构,BN以及迁移学习详解

 左边的残差结构主要是针对于网络层数较少的网络所使用的残差结构(ResNet-34)

主线是通过2个3*3的卷积层得到我们的一个结果,右边有一条弧线从输入连接到输出

将卷积之后的特征矩阵与我们输入的特征矩阵进行相加,相加之后再通过Relu激活函数

能相加就要求主分支和侧分支(shortcut)的输出特征矩阵shape必须相同(shape:高,宽,channel)

右边是针对网络层数较多的网络(50/101/152)

主线是先通过一个1*1的卷积层(降维),再通过一个3*3的卷积层,再通过一个1*1的卷积层(升维)

通过两个网络所需参数的对比可以发现,残差结构越多,所节省的参数越多

ResNet网络结构,BN以及迁移学习详解

 左边的实线部分,输入的特征矩阵与输出的特征矩阵shape相同,所以可以直接相加

右边虚线输入与输出shape不同

Batch Normalization

目的是使我们的一批(Batch)数据所对应的feature map(特征矩阵)每一个维度(channel)满足均值为0,方差为1的分布规律

 通过该方法能够加速网络的收敛(训练)并提升准确率

 对于一个拥有d维的输入x,我们将对它的每一个维度进行标准化处理

假设我们输入的x是RGB三通道的彩色图像,这里的d就是图像的channels,即d=3

ResNet网络结构,BN以及迁移学习详解

 使用BN时,在训练时将trainning参数设置为True,在验证时将trainning设置为False

将BN层放在卷积层和激活层的中间

迁移学习的简介

优势:

1.能够快速的训练出一个理想的结果(训练的epoch较少)

2.当数据集较少时也能训练出理想的结果

注意:使用别人的预训练模型参数时,注意别人的预处理方式

ResNet网络结构,BN以及迁移学习详解

 迁移学习就是将学习好的一些浅层网络的参数迁移到我们新的网络当中来

这样我们新的网络也有了识别底层通用特征的能力了

常见的迁移学习方式:

1.载入权重后训练所有参数

2.载入权重后只训练最后几层参数

3.载入权重后再原网络的基础上在添加一层全连接层,仅训练最后一个全连接层

ResNext

ResNet网络结构,BN以及迁移学习详解

ResNet网络结构,BN以及迁移学习详解 组卷积

ResNet网络结构,BN以及迁移学习详解

 g是组数

代码实现

网络搭建

import torch.nn as nn
import torch


class BasicBlock(nn.Module):#对应的是18层和34层对应的残差结构
    expansion = 1#(扩张)对应的是残差层的卷积核个数有没有发生变化  18 34 layers都没有发生变化

    def __init__(self, in_channel, out_channel, stride=1, downsample=None, **kwargs):
        super(BasicBlock, self).__init__()
        self.conv1 = nn.Conv2d(in_channels=in_channel, out_channels=out_channel,
                               kernel_size=3, stride=stride, padding=1, bias=False)
        self.bn1 = nn.BatchNorm2d(out_channel)
        self.relu = nn.ReLU()
        self.conv2 = nn.Conv2d(in_channels=out_channel, out_channels=out_channel,
                               kernel_size=3, stride=1, padding=1, bias=False)
        self.bn2 = nn.BatchNorm2d(out_channel)
        self.downsample = downsample

    def forward(self, x):
        identity = x#侧分支的输出值
        if self.downsample is not None:
            identity = self.downsample(x)

        out = self.conv1(x)
        out = self.bn1(out)
        out = self.relu(out)

        out = self.conv2(out)
        out = self.bn2(out)

        out += identity
        out = self.relu(out)

        return out


class Bottleneck(nn.Module):#50/101/152层的残差结构
    """
    注意:原论文中,在虚线残差结构的主分支上,第一个1x1卷积层的步距是2,第二个3x3卷积层步距是1。
    但在pytorch官方实现过程中是第一个1x1卷积层的步距是1,第二个3x3卷积层步距是2,
    这么做的好处是能够在top1上提升大概0.5%的准确率。
    可参考Resnet v1.5 https://ngc.nvidia.com/catalog/model-scripts/nvidia:resnet_50_v1_5_for_pytorch
    """
    expansion = 4#4对应的就是残差结构所使用的卷积核的变化

    def __init__(self, in_channel, out_channel, stride=1, downsample=None,
                 groups=1, width_per_group=64):
        super(Bottleneck, self).__init__()

        width = int(out_channel * (width_per_group / 64.)) * groups

        self.conv1 = nn.Conv2d(in_channels=in_channel, out_channels=width,
                               kernel_size=1, stride=1, bias=False)  # squeeze channels
        self.bn1 = nn.BatchNorm2d(width)
        # -----------------------------------------
        self.conv2 = nn.Conv2d(in_channels=width, out_channels=width, groups=groups,
                               kernel_size=3, stride=stride, bias=False, padding=1)
        self.bn2 = nn.BatchNorm2d(width)
        # -----------------------------------------
        self.conv3 = nn.Conv2d(in_channels=width, out_channels=out_channel*self.expansion,
                               kernel_size=1, stride=1, bias=False)  # unsqueeze channels
        self.bn3 = nn.BatchNorm2d(out_channel*self.expansion)
        self.relu = nn.ReLU(inplace=True)
        self.downsample = downsample

    def forward(self, x):
        identity = x
        if self.downsample is not None:
            identity = self.downsample(x)

        out = self.conv1(x)
        out = self.bn1(out)
        out = self.relu(out)

        out = self.conv2(out)
        out = self.bn2(out)
        out = self.relu(out)

        out = self.conv3(out)
        out = self.bn3(out)

        out += identity
        out = self.relu(out)

        return out


class ResNet(nn.Module):

    def __init__(self,
                 block,
                 blocks_num,
                 num_classes=1000,
                 include_top=True,
                 groups=1,
                 width_per_group=64):
        super(ResNet, self).__init__()
        self.include_top = include_top
        self.in_channel = 64

        self.groups = groups
        self.width_per_group = width_per_group

        self.conv1 = nn.Conv2d(3, self.in_channel, kernel_size=7, stride=2,
                               padding=3, bias=False)
        self.bn1 = nn.BatchNorm2d(self.in_channel)
        self.relu = nn.ReLU(inplace=True)
        self.maxpool = nn.MaxPool2d(kernel_size=3, stride=2, padding=1)
        self.layer1 = self._make_layer(block, 64, blocks_num[0])
        self.layer2 = self._make_layer(block, 128, blocks_num[1], stride=2)
        self.layer3 = self._make_layer(block, 256, blocks_num[2], stride=2)
        self.layer4 = self._make_layer(block, 512, blocks_num[3], stride=2)
        if self.include_top:
            self.avgpool = nn.AdaptiveAvgPool2d((1, 1))  # output size = (1, 1)
            self.fc = nn.Linear(512 * block.expansion, num_classes)

        for m in self.modules():
            if isinstance(m, nn.Conv2d):
                nn.init.kaiming_normal_(m.weight, mode='fan_out', nonlinearity='relu')

    def _make_layer(self, block, channel, block_num, stride=1):
        downsample = None
        if stride != 1 or self.in_channel != channel * block.expansion:
            downsample = nn.Sequential(
                nn.Conv2d(self.in_channel, channel * block.expansion, kernel_size=1, stride=stride, bias=False),
                nn.BatchNorm2d(channel * block.expansion))

        layers = []
        layers.append(block(self.in_channel,
                            channel,
                            downsample=downsample,
                            stride=stride,
                            groups=self.groups,
                            width_per_group=self.width_per_group))
        self.in_channel = channel * block.expansion

        for _ in range(1, block_num):
            layers.append(block(self.in_channel,
                                channel,
                                groups=self.groups,
                                width_per_group=self.width_per_group))

        return nn.Sequential(*layers)

    def forward(self, x):
        x = self.conv1(x)
        x = self.bn1(x)
        x = self.relu(x)
        x = self.maxpool(x)

        x = self.layer1(x)
        x = self.layer2(x)
        x = self.layer3(x)
        x = self.layer4(x)

        if self.include_top:
            x = self.avgpool(x)
            x = torch.flatten(x, 1)
            x = self.fc(x)

        return x


def resnet34(num_classes=1000, include_top=True):
    # https://download.pytorch.org/models/resnet34-333f7ec4.pth
    return ResNet(BasicBlock, [3, 4, 6, 3], num_classes=num_classes, include_top=include_top)


def resnet50(num_classes=1000, include_top=True):
    # https://download.pytorch.org/models/resnet50-19c8e357.pth
    return ResNet(Bottleneck, [3, 4, 6, 3], num_classes=num_classes, include_top=include_top)


def resnet101(num_classes=1000, include_top=True):
    # https://download.pytorch.org/models/resnet101-5d3b4d8f.pth
    return ResNet(Bottleneck, [3, 4, 23, 3], num_classes=num_classes, include_top=include_top)


def resnext50_32x4d(num_classes=1000, include_top=True):
    # https://download.pytorch.org/models/resnext50_32x4d-7cdf4587.pth
    groups = 32
    width_per_group = 4
    return ResNet(Bottleneck, [3, 4, 6, 3],
                  num_classes=num_classes,
                  include_top=include_top,
                  groups=groups,
                  width_per_group=width_per_group)


def resnext101_32x8d(num_classes=1000, include_top=True):
    # https://download.pytorch.org/models/resnext101_32x8d-8ba56ff5.pth
    groups = 32
    width_per_group = 8
    return ResNet(Bottleneck, [3, 4, 23, 3],
                  num_classes=num_classes,
                  include_top=include_top,
                  groups=groups,
                  width_per_group=width_per_group)

 训练模块

import os
import sys
import json

import torch
import torch.nn as nn
import torch.optim as optim
from torchvision import transforms, datasets
from tqdm import tqdm

from model import resnet34


def main():
    device = torch.device("cuda:0" if torch.cuda.is_available() else "cpu")
    print("using {} device.".format(device))

    data_transform = {
        "train": transforms.Compose([transforms.RandomResizedCrop(224),
                                     transforms.RandomHorizontalFlip(),
                                     transforms.ToTensor(),
                                     transforms.Normalize([0.485, 0.456, 0.406], [0.229, 0.224, 0.225])]),
        "val": transforms.Compose([transforms.Resize(256),
                                   transforms.CenterCrop(224),
                                   transforms.ToTensor(),
                                   transforms.Normalize([0.485, 0.456, 0.406], [0.229, 0.224, 0.225])])}

    data_root = os.path.abspath(os.path.join(os.getcwd(), "../.."))  # get data root path
    image_path = os.path.join(data_root, "data_set", "flower_data")  # flower data set path
    assert os.path.exists(image_path), "{} path does not exist.".format(image_path)
    train_dataset = datasets.ImageFolder(root=os.path.join(image_path, "train"),
                                         transform=data_transform["train"])
    train_num = len(train_dataset)

    # {'daisy':0, 'dandelion':1, 'roses':2, 'sunflower':3, 'tulips':4}
    flower_list = train_dataset.class_to_idx
    cla_dict = dict((val, key) for key, val in flower_list.items())
    # write dict into json file
    json_str = json.dumps(cla_dict, indent=4)
    with open('class_indices.json', 'w') as json_file:
        json_file.write(json_str)

    batch_size = 16
    nw = min([os.cpu_count(), batch_size if batch_size > 1 else 0, 8])  # number of workers
    print('Using {} dataloader workers every process'.format(nw))

    train_loader = torch.utils.data.DataLoader(train_dataset,
                                               batch_size=batch_size, shuffle=True,
                                               num_workers=0)

    validate_dataset = datasets.ImageFolder(root=os.path.join(image_path, "val"),
                                            transform=data_transform["val"])
    val_num = len(validate_dataset)
    validate_loader = torch.utils.data.DataLoader(validate_dataset,
                                                  batch_size=batch_size, shuffle=False,
                                                  num_workers=0)

    print("using {} images for training, {} images for validation.".format(train_num,
                                                                           val_num))
    
    net = resnet34()
    # load pretrain weights
    # download url: https://download.pytorch.org/models/resnet34-333f7ec4.pth
    model_weight_path = "./resnet34-pre.pth"#载入迁移学习模型参数
    assert os.path.exists(model_weight_path), "file {} does not exist.".format(model_weight_path)
    net.load_state_dict(torch.load(model_weight_path, map_location='cpu'))
    # for param in net.parameters():
    #     param.requires_grad = False

    # change fc layer structure
    in_channel = net.fc.in_features
    net.fc = nn.Linear(in_channel, 5)
    net.to(device)

    # define loss function
    loss_function = nn.CrossEntropyLoss()

    # construct an optimizer
    params = [p for p in net.parameters() if p.requires_grad]
    optimizer = optim.Adam(params, lr=0.0001)

    epochs = 3
    best_acc = 0.0
    save_path = './resNet34.pth'
    train_steps = len(train_loader)
    for epoch in range(epochs):
        # train
        net.train()
        running_loss = 0.0
        train_bar = tqdm(train_loader, file=sys.stdout)
        for step, data in enumerate(train_bar):
            images, labels = data
            optimizer.zero_grad()
            logits = net(images.to(device))
            loss = loss_function(logits, labels.to(device))
            loss.backward()
            optimizer.step()

            # print statistics
            running_loss += loss.item()

            train_bar.desc = "train epoch[{}/{}] loss:{:.3f}".format(epoch + 1,
                                                                     epochs,
                                                                     loss)

        # validate
        net.eval()
        acc = 0.0  # accumulate accurate number / epoch
        with torch.no_grad():
            val_bar = tqdm(validate_loader, file=sys.stdout)
            for val_data in val_bar:
                val_images, val_labels = val_data
                outputs = net(val_images.to(device))
                # loss = loss_function(outputs, test_labels)
                predict_y = torch.max(outputs, dim=1)[1]
                acc += torch.eq(predict_y, val_labels.to(device)).sum().item()

                val_bar.desc = "valid epoch[{}/{}]".format(epoch + 1,
                                                           epochs)

        val_accurate = acc / val_num
        print('[epoch %d] train_loss: %.3f  val_accuracy: %.3f' %
              (epoch + 1, running_loss / train_steps, val_accurate))

        if val_accurate > best_acc:
            best_acc = val_accurate
            torch.save(net.state_dict(), save_path)

    print('Finished Training')


if __name__ == '__main__':
    main()

训练结果ResNet网络结构,BN以及迁移学习详解

预测模块

import os
import json

import torch
from PIL import Image
from torchvision import transforms
import matplotlib.pyplot as plt

from model import resnet34


def main():
    device = torch.device("cuda:0" if torch.cuda.is_available() else "cpu")

    data_transform = transforms.Compose(
        [transforms.Resize(256),
         transforms.CenterCrop(224),
         transforms.ToTensor(),
         transforms.Normalize([0.485, 0.456, 0.406], [0.229, 0.224, 0.225])])

    # load image
    img_path = "../tulip.jpg"
    assert os.path.exists(img_path), "file: '{}' dose not exist.".format(img_path)
    img = Image.open(img_path)
    plt.imshow(img)
    # [N, C, H, W]
    img = data_transform(img)
    # expand batch dimension
    img = torch.unsqueeze(img, dim=0)

    # read class_indict
    json_path = './class_indices.json'
    assert os.path.exists(json_path), "file: '{}' dose not exist.".format(json_path)

    with open(json_path, "r") as f:
        class_indict = json.load(f)

    # create model
    model = resnet34(num_classes=5).to(device)

    # load model weights
    weights_path = "./resNet34.pth"
    assert os.path.exists(weights_path), "file: '{}' dose not exist.".format(weights_path)
    model.load_state_dict(torch.load(weights_path, map_location=device))

    # prediction
    model.eval()
    with torch.no_grad():
        # predict class
        output = torch.squeeze(model(img.to(device))).cpu()
        predict = torch.softmax(output, dim=0)
        predict_cla = torch.argmax(predict).numpy()

    print_res = "class: {}   prob: {:.3}".format(class_indict[str(predict_cla)],
                                                 predict[predict_cla].numpy())
    plt.title(print_res)
    for i in range(len(predict)):
        print("class: {:10}   prob: {:.3}".format(class_indict[str(i)],
                                                  predict[i].numpy()))
    plt.show()


if __name__ == '__main__':
    main()

ResNet网络结构,BN以及迁移学习详解文章来源地址https://www.toymoban.com/news/detail-500075.html

到了这里,关于ResNet网络结构,BN以及迁移学习详解的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处: 如若内容造成侵权/违法违规/事实不符,请点击违法举报进行投诉反馈,一经查实,立即删除!

领支付宝红包 赞助服务器费用

相关文章

  • ResNet详解:网络结构解读与PyTorch实现教程

    本文深入探讨了深度残差网络(ResNet)的核心概念和架构组成。我们从深度学习和梯度消失问题入手,逐一解析了残差块、初始卷积层、残差块组、全局平均池化和全连接层的作用和优点。文章还包含使用PyTorch构建和训练ResNet模型的实战部分,带有详细的代码和解释。 关注

    2024年02月10日
    浏览(32)
  • 【pytorch】ResNet18、ResNet20、ResNet34、ResNet50网络结构与实现

    选取经典的早期Pytorch官方实现代码进行分析 https://github.com/pytorch/vision/blob/9a481d0bec2700763a799ff148fe2e083b575441/torchvision/models/resnet.py 各种ResNet网络是由BasicBlock或者bottleneck构成的,它们是构成深度残差网络的基本模块 ResNet的大部分各种结构是1层conv+4个block+1层fc 需要注意的是最后

    2024年02月02日
    浏览(31)
  • torchvision中的标准ResNet50网络结构

    注:仅用以记录学习 打印出来的网络结构如下: 结构: 修改最后一层(fc层)代码: 用于特定的分类任务,其中最后一层全连接层的输出类别数量被指定为输入参数

    2024年02月04日
    浏览(28)
  • 关于3DCNN卷积网络卷积网络结构以及参数变化

    以下是使用3DCNN的相关代码 过程中的一般形式 静态图片描述: (a)、(b)图分别表示单通道与多通道的2D卷积操作,其特点即L(Input_shape)=L(kernel),(c)图为3D卷积操作卷积核的深度输入数据的深度,在同一深度操作完成之后会根据stride跳入下一深度继续进行卷积操作。 动态

    2024年02月04日
    浏览(24)
  • Mask RCNN网络结构以及整体流程的详细解读

    Mask RCNN是在Faster RCNN的基础上增加了mask head用于实例分割的模型。 总体来说,Mask RCNN网络结构可以分为: BackBone(ResNet+FPN) — RPN网络(Region Proposal Network) — ROI Head(ROIAlign + cls head + bbox head + mask head) 整体网络结构如下(来自原论文https://arxiv.org/pdf/1703.06870.pdf): Backbone主要由R

    2024年02月13日
    浏览(26)
  • VGG网络结构详解

    参考论文:Very Deep Convolutional Networks for Large-Scale Image Recognition 论文中提供的6种网络配置: 整个网络有5个vgg-block块和5个maxpool层逐个相连,然后进入FC层,直到最后1000个softmax输出。多种VGG网络设计都很统一,都有相同的224×224×3的input层+5个maxpool层+3层fc全连接层,区别在于中

    2024年02月07日
    浏览(27)
  • YOLOX网络结构详解

    论文名称:YOLOX: Exceeding YOLO Series in 2021 论文下载地址:https://arxiv.org/abs/2107.08430 论文对应源码地址:https://github.com/Megvii-BaseDetection/YOLOX 在bilibili上的视频讲解:https://www.bilibili.com/video/BV1JW4y1k76c 在之前文章中我们已经聊过YOLO v5了,今天我们再来聊聊YOLOX。YOLOX是旷视科技在2

    2024年02月04日
    浏览(30)
  • PointNet++详解(二):网络结构解析

    如有错误,恳请指出。 在之前对PointNet与PointNet++网络进行了介绍,接下来是对其代码的解析。 1. 论文阅读笔记 | 三维目标检测——PointNet 2. 论文阅读笔记 | 三维目标检测——PointNet++ 参考的github项目为:https://github.com/yanx27/Pointnet_Pointnet2_pytorch 这篇博客的内容主要是将PointN

    2024年02月02日
    浏览(31)
  • 深度学习之图像分割—— SegNet基本思想和网络结构以及论文补充

    原文链接:SegNet SegNet是2016年cvpr由Cambridge提出旨在解决自动驾驶或者智能机器人的图像语义分割深度网络,开放源码,基于caffe框架。SegNet基于FCN,修改VGG-16网络得到的语义分割网络,有两种版本的SegNet,分别为SegNet与Bayesian SegNet,同时SegNet作者根据网络的深度提供了一个b

    2024年02月06日
    浏览(32)
  • AlexNet网络结构的实现和详解

    AlexNet网络结构特点: 1.首次利用GPU进行网络加速训练 (GPU处理速度是CPU的20-50倍) 2.使用了ReLU激活函数,而不是传统的Sigmoid激活函数(缺点:求导麻烦、容易梯度消失)以及tanh激活函数  3.使用了LRN(Local Response Normalization)局部相应归一化 4.在全连接层的前两层中使用了Dr

    2024年02月05日
    浏览(26)

觉得文章有用就打赏一下文章作者

支付宝扫一扫打赏

博客赞助

微信扫一扫打赏

请作者喝杯咖啡吧~博客赞助

支付宝扫一扫领取红包,优惠每天领

二维码1

领取红包

二维码2

领红包