A ResNet layer used to build the ResNet network. Architecture: --> conv-bn-relu -> conv -> + -> bn-relu -> conv-bn-relu -> conv -> + -> bn-relu --> | | | | -----> downsample ------> ---------------------------
| 9 | |
| 10 | |
| 11 | class ResNetLayer(nn.Module): |
| 12 | |
| 13 | """ |
| 14 | A ResNet layer used to build the ResNet network. |
| 15 | Architecture: |
| 16 | --> conv-bn-relu -> conv -> + -> bn-relu -> conv-bn-relu -> conv -> + -> bn-relu --> |
| 17 | | | | | |
| 18 | -----> downsample ------> -------------------------------------> |
| 19 | """ |
| 20 | |
| 21 | def __init__(self, inplanes, outplanes, stride): |
| 22 | super(ResNetLayer, self).__init__() |
| 23 | self.conv1a = nn.Conv2d(inplanes, outplanes, kernel_size=3, stride=stride, padding=1, bias=False) |
| 24 | self.bn1a = nn.BatchNorm2d(outplanes, momentum=0.01, eps=0.001) |
| 25 | self.conv2a = nn.Conv2d(outplanes, outplanes, kernel_size=3, stride=1, padding=1, bias=False) |
| 26 | self.stride = stride |
| 27 | self.downsample = nn.Conv2d(inplanes, outplanes, kernel_size=(1,1), stride=stride, bias=False) |
| 28 | self.outbna = nn.BatchNorm2d(outplanes, momentum=0.01, eps=0.001) |
| 29 | |
| 30 | self.conv1b = nn.Conv2d(outplanes, outplanes, kernel_size=3, stride=1, padding=1, bias=False) |
| 31 | self.bn1b = nn.BatchNorm2d(outplanes, momentum=0.01, eps=0.001) |
| 32 | self.conv2b = nn.Conv2d(outplanes, outplanes, kernel_size=3, stride=1, padding=1, bias=False) |
| 33 | self.outbnb = nn.BatchNorm2d(outplanes, momentum=0.01, eps=0.001) |
| 34 | return |
| 35 | |
| 36 | |
| 37 | def forward(self, inputBatch): |
| 38 | batch = F.relu(self.bn1a(self.conv1a(inputBatch))) |
| 39 | batch = self.conv2a(batch) |
| 40 | if self.stride == 1: |
| 41 | residualBatch = inputBatch |
| 42 | else: |
| 43 | residualBatch = self.downsample(inputBatch) |
| 44 | batch = batch + residualBatch |
| 45 | intermediateBatch = batch |
| 46 | batch = F.relu(self.outbna(batch)) |
| 47 | |
| 48 | batch = F.relu(self.bn1b(self.conv1b(batch))) |
| 49 | batch = self.conv2b(batch) |
| 50 | residualBatch = intermediateBatch |
| 51 | batch = batch + residualBatch |
| 52 | outputBatch = F.relu(self.outbnb(batch)) |
| 53 | return outputBatch |
| 54 | |
| 55 | |
| 56 |