-
Notifications
You must be signed in to change notification settings - Fork 11
/
discriminator.py
193 lines (168 loc) · 5.64 KB
/
discriminator.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
from collections import OrderedDict
import torch
from torch import nn
import torch.nn.functional as F
class _LayerNorm(nn.Module):
def __init__(self, num_features, img_size):
"""
Normalizes over the entire image and scales + weights for each feature
"""
super().__init__()
self.layer_norm = nn.LayerNorm(
(num_features, img_size, img_size), elementwise_affine=False, eps=1e-12
)
self.weight = torch.nn.Parameter(
torch.ones(num_features).float().unsqueeze(-1).unsqueeze(-1),
requires_grad=True,
)
self.bias = torch.nn.Parameter(
torch.zeros(num_features).float().unsqueeze(-1).unsqueeze(-1),
requires_grad=True,
)
def forward(self, x):
out = self.layer_norm(x)
out = out * self.weight + self.bias
return out
class _SamePad(nn.Module):
"""
Pads equivalent to the behavior of tensorflow "SAME"
"""
def __init__(self, stride):
super().__init__()
self.stride = stride
def forward(self, x):
if self.stride == 2 and x.shape[2] % 2 == 0:
return F.pad(x, (0, 1, 0, 1))
return F.pad(x, (1, 1, 1, 1))
def _conv2d(
in_channels,
out_channels,
kernel_size,
stride,
out_size=None,
activate=True,
dropout=0.0,
):
layers = OrderedDict()
layers["pad"] = _SamePad(stride)
layers["conv"] = nn.Conv2d(in_channels, out_channels, kernel_size, stride)
if activate:
if out_size is None:
raise ValueError("Must provide out_size if activate is True")
layers["relu"] = nn.LeakyReLU(0.2)
layers["norm"] = _LayerNorm(out_channels, out_size)
if dropout > 0.0:
layers["dropout"] = nn.Dropout(dropout)
return nn.Sequential(layers)
class _EncoderBlock(nn.Module):
def __init__(
self,
pre_channels,
in_channels,
out_channels,
num_layers,
out_size,
dropout_rate=0.0,
):
super().__init__()
self.num_layers = num_layers
self.pre_conv = _conv2d(
in_channels=pre_channels,
out_channels=pre_channels,
kernel_size=3,
stride=2,
activate=False,
)
self.conv0 = _conv2d(
in_channels=in_channels + pre_channels,
out_channels=out_channels,
kernel_size=3,
stride=1,
out_size=out_size,
)
total_channels = in_channels + out_channels
for i in range(1, num_layers):
self.add_module(
"conv%d" % i,
_conv2d(
in_channels=total_channels,
out_channels=out_channels,
kernel_size=3,
stride=1,
out_size=out_size,
),
)
total_channels += out_channels
self.add_module(
"conv%d" % num_layers,
_conv2d(
in_channels=total_channels,
out_channels=out_channels,
kernel_size=3,
stride=2,
out_size=(out_size + 1) // 2,
dropout=dropout_rate,
),
)
def forward(self, inp):
pre_input, x = inp
pre_input = self.pre_conv(pre_input)
out = self.conv0(torch.cat([x, pre_input], 1))
all_outputs = [x, out]
for i in range(1, self.num_layers + 1):
input_features = torch.cat(
[all_outputs[-1], all_outputs[-2]] + all_outputs[:-2], 1
)
module = self._modules["conv%d" % i]
out = module(input_features)
all_outputs.append(out)
return all_outputs[-2], all_outputs[-1]
class Discriminator(nn.Module):
def __init__(self, dim, channels, dropout_rate=0.0, z_dim=100):
super().__init__()
self.dim = dim
self.z_dim = z_dim
self.channels = channels
self.layer_sizes = [64, 64, 128, 128]
self.num_inner_layers = 5
# Number of times dimension is halved
self.depth = len(self.layer_sizes)
# dimension at each level of U-net
self.dim_arr = [dim]
for i in range(self.depth):
self.dim_arr.append((self.dim_arr[-1] + 1) // 2)
# Encoders
self.encode0 = _conv2d(
in_channels=self.channels,
out_channels=self.layer_sizes[0],
kernel_size=3,
stride=2,
out_size=self.dim_arr[1],
)
for i in range(1, self.depth):
self.add_module(
"encode%d" % i,
_EncoderBlock(
pre_channels=self.channels if i == 1 else self.layer_sizes[i - 1],
in_channels=self.layer_sizes[i - 1],
out_channels=self.layer_sizes[i],
num_layers=self.num_inner_layers,
out_size=self.dim_arr[i],
dropout_rate=dropout_rate,
),
)
self.dense1 = nn.Linear(self.layer_sizes[-1], 1024)
self.leaky_relu = nn.LeakyReLU(0.2)
self.dense2 = nn.Linear(self.layer_sizes[-1] * self.dim_arr[-1] ** 2 + 1024, 1)
def forward(self, x1, x2):
x = torch.cat([x1, x2], 1)
out = [x, self.encode0(x)]
for i in range(1, len(self.layer_sizes)):
out = self._modules["encode%d" % i](out)
out = out[1]
out_mean = out.mean([2, 3])
out_flat = torch.flatten(out, 1)
out = self.dense1(out_mean)
out = self.leaky_relu(out)
out = self.dense2(torch.cat([out, out_flat], 1))
return out