VGG model
| 14 | |
| 15 | |
| 16 | class VGG(nn.Module): |
| 17 | ''' |
| 18 | VGG model |
| 19 | ''' |
| 20 | |
| 21 | def __init__(self, cfg, num_classes=10, batch_norm=True, in_c=3, **lif_parameters): |
| 22 | super(VGG, self).__init__() |
| 23 | |
| 24 | self.features, out_c = make_layers(cfg, batch_norm, in_c, **lif_parameters) |
| 25 | self.avgpool = SeqToANNContainer(nn.AdaptiveAvgPool2d((1, 1))) |
| 26 | self.classifier = nn.Sequential( |
| 27 | SeqToANNContainer(nn.Linear(out_c, num_classes)), |
| 28 | ) |
| 29 | for m in self.modules(): |
| 30 | if isinstance(m, nn.Conv2d): |
| 31 | n = m.kernel_size[0] * m.kernel_size[1] * m.out_channels |
| 32 | m.weight.data.normal_(0, math.sqrt(2. / n)) |
| 33 | m.bias.data.zero_() |
| 34 | |
| 35 | self.add_dim = lambda x: add_dimention(x, self.T) |
| 36 | |
| 37 | def forward(self, x): |
| 38 | x = self.add_dim(x) if len(x.shape) == 4 else x |
| 39 | x = self.features(x) |
| 40 | x = self.avgpool(x) |
| 41 | x = torch.flatten(x, 1) if len(x.shape) == 4 else torch.flatten(x, 2) |
| 42 | x = self.classifier(x) |
| 43 | return x |
| 44 | |
| 45 | |
| 46 | def make_layers(cfg, batch_norm=False, in_c=3, **lif_parameters): |