r"""Densenet-BC model class, based on `"Densely Connected Convolutional Networks" `_. Args: growth_rate (int) - how many filters to add each layer (`k` in paper) block_config (list of 4 ints) - how many layers in each pooling block
| 136 | |
| 137 | |
| 138 | class DenseNet(nn.Module): |
| 139 | r"""Densenet-BC model class, based on |
| 140 | `"Densely Connected Convolutional Networks" <https://arxiv.org/pdf/1608.06993.pdf>`_. |
| 141 | |
| 142 | Args: |
| 143 | growth_rate (int) - how many filters to add each layer (`k` in paper) |
| 144 | block_config (list of 4 ints) - how many layers in each pooling block |
| 145 | num_init_features (int) - the number of filters to learn in the first convolution layer |
| 146 | bn_size (int) - multiplicative factor for number of bottle neck layers |
| 147 | (i.e. bn_size * k features in the bottleneck layer) |
| 148 | drop_rate (float) - dropout rate after each dense layer |
| 149 | num_classes (int) - number of classification classes |
| 150 | memory_efficient (bool) - If True, uses checkpointing. Much more memory efficient, |
| 151 | but slower. Default: *False*. See `"paper" <https://arxiv.org/pdf/1707.06990.pdf>`_. |
| 152 | """ |
| 153 | |
| 154 | def __init__( |
| 155 | self, |
| 156 | growth_rate: int = 32, |
| 157 | block_config: Tuple[int, int, int, int] = (6, 12, 24, 16), |
| 158 | num_init_features: int = 64, |
| 159 | bn_size: int = 4, |
| 160 | drop_rate: float = 0, |
| 161 | num_classes: int = 1000, |
| 162 | memory_efficient: bool = False |
| 163 | ) -> None: |
| 164 | |
| 165 | super(DenseNet, self).__init__() |
| 166 | |
| 167 | # First convolution |
| 168 | self.features = nn.Sequential(OrderedDict([ |
| 169 | ('conv0', nn.Conv2d(3, num_init_features, kernel_size=7, stride=2, |
| 170 | padding=3, bias=False)), |
| 171 | ('norm0', nn.BatchNorm2d(num_init_features)), |
| 172 | ('relu0', nn.ReLU(inplace=True)), |
| 173 | ('pool0', nn.MaxPool2d(kernel_size=3, stride=2, padding=1)), |
| 174 | ])) |
| 175 | |
| 176 | # Each denseblock |
| 177 | num_features = num_init_features |
| 178 | for i, num_layers in enumerate(block_config): |
| 179 | block = _DenseBlock( |
| 180 | num_layers=num_layers, |
| 181 | num_input_features=num_features, |
| 182 | bn_size=bn_size, |
| 183 | growth_rate=growth_rate, |
| 184 | drop_rate=drop_rate, |
| 185 | memory_efficient=memory_efficient |
| 186 | ) |
| 187 | self.features.add_module('denseblock%d' % (i + 1), block) |
| 188 | num_features = num_features + num_layers * growth_rate |
| 189 | if i != len(block_config) - 1: |
| 190 | trans = _Transition(num_input_features=num_features, |
| 191 | num_output_features=num_features // 2) |
| 192 | self.features.add_module('transition%d' % (i + 1), trans) |
| 193 | num_features = num_features // 2 |
| 194 | |
| 195 | # Final batch norm |