文章目录
- Resnet需要注意的是: 因为是x和处理后的特征相加, 所以经过处理后的特征的维度和输入是一致的,假如说有变化的话,那么x可以加一个1*1卷积,变换通道数
- 用个例程说明
- Bn层会减去均值, 所以cnn的bias可以设置为False加快运算
def conv3_3 (in_planes, out_planes, stride=1, groups=1, dilation=1):
return nn.Conv2d(in_planes, out_planes, kernel_size=3, stride=stride,
padding=dilation, groups=groups,bias=False, dilation=dilation)
class BasicBlock(nn.Module):
expansion=1
__constants__ = ['downsample']
def __init__(self, inplanes, planes, stride=1, downsample=None, groups=1,
base_width=64, dialation=1, norm_layer=None):
super(BasicBlock, self).__init__()
if norm_layer is None:
norm_layer = nn.BatchNorm2d
self.conv1 = conv3_3(inplanes, planes, stride)
self.bn1 = norm_layer(planes)
self.relu = nn.Relu(inplace=True)
self.conv2 = nn.conv3_3(planes, planes)
self.bn2 = norm_layer(planes)
self.downsample = downsample
self.stride = stride
def forward(self, x):
identity = x
out = self.conv1(x)
out = self.bn1(out)
out = self.relu(out)
out = self.conv2(out)
out = self.bn2(out)
if self.downsample is Not None:
identity = self.downsample(x)
out += identity
out = self.relu(out)
return out
- 一般cnn构架是bn后加Relu, Resnet最后相加后也需要Relu, 所以cnn的后面没有Relu
|