当前位置:网站首页>Pytorch notes - get familiar with the network construction method by building RESNET (complete code)
Pytorch notes - get familiar with the network construction method by building RESNET (complete code)
2022-04-23 05:59:00 【umbrellalalalala】
Reference material :《 Deep learning framework PyTorch: Introduction and practice 》
Be careful : The author of the book said that we should pay attention to the simplicity of programming , Therefore, the network construction method of this paper can be used for reference .
Complete code
See notes for some explanations :
from torch import nn
import torch as t
from torch.nn import functional as F
# Implementer module:Residual Block
class ResidualBlock(nn.Module):
def __init__(self, inchannel, outchannel, stride=1, shortcut=None):
super().__init__()
self.left = nn.Sequential(
# padding=1 It refers to filling a line up, down, left and right , If it is padding=(1, 1) It is
# Fill in a line from top to bottom 、 Fill in a line on the left and right ( First high then wide )
nn.Conv2d(inchannel, outchannel, 3, stride, 1, bias=False),
nn.BatchNorm2d(outchannel),
# inplace=True It refers to in-situ operation , Directly modify the... Passed down from the upper layer tensor, Save memory
nn.ReLU(inplace=True),
nn.Conv2d(outchannel, outchannel, 3, 1, 1, bias=False),
nn.BatchNorm2d(outchannel))
self.right = shortcut
def forward(self, x):
out = self.left(x)
residual = x if self.right is None else self.right(x)
out += residual
return F.relu(out)
class ResNet(nn.Module):
def __init__(self, num_classes=1000):
super().__init__()
self.pre = nn.Sequential(
nn.Conv2d(3, 64, 7, 2, 3, bias=False),
nn.BatchNorm2d(64),
nn.ReLU(inplace=True),
# kernel_size, stride, padding
nn.MaxPool2d(3, 2, 1))
self.layer1 = self._make_layer(64, 128, 3)
self.layer2 = self._make_layer(128, 256, 4, stride=2)
self.layer3 = self._make_layer(256, 512, 6, stride=2)
self.layer4 = self._make_layer(512, 512, 3, stride=2)
self.fc = nn.Linear(512, num_classes)
def _make_layer(self, inchannel, outchannel, block_num, stride=1):
shortcut = nn.Sequential(
nn.Conv2d(inchannel, outchannel, 1, stride, bias=False),
nn.BatchNorm2d(outchannel))
layers = []
# The first one to put inchannel become outchannel
layers.append(ResidualBlock(inchannel, outchannel, stride, shortcut))
for i in range(1, block_num):
layers.append(ResidualBlock(outchannel, outchannel))
# list a=[1,2,3], that *a Is refers to 1 2 3
return nn.Sequential(*layers)
def forward(self, x):
x = self.pre(x)
x = self.layer1(x)
x = self.layer2(x)
x = self.layer3(x)
x = self.layer4(x)
x = F.avg_pool2d(x, 7)
# Turn into [batch, ...]
x = x.view(x.size(0), -1)
return self.fc(x)
Test forward propagation
model = ResNet()
# simulation 224×224 Pictures of the , Passageway is 3. At the beginning 1 yes batch_size
input = t.randn(1, 3, 224, 224)
o = model(input)
o.shape
Output results :
torch.Size([1, 1000])
Corresponding classification problem 1000 Categories .
版权声明
本文为[umbrellalalalala]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/04/202204230543474417.html
边栏推荐
- The user name and password of users in the domain accessing the samba server outside the domain are wrong
- Postfix变成垃圾邮件中转站后的补救
- The attendance client date of K / 3 wise system can only be selected to 2019
- In depth source code analysis servlet first program
- 图像恢复论文简记——Uformer: A General U-Shaped Transformer for Image Restoration
- 你不能访问此共享文件夹,因为你组织的安全策略阻止未经身份验证的来宾访问
- Pytorch Learning record (XIII): Recurrent Neural Network
- PyTorch笔记——观察DataLoader&用torch构建LeNet处理CIFAR-10完整代码
- 建表到页面完整实例演示—联表查询
- 2.devops-sonar安装
猜你喜欢
Ptorch learning record (XIII): recurrent neural network
Anaconda installed pyqt5 and pyqt5 tools without designer Exe problem solving
容器
delete和truncate
LDCT图像重建论文——Eformer: Edge Enhancement based Transformer for Medical Image Denoising
Conda 虚拟环境管理(创建、删除、克隆、重命名、导出和导入)
PyQt5学习(一):布局管理+信号和槽关联+菜单栏与工具栏+打包资源包
框架解析1.系统架构简介
深入源码分析Servlet第一个程序
JVM series (3) -- memory allocation and recycling strategy
随机推荐
MySQL realizes master-slave replication / master-slave synchronization
治疗TensorFlow后遗症——简单例子记录torch.utils.data.dataset.Dataset重写时的图片维度问题
多线程与高并发(3)——synchronized原理
MySQL triggers, stored procedures, stored functions
PyTorch笔记——实现线性回归完整代码&手动或自动计算梯度代码对比
PyQy5学习(四):QAbstractButton+QRadioButton+QCheckBox
Fundamentals of SQL: first knowledge of database and SQL - installation and basic introduction - Alibaba cloud Tianchi
无监督去噪——[TMI2022]ISCL: Interdependent Self-Cooperative Learning for Unpaired Image Denoising
Latex quick start
Pytorch——数据加载和处理
Practical operation - Nacos installation and configuration
protected( 被 protected 修饰的成员对于本包和其子类可见)
JVM family (4) -- memory overflow (OOM)
PyTorch笔记——观察DataLoader&用torch构建LeNet处理CIFAR-10完整代码
PyQy5学习(二):QMainWindow+QWidget+QLabel
opensips(1)——安装opensips详细流程
Pytorch学习记录(十):数据预处理+Batch Normalization批处理(BN)
框架解析2.源码-登录认证
EditorConfig
实操—Nacos安装与配置