forked from pclubiitk/model-zoo
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmodel.py
131 lines (101 loc) · 3.18 KB
/
model.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
import torch
import torch.nn as nn
import torch.nn.functional as F
"""
Architecture as mentioned in InfoGAN paper for MNIST dataset.
Link: https://arxiv.org/pdf/1606.03657.pdf
"""
class Generator(nn.Module):
def __init__(self):
super(Generator, self).__init__()
self.layer1 = nn.Sequential(
nn.Linear(74, 1024),
nn.BatchNorm1d(1024),
nn.ReLU()
)
self.layer2 = nn.Sequential(
nn.Linear(1024, 7*7*128)
)
self.layer3 = nn.Sequential(
nn.BatchNorm2d(128),
nn.ReLU(),
nn.ConvTranspose2d(128, 64, 4, stride=2, padding=1, bias=False),
nn.BatchNorm2d(64),
nn.ReLU()
)
self.layer4 = nn.Sequential(
nn.ConvTranspose2d(64, 1, 4, stride=2, padding=1, bias=False)
)
def forward(self, x):
x = self.layer1(x)
x = self.layer2(x)
x = x.view(-1, 128, 7, 7)
x = self.layer3(x)
# nn.functional.tanh is deprecated. Use torch.tanh instead.
x = torch.tanh(self.layer4(x))
return x
class SharedNetwork(nn.Module):
def __init__(self):
super(SharedNetwork, self).__init__()
self.layer1 = nn.Sequential(
nn.Conv2d(1, 64, 4, stride=2, padding=1, bias=False),
nn.LeakyReLU(0.1)
)
self.layer2 = nn.Sequential(
nn.Conv2d(64, 128, 4, stride=2, padding=1, bias=False),
nn.BatchNorm2d(128),
nn.LeakyReLU(0.1)
)
self.layer3 = nn.Sequential(
nn.Linear(128*7*7, 1024),
nn.BatchNorm1d(1024),
nn.LeakyReLU(0.1)
)
def forward(self, x):
x = self.layer1(x)
x = self.layer2(x)
x = x.view(-1, 128*7*7)
x = self.layer3(x)
return x
class Discriminator(nn.Module):
def __init__(self):
super(Discriminator, self).__init__()
self.layer4 = nn.Sequential(
nn.Linear(1024, 1)
)
def forward(self, x):
# nn.functional.sigmoid is deprecated. Use torch.sigmoid instead.
x = torch.sigmoid(self.layer4(x))
return x
class Recogniser(nn.Module):
def __init__(self):
super(Recogniser, self).__init__()
self.layer4 = nn.Sequential(
nn.Linear(1024, 128),
nn.BatchNorm1d(128),
nn.LeakyReLU(0.1)
)
self.layer5_disc = nn.Sequential(
nn.Linear(128, 10)
)
self.layer5_mu = nn.Sequential(
nn.Linear(128, 2)
)
self.layer5_var = nn.Sequential(
nn.Linear(128, 2)
)
def forward(self, x):
x = self.layer4(x)
disc_logits = self.layer5_disc(x)
mu = self.layer5_mu(x)
var = self.layer5_var(x)
return disc_logits, mu, var
def init_weights(model):
classname = model.__class__.__name__
if classname.find('Conv') != -1:
model.weight.data.normal_(0.0, 0.02)
elif classname.find('Linear') != -1:
model.weight.data.normal_(0.0, 0.02)
elif classname.find('BatchNorm') != -1:
model.weight.data.normal_(1.0, 0.02)
model.bias.data.fill_(0)