-
Notifications
You must be signed in to change notification settings - Fork 1
/
transgan.py
192 lines (142 loc) · 5.37 KB
/
transgan.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
import numpy as np
import torch
import torch.nn as nn
import torch.nn.functional as F
"""
Paper uses Vaswani (2017) Attention with minimal changes.
Multi-head self-attention with a feed-forward MLP with GELU non-linearity. Layer normalisation is used before each segment and employs residual skip connections.
"""
class Attention(nn.Module):
def __init__(self, D, heads=8):
super().__init__()
self.D = D
self.heads = heads
assert (D % heads == 0), "Embedding size should be divisble by number of heads"
self.head_dim = self.D // heads
self.queries = nn.Linear(self.head_dim, self.head_dim, bias=False)
self.keys = nn.Linear(self.head_dim, self.head_dim, bias=False)
self.values = nn.Linear(self.head_dim, self.head_dim, bias=False)
self.H = nn.Linear(self.D, self.D)
def forward(self, Q, K, V, mask):
batch_size = Q.shape[0]
q_len, k_len, v_len = Q.shape[1], K.shape[1], V.shape[1]
Q = Q.reshape(batch_size, q_len, self.heads, self.head_dim)
K = K.reshape(batch_size, k_len, self.heads, self.head_dim)
V = V.reshape(batch_size, v_len, self.heads, self.head_dim)
# performing batch-wise matrix multiplication
raw_scores = torch.einsum("bqhd,bkhd->bhqk", [Q, K])
# shut off triangular matrix with very small value
scores = raw_scores.masked_fill(mask == 0, -np.inf) if mask else raw_scores
attn = torch.softmax(scores / np.sqrt(self.D), dim=3)
attn_output = torch.einsum("bhql,blhd->bqhd", [attn, V])
attn_output = attn_output.reshape(batch_size, q_len, self.D)
output = self.H(attn_output)
return output
class EncoderBlock(nn.Module):
def __init__(self, D, heads, p, fwd_exp):
super().__init__()
self.mha = Attention(D, heads)
self.drop_prob = p
self.n1 = nn.LayerNorm(D)
self.n2 = nn.LayerNorm(D)
self.mlp = nn.Sequential(
nn.Linear(D, fwd_exp*D),
nn.ReLU(),
nn.Linear(fwd_exp*D, D),
)
self.dropout = nn.Dropout(p)
def forward(self, Q, K, V, mask):
attn = self.mha(Q, K, V, mask)
"""
Layer normalisation with residual connections
"""
x = self.n1(attn + Q)
x = self.dropout(x)
forward = self.mlp(x)
x = self.n2(forward + x)
out = self.dropout(x)
return out
class MLP(nn.Module):
def __init__(self, noise_w, noise_h, channels):
super().__init__()
self.l1 = nn.Linear(
noise_w*noise_h*channels,
(8*8)*noise_w*noise_h*channels,
bias=False
)
def forward(self, x):
out = self.l1(x)
return out
class PixelShuffle(nn.Module):
def __init__(self):
super().__init__()
pass
#--------------------------------------------------------------------------------------------
# Generator
#--------------------------------------------------------------------------------------------
class Generator(nn.Module):
def __init__(self):
super().__init__()
self.mlp = MLP(32, 32, 1)
# stage 1
self.s1_enc = nn.ModuleList([
EncoderBlock(1024*8*8)
for _ in range(5)
])
# stage 2
self.s2_pix_shuffle = PixelShuffle()
self.s2_enc = nn.ModuleList([
EncoderBlock(256*16*16)
for _ in range (4)
])
# stage 3
self.s3_pix_shuffle = PixelShuffle()
self.s3_enc = nn.ModuleList([
EncoderBlock(64*32*32)
for _ in range(2)
])
# stage 4
self.linear = nn.Linear(32*32*64, 32*32*3)
def forward(self, noise):
x = self.mlp(noise)
for layer in self.s1_enc:
x = layer(x)
x = self.s2_pix_shuffle(x)
for layer in self.s2_enc:
x = layer(x)
x - self.s3_pix_shuffle(x)
for layer in self.s3_enc:
x = layer(x)
img = self.linear(x)
return img
#--------------------------------------------------------------------------------------------
# Discriminator
#--------------------------------------------------------------------------------------------
class Discriminator(nn.Module):
def __init__(self):
super().__init__()
self.l1 = nn.Linear(32*32*3, (8*8+1)*384)
self.s2_enc = nn.ModuleList([
EncoderBlock((8*8+1)*284)
for _ in range(7)
])
self.classification_head = nn.Linear(1*384, 1)
def forward(self, img):
x = self.l1(img)
for layer in self.s2_enc:
x = layer(x)
logits = self.classification_head(x)
pred = F.softmax(logits)
return pred
#--------------------------------------------------------------------------------------------
# Main model
#--------------------------------------------------------------------------------------------
class TransGAN(nn.Module):
def __init__(self):
super().__init__()
self.gen = Generator()
self.disc = Discriminator()
def forward(self, noise):
img = self.gen(noise)
pred = self.disc(img)
return img, pred