-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathinfo_GAN_model_MNIST.py
86 lines (56 loc) · 2.16 KB
/
info_GAN_model_MNIST.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
import torch
import torch.nn as nn
import torch.nn.functional as F
"""
Architecture based on InfoGAN paper.
"""
class Generator(nn.Module):
def __init__(self):
super().__init__()
self.tconv1 = nn.ConvTranspose2d(74, 1024, 1, 1, bias=False)
self.bn1 = nn.BatchNorm2d(1024)
self.tconv2 = nn.ConvTranspose2d(1024, 128, 7, 1, bias=False)
self.bn2 = nn.BatchNorm2d(128)
self.tconv3 = nn.ConvTranspose2d(128, 64, 4, 2, padding=1, bias=False)
self.bn3 = nn.BatchNorm2d(64)
self.tconv4 = nn.ConvTranspose2d(64, 1, 4, 2, padding=1, bias=False)
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)))
img = torch.sigmoid(self.tconv4(x))
return img
class Discriminator(nn.Module):
def __init__(self):
super().__init__()
self.conv1 = nn.Conv2d(1, 64, 4, 2, 1)
self.conv2 = nn.Conv2d(64, 128, 4, 2, 1, bias=False)
self.bn2 = nn.BatchNorm2d(128)
self.conv3 = nn.Conv2d(128, 1024, 7, bias=False)
self.bn3 = nn.BatchNorm2d(1024)
def forward(self, x):
x = F.leaky_relu(self.conv1(x), 0.1, inplace=True)
x = F.leaky_relu(self.bn2(self.conv2(x)), 0.1, inplace=True)
x = F.leaky_relu(self.bn3(self.conv3(x)), 0.1, inplace=True)
return x
class DHead(nn.Module):
def __init__(self):
super().__init__()
self.conv = nn.Conv2d(1024, 1, 1)
def forward(self, x):
output = torch.sigmoid(self.conv(x))
return output
class QHead(nn.Module):
def __init__(self):
super().__init__()
self.conv1 = nn.Conv2d(1024, 128, 1, bias=False)
self.bn1 = nn.BatchNorm2d(128)
self.conv_disc = nn.Conv2d(128, 10, 1)
self.conv_mu = nn.Conv2d(128, 2, 1)
self.conv_var = nn.Conv2d(128, 2, 1)
def forward(self, x):
x = F.leaky_relu(self.bn1(self.conv1(x)), 0.1, inplace=True)
disc_logits = self.conv_disc(x).squeeze()
mu = self.conv_mu(x).squeeze()
var = torch.exp(self.conv_var(x).squeeze())
return disc_logits, mu, var