MCPcopy Create free account
hub / github.com/M-Nauta/ProtoTree / __init__

Method __init__

features/resnet_features.py:130–170  ·  view source on GitHub ↗
(self, block, layers, num_classes=1000, zero_init_residual=False)

Source from the content-addressed store, hash-verified

128 '''
129
130 def __init__(self, block, layers, num_classes=1000, zero_init_residual=False):
131 super(ResNet_features, self).__init__()
132
133 self.inplanes = 64
134
135 # the first convolutional layer before the structured sequence of blocks
136 self.conv1 = nn.Conv2d(3, 64, kernel_size=7, stride=2, padding=3,
137 bias=False)
138 self.bn1 = nn.BatchNorm2d(64)
139 self.relu = nn.ReLU(inplace=True)
140 self.maxpool = nn.MaxPool2d(kernel_size=3, stride=2, padding=1)
141 # comes from the first conv and the following max pool
142 self.kernel_sizes = [7, 3]
143 self.strides = [2, 2]
144 self.paddings = [3, 1]
145
146 # the following layers, each layer is a sequence of blocks
147 self.block = block
148 self.layers = layers
149 self.layer1 = self._make_layer(block=block, planes=64, num_blocks=self.layers[0])
150 self.layer2 = self._make_layer(block=block, planes=128, num_blocks=self.layers[1], stride=2)
151 self.layer3 = self._make_layer(block=block, planes=256, num_blocks=self.layers[2], stride=2)
152 self.layer4 = self._make_layer(block=block, planes=512, num_blocks=self.layers[3], stride=2)
153
154 # initialize the parameters
155 for m in self.modules():
156 if isinstance(m, nn.Conv2d):
157 nn.init.kaiming_normal_(m.weight, mode='fan_out', nonlinearity='relu')
158 elif isinstance(m, nn.BatchNorm2d):
159 nn.init.constant_(m.weight, 1)
160 nn.init.constant_(m.bias, 0)
161
162 # Zero-initialize the last BN in each residual branch,
163 # so that the residual branch starts with zeros, and each residual block behaves like an identity.
164 # This improves the model by 0.2~0.3% according to https://arxiv.org/abs/1706.02677
165 if zero_init_residual:
166 for m in self.modules():
167 if isinstance(m, Bottleneck):
168 nn.init.constant_(m.bn3.weight, 0)
169 elif isinstance(m, BasicBlock):
170 nn.init.constant_(m.bn2.weight, 0)
171
172 def _make_layer(self, block, planes, num_blocks, stride=1):
173 downsample = None

Callers

nothing calls this directly

Calls 2

_make_layerMethod · 0.95
__init__Method · 0.45

Tested by

no test coverage detected