https://www.bilibili.com/video/BV1T7411T7wa
分析ResNet网络的结构,了解Batch Normalization和迁移学习的相关概念

不能简单堆叠cnn与pool:效果差了
1.梯度消失与爆炸明显
消失:

解决:数据标准化处理,模型初始化,以及BatchNormalization
2.退化的问题
用残差结构解决



两种残差结构:左面的给小的,右边的给大的

7:35 很详细

下采样(用右边的卷积核降维)





迁移学习:
31:58

model

import torch.nn as nn
import torchclass BasicBlock(nn.Module):expansion = 1#stride,实线为1 虚线为2 05:04def __init__(self, in_channel, out_channel, stride=1, downsample=None):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)#bias 不需要使用偏置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 += identityout = self.relu(out)return outclass Bottleneck(nn.Module):#50,101,152的残差结构expansion = 4#4倍def __init__(self, in_channel, out_channel, stride=1, downsample=None):super(Bottleneck, self).__init__()self.conv1 = nn.Conv2d(in_channels=in_channel, out_channels=out_channel,kernel_size=1, stride=1, bias=False)  # squeeze channelsself.bn1 = nn.BatchNorm2d(out_channel)# -----------------------------------------self.conv2 = nn.Conv2d(in_channels=out_channel, out_channels=out_channel,kernel_size=3, stride=stride, bias=False, padding=1)self.bn2 = nn.BatchNorm2d(out_channel)# -----------------------------------------self.conv3 = nn.Conv2d(in_channels=out_channel, out_channels=out_channel*self.expansion,kernel_size=1, stride=1, bias=False)  # unsqueeze channels#out*expansion = 4#4倍self.bn3 = nn.BatchNorm2d(out_channel*self.expansion)self.relu = nn.ReLU(inplace=True)self.downsample = downsampledef forward(self, x):identity = xif 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 += identityout = self.relu(out)return outclass ResNet(nn.Module):def __init__(self, block, blocks_num, num_classes=1000, include_top=True):super(ResNet, self).__init__()self.include_top = include_topself.in_channel = 64self.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 = Noneif 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))self.in_channel = channel * block.expansionfor _ in range(1, block_num):layers.append(block(self.in_channel, channel))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 xdef resnet34(num_classes=2, include_top=True):return ResNet(BasicBlock, [3, 4, 6, 3], num_classes=num_classes, include_top=include_top)def resnet101(num_classes=2, include_top=True):return ResNet(Bottleneck, [3, 4, 23, 3], num_classes=num_classes, include_top=include_top)

train


net = resnet34()
# load pretrain weights
# model_weight_path = "./resnet34-pre.pth"
# missing_keys, unexpected_keys = net.load_state_dict(torch.load(model_weight_path), strict=False)
# for param in net.parameters():
#     param.requires_grad = False
# change fc layer structure
inchannel = net.fc.in_features
net.fc = nn.Linear(inchannel, 2)
net.to(device)loss_function = nn.CrossEntropyLoss()
optimizer = optim.Adam(net.parameters(), lr=0.0001)

ResNet学习笔记相关推荐

  1. ResNet网络学习笔记。

    ResNet网络学习 看b站 霹雳吧啦Wz 的视频总结的学习笔记! 视频的地址 大佬的Github代码 1.ResNet详解 ResNet 网络是在2015年由微软实验室提出,斩获当年 ImageNe ...

  2. resnet50网络结构_学习笔记(一):分析resnet源码理解resnet网络结构

    最近在跑实验的过程中一直在使用resnet50和resnet34,为了弄清楚网络的结构和原理的实现,打开resnet的源码进行了学习. 残差网络学习的原理 针对神经网络过深而导致的学习准确率饱和甚至是 ...

  3. ResNet 小白学习笔记

    写在前面 直接看论文对我这个小白来说太不友好了,几次放弃 /(ㄒoㄒ)/~~ .幸好找到了一个通俗易懂的视频:6.1 ResNet网络结构,BN以及迁移学习详解,以下笔记大部分基于视频内容,再补充了一 ...

  4. 深度学习入门之PyTorch学习笔记:卷积神经网络

    深度学习入门之PyTorch学习笔记 绪论 1 深度学习介绍 2 深度学习框架 3 多层全连接网络 4 卷积神经网络 4.1 主要任务及起源 4.2 卷积神经网络的原理和结构 4.2.1 卷积层 1. ...

  5. AI学习笔记(十四)CNN之图像分割

    AI学习笔记之CNN之图像分割 图像分割 问题引入 实现技术手段及分类 语义分割-FCN(Fully Convolutional Networks) FCN--deconv 反卷积的具体步骤 Unpo ...

  6. AI学习笔记(十一)CNN之图像识别(上)

    AI学习笔记之CNN之图像识别(上) 图像识别 图像识别简介 模式识别 图像识别的过程 图像识别的应用 分类与检测 VGG Resnet 迁移学习&inception 卷积神经网络迁移学习fi ...

  7. AI学习笔记(十一)CNN之图像识别(下)

    AI学习笔记之CNN之图像识别(下) 图像识别 图像识别简介 模式识别 图像识别的过程 图像识别的应用 分类与检测 VGG Resnet 迁移学习&inception 卷积神经网络迁移学习fi ...

  8. AI学习笔记(十二)物体检测(上)

    AI学习笔记之物体检测(上) 物体检测简介 常见检测网络 IOU TP.TN.FP.FN precision(精确度)和recall(召回率) 边框回归 边框回归具体方法 Selective Sear ...

  9. AI学习笔记(十)卷积神经网络

    AI学习笔记之卷积神经网络 卷积神经网络简介 卷积层 池化层 卷积神经网络 卷积核 填充padding 常见的卷积神经网络 cifar-10预测实例 数据预处理--图像增强 图像增强常用方法 Alex ...

最新文章

  1. 基于点云描述子的立体视觉里程计快速鲁棒的位置识别方法
  2. JVM 调优实战--什么是调优及如何调优的思路
  3. 计算机在材料科学的应用论文,计算机在材料科学中的应用论文
  4. Python进行数据分析—可视化之seaborn
  5. Python Demo 04-蒙特卡罗猜测与计时
  6. 25条写代码建议,句句真言,值得牢记!
  7. Oracle查询表|注释|字段|字段注释
  8. 哪一类人用苹果手机最多?
  9. Android(java)学习笔记97:使用GridView以及重写BaseAdapter
  10. 华为音量键只能调通话_华为手机音量键的隐藏功能,知道一个就会好用不少!...
  11. perl:非贪婪的数量词
  12. python中pdfplumber解析pdf_Python中pdfplumber如何提取pdf中的表格数据
  13. 抖音便捷小空调特效 html+css+js
  14. 一元多项式的相加和相减操作(链表)
  15. Vue 项目实战五 参数管理 商品列表
  16. 信号(signal,kill,raise)
  17. 在php中将Unicode字符转成中文
  18. python画图代码100行_用100行Python代码告诉你国庆那些景点爆满!
  19. grpc+gateway使用说明
  20. 序列模型 - 词向量的运算与Emoji生成器

热门文章

  1. jzoj100029. 【NOIP2017提高A组模拟7.8】陪审团(贪心,排序)
  2. JavaScript+HTML设置视频预览图
  3. RabbitMQ基础概念详细介绍
  4. CSS扩展“less和”sass“
  5. [翻译]load-on-startup 元素的作用
  6. SVN合并(merge)的使用
  7. 西南交大计算机专硕就业怎么样,国内四所交通大学,有985也有211,就业、深造容易,值得报考...
  8. java 处理byte_java - 文件到Java中的byte [] - 堆栈内存溢出
  9. zincrby redis python_【Redis数据结构 序】使用redispy操作Redis数据库
  10. Java自动拆装箱面试_跟王老师学泛型(二):Java自动装箱与拆箱