-
Notifications
You must be signed in to change notification settings - Fork 42
/
nets.py
208 lines (183 loc) · 6.65 KB
/
nets.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
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
199
200
201
202
203
204
205
206
207
208
# -*- coding:utf-8 -*-
# Created Time: Wed 07 Mar 2018 12:38:26 PM CST
# Author: Taihong Xiao <[email protected]>
import torch
import torch.nn as nn
from torch.autograd import Variable
from torch.nn.parameter import Parameter
class NTimesTanh(nn.Module):
def __init__(self, N):
super(NTimesTanh, self).__init__()
self.N = N
self.tanh = nn.Tanh()
def forward(self, x):
return self.tanh(x) * self.N
class Normalization(nn.Module):
def __init__(self):
super(Normalization, self).__init__()
self.alpha = Parameter(torch.ones(1))
self.beta = Parameter(torch.zeros(1))
def forward(self, x):
x = torch.nn.functional.normalize(x, dim=1)
return x * self.alpha + self.beta
class Encoder(nn.Module):
def __init__(self):
super(Encoder, self).__init__()
self.main = nn.ModuleList([
nn.Sequential(
nn.Conv2d(3,64,3,2,1,bias=True),
Normalization(),
nn.LeakyReLU(negative_slope=0.2),
),
nn.Sequential(
nn.Conv2d(64,128,3,2,1,bias=True),
Normalization(),
nn.LeakyReLU(negative_slope=0.2),
),
nn.Sequential(
nn.Conv2d(128,256,3,2,1,bias=True),
Normalization(),
nn.LeakyReLU(negative_slope=0.2),
),
nn.Sequential(
nn.Conv2d(256,512,3,2,1,bias=True),
Normalization(),
nn.LeakyReLU(negative_slope=0.2),
),
nn.Sequential(
nn.Conv2d(512,512,3,2,1,bias=True),
Normalization(),
nn.LeakyReLU(negative_slope=0.2),
),
])
# init weight
for m in self.modules():
if isinstance(m, nn.Conv2d):
nn.init.normal(m.weight, 0, 0.02)
elif isinstance(m, nn.ConvTranspose2d):
nn.init.normal(m.weight, 0, 0.02)
elif isinstance(m, nn.BatchNorm2d):
nn.init.normal(m.weight, 1, 0.02)
nn.init.constant(m.bias, 0)
elif isinstance(m, nn.Linear):
nn.init.normal(m.weight, 0, 0.02)
nn.init.constant(m.bias, 0)
def forward(self, x, return_skip=True):
skip = []
for i in range(len(self.main)):
x = self.main[i](x)
if i < len(self.main) - 1:
skip.append(x)
if return_skip:
return x, skip
else:
return x
class Decoder(nn.Module):
def __init__(self):
super(Decoder, self).__init__()
self.main = nn.ModuleList([
nn.Sequential(
nn.ConvTranspose2d(1024,512,3,2,1,1,bias=True),
Normalization(),
nn.ReLU(),
),
nn.Sequential(
nn.ConvTranspose2d(512,256,3,2,1,1,bias=True),
Normalization(),
nn.ReLU(),
),
nn.Sequential(
nn.ConvTranspose2d(256,128,3,2,1,1,bias=True),
Normalization(),
nn.ReLU(),
),
nn.Sequential(
nn.ConvTranspose2d(128,64,3,2,1,1,bias=True),
Normalization(),
nn.ReLU(),
),
nn.Sequential(
nn.ConvTranspose2d(64,3,3,2,1,1,bias=True),
),
])
self.activation = NTimesTanh(2)
# init weight
for m in self.modules():
if isinstance(m, nn.Conv2d):
nn.init.normal(m.weight, 0, 0.02)
elif isinstance(m, nn.ConvTranspose2d):
nn.init.normal(m.weight, 0, 0.02)
elif isinstance(m, nn.BatchNorm2d):
nn.init.normal(m.weight, 1, 0.02)
nn.init.constant(m.bias, 0)
elif isinstance(m, nn.Linear):
nn.init.normal(m.weight, 0, 0.02)
nn.init.constant(m.bias, 0)
def forward(self, enc1, enc2, skip=None):
x = torch.cat([enc1, enc2], 1)
for i in range(len(self.main)):
x = self.main[i](x)
if skip is not None and i < len(skip):
x += skip[-i-1]
return self.activation(x)
class Discriminator(nn.Module):
def __init__(self, n_attributes, img_size):
super(Discriminator, self).__init__()
self.n_attributes = n_attributes
self.img_size = img_size
self.conv = nn.Sequential(
nn.Conv2d(3+n_attributes,64,3,2,1,bias=True),
Normalization(),
nn.LeakyReLU(negative_slope=0.2),
nn.Conv2d(64,128,3,2,1,bias=True),
Normalization(),
nn.LeakyReLU(negative_slope=0.2),
nn.Conv2d(128,256,3,2,1,bias=True),
Normalization(),
nn.LeakyReLU(negative_slope=0.2),
nn.Conv2d(256,512,3,2,1,bias=True),
Normalization(),
nn.LeakyReLU(negative_slope=0.2),
)
self.linear = nn.Sequential(
nn.Linear(512*(self.img_size//16)*(self.img_size//16), 1),
nn.Sigmoid(),
)
self.downsample = torch.nn.AvgPool2d(2, stride=2)
# init weight
for m in self.modules():
if isinstance(m, nn.Conv2d):
nn.init.normal(m.weight, 0, 0.02)
elif isinstance(m, nn.ConvTranspose2d):
nn.init.normal(m.weight, 0, 0.02)
elif isinstance(m, nn.BatchNorm2d):
nn.init.normal(m.weight, 1, 0.02)
nn.init.constant(m.bias, 0)
elif isinstance(m, nn.Linear):
nn.init.normal(m.weight, 0, 0.02)
nn.init.constant(m.bias, 0)
def forward(self, image, label):
'''
image: (n * c * h * w)
label: (n * n_attributes)
'''
while image.shape[-1] != self.img_size or image.shape[-2] != self.img_size:
image = self.downsample(image)
new_label = label.view((image.shape[0], self.n_attributes, 1, 1)).expand((image.shape[0], self.n_attributes, image.shape[2], image.shape[3]))
x = torch.cat([image, new_label], 1)
output = self.conv(x)
output = output.view(output.shape[0], -1)
output = self.linear(output)
return output
if __name__ == "__main__":
enc = Encoder()
dec = Decoder()
D1 = Discriminator(3, 256)
D2 = Discriminator(3, 128)
imgs = Variable(torch.rand(32,3,256,256))
labels = Variable(torch.ones(32,3))
out, skip = enc(imgs)
rec = dec(enc1=out, enc2=out, skip=skip)
fake1 = D1(imgs, labels)
fake2 = D2(imgs, labels)
from IPython import embed; embed(); exit()