-
Notifications
You must be signed in to change notification settings - Fork 25
/
model.py
207 lines (174 loc) · 6.88 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
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
import torch
import torch.nn as nn
import torch.nn.functional as F
from torch.autograd import Variable
import torchvision.models as models
def init_weights(m):
if isinstance(m, nn.Conv2d) or isinstance(m, nn.Linear):
if m.weight.requires_grad:
m.weight.data.normal_(std=0.02)
if m.bias is not None and m.bias.requires_grad:
m.bias.data.fill_(0)
elif isinstance(m, nn.BatchNorm2d) and m.affine:
if m.weight.requires_grad:
m.weight.data.normal_(1, 0.02)
if m.bias.requires_grad:
m.bias.data.fill_(0)
class VisualSemanticEmbedding(nn.Module):
def __init__(self, embed_ndim):
super(VisualSemanticEmbedding, self).__init__()
self.embed_ndim = embed_ndim
# image feature
self.img_encoder = models.vgg16(pretrained=True)
for param in self.img_encoder.parameters():
param.requires_grad = False
self.feat_extractor = nn.Sequential(*(self.img_encoder.classifier[i] for i in range(6)))
self.W = nn.Linear(4096, embed_ndim, False)
# text feature
self.txt_encoder = nn.GRU(embed_ndim, embed_ndim, 1)
def forward(self, img, txt):
# image feature
img_feat = self.img_encoder.features(img)
img_feat = img_feat.view(img_feat.size(0), -1)
img_feat = self.feat_extractor(img_feat)
img_feat = self.W(img_feat)
# text feature
h0 = torch.zeros(1, img.size(0), self.embed_ndim)
h0 = Variable(h0.cuda() if txt.data.is_cuda else h0)
_, txt_feat = self.txt_encoder(txt, h0)
txt_feat = txt_feat.squeeze()
return img_feat, txt_feat
class ResidualBlock(nn.Module):
def __init__(self):
super(ResidualBlock, self).__init__()
self.encoder = nn.Sequential(
nn.Conv2d(512, 512, 3, padding=1, bias=False),
nn.BatchNorm2d(512),
nn.ReLU(inplace=True),
nn.Conv2d(512, 512, 3, padding=1, bias=False),
nn.BatchNorm2d(512)
)
def forward(self, x):
return F.relu(x + self.encoder(x))
class Generator(nn.Module):
def __init__(self, use_vgg=True):
super(Generator, self).__init__()
# encoder
if use_vgg:
self.encoder = models.vgg16_bn(pretrained=True)
self.encoder = \
nn.Sequential(*(self.encoder.features[i] for i in range(23) + range(24, 33)))
self.encoder[24].dilation = (2, 2)
self.encoder[24].padding = (2, 2)
self.encoder[27].dilation = (2, 2)
self.encoder[27].padding = (2, 2)
self.encoder[30].dilation = (2, 2)
self.encoder[30].padding = (2, 2)
for param in self.encoder.parameters():
param.requires_grad = False
self.encoder.eval()
else:
self.encoder = nn.Sequential(
nn.Conv2d(3, 128, 3, padding=1),
nn.ReLU(inplace=True),
nn.Conv2d(128, 256, 4, 2, padding=1, bias=False),
nn.BatchNorm2d(256),
nn.ReLU(inplace=True),
nn.Conv2d(256, 512, 4, 2, padding=1, bias=False),
nn.BatchNorm2d(512),
nn.ReLU(inplace=True)
)
# residual blocks
self.residual_blocks = nn.Sequential(
nn.Conv2d(512 + 128, 512, 3, padding=1, bias=False),
nn.BatchNorm2d(512),
ResidualBlock(),
ResidualBlock(),
ResidualBlock(),
ResidualBlock()
)
# decoder
self.decoder = nn.Sequential(
nn.Upsample(scale_factor=2, mode='nearest'),
nn.Conv2d(512, 256, 3, padding=1, bias=False),
nn.BatchNorm2d(256),
nn.ReLU(inplace=True),
nn.Upsample(scale_factor=2, mode='nearest'),
nn.Conv2d(256, 128, 3, padding=1, bias=False),
nn.BatchNorm2d(128),
nn.ReLU(inplace=True),
nn.Conv2d(128, 3, 3, padding=1),
nn.Tanh()
)
# conditioning augmentation
self.mu = nn.Sequential(
nn.Linear(300, 128, bias=False),
nn.LeakyReLU(0.2, inplace=True)
)
self.log_sigma = nn.Sequential(
nn.Linear(300, 128, bias=False),
nn.LeakyReLU(0.2, inplace=True)
)
self.apply(init_weights)
def forward(self, img, txt_feat, z=None):
# encoder
img_feat = self.encoder(img)
z_mean = self.mu(txt_feat)
z_log_stddev = self.log_sigma(txt_feat)
z = torch.randn(txt_feat.size(0), 128)
if next(self.parameters()).is_cuda:
z = z.cuda()
txt_feat = z_mean + z_log_stddev.exp() * Variable(z)
# residual blocks
txt_feat = txt_feat.unsqueeze(-1).unsqueeze(-1)
txt_feat = txt_feat.repeat(1, 1, img_feat.size(2), img_feat.size(3))
fusion = torch.cat((img_feat, txt_feat), dim=1)
fusion = self.residual_blocks(fusion)
# decoder
output = self.decoder(fusion)
return output, (z_mean, z_log_stddev)
class Discriminator(nn.Module):
def __init__(self):
super(Discriminator, self).__init__()
self.encoder = nn.Sequential(
nn.Conv2d(3, 64, 4, 2, padding=1),
nn.LeakyReLU(0.2, inplace=True),
nn.Conv2d(64, 128, 4, 2, padding=1, bias=False),
nn.BatchNorm2d(128),
nn.LeakyReLU(0.2, inplace=True),
nn.Conv2d(128, 256, 4, 2, padding=1, bias=False),
nn.BatchNorm2d(256),
nn.LeakyReLU(0.2, inplace=True),
nn.Conv2d(256, 512, 4, 2, padding=1, bias=False),
nn.BatchNorm2d(512)
)
self.residual_branch = nn.Sequential(
nn.Conv2d(512, 128, 1, bias=False),
nn.BatchNorm2d(128),
nn.LeakyReLU(0.2, inplace=True),
nn.Conv2d(128, 128, 3, padding=1, bias=False),
nn.BatchNorm2d(128),
nn.LeakyReLU(0.2, inplace=True),
nn.Conv2d(128, 512, 3, padding=1, bias=False),
nn.BatchNorm2d(512)
)
self.classifier = nn.Sequential(
nn.Conv2d(512 + 128, 512, 1, bias=False),
nn.BatchNorm2d(512),
nn.LeakyReLU(0.2, inplace=True),
nn.Conv2d(512, 1, 4)
)
self.compression = nn.Sequential(
nn.Linear(300, 128),
nn.LeakyReLU(0.2, inplace=True)
)
self.apply(init_weights)
def forward(self, img, txt_feat):
img_feat = self.encoder(img)
img_feat = F.leaky_relu(img_feat + self.residual_branch(img_feat), 0.2)
txt_feat = self.compression(txt_feat)
txt_feat = txt_feat.unsqueeze(-1).unsqueeze(-1)
txt_feat = txt_feat.repeat(1, 1, img_feat.size(2), img_feat.size(3))
fusion = torch.cat((img_feat, txt_feat), dim=1)
output = self.classifier(fusion)
return output.squeeze()