|
| 1 | +import math |
| 2 | +import torch |
| 3 | +import torch.nn as nn |
| 4 | +import torch.nn.functional as F |
| 5 | +from models import conv_block |
| 6 | + |
| 7 | +class BasicBlock(nn.Module): |
| 8 | + def __init__(self, in_planes, out_planes, args): |
| 9 | + super(BasicBlock, self).__init__() |
| 10 | + self.conv = conv_block(in_planes, out_planes, 3, args.block_type, |
| 11 | + args.use_gn, args.gn_groups, args.drop_type, |
| 12 | + args.drop_rate, padding=1, track_stats=args.report_ratio) |
| 13 | + |
| 14 | + def forward(self, x): |
| 15 | + out = self.conv(x) |
| 16 | + return torch.cat([x, out], 1) |
| 17 | + |
| 18 | +class Bottleneck(nn.Module): |
| 19 | + def __init__(self, in_planes, out_planes, args): |
| 20 | + super(Bottleneck, self).__init__() |
| 21 | + inter_planes = out_planes * 4 |
| 22 | + self.conv1 = conv_block(in_planes, inter_planes, 1, args.block_type, |
| 23 | + args.use_gn, args.gn_groups, args.drop_type, args.drop_rate, |
| 24 | + track_stats=args.report_ratio) |
| 25 | + self.conv2 = conv_block(inter_planes, out_planes, 3, args.block_type, |
| 26 | + args.use_gn, args.gn_groups, args.drop_type, args.drop_rate, |
| 27 | + padding=1, track_stats=args.report_ratio) |
| 28 | + |
| 29 | + def forward(self, x): |
| 30 | + out = self.conv2(self.conv1(x)) |
| 31 | + return torch.cat([x, out], 1) |
| 32 | + |
| 33 | +class TransitionBlock(nn.Module): |
| 34 | + def __init__(self, in_planes, out_planes, args): |
| 35 | + super(TransitionBlock, self).__init__() |
| 36 | + self.conv = conv_block(in_planes, out_planes, 1, args.block_type, |
| 37 | + args.use_gn, args.gn_groups, args.drop_type, args.drop_rate, |
| 38 | + track_stats=args.report_ratio) |
| 39 | + |
| 40 | + def forward(self, x): |
| 41 | + out = self.conv(x) |
| 42 | + return F.avg_pool2d(out, 2) |
| 43 | + |
| 44 | +class DenseBlock(nn.Module): |
| 45 | + def __init__(self, num_layers, in_planes, growth_rate, block, args): |
| 46 | + super(DenseBlock, self).__init__() |
| 47 | + self.layer = nn.Sequential(*[block(in_planes+i*growth_rate, growth_rate, args) |
| 48 | + for i in range(num_layers)]) |
| 49 | + |
| 50 | + def forward(self, x): |
| 51 | + return self.layer(x) |
| 52 | + |
| 53 | +# For CIFAR-10/100 dataset |
| 54 | +class DenseNet(nn.Module): |
| 55 | + def __init__(self, args, growth_rate=12, |
| 56 | + reduction=0.5, bottleneck=True): |
| 57 | + super(DenseNet, self).__init__() |
| 58 | + in_planes = 2 * growth_rate |
| 59 | + n = int((args.depth - 4) / 3) |
| 60 | + if bottleneck == True: |
| 61 | + n = n//2 |
| 62 | + block = Bottleneck |
| 63 | + else: |
| 64 | + block = BasicBlock |
| 65 | + # 1st conv before any dense block |
| 66 | + self.conv1 = nn.Conv2d(3, in_planes, kernel_size=3, stride=1, |
| 67 | + padding=1, bias=False) |
| 68 | + # 1st block |
| 69 | + self.block1 = DenseBlock(n, in_planes, growth_rate, block, args) |
| 70 | + in_planes = int(in_planes+n*growth_rate) |
| 71 | + self.trans1 = TransitionBlock(in_planes, int(math.floor(in_planes*reduction)), args) |
| 72 | + in_planes = int(math.floor(in_planes*reduction)) |
| 73 | + # 2nd block |
| 74 | + self.block2 = DenseBlock(n, in_planes, growth_rate, block, args) |
| 75 | + in_planes = int(in_planes+n*growth_rate) |
| 76 | + self.trans2 = TransitionBlock(in_planes, int(math.floor(in_planes*reduction)), args) |
| 77 | + in_planes = int(math.floor(in_planes*reduction)) |
| 78 | + # 3rd block |
| 79 | + self.block3 = DenseBlock(n, in_planes, growth_rate, block, args) |
| 80 | + in_planes = int(in_planes+n*growth_rate) |
| 81 | + # global average pooling and classifier |
| 82 | + self.bn = nn.BatchNorm2d(in_planes) |
| 83 | + self.relu = nn.ReLU(inplace=True) |
| 84 | + self.fc = nn.Linear(in_planes, args.class_num) |
| 85 | + self.in_planes = in_planes |
| 86 | + |
| 87 | + def forward(self, x): |
| 88 | + out = self.conv1(x) |
| 89 | + out = self.trans1(self.block1(out)) |
| 90 | + out = self.trans2(self.block2(out)) |
| 91 | + out = self.block3(out) |
| 92 | + out = self.relu(self.bn(out)) |
| 93 | + out = F.avg_pool2d(out, 8) |
| 94 | + out = out.view(-1, self.in_planes) |
| 95 | + return self.fc(out) |
| 96 | + |
| 97 | + |
| 98 | +# https://github.com/liuzhuang13/DenseNet#results-on-cifar |
| 99 | +# CIFAR DenseNet3(depth=100, num_classes=10., growth_rate=12, reduction=0.5, bottleneck=True, drop_rate=0.2) |
| 100 | +# SVHN DenseNet3(depth=100, num_classes=10., growth_rate=24, reduction=0.5, bottleneck=True, drop_rate=0.2) |
| 101 | +# DenseNet3(depth=250, num_classes=10., growth_rate=24, reduction=0.5, bottleneck=True, drop_rate=0.2) |
| 102 | +# DenseNet3(depth=190, num_classes=10., growth_rate=40, reduction=0.5, bottleneck=True, drop_rate=0.2) |
| 103 | +def get_densenet(args): |
| 104 | + return DenseNet(args, args.arg1) |
| 105 | + |
| 106 | +if __name__ == '__main__': |
| 107 | + import argparse |
| 108 | + |
| 109 | + parser = argparse.ArgumentParser(description='WideResNet') |
| 110 | + args = parser.parse_args() |
| 111 | + args.depth = 100 |
| 112 | + args.class_num = 10 |
| 113 | + args.block_type = 0 |
| 114 | + args.use_gn = False |
| 115 | + args.gn_groups = 6 |
| 116 | + args.drop_type = 1 |
| 117 | + args.drop_rate = 0.1 |
| 118 | + args.report_ratio = True |
| 119 | + args.arg1 = 12 |
| 120 | + |
| 121 | + net = DenseNet(args, args.arg1) |
| 122 | + y = net(torch.randn(1, 3, 32, 32)) |
| 123 | + print(y.size()) |
| 124 | + print(net) |
| 125 | + print(sum([p.data.nelement() for p in net.parameters()])) |
| 126 | + |
| 127 | + from convBlock import Norm2d, norm2d_stats, norm2d_track_stats |
| 128 | + |
| 129 | + # norm2d_track_stats(net, False) |
| 130 | + mean, var = norm2d_stats(net) |
| 131 | + print(len(mean), mean) |
| 132 | + print(var) |
0 commit comments