ResNet残差网络Pytorch实现——Bottleneck残差块


上一篇:​​【BasicBlock残差块】​​ ✌✌✌✌ ​​【目录】​​ ✌✌✌✌ 下一篇:​​【结合各个残差块】​​


大学生一枚,最近在学习神经网络,写这篇文章只是记录自己的学习历程,本文参考了​​Github上fengdu78老师的文章​​进行学习


✌ Bottleneck

# 50、101、152层残差块,三个卷积层,1*1,3*3,1*1
class Bottleneck(nn.Module):
# 这里对应是4,对应每层中的64,64,256
expansion=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)
self.bn1=nn.BatchNorm2d(out_channel)

self.conv2=nn.Conv2d(in_channels=out_channel,out_channels=out_channel,
kernel_size=3,stride=stride,padding=1,bias=False)
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)
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=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