|
| 1 | +import math |
| 2 | +import torch |
| 3 | +import torch.nn as nn |
| 4 | +import torch.nn.functional as F |
| 5 | + |
| 6 | + |
| 7 | +def densenet121_32x32(num_classes): |
| 8 | + """ densenet121 that works with 32x32 input (e.g. CIFAR10) """ |
| 9 | + return DenseNet(Bottleneck, [6, 12, 24, 16], growth_rate=32, num_classes=num_classes) |
| 10 | + |
| 11 | + |
| 12 | +def densenet169_32x32(num_classes): |
| 13 | + """ densenet169 that works with 32x32 input (e.g. CIFAR10) """ |
| 14 | + return DenseNet(Bottleneck, [6, 12, 32, 32], growth_rate=32, num_classes=num_classes) |
| 15 | + |
| 16 | + |
| 17 | +def densenet201_32x32(num_classes): |
| 18 | + """ densenet201 that works with 32x32 input (e.g. CIFAR10) """ |
| 19 | + return DenseNet(Bottleneck, [6, 12, 48, 32], growth_rate=32, num_classes=num_classes) |
| 20 | + |
| 21 | + |
| 22 | +def densenet161_32x32(num_classes): |
| 23 | + """ densenet161 that works with 32x32 input (e.g. CIFAR10) """ |
| 24 | + return DenseNet(Bottleneck, [6, 12, 36, 24], growth_rate=48, num_classes=num_classes) |
| 25 | + |
| 26 | + |
| 27 | +class Bottleneck(nn.Module): |
| 28 | + def __init__(self, in_planes, growth_rate): |
| 29 | + super(Bottleneck, self).__init__() |
| 30 | + self.bn1 = nn.BatchNorm2d(in_planes) |
| 31 | + self.conv1 = nn.Conv2d(in_planes, 4 * growth_rate, kernel_size=1, bias=False) |
| 32 | + self.bn2 = nn.BatchNorm2d(4 * growth_rate) |
| 33 | + self.conv2 = nn.Conv2d(4 * growth_rate, growth_rate, kernel_size=3, padding=1, bias=False) |
| 34 | + |
| 35 | + def forward(self, x): |
| 36 | + out = self.conv1(F.relu(self.bn1(x))) |
| 37 | + out = self.conv2(F.relu(self.bn2(out))) |
| 38 | + out = torch.cat([out, x], 1) |
| 39 | + return out |
| 40 | + |
| 41 | + |
| 42 | +class Transition(nn.Module): |
| 43 | + def __init__(self, in_planes, out_planes): |
| 44 | + super(Transition, self).__init__() |
| 45 | + self.bn = nn.BatchNorm2d(in_planes) |
| 46 | + self.conv = nn.Conv2d(in_planes, out_planes, kernel_size=1, bias=False) |
| 47 | + |
| 48 | + def forward(self, x): |
| 49 | + out = self.conv(F.relu(self.bn(x))) |
| 50 | + out = F.avg_pool2d(out, 2) |
| 51 | + return out |
| 52 | + |
| 53 | + |
| 54 | +class DenseNet(nn.Module): |
| 55 | + def __init__(self, block, nblocks, growth_rate=12, reduction=0.5, num_classes=10): |
| 56 | + super(DenseNet, self).__init__() |
| 57 | + self.growth_rate = growth_rate |
| 58 | + |
| 59 | + num_planes = 2 * growth_rate |
| 60 | + self.conv1 = nn.Conv2d(3, num_planes, kernel_size=3, padding=1, bias=False) |
| 61 | + |
| 62 | + self.dense1 = self._make_dense_layers(block, num_planes, nblocks[0]) |
| 63 | + num_planes += nblocks[0] * growth_rate |
| 64 | + out_planes = int(math.floor(num_planes * reduction)) |
| 65 | + self.trans1 = Transition(num_planes, out_planes) |
| 66 | + num_planes = out_planes |
| 67 | + |
| 68 | + self.dense2 = self._make_dense_layers(block, num_planes, nblocks[1]) |
| 69 | + num_planes += nblocks[1] * growth_rate |
| 70 | + out_planes = int(math.floor(num_planes * reduction)) |
| 71 | + self.trans2 = Transition(num_planes, out_planes) |
| 72 | + num_planes = out_planes |
| 73 | + |
| 74 | + self.dense3 = self._make_dense_layers(block, num_planes, nblocks[2]) |
| 75 | + num_planes += nblocks[2] * growth_rate |
| 76 | + out_planes = int(math.floor(num_planes * reduction)) |
| 77 | + self.trans3 = Transition(num_planes, out_planes) |
| 78 | + num_planes = out_planes |
| 79 | + |
| 80 | + self.dense4 = self._make_dense_layers(block, num_planes, nblocks[3]) |
| 81 | + num_planes += nblocks[3] * growth_rate |
| 82 | + |
| 83 | + self.bn = nn.BatchNorm2d(num_planes) |
| 84 | + self.linear = nn.Linear(num_planes, num_classes) |
| 85 | + |
| 86 | + def _make_dense_layers(self, block, in_planes, nblock): |
| 87 | + layers = [] |
| 88 | + for i in range(nblock): |
| 89 | + layers.append(block(in_planes, self.growth_rate)) |
| 90 | + in_planes += self.growth_rate |
| 91 | + return nn.Sequential(*layers) |
| 92 | + |
| 93 | + def forward(self, x): |
| 94 | + out = self.conv1(x) |
| 95 | + out = self.trans1(self.dense1(out)) |
| 96 | + out = self.trans2(self.dense2(out)) |
| 97 | + out = self.trans3(self.dense3(out)) |
| 98 | + out = self.dense4(out) |
| 99 | + out = F.avg_pool2d(F.relu(self.bn(out)), 4) |
| 100 | + out = out.view(out.size(0), -1) |
| 101 | + out = self.linear(out) |
| 102 | + return out |
0 commit comments