-
Notifications
You must be signed in to change notification settings - Fork 32
/
Copy pathdcgan.py
92 lines (72 loc) · 3.01 KB
/
dcgan.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
import torch
import torch.nn as nn
import torch.nn.functional as F
def weights_init(w):
"""
Initializes the weights of the layer, w.
"""
classname = w.__class__.__name__
if classname.find('conv') != -1:
nn.init.normal_(w.weight.data, 0.0, 0.02)
elif classname.find('bn') != -1:
nn.init.normal_(w.weight.data, 1.0, 0.02)
nn.init.constant_(w.bias.data, 0)
# Define the Generator Network
class Generator(nn.Module):
def __init__(self, params):
super().__init__()
# Input is the latent vector Z.
self.tconv1 = nn.ConvTranspose2d(params['nz'], params['ngf']*8,
kernel_size=4, stride=1, padding=0, bias=False)
self.bn1 = nn.BatchNorm2d(params['ngf']*8)
# Input Dimension: (ngf*8) x 4 x 4
self.tconv2 = nn.ConvTranspose2d(params['ngf']*8, params['ngf']*4,
4, 2, 1, bias=False)
self.bn2 = nn.BatchNorm2d(params['ngf']*4)
# Input Dimension: (ngf*4) x 8 x 8
self.tconv3 = nn.ConvTranspose2d(params['ngf']*4, params['ngf']*2,
4, 2, 1, bias=False)
self.bn3 = nn.BatchNorm2d(params['ngf']*2)
# Input Dimension: (ngf*2) x 16 x 16
self.tconv4 = nn.ConvTranspose2d(params['ngf']*2, params['ngf'],
4, 2, 1, bias=False)
self.bn4 = nn.BatchNorm2d(params['ngf'])
# Input Dimension: (ngf) * 32 * 32
self.tconv5 = nn.ConvTranspose2d(params['ngf'], params['nc'],
4, 2, 1, bias=False)
#Output Dimension: (nc) x 64 x 64
def forward(self, x):
x = F.relu(self.bn1(self.tconv1(x)))
x = F.relu(self.bn2(self.tconv2(x)))
x = F.relu(self.bn3(self.tconv3(x)))
x = F.relu(self.bn4(self.tconv4(x)))
x = F.tanh(self.tconv5(x))
return x
# Define the Discriminator Network
class Discriminator(nn.Module):
def __init__(self, params):
super().__init__()
# Input Dimension: (nc) x 64 x 64
self.conv1 = nn.Conv2d(params['nc'], params['ndf'],
4, 2, 1, bias=False)
# Input Dimension: (ndf) x 32 x 32
self.conv2 = nn.Conv2d(params['ndf'], params['ndf']*2,
4, 2, 1, bias=False)
self.bn2 = nn.BatchNorm2d(params['ndf']*2)
# Input Dimension: (ndf*2) x 16 x 16
self.conv3 = nn.Conv2d(params['ndf']*2, params['ndf']*4,
4, 2, 1, bias=False)
self.bn3 = nn.BatchNorm2d(params['ndf']*4)
# Input Dimension: (ndf*4) x 8 x 8
self.conv4 = nn.Conv2d(params['ndf']*4, params['ndf']*8,
4, 2, 1, bias=False)
self.bn4 = nn.BatchNorm2d(params['ndf']*8)
# Input Dimension: (ndf*8) x 4 x 4
self.conv5 = nn.Conv2d(params['ndf']*8, 1, 4, 1, 0, bias=False)
def forward(self, x):
x = F.leaky_relu(self.conv1(x), 0.2, True)
x = F.leaky_relu(self.bn2(self.conv2(x)), 0.2, True)
x = F.leaky_relu(self.bn3(self.conv3(x)), 0.2, True)
x = F.leaky_relu(self.bn4(self.conv4(x)), 0.2, True)
x = F.sigmoid(self.conv5(x))
return x