残差网络:将输入层与输出层进行链接,保证了通过这层网路结构,网络的运算能力不会出现较大的改变网络
网络解析:ide
第一层网络: 输入网络通过一个卷积层,再通过一个batch_normalize, 再通过一个relu层spa
第二层网络;通过一层卷积层,将卷积后的网络与原输入数据进行对应位置相加操做, 将加和后的网络进行batch_normalize, 再通过一层relu code
import torch from torch import nn def conv3x3(in_planes, out_planes, stride=1): return nn.Conv2d(in_planes, out_planes, kernel_size=3, stride=1, padding=1, bias=False) # 定义卷积层 class BasicBlock(nn.Module): def __init__(self, inplanes, outplanes, stride, downsample=None): super(BasicBlock, self).__init__() self.conv1 = conv3x3(inplanes, outplanes, stride=stride) # 第一个卷积 self.bn = nn.BatchNorm2d(outplanes) # 定义batch_norm层 self.relu = nn.ReLU(inplace=True) # 定义激活层 self.conv2 = conv3x3(outplanes, outplanes, stride=stride) # 第二个卷积 self.bn2 = nn.BatchNorm1d(outplanes) # 进行标准化操做 self.downsample = downsample # 进行维度的下降, 一般使用卷积操做来进行维度的下降 def forward(self, x): residual = x # 原始的残差模块 x = self.conv1(x) # 第一次卷积 x = self.bn(x) # 归一化操做 x = self.relu(x) # 激活操做 x = self.conv2(x) # 第二次卷积 out = self.bn2(x) # 归一化操做 if self.downsample is not None: residual = self.downsample(x) # 是否须要对原始的样本作降采样操做 out += residual # 进行加和操做 out = self.relu(out) # 进行激活操做 return out