-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathcDCGAN.py
87 lines (79 loc) · 2.69 KB
/
cDCGAN.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
"""
Author: Md Mijanur Rahman
"""
import torch
import torch.nn as nn
# conditional generator
class cGenerator(nn.Module):
def __init__(self, noise_dim:int, n_classes:int):
super(cGenerator, self).__init__()
self.noise_dim = noise_dim
self.n_classes = n_classes
self.embed = nn.Embedding(n_classes, n_classes)
self.linear = nn.Linear(noise_dim+n_classes, 128*7*7)
self.conv = nn.Sequential(
nn.ConvTranspose2d(128, 64, 4, 2, 1),
nn.BatchNorm2d(64),
nn.ReLU(inplace=True),
nn.ConvTranspose2d(64, 32, 4, 2, 1),
nn.BatchNorm2d(32),
nn.ReLU(inplace=True),
nn.ConvTranspose2d(32, 1, 3, 1, 1),
nn.Tanh()
)
def forward(self, x, labels):
embed = self.embed(labels)
x = torch.cat([x, embed], dim=1)
x = self.linear(x)
x = x.view(-1, 128, 7, 7)
x = self.conv(x)
return x
# conditional discriminator
class cDiscriminator(nn.Module):
def __init__(self, n_classes:int):
super(cDiscriminator, self).__init__()
self.n_classes = n_classes
self.embed = nn.Embedding(n_classes, n_classes)
self.conv = nn.Sequential(
nn.Conv2d(1, 32, 3, 1, 1),
nn.LeakyReLU(0.2, inplace=True),
nn.Conv2d(32, 64, 4, 2, 1),
nn.BatchNorm2d(64),
nn.LeakyReLU(0.2, inplace=True),
nn.Conv2d(64, 128, 4, 2, 1),
nn.BatchNorm2d(128),
nn.LeakyReLU(0.2, inplace=True)
)
self.linear = nn.Sequential(
nn.Flatten(),
nn.LazyLinear(out_features= 128),
nn.LeakyReLU(0.2, inplace=True),
nn.Linear(128, 1),
nn.Sigmoid()
)
def forward(self, x, labels):
x = x.view(-1, 28, 28)
labels = self.embed(labels)
labels = labels.unsqueeze(2).repeat(1, 1, 28)
x = torch.cat([x, labels], dim=1)
x = x.unsqueeze(1)
x = self.conv(x)
x = self.linear(x)
return x
if __name__ == "__main__":
device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
# test generator
noise_dim = 100
n_classes = 10
x = torch.randn(32, noise_dim).to(device)
labels = torch.randint(0, n_classes, (32,)).to(device)
cgenerator = cGenerator(noise_dim, n_classes).to(device)
x_hat = cgenerator(x, labels)
print(x_hat.shape)
# test discriminator
n_classes = 10
x = torch.randn(32, 1, 28, 28).to(device)
labels = torch.randint(0, n_classes, (32,)).to(device)
cdiscriminator = cDiscriminator(n_classes).to(device)
pred_labels = cdiscriminator(x, labels)
print(pred_labels.shape)