|
| 1 | +import torch |
| 2 | +import torch.nn as nn |
| 3 | +import torch.nn.functional as F |
| 4 | +from collections import OrderedDict |
| 5 | +from .conv import MaskedConv2d, TimeNorm2d |
| 6 | + |
| 7 | + |
| 8 | +def init_model(model): |
| 9 | + # Official init from torch repo. |
| 10 | + for m in model.modules(): |
| 11 | + if isinstance(m, nn.Conv2d): |
| 12 | + nn.init.kaiming_normal_(m.weight) |
| 13 | + elif isinstance(m, nn.BatchNorm2d): |
| 14 | + nn.init.constant_(m.weight, 1) |
| 15 | + nn.init.constant_(m.bias, 0) |
| 16 | + elif isinstance(m, nn.Linear): |
| 17 | + nn.init.constant_(m.bias, 0) |
| 18 | + |
| 19 | + |
| 20 | +class _DenseLayer(nn.Sequential): |
| 21 | + def __init__(self, num_input_features, growth_rate, bn_size, drop_rate): |
| 22 | + super(_DenseLayer, self).__init__() |
| 23 | + # self.norm1 = nn.BatchNorm2d(num_input_features) |
| 24 | + self.relu = nn.ReLU(inplace=True) |
| 25 | + self.conv1 = nn.Conv2d( |
| 26 | + num_input_features, bn_size * growth_rate, kernel_size=1, stride=1, bias=True) |
| 27 | + # self.norm2 = nn.BatchNorm2d(bn_size * growth_rate) |
| 28 | + self.conv2 = MaskedConv2d(bn_size * growth_rate, growth_rate, |
| 29 | + kernel_size=3, stride=1, padding=1, bias=True) |
| 30 | + self.drop_rate = drop_rate |
| 31 | + |
| 32 | + def forward(self, inputs): |
| 33 | + # x = self.norm1(inputs) |
| 34 | + x = self.relu(inputs) |
| 35 | + x = self.conv1(x) |
| 36 | + # x = self.norm2(x) |
| 37 | + x = self.relu(x) |
| 38 | + new_features, _ = self.conv2(x) |
| 39 | + if self.drop_rate > 0: |
| 40 | + new_features = F.dropout( |
| 41 | + new_features, p=self.drop_rate, training=self.training) |
| 42 | + return torch.cat([inputs, new_features], 1) |
| 43 | + |
| 44 | + |
| 45 | +class _DenseBlock(nn.Sequential): |
| 46 | + def __init__(self, num_layers, num_input_features, bn_size, growth_rate, drop_rate): |
| 47 | + super(_DenseBlock, self).__init__() |
| 48 | + for i in range(num_layers): |
| 49 | + layer = _DenseLayer(num_input_features + i * |
| 50 | + growth_rate, growth_rate, bn_size, drop_rate) |
| 51 | + self.add_module('denselayer%d' % (i + 1), layer) |
| 52 | + |
| 53 | + |
| 54 | +class _Transition(nn.Sequential): |
| 55 | + def __init__(self, num_input_features, num_output_features): |
| 56 | + super(_Transition, self).__init__() |
| 57 | + # self.add_module('norm', nn.BatchNorm2d(num_input_features)) |
| 58 | + self.add_module('relu', nn.ReLU(inplace=True)) |
| 59 | + self.add_module('conv', nn.Conv2d(num_input_features, num_output_features, |
| 60 | + kernel_size=1, stride=1, bias=True)) |
| 61 | + # self.add_module('pool', nn.AvgPool2d(kernel_size=(2,1), stride=(2, 1))) |
| 62 | + |
| 63 | + |
| 64 | +class DenseNet(nn.Module): |
| 65 | + r"""Densenet-BC model class, based on |
| 66 | + `"Densely Connected Convolutional Networks" <https://arxiv.org/pdf/1608.06993.pdf>`_ |
| 67 | +
|
| 68 | + Args: |
| 69 | + growth_rate (int) - how many filters to add each layer (`k` in paper) |
| 70 | + block_config (list of 4 ints) - how many layers in each pooling block |
| 71 | + num_init_features (int) - the number of filters to learn in the first convolution layer |
| 72 | + bn_size (int) - multiplicative factor for number of bottle neck layers |
| 73 | + (i.e. bn_size * k features in the bottleneck layer) |
| 74 | + drop_rate (float) - dropout rate after each dense layer |
| 75 | + num_classes (int) - number of classification classes |
| 76 | + """ |
| 77 | + |
| 78 | + def __init__(self, input_size, output_size, growth_rate=32, block_config=(6, 12, 24, 16), |
| 79 | + num_init_features=64, bn_size=4, drop_rate=0): |
| 80 | + |
| 81 | + super(DenseNet, self).__init__() |
| 82 | + num_init_features = input_size |
| 83 | + # self.conv0 = MaskedConv2d(input_size, num_init_features, |
| 84 | + # kernel_size=7, stride=(2, 1), padding=3, bias=True) |
| 85 | + # self.norm0 = nn.BatchNorm2d(num_init_features) |
| 86 | + # self.relu0 = nn.ReLU(inplace=True) |
| 87 | + # self.pool0 = nn.MaxPool2d(kernel_size=(3, 1), |
| 88 | + # stride=(2, 1), padding=(1, 0)) |
| 89 | + self.features = nn.Sequential() |
| 90 | + # Each denseblock |
| 91 | + num_features = num_init_features |
| 92 | + for i, num_layers in enumerate(block_config): |
| 93 | + block = _DenseBlock(num_layers=num_layers, num_input_features=num_features, |
| 94 | + bn_size=bn_size, growth_rate=growth_rate, drop_rate=drop_rate) |
| 95 | + self.features.add_module('denseblock%d' % (i + 1), block) |
| 96 | + num_features = num_features + num_layers * growth_rate |
| 97 | + if i != len(block_config) - 1: |
| 98 | + trans = _Transition( |
| 99 | + num_input_features=num_features, num_output_features=num_features // 2) |
| 100 | + self.features.add_module('transition%d' % (i + 1), trans) |
| 101 | + num_features = num_features // 2 |
| 102 | + |
| 103 | + # Final batch norm |
| 104 | + # self.features.add_module('norm5', nn.BatchNorm2d(num_features)) |
| 105 | + |
| 106 | + self.conv2 = nn.Conv2d(num_features, output_size, |
| 107 | + kernel_size=1, bias=True) |
| 108 | + # self.bn2 = nn.BatchNorm2d(output_size) |
| 109 | + self.relu = nn.ReLU(inplace=True) |
| 110 | + # Linear layer |
| 111 | + init_model(self) |
| 112 | + |
| 113 | + def forward(self, x): |
| 114 | + # x, _ = self.conv0(x) |
| 115 | + # x = self.norm0(x) |
| 116 | + # x = self.relu(x) |
| 117 | + # x = self.pool0(x) |
| 118 | + features = self.features(x) |
| 119 | + out = F.relu(features) |
| 120 | + out = self.conv2(out) |
| 121 | + # out = self.bn2(out) |
| 122 | + out = self.relu(out) |
| 123 | + return out |
0 commit comments