-
Notifications
You must be signed in to change notification settings - Fork 0
/
models.py
65 lines (51 loc) · 1.68 KB
/
models.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
import torch
from torch import nn as nn
class OutputBlock(nn.Module):
def __init__(self, num_classes, in_channels=512):
super().__init__()
self.avgpool = nn.AdaptiveAvgPool2d((7, 7))
self.classifier = nn.Sequential(
nn.Linear(in_channels * 7 * 7, num_classes),
nn.ReLU(True),
nn.Dropout(),
nn.Linear(4096, 4096),
nn.Dropout(),
nn.ReLU(True),
nn.Linear(4096, num_classes),
)
def forward(self, x):
x = self.avgpool(x)
x = torch.flatten(x, start_dim=1)
x = self.classifier(x)
return x
class Vectorizer(nn.Module):
def __init__(self, num_classes, input_shape):
super().__init__()
in_dim = 1
for i in input_shape:
in_dim = in_dim * i
self.classifier = nn.Linear(in_dim, num_classes)
#nn.init.constant_(self.classifier.weight, 1)
nn.init.constant_(self.classifier.weight, 0)
nn.init.constant_(self.classifier.bias, 0)
def forward(self, x):
x = torch.flatten(x, start_dim=1)
x = self.classifier(x)
return x
class Classifier(nn.Module):
def __init__(self, encoder, shape, num_classes):
super().__init__()
self.encoder = encoder
self.output_block = Vectorizer(num_classes, shape)
def forward(self, x):
h = self.encoder(x)
return self.output_block(h)
class AutoEncoder(nn.Module):
def __init__(self, encoder, decoder):
super().__init__()
self.encoder = encoder
self.decoder = decoder
def forward(self, x):
z = self.encoder(x)
x = self.decoder(z)
return z, x