-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathmodel.py
167 lines (109 loc) · 3.77 KB
/
model.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
import torch.nn as nn
import torch.nn.functional as F
import torch
def get_net(name):
if name == 'FashionMNIST':
return Net1_fea, Net1_clf, Net1_dis
elif name == 'SVHN':
return VGG_10_fea, VGG_10_clf, VGG_10_dis
elif name == 'CIFAR10':
return VGG_10_fea, VGG_10_clf, VGG_10_dis
# net_1 for Mnist and Fashion_mnist
class Net1_fea(nn.Module):
"""
Feature extractor network
"""
def __init__(self):
super(Net1_fea, self).__init__()
self.conv1 = nn.Conv2d(1, 10, kernel_size=5)
self.conv2 = nn.Conv2d(10, 20, kernel_size=5)
self.conv2_drop = nn.Dropout2d()
def forward(self,x):
x = F.relu(F.max_pool2d(self.conv1(x), 2))
x = F.relu(F.max_pool2d(self.conv2_drop(self.conv2(x)), 2))
x = x.view(-1, 320)
return x
class Net1_clf(nn.Module):
"""
Classifier network, also give the latent space and embedding dimension
"""
def __init__(self):
super(Net1_clf,self).__init__()
self.fc1 = nn.Linear(320, 50)
self.fc2 = nn.Linear(50, 10)
def forward(self,x):
e1 = F.relu(self.fc1(x))
x = F.dropout(e1, training=self.training)
x = self.fc2(x)
return x, e1
def get_embedding_dim(self):
return 50
class Net1_dis(nn.Module):
"""
Discriminator network, output with [0,1] (sigmoid function)
"""
def __init__(self):
super(Net1_dis,self).__init__()
self.fc1 = nn.Linear(320, 50)
self.fc2 = nn.Linear(50, 1)
def forward(self,x):
e1 = F.relu(self.fc1(x))
x = F.dropout(e1, training=self.training)
x = self.fc2(x)
x = torch.sigmoid(x)
return x
# VGG_three parts
cfg = {
'VGG11': [64, 'M', 128, 'M', 256, 256, 'M', 512, 512, 'M', 512, 512, 'M'],
'VGG13': [64, 64, 'M', 128, 128, 'M', 256, 256, 'M', 512, 512, 'M', 512, 512, 'M'],
'VGG16': [64, 64, 'M', 128, 128, 'M', 256, 256, 256, 'M', 512, 512, 512, 'M', 512, 512, 512, 'M'],
'VGG19': [64, 64, 'M', 128, 128, 'M', 256, 256, 256, 256, 'M', 512, 512, 512, 512, 'M', 512, 512, 512, 512, 'M'],
}
## VGG for CIFAR 10/SVHN (since they are 32 * 32)
class VGG_10_fea(nn.Module):
def __init__(self):
super(VGG_10_fea, self).__init__()
# the vgg model can be changed to vgg11/vgg16
# vgg 11 for svhn
# vgg 16 for cifar 10 and cifar 100
self.features = self._make_layers(cfg['VGG16'])
def forward(self, x):
out = self.features(x)
out = out.view(out.size(0), -1)
return out
def _make_layers(self, cfg):
layers = []
in_channels = 3
for x in cfg:
if x == 'M':
layers += [nn.MaxPool2d(kernel_size=2, stride=2)]
else:
layers += [nn.Conv2d(in_channels, x, kernel_size=3, padding=1),
nn.BatchNorm2d(x),
nn.ReLU(inplace=True)]
in_channels = x
layers += [nn.AvgPool2d(kernel_size=1, stride=1)]
return nn.Sequential(*layers)
class VGG_10_clf(nn.Module):
def __init__(self):
super(VGG_10_clf, self).__init__()
self.fc1 = nn.Linear(512,50)
self.fc2 = nn.Linear(50,10)
def forward(self,x):
e1 = F.relu(self.fc1(x))
x = F.dropout(e1, training=self.training)
x = self.fc2(x)
return x, e1
def get_embedding_dim(self):
return 50
class VGG_10_dis(nn.Module):
def __init__(self):
super(VGG_10_dis,self).__init__()
self.fc1 = nn.Linear(512, 50)
self.fc2 = nn.Linear(50, 1)
def forward(self,x):
e1 = F.relu(self.fc1(x))
x = F.dropout(e1, training=self.training)
x = self.fc2(x)
x = torch.sigmoid(x)
return x