-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathnet_architectures.py
More file actions
199 lines (188 loc) · 7.54 KB
/
net_architectures.py
File metadata and controls
199 lines (188 loc) · 7.54 KB
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
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
#This module contains the different GAN architectures used in the project
#Each network class contains a subclass for the Generator and a subclass for the Discriminator
#Throughout the file nz represents the length of the random vector passed to the Generator, and nc represents the number of channels used
import torch
import torch.nn as nn
#https://discuss.pytorch.org/t/how-do-i-print-output-of-each-layer-in-sequential/5773/3
class PrintLayer(nn.Module):
def __init__(self,f):
self.f = f
super(PrintLayer, self).__init__()
def forward(self, x):
if self.f:
print(x.shape)
return x
#256 by 256 network
class Large_Net:
class Discriminator(nn.Module):
def __init__(self, ngpu, nc, f=False):
super(Large_Net.Discriminator, self).__init__()
self.ngpu = ngpu
self.main = nn.Sequential(
PrintLayer(f),
# input is (nc) x 256 x 256
nn.Conv2d(nc, 8, 4, 2, 1, bias=False),
nn.LeakyReLU(0.2, inplace=True),
nn.MaxPool2d(2,1),
PrintLayer(f),
# state size. 8 x 127 x 127
nn.Conv2d(8,16, 4, 2, 1, bias=False),
nn.BatchNorm2d(16),
nn.LeakyReLU(0.2, inplace=True),
nn.MaxPool2d(2,1),
PrintLayer(f),
# state size. 16 x 62 x 62
nn.Conv2d(16,32, 4, 2, 1, bias=False),
nn.BatchNorm2d(32),
nn.LeakyReLU(0.2, inplace=True),
nn.MaxPool2d(2,1),
PrintLayer(f),
# state size. 32 x 30 x 30
nn.Conv2d(32,64, 4, 2, 1, bias=False),
nn.BatchNorm2d(64),
nn.LeakyReLU(0.2, inplace=True),
nn.MaxPool2d(2,1),
PrintLayer(f),
# state size. 64 x 14 x 14
nn.Conv2d(64,128, 4, 2, 1, bias=False),
nn.BatchNorm2d(128),
nn.LeakyReLU(0.2, inplace=True),
nn.MaxPool2d(2,1),
PrintLayer(f),
# state size. 128 x 6 x 6
nn.Conv2d(128,256, 4, 2, 1, bias=False),
nn.BatchNorm2d(256),
nn.LeakyReLU(0.2, inplace=True),
nn.MaxPool2d(2,1),
PrintLayer(f),
# state size. 256 x 2 x 2
nn.Flatten(),
nn.Linear(1024,128, bias=False),
nn.LeakyReLU(0.2, inplace=True),
PrintLayer(f),
# state size. 128
nn.Linear(128,1, bias=False),
nn.Sigmoid(),
PrintLayer(f)
# state size. 1
)
def forward(self, input):
return self.main(input)
class Generator(nn.Module):
def __init__(self, ngpu,nc, nz,f=False):
super(Large_Net.Generator, self).__init__()
self.ngpu = ngpu
self.main = nn.Sequential(
PrintLayer(f),
# input is 100 x 1 x 1, going into a convolution
nn.ConvTranspose2d( nz, 512, 4, 1, 0, bias=False),
nn.BatchNorm2d(512),
nn.ReLU(True),
PrintLayer(f),
# state size. (512) x 4 x 4
nn.ConvTranspose2d(512, 256, 4, 2, 1, bias=False),
nn.BatchNorm2d(256),
nn.ReLU(True),
PrintLayer(f),
# state size. (256) x 8 x 8
nn.ConvTranspose2d( 256, 128, 4, 2, 1, bias=False),
nn.BatchNorm2d(128),
nn.ReLU(True),
PrintLayer(f),
# state size. (128) x 16 x 16
nn.ConvTranspose2d( 128, 64, 4, 2, 1, bias=False),
nn.BatchNorm2d(64),
nn.ReLU(True),
PrintLayer(f),
# state size. (64) x 32 x 32
nn.ConvTranspose2d( 64,32, 4, 2, 1, bias=False),
nn.BatchNorm2d(32),
nn.ReLU(True),
PrintLayer(f),
# state size. 32 x 64 x 64
nn.ConvTranspose2d( 32,16, 4, 2, 1, bias=False),
nn.BatchNorm2d(16),
nn.ReLU(True),
PrintLayer(f),
# state size. 16 x 128 x 128
nn.ConvTranspose2d( 16,8, 4, 2, 1, bias=False),
nn.BatchNorm2d(8),
nn.ReLU(True),
PrintLayer(f),
# state size. 8 x 256 x 256
nn.Conv2d( 8,nc, 3, 1, 1, bias=False),
nn.Tanh(),
PrintLayer(f)
# state size. 1 x 256 x 256
)
def forward(self, input):
return self.main(input)
#64 by 64 network, based on https://pytorch.org/tutorials/beginner/dcgan_faces_tutorial.html
class Small_Net:
class Discriminator(nn.Module):
def __init__(self, ngpu, nc, f=False):
super(Small_Net.Discriminator, self).__init__()
self.ngpu = ngpu
self.main = nn.Sequential(
PrintLayer(f),
# input is 1 x 64 x 64
nn.Conv2d(nc, 64, 4, 2, 1, bias=False),
nn.LeakyReLU(0.2, inplace=True),
PrintLayer(f),
# state size. 64 x 32 x 32
nn.Conv2d(64, 64*2, 4, 2, 1, bias=False),
nn.BatchNorm2d(64 * 2),
nn.LeakyReLU(0.2, inplace=True),
PrintLayer(f),
# state size. 128 x 16 x 16
nn.Conv2d(64 * 2, 64 * 4, 4, 2, 1, bias=False),
nn.BatchNorm2d(64 * 4),
nn.LeakyReLU(0.2, inplace=True),
PrintLayer(f),
# state size. 256 x 8 x 8
nn.Conv2d(64 * 4, 64 * 8, 4, 2, 1, bias=False),
nn.BatchNorm2d(64 * 8),
nn.LeakyReLU(0.2, inplace=True),
PrintLayer(f),
# state size. 512 x 4 x 4
nn.Conv2d(64 * 8, 1, 4, 1, 0, bias=False),
nn.Sigmoid(),
PrintLayer(f)
# state size. 1
)
def forward(self, input):
return self.main(input)
class Generator(nn.Module):
def __init__(self, ngpu, nc, nz, f=False):
super(Small_Net.Generator, self).__init__()
self.ngpu = ngpu
self.main = nn.Sequential(
PrintLayer(f),
# input is 100 x 1 x 1, going into a convolution
nn.ConvTranspose2d( nz, 512, 4, 1, 0, bias=False),
nn.BatchNorm2d(512),
nn.ReLU(True),
PrintLayer(f),
# state size. 512 x 4 x 4
nn.ConvTranspose2d(512, 256, 4, 2, 1, bias=False),
nn.BatchNorm2d(256),
nn.ReLU(True),
PrintLayer(f),
# state size. 256 x 8 x 8
nn.ConvTranspose2d( 256, 128, 4, 2, 1, bias=False),
nn.BatchNorm2d(128),
nn.ReLU(True),
PrintLayer(f),
# state size. 128 x 16 x 16
nn.ConvTranspose2d( 128, 64, 4, 2, 1, bias=False),
nn.BatchNorm2d(64),
nn.ReLU(True),
PrintLayer(f),
# state size. 64 x 32 x 32
nn.ConvTranspose2d( 64,nc, 4, 2, 1, bias=False),
nn.Tanh(),
PrintLayer(f)
# state size. 1 x 64 x 64
)
def forward(self, input):
return self.main(input)