| 36 | |
| 37 | |
| 38 | class VGG16(nn.Module): |
| 39 | def __init__(self, n_classes): |
| 40 | super(VGG16, self).__init__() |
| 41 | model = torchvision.models.vgg16_bn(pretrained=True) |
| 42 | self.feature = model.features |
| 43 | self.feat_dim = 512 * 2 * 2 |
| 44 | self.n_classes = n_classes |
| 45 | self.bn = nn.BatchNorm1d(self.feat_dim) |
| 46 | self.bn.bias.requires_grad_(False) # no shift |
| 47 | self.fc_layer = nn.Linear(self.feat_dim, self.n_classes) |
| 48 | |
| 49 | def forward(self, x): |
| 50 | feature = self.feature(x) |
| 51 | feature = feature.view(feature.size(0), -1) |
| 52 | feature = self.bn(feature) |
| 53 | res = self.fc_layer(feature) |
| 54 | |
| 55 | return [feature, res] |
| 56 | |
| 57 | def predict(self, x): |
| 58 | feature = self.feature(x) |
| 59 | feature = feature.view(feature.size(0), -1) |
| 60 | feature = self.bn(feature) |
| 61 | res = self.fc_layer(feature) |
| 62 | out = F.softmax(res, dim=1) |
| 63 | |
| 64 | return out |
| 65 | |
| 66 | |
| 67 | class VGG16_vib(nn.Module): |
no outgoing calls
no test coverage detected