-
Notifications
You must be signed in to change notification settings - Fork 0
/
module.py
251 lines (215 loc) · 8.02 KB
/
module.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
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
import torch
import torch.nn as nn
device = 'cuda' if torch.cuda.is_available() else 'cpu'
device = 'cpu'
######################################
# Generators #
######################################
class Generator(nn.Module):
def __init__(self,num_classes):
super(Generator,self).__init__()
self.embed = nn.Embedding(num_classes, 100)
self.dense = nn.Linear(100, 7*7*256)
self.layer1 = nn.Sequential(
nn.ConvTranspose2d(256, 128, 3, stride=2, padding=1, output_padding=1),
nn.BatchNorm2d(128),
nn.LeakyReLU())
self.layer2 = nn.Sequential(
nn.ConvTranspose2d(128, 64, 3, stride=1, padding=1),
nn.BatchNorm2d(64),
nn.LeakyReLU())
self.layer3 = nn.Sequential(
nn.ConvTranspose2d(64, 1, 3, stride=2, padding=1, output_padding=1),
nn.Tanh())
def forward(self,label):
embedded_label = self.embed(label)
z = torch.randn(len(label),100).to(device)
x = embedded_label * z
x = self.dense(x).view(-1, 256, 7, 7)
x = self.layer1(x)
x = self.layer2(x)
x = self.layer3(x)
return x
class Generator_num(nn.Module):
def __init__(self):
super(Generator_num, self).__init__()
self.linear = nn.Sequential(
nn.Linear(100, 256 * 7 * 7)
#nn.BatchNorm1d(256 * 7 * 7)
)
self.layer1 = nn.Sequential(
nn.ConvTranspose2d(256, 128, 3, stride=2, padding=1, output_padding=1),
nn.BatchNorm2d(128),
nn.LeakyReLU())
self.layer2 = nn.Sequential(
nn.ConvTranspose2d(128, 64, 3, stride=1, padding=1),
nn.BatchNorm2d(64),
nn.LeakyReLU())
self.layer3 = nn.Sequential(
nn.ConvTranspose2d(64, 1, 3, stride=2, padding=1, output_padding=1),
nn.Tanh())
def forward(self,batch_size):
x = torch.randn(batch_size,100).to(device)
x = self.linear(x)
x = x.view(-1, 256, 7, 7)
x = self.layer1(x)
x = self.layer2(x)
x = self.layer3(x)
return x
class Generator_acgan(nn.Module):
def __init__(self, num_classes):
super().__init__()
# Embedding which outputs a vector of dimension z_dim
self.embed = nn.Embedding(num_classes, 100)
# Linear combination of the latent vector z
self.dense = nn.Linear(100, 7 * 7 * 256)
# The transposed convolutional layers are wrapped in nn.Sequential
self.trans1 = nn.Sequential(
nn.ConvTranspose2d(256, 128, 3, stride=2, padding=1, output_padding=1),
nn.BatchNorm2d(128),
nn.LeakyReLU())
self.trans2 = nn.Sequential(
nn.ConvTranspose2d(128, 64, 3, stride=1, padding=1),
nn.BatchNorm2d(64),
nn.LeakyReLU())
self.trans3 = nn.Sequential(
nn.ConvTranspose2d(64, 1, 3, stride=2, padding=1, output_padding=1),
nn.Tanh())
def forward(self, label):
# Apply embedding to the input label
embedded_label = self.embed(label)
z = torch.randn(len(label), 100).to(device)
# Element wise multiplication of latent vector and embedding
x = embedded_label * z
# Application of dense layer and transforming to 3d shape
x = self.dense(x).view(-1, 256, 7, 7)
x = self.trans1(x)
x = self.trans2(x)
x = self.trans3(x)
return x
######################################
# Discriminators #
######################################
class Discriminator(nn.Module):
def __init__(self,num_classes):
super(Discriminator,self).__init__()
self.embed = nn.Embedding(num_classes,28*28)
self.layer1 = nn.Sequential(
nn.Conv2d(2, 64, 3, stride=2, padding=1),
nn.LeakyReLU())
self.layer2 = nn.Sequential(
nn.Conv2d(64, 64, 3, stride=2, padding=1),
nn.BatchNorm2d(64),
nn.LeakyReLU())
self.layer3 = nn.Sequential(
nn.Conv2d(64, 128, 3, stride=2, padding=0),
nn.BatchNorm2d(128),
nn.LeakyReLU())
self.dense = nn.Sequential(
nn.Flatten(),
nn.Linear(3 * 3 * 128, 1),
nn.Sigmoid())
def forward(self, x, label):
embedded_label = self.embed(label).view_as(x)
x = torch.cat([x, embedded_label], dim=1)
x = self.layer1(x)
x = self.layer2(x)
x = self.layer3(x)
x = self.dense(x)
return x
class Discriminator_num(nn.Module):
def __init__(self):
super(Discriminator_num,self).__init__()
self.weight = nn.Parameter(torch.randn(1,1,28,28))
self.layer1 = nn.Sequential(
nn.Conv2d(2, 64, 3, stride=2, padding=1),
nn.LeakyReLU())
self.layer2 = nn.Sequential(
nn.Conv2d(64, 64, 3, stride=2, padding=1),
nn.BatchNorm2d(64),
nn.LeakyReLU())
self.layer3 = nn.Sequential(
nn.Conv2d(64, 128, 3, stride=2, padding=0),
nn.BatchNorm2d(128),
nn.LeakyReLU())
self.dense = nn.Sequential(
nn.Flatten(),
nn.Linear(3 * 3 * 128, 1),
nn.Sigmoid())
def forward(self,x):
x = torch.cat([x,self.weight.repeat(x.shape[0],1,1,1).to(device)], dim=1)
x = self.layer1(x)
x = self.layer2(x)
x = self.layer3(x)
x = self.dense(x)
return x
class Discriminator_acgan(nn.Module):
def __init__(self, num_classes):
super().__init__()
# Embedding which outputs a vector of img_size
self.embed = nn.Embedding(num_classes, 28 * 28)
# It convenient to group conv layers with nn.Sequential
self.conv1 = nn.Sequential(
nn.Conv2d(2, 64, 3, stride=2, padding=1),
nn.LeakyReLU())
self.conv_1 = nn.Sequential(
nn.Conv2d(1, 64, 3, stride=2, padding=1),
nn.LeakyReLU())
self.conv2 = nn.Sequential(
nn.Conv2d(64, 64, 3, stride=2, padding=1),
nn.BatchNorm2d(64),
nn.LeakyReLU())
self.conv3 = nn.Sequential(
nn.Conv2d(64, 128, 3, stride=2, padding=0),
nn.BatchNorm2d(128),
nn.LeakyReLU())
# The 3D feature map is flattened to perform a linear combination
self.dense_1 = nn.Sequential(
nn.Flatten(),
nn.Linear(3 * 3 * 128, 1),
nn.Sigmoid())
self.dense_2 = nn.Sequential(
nn.Flatten(),
nn.Linear(3 * 3 * 128, 20),
nn.Softmax())
def forward(self, x, label):
# Apply embedding and convert to same shape as x
embedded_label = self.embed(label).view_as(x)
x_1 = self.conv_1(x)
x_1 = self.conv2(x_1)
x_1 = self.conv3(x_1)
x_2 = self.dense_2(x_1)
# Concatenation of x and embedded label
x = torch.cat([x, embedded_label], dim=1)
x = self.conv1(x)
x = self.conv2(x)
x = self.conv3(x)
x_1 = self.dense_1(x)
# x_2 = self.dense_2(x)
return x_1, x_2
######################################
# Classifiers #
######################################
class Classifier(nn.Module):
def __init__(self,num_class):
super(Classifier,self).__init__()
self.layer1 = nn.Sequential(
nn.Conv2d(1, 64, 3, stride=2, padding=1),
nn.LeakyReLU())
self.layer2 = nn.Sequential(
nn.Conv2d(64, 64, 3, stride=2, padding=1),
nn.BatchNorm2d(64),
nn.LeakyReLU())
self.layer3 = nn.Sequential(
nn.Conv2d(64, 128, 3, stride=2, padding=0),
nn.BatchNorm2d(128),
nn.LeakyReLU())
self.dense = nn.Sequential(
nn.Flatten(),
nn.Linear(3 * 3 * 128, num_class))
def forward(self,x):
x = self.layer1(x)
x = self.layer2(x)
x = self.layer3(x)
x = self.dense(x)
return x