MCPcopy Create free account
hub / github.com/NVIDIA/semantic-segmentation / ResNet

Class ResNet

network/Resnet.py:136–194  ·  view source on GitHub ↗

Resnet Global Module for Initialization

Source from the content-addressed store, hash-verified

134
135
136class ResNet(nn.Module):
137 """
138 Resnet Global Module for Initialization
139 """
140 def __init__(self, block, layers, num_classes=1000):
141 self.inplanes = 64
142 super(ResNet, self).__init__()
143 self.conv1 = nn.Conv2d(3, 64, kernel_size=7, stride=2, padding=3,
144 bias=False)
145 self.bn1 = mynn.Norm2d(64)
146 self.relu = nn.ReLU(inplace=True)
147 self.maxpool = nn.MaxPool2d(kernel_size=3, stride=2, padding=1)
148 self.layer1 = self._make_layer(block, 64, layers[0])
149 self.layer2 = self._make_layer(block, 128, layers[1], stride=2)
150 self.layer3 = self._make_layer(block, 256, layers[2], stride=2)
151 self.layer4 = self._make_layer(block, 512, layers[3], stride=2)
152 self.avgpool = nn.AvgPool2d(7, stride=1)
153 self.fc = nn.Linear(512 * block.expansion, num_classes)
154
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 def _make_layer(self, block, planes, blocks, stride=1):
163 downsample = None
164 if stride != 1 or self.inplanes != planes * block.expansion:
165 downsample = nn.Sequential(
166 nn.Conv2d(self.inplanes, planes * block.expansion,
167 kernel_size=1, stride=stride, bias=False),
168 mynn.Norm2d(planes * block.expansion),
169 )
170
171 layers = []
172 layers.append(block(self.inplanes, planes, stride, downsample))
173 self.inplanes = planes * block.expansion
174 for index in range(1, blocks):
175 layers.append(block(self.inplanes, planes))
176
177 return nn.Sequential(*layers)
178
179 def forward(self, x):
180 x = self.conv1(x)
181 x = self.bn1(x)
182 x = self.relu(x)
183 x = self.maxpool(x)
184
185 x = self.layer1(x)
186 x = self.layer2(x)
187 x = self.layer3(x)
188 x = self.layer4(x)
189
190 x = self.avgpool(x)
191 x = x.view(x.size(0), -1)
192 x = self.fc(x)
193

Callers 5

resnet18Function · 0.85
resnet34Function · 0.85
resnet50Function · 0.85
resnet101Function · 0.85
resnet152Function · 0.85

Calls

no outgoing calls

Tested by

no test coverage detected