MCPcopy Create free account
hub / github.com/Sirwenhao/Deep-Learning-Notes / BasicBlock

Class BasicBlock

CV/Pytorch_classification/ResNet/model.py:9–38  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

7# 此处定义的块结构是针对resnet18和resnet34这两种结构的,expansion参数表述主分支结构上卷积通道数的变化
8# 在18和34两种结构中,每一个基础块结构中,主分支上的通道数事不发生变化的,故expansion=1
9class BasicBlock(nn.Module):
10 expansion = 1
11
12 def __init__(self, in_channel, out_channel, stride=1, downsample=None, **kwargs):
13 super(BasicBlock, self).__init__()
14 self.conv1 = nn.Conv2d(in_channels=in_channel, out_channels=out_channel,
15 kernel_size=3, stride=stride, padding=1, bias=False)
16 self.bn1 = nn.BatchNorm2d(out_channel)
17 self.relu = nn.ReLU()
18 self.conv2 = nn.Conv2d(in_channels=out_channel, out_channels=out_channel,
19 kernel_size=3, stride=1, padding=1, bias=False)
20 self.bn2 = nn.BatchNorm2d(out_channel)
21 self.downsample = downsample
22
23 def forward(self, x):
24 identity = x
25 if self.downsample is not None:
26 identity = self.downsample(x)
27
28 out = self.conv1(x)
29 out = self.bn1(out)
30 out = self.relu(out)
31
32 out = self.conv2(out)
33 out = self.bn2(out)
34
35 out += identity
36 out = self.relu(out)
37
38 return out
39
40class Bottleneck(nn.Module):
41 expansion = 4

Callers

nothing calls this directly

Calls

no outgoing calls

Tested by

no test coverage detected