-
Notifications
You must be signed in to change notification settings - Fork 25
/
gan.py
84 lines (76 loc) · 2.76 KB
/
gan.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
from torch import nn
from torch.nn import functional as F
class Critic(nn.Module):
def __init__(self, image_size, image_channel_size, channel_size):
# configurations
super().__init__()
self.image_size = image_size
self.image_channel_size = image_channel_size
self.channel_size = channel_size
# layers
self.conv1 = nn.Conv2d(
image_channel_size, channel_size,
kernel_size=4, stride=2, padding=1
)
self.conv2 = nn.Conv2d(
channel_size, channel_size*2,
kernel_size=4, stride=2, padding=1
)
self.conv3 = nn.Conv2d(
channel_size*2, channel_size*4,
kernel_size=4, stride=2, padding=1
)
self.conv4 = nn.Conv2d(
channel_size*4, channel_size*8,
kernel_size=4, stride=1, padding=1,
)
self.fc = nn.Linear((image_size//8)**2 * channel_size*4, 1)
def forward(self, x):
x = F.leaky_relu(self.conv1(x))
x = F.leaky_relu(self.conv2(x))
x = F.leaky_relu(self.conv3(x))
x = F.leaky_relu(self.conv4(x))
x = x.view(-1, (self.image_size//8)**2 * self.channel_size*4)
return self.fc(x)
class Generator(nn.Module):
def __init__(self, z_size, image_size, image_channel_size, channel_size):
# configurations
super().__init__()
self.z_size = z_size
self.image_size = image_size
self.image_channel_size = image_channel_size
self.channel_size = channel_size
# layers
self.fc = nn.Linear(z_size, (image_size//8)**2 * channel_size*8)
self.bn0 = nn.BatchNorm2d(channel_size*8)
self.bn1 = nn.BatchNorm2d(channel_size*4)
self.deconv1 = nn.ConvTranspose2d(
channel_size*8, channel_size*4,
kernel_size=4, stride=2, padding=1
)
self.bn2 = nn.BatchNorm2d(channel_size*2)
self.deconv2 = nn.ConvTranspose2d(
channel_size*4, channel_size*2,
kernel_size=4, stride=2, padding=1,
)
self.bn3 = nn.BatchNorm2d(channel_size)
self.deconv3 = nn.ConvTranspose2d(
channel_size*2, channel_size,
kernel_size=4, stride=2, padding=1
)
self.deconv4 = nn.ConvTranspose2d(
channel_size, image_channel_size,
kernel_size=3, stride=1, padding=1
)
def forward(self, z):
g = F.relu(self.bn0(self.fc(z).view(
z.size(0),
self.channel_size*8,
self.image_size//8,
self.image_size//8,
)))
g = F.relu(self.bn1(self.deconv1(g)))
g = F.relu(self.bn2(self.deconv2(g)))
g = F.relu(self.bn3(self.deconv3(g)))
g = self.deconv4(g)
return F.sigmoid(g)