|
| 1 | +""" |
| 2 | +------------------------------------------------- |
| 3 | + File Name: blocks.py |
| 4 | + Author: Zhonghao Huang |
| 5 | + Date: 2019/9/10 |
| 6 | + Description: |
| 7 | +------------------------------------------------- |
| 8 | +""" |
| 9 | + |
| 10 | +import torch.nn as nn |
| 11 | + |
| 12 | + |
| 13 | +def weights_init_kaiming(m): |
| 14 | + classname = m.__class__.__name__ |
| 15 | + if classname.find('Linear') != -1: |
| 16 | + nn.init.kaiming_normal_(m.weight, a=0, mode='fan_out') |
| 17 | + nn.init.constant_(m.bias, 0.0) |
| 18 | + elif classname.find('Conv') != -1: |
| 19 | + nn.init.kaiming_normal_(m.weight, a=0, mode='fan_in') |
| 20 | + if m.bias is not None: |
| 21 | + nn.init.constant_(m.bias, 0.0) |
| 22 | + elif classname.find('BatchNorm') != -1: |
| 23 | + if m.affine: |
| 24 | + nn.init.constant_(m.weight, 1.0) |
| 25 | + nn.init.constant_(m.bias, 0.0) |
| 26 | + |
| 27 | + |
| 28 | +def weights_init_classifier(m): |
| 29 | + classname = m.__class__.__name__ |
| 30 | + if classname.find('Linear') != -1: |
| 31 | + nn.init.normal_(m.weight, std=0.001) |
| 32 | + if m.bias is not None: |
| 33 | + nn.init.constant_(m.bias, 0.0) |
| 34 | + |
| 35 | + |
| 36 | +# Defines the new fc layer and classification layer |
| 37 | +# |--Linear--|--bn--|--relu--|--Linear--| |
| 38 | +class ClassBlock(nn.Module): |
| 39 | + def __init__(self, input_dim, class_num, linear=True, num_bottleneck=512, |
| 40 | + bn=True, relu=False, drop=0.5, return_feat=False): |
| 41 | + super(ClassBlock, self).__init__() |
| 42 | + self.return_feat = return_feat |
| 43 | + add_block = [] |
| 44 | + if linear: |
| 45 | + add_block += [nn.Linear(input_dim, num_bottleneck)] |
| 46 | + else: |
| 47 | + num_bottleneck = input_dim |
| 48 | + if bn: |
| 49 | + add_block += [nn.BatchNorm1d(num_bottleneck)] |
| 50 | + if relu: |
| 51 | + add_block += [nn.LeakyReLU(0.1)] |
| 52 | + if drop > 0: |
| 53 | + add_block += [nn.Dropout(p=drop)] |
| 54 | + add_block = nn.Sequential(*add_block) |
| 55 | + add_block.apply(weights_init_kaiming) |
| 56 | + |
| 57 | + fc = nn.Linear(num_bottleneck, class_num) |
| 58 | + fc.apply(weights_init_classifier) |
| 59 | + |
| 60 | + self.add_block = add_block |
| 61 | + self.fc = fc |
| 62 | + |
| 63 | + def forward(self, x): |
| 64 | + x = self.add_block(x) |
| 65 | + if self.return_feat: |
| 66 | + feat = x |
| 67 | + cls = self.fc(x) |
| 68 | + return cls, feat |
| 69 | + else: |
| 70 | + cls = self.fc(x) |
| 71 | + return cls |
| 72 | + |
| 73 | + |
| 74 | +class BNNeck(nn.Module): |
| 75 | + def __init__(self, in_planes, num_classes): |
| 76 | + super(BNNeck, self).__init__() |
| 77 | + self.in_planes = in_planes |
| 78 | + self.num_classes = num_classes |
| 79 | + self.bn = nn.BatchNorm1d(self.in_planes) |
| 80 | + self.bn.bias.requires_grad_(False) # no shift |
| 81 | + self.classifier = nn.Linear(self.in_planes, self.num_classes) |
| 82 | + |
| 83 | + self.bn.apply(weights_init_kaiming) |
| 84 | + self.classifier.apply(weights_init_classifier) |
| 85 | + |
| 86 | + def forward(self, x): |
| 87 | + feat = self.bn(x) |
| 88 | + cls = self.classifier(feat) |
| 89 | + |
| 90 | + if self.training: |
| 91 | + return cls, x |
| 92 | + else: |
| 93 | + return cls, feat |
0 commit comments