Densenet-BC model class for imagenet 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 num_init_features (int) - the number of filters to learn in the first convolution l
| 81 | self.add_module("pool", nn.AvgPool2d(kernel_size=2, stride=2)) |
| 82 | |
| 83 | class DenseNet(nn.Module): |
| 84 | """ |
| 85 | Densenet-BC model class for imagenet |
| 86 | |
| 87 | Args: |
| 88 | growth_rate (int) - how many filters to add each layer (`k` in paper) |
| 89 | block_config (list of 4 ints) - how many layers in each pooling block |
| 90 | num_init_features (int) - the number of filters to learn in the first convolution layer |
| 91 | bn_size (int) - multiplicative factor for number of bottle neck layers |
| 92 | (i.e. bn_size * k features in the bottleneck layer) |
| 93 | drop_rate (float) - dropout rate after each dense layer |
| 94 | num_classes (int) - number of classification classes |
| 95 | memory_efficient (bool) - If True, uses checkpointing. Much more memory efficient |
| 96 | """ |
| 97 | |
| 98 | def __init__(self, growth_rate=32, block_config=(6, 12, 24, 16), |
| 99 | num_init_features=64, bn_size=4, drop_rate=0, num_classes=1000, memory_efficient=False): |
| 100 | super(DenseNet, self).__init__() |
| 101 | |
| 102 | # first conv+bn+relu+pool |
| 103 | self.features = nn.Sequential(OrderedDict([ |
| 104 | ("conv0", nn.Conv2d(3, num_init_features, kernel_size=7, stride=2, padding=3, bias=False)), |
| 105 | ("norm0", nn.BatchNorm2d(num_init_features)), |
| 106 | ("relu0", nn.ReLU(inplace=True)), |
| 107 | ("pool0", nn.MaxPool2d(kernel_size=3, stride=2, padding=1)), |
| 108 | ])) |
| 109 | |
| 110 | # each dense block |
| 111 | num_features = num_init_features |
| 112 | for i, num_layers in enumerate(block_config): |
| 113 | block = _DenseBlock(num_layers=num_layers, input_c=num_features, bn_size=bn_size, growth_rate=growth_rate, drop_rate=drop_rate, memory_efficient=memory_efficient) |
| 114 | self.features.add_module("denseblock %d" %(i+1), block) |
| 115 | num_features = num_features + num_layers*growth_rate |
| 116 | |
| 117 | if i != len(block_config)-1: |
| 118 | trans = _Transition(input_c=num_features, output_c=num_features//2) |
| 119 | self.features.add_module("transition %d" %(i+1), trans) |
| 120 | num_features = num_features // 2 |
| 121 | |
| 122 | # finnal batch norm |
| 123 | self.features.add_module("norm5", nn.BatchNorm2d(num_features)) |
| 124 | |
| 125 | # fc layer |
| 126 | self.classifier = nn.Linear(num_features, num_classes) |
| 127 | |
| 128 | |
| 129 | # init weights |
| 130 | for m in self.modules(): |
| 131 | if isinstance(m, nn.Conv2d): |
| 132 | nn.init.kaiming_normal_(m.weight) |
| 133 | elif isinstance(m, nn.BatchNorm2d): |
| 134 | nn.init.constant_(m.weight, 1) |
| 135 | nn.init.constant_(m.bias, 0) |
| 136 | elif isinstance(m, nn.Linear): |
| 137 | nn.init.constant_(m.bias, 0) |
| 138 | |
| 139 | def forward(self, x): |
| 140 | features = self.features(x) |
no outgoing calls
no test coverage detected