-
Notifications
You must be signed in to change notification settings - Fork 8
/
architecture.py
166 lines (151 loc) · 6 KB
/
architecture.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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
import models.cifar as models
import torch
import torch.nn as nn
# A model predicts category labels.
class CategoryModel(nn.Module):
def __init__(self, model_name, num_classes):
super(CategoryModel, self).__init__()
# CNN
if model_name == 'vgg19':
cnn = models.__dict__['vgg19_bn'](num_classes=num_classes)
self.features = nn.Sequential(*list(cnn.children())[:-1])
self.fc = nn.Linear(cnn.classifier.in_features, num_classes)
elif model_name == 'resnet110':
cnn = models.__dict__['resnet'](
num_classes=num_classes,
depth=110,
block_name='BasicBlock')
self.features = nn.Sequential(*list(cnn.children())[:-1])
self.fc = nn.Linear(cnn.fc.in_features, num_classes)
elif model_name == 'resnet32':
cnn = models.__dict__['resnet'](
num_classes=num_classes,
depth=32,
block_name='BasicBlock')
self.features = nn.Sequential(*list(cnn.children())[:-1])
self.fc = nn.Linear(cnn.fc.in_features, num_classes)
def forward(self, data):
x = self.features(data)
x = x.view(x.size(0), -1)
prediction = self.fc(x)
return prediction
# A model predicts high-dimensional labels.
class HighDimensionalModel(nn.Module):
def __init__(self, model_name, num_classes):
super(HighDimensionalModel, self).__init__()
# CNN
if model_name == 'vgg19':
cnn = models.__dict__['vgg19_bn'](num_classes=num_classes)
self.features = nn.Sequential(*list(cnn.children())[:-1])
self.fc = nn.Sequential(
nn.BatchNorm1d(cnn.classifier.in_features),
nn.LeakyReLU(),
nn.Linear(cnn.classifier.in_features, 64))
elif model_name == 'resnet110':
cnn = models.__dict__['resnet'](
num_classes=num_classes,
depth=110,
block_name='BasicBlock')
self.features = nn.Sequential(*list(cnn.children())[:-1])
self.fc = nn.Sequential(
nn.BatchNorm1d(cnn.fc.in_features),
nn.LeakyReLU(),
nn.Linear(cnn.fc.in_features, 64))
elif model_name == 'resnet32':
cnn = models.__dict__['resnet'](
num_classes=num_classes,
depth=32,
block_name='BasicBlock')
self.features = nn.Sequential(*list(cnn.children())[:-1])
self.fc = nn.Sequential(
nn.BatchNorm1d(cnn.fc.in_features),
nn.LeakyReLU(),
nn.Linear(cnn.fc.in_features, 64))
self.deconvs = nn.Sequential(
nn.ConvTranspose2d(64, 64, 4, 1, 0),
nn.BatchNorm2d(64),
nn.LeakyReLU(),
# state size. 64 x 4 x 4
nn.ConvTranspose2d(64, 32, 4, 2, 1),
nn.BatchNorm2d(32),
nn.LeakyReLU(),
# state size. 32 x 8 x 8
nn.ConvTranspose2d(32, 16, 4, 2, 1),
nn.BatchNorm2d(16),
nn.LeakyReLU(),
# state size. 16 x 16 x 16
nn.ConvTranspose2d(16, 8, 4, 2, 1),
nn.BatchNorm2d(8),
nn.LeakyReLU(),
# state size. 8 x 32 x 32
nn.ConvTranspose2d(8, 1, 4, 2, 1)
)
def forward(self, data):
x = self.features(data)
x = x.view(x.size(0), -1)
x = self.fc(x)
x = torch.unsqueeze(x, -1)
x = torch.unsqueeze(x, -1)
x = self.deconvs(x)
prediction = x.view(-1, 64, 64)
return prediction
# A model predicts high-dimensional labels.
class BERTHighDimensionalModel(nn.Module):
def __init__(self, model_name, num_classes):
super(BERTHighDimensionalModel, self).__init__()
# CNN
if model_name == 'vgg19':
cnn = models.__dict__['vgg19_bn'](num_classes=num_classes)
self.features = nn.Sequential(*list(cnn.children())[:-1])
self.fc = nn.Sequential(
nn.BatchNorm1d(cnn.classifier.in_features),
nn.LeakyReLU(),
nn.Linear(cnn.classifier.in_features, 64))
elif model_name == 'resnet110':
cnn = models.__dict__['resnet'](
num_classes=num_classes,
depth=110,
block_name='BasicBlock')
self.features = nn.Sequential(*list(cnn.children())[:-1])
self.fc = nn.Sequential(
nn.BatchNorm1d(cnn.fc.in_features),
nn.LeakyReLU(),
nn.Linear(cnn.fc.in_features, 64))
elif model_name == 'resnet32':
cnn = models.__dict__['resnet'](
num_classes=num_classes,
depth=32,
block_name='BasicBlock')
self.features = nn.Sequential(*list(cnn.children())[:-1])
self.fc = nn.Sequential(
nn.BatchNorm1d(cnn.fc.in_features),
nn.LeakyReLU(),
nn.Linear(cnn.fc.in_features, 64))
self.deconvs = nn.Sequential(
nn.ConvTranspose2d(64, 64, 3, 1, 0),
nn.BatchNorm2d(64),
nn.LeakyReLU(),
# state size. 64 x 3 x 3
nn.ConvTranspose2d(64, 32, 4, 2, 1),
nn.BatchNorm2d(32),
nn.LeakyReLU(),
# state size. 32 x 6 x 6
nn.ConvTranspose2d(32, 16, 4, 2, 1),
nn.BatchNorm2d(16),
nn.LeakyReLU(),
# state size. 16 x 12 x 12
nn.ConvTranspose2d(16, 8, 4, 2, 1),
nn.BatchNorm2d(8),
nn.LeakyReLU(),
# state size. 8 x 24 x 24
nn.ConvTranspose2d(8, 1, 4, 2, 1),
)
def forward(self, data):
x = self.features(data)
x = x.view(x.size(0), -1)
x = self.fc(x)
x = torch.unsqueeze(x, -1)
x = torch.unsqueeze(x, -1)
x = self.deconvs(x)
prediction = x.view(-1, 48, 48)
return prediction