-
Notifications
You must be signed in to change notification settings - Fork 3
/
model.py
367 lines (315 loc) · 13.1 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
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
import torch
import torch.nn as nn
import torch.nn.parallel
# from miscc.config import cfg
from torch.autograd import Variable
from torch_geometric.nn import GCNConv, TopKPooling
from torch_geometric.nn import global_mean_pool as gap, global_max_pool as gmp
import torch.nn.functional as F
def conv3x1(in_planes, out_planes, stride=1):
"3x1 convolution with padding"
kernel_length = 41
return nn.Conv1d(in_planes, out_planes, kernel_size=kernel_length, stride=stride,
padding=20, bias=False)
def old_conv3x1(in_planes, out_planes, stride=1):
"3x1 convolution with padding"
kernel_length = 3
return nn.Conv1d(in_planes, out_planes, kernel_size=kernel_length, stride=stride,
padding=1, bias=False)
# def convn3x1(in_planes, out_planes, stride=1):
# "3x1 convolution with padding"
# return nn.Conv1d(in_planes, out_planes, kernel_size=9, stride=stride,
# padding=4, bias=False)
# Upsale the spatial size by a factor of 2
def upBlock4(in_planes, out_planes):
kernel_length = 41
stride = 4
block = nn.Sequential(
# nn.Upsample(scale_factor=4, mode='nearest'),
# conv3x1(in_planes, out_planes),
nn.ConvTranspose1d(in_planes,out_planes,kernel_size=kernel_length,stride=stride, padding=19,output_padding=1),
nn.BatchNorm1d(out_planes),
# nn.ReLU(True)
nn.PReLU())
return block
def upBlock2(in_planes, out_planes):
kernel_length = 41
stride = 2
block = nn.Sequential(
# nn.Upsample(scale_factor=4, mode='nearest'),
# conv3x1(in_planes, out_planes),
nn.ConvTranspose1d(in_planes,out_planes,kernel_size=kernel_length,stride=stride, padding=20,output_padding=1),
nn.BatchNorm1d(out_planes),
# nn.ReLU(True)
nn.PReLU())
return block
def sameBlock(in_planes, out_planes):
block = nn.Sequential(
# nn.Upsample(scale_factor=4, mode='nearest'),
conv3x1(in_planes, out_planes),
nn.BatchNorm1d(out_planes),
# nn.ReLU(True)
nn.PReLU())
return block
class ResBlock(nn.Module):
def __init__(self, channel_num):
super(ResBlock, self).__init__()
self.block = nn.Sequential(
conv3x1(channel_num, channel_num),
nn.BatchNorm1d(channel_num),
# nn.ReLU(True),
nn.PReLU(),
conv3x1(channel_num, channel_num),
nn.BatchNorm1d(channel_num))
self.relu = nn.PReLU()#nn.ReLU(inplace=True)
def forward(self, x):
residual = x
out = self.block(x)
out += residual
out = self.relu(out)
return out
# class CA_NET(nn.Module): #not chnaged yet
# # some code is modified from vae examples
# # (https://github.com/pytorch/examples/blob/master/vae/main.py)
# def __init__(self):
# super(CA_NET, self).__init__()
# self.t_dim = cfg.TEXT.DIMENSION
# self.c_dim = cfg.GAN.CONDITION_DIM
# self.fc = nn.Linear(self.t_dim, self.c_dim * 2, bias=True)
# self.relu = nn.ReLU()
# def encode(self, text_embedding):
# x = self.relu(self.fc(text_embedding))
# mu = x[:, :self.c_dim]
# logvar = x[:, self.c_dim:]
# return mu, logvar
# def reparametrize(self, mu, logvar):
# std = logvar.mul(0.5).exp_()
# if cfg.CUDA:
# eps = torch.cuda.FloatTensor(std.size()).normal_()
# else:
# eps = torch.FloatTensor(std.size()).normal_()
# eps = Variable(eps)
# return eps.mul(std).add_(mu)
# def forward(self, text_embedding):
# mu, logvar = self.encode(text_embedding)
# c_code = self.reparametrize(mu, logvar)
# return c_code, mu, logvar
class COND_NET(nn.Module): #not chnaged yet
# some code is modified from vae examples
# (https://github.com/pytorch/examples/blob/master/vae/main.py)
def __init__(self):
super(COND_NET, self).__init__()
self.t_dim = 14#cfg.TEXT.DIMENSION
self.c_dim = 10#cfg.GAN.CONDITION_DIM
self.fc = nn.Linear(self.t_dim, self.c_dim, bias=True)
self.relu = nn.PReLU()#nn.ReLU()
def encode(self, full_embed):
x = self.relu(self.fc(full_embed))
# mu = x[:, :self.c_dim]
# logvar = x[:, self.c_dim:]
return x
# def reparametrize(self, mu, logvar):
# std = logvar.mul(0.5).exp_()
# if cfg.CUDA:
# eps = torch.cuda.FloatTensor(std.size()).normal_()
# else:
# eps = torch.FloatTensor(std.size()).normal_()
# eps = Variable(eps)
# return eps.mul(std).add_(mu)
def forward(self, full_embed):
c_code = self.encode(full_embed)
# c_code = self.reparametrize(mu, logvar)
return c_code #, mu, logvar
class MESH_NET(nn.Module):
def __init__(self):
super(MESH_NET,self).__init__()
self.feature_dim = 5#3
self.conv1 = GCNConv(self.feature_dim, 32)
self.pool1 = TopKPooling(32, ratio=0.6)
self.conv2 = GCNConv(32, 32) #(32, 64)
self.pool2 = TopKPooling(32, ratio=0.6) #64, ratio=0.6)
self.conv3 = GCNConv(32, 32) #(64, 128)
self.pool3 = TopKPooling(32, ratio=0.6) #(128, ratio=0.6)
# self.item_embedding = torch.nn.Embedding(num_embeddings=df.item_id.max() +1, embedding_dim=self.feature_dim)
self.lin1 = torch.nn.Linear(64, 16) #(256, 128)
self.lin2 = torch.nn.Linear(16, 8) #(128, 64)
# self.lin3 = torch.nn.Linear(8, 1) #(64, 1)
self.bn1 = torch.nn.BatchNorm1d(16) #(128)
self.bn2 = torch.nn.BatchNorm1d(8) #(64)
self.act1 = torch.nn.ReLU()
# self.act2 = torch.nn.ReLU()
def forward(self, data):
x, edge_index, batch = data.pos, data.edge_index, data.batch
# x = self.item_embedding(x)
# x = x.squeeze(1)
# print("batch ",batch)
x = F.relu(self.conv1(x, edge_index))
x, edge_index, _, batch, _ ,_= self.pool1(x, edge_index, None, batch)
x1 = torch.cat([gmp(x, batch), gap(x, batch)], dim=1)
x = F.relu(self.conv2(x, edge_index))
x, edge_index, _, batch, _,_ = self.pool2(x, edge_index, None, batch)
x2 = torch.cat([gmp(x, batch), gap(x, batch)], dim=1)
x = F.relu(self.conv3(x, edge_index))
x, edge_index, _, batch, _,_ = self.pool3(x, edge_index, None, batch)
x3 = torch.cat([gmp(x, batch), gap(x, batch)], dim=1)
# print("x1 shape ", x1.shape)
# print("x2 shape ", x2.shape)
# print("x3 shape ", x3.shape)
x = x1 + x2 + x3
x = self.lin1(x)
x = self.act1(x)
# print("x shape1 ", x.shape)
# x = self.lin2(x)
# x = self.act2(x)
x = F.dropout(x, p=0.5, training=self.training)
# print("x shape2 ", x.shape)
x = torch.sigmoid(self.lin2(x)).squeeze(1)
# print("x shape3 ", x.shape)
return x
class D_GET_LOGITS(nn.Module): #not chnaged yet
def __init__(self, ndf, nef, bcondition=True):
super(D_GET_LOGITS, self).__init__()
self.df_dim = ndf
self.ef_dim = nef
self.bcondition = bcondition
kernel_length =41
if bcondition:
self.convd1d = nn.ConvTranspose1d(ndf*8,ndf //2,kernel_size=kernel_length,stride=1, padding=20)
# self.outlogits = nn.Sequential(
# old_conv3x1(ndf * 8 + nef, ndf * 8),
# nn.BatchNorm1d(ndf * 8),
# nn.LeakyReLU(0.2, inplace=True),
# nn.Conv1d(ndf * 8, 1, kernel_size=16, stride=4),
# # nn.Conv1d(1, 1, kernel_size=16, stride=4),
# nn.Sigmoid()
# )
self.outlogits = nn.Sequential(
old_conv3x1(ndf //2 + nef, ndf //2 ),
nn.BatchNorm1d(ndf //2 ),
nn.LeakyReLU(0.2, inplace=True),
nn.Conv1d(ndf //2 , 1, kernel_size=16, stride=4),
# nn.Conv1d(1, 1, kernel_size=16, stride=4),
nn.Sigmoid()
)
else:
# self.outlogits = nn.Sequential(
# nn.Conv1d(ndf * 8, 1, kernel_size=16, stride=4),
# # nn.Conv1d(1, 1, kernel_size=16, stride=4),
# nn.Sigmoid())
self.convd1d = nn.ConvTranspose1d(ndf*8,ndf //2,kernel_size=kernel_length,stride=1, padding=20)
self.outlogits = nn.Sequential(
nn.Conv1d(ndf // 2 , 1, kernel_size=16, stride=4),
# nn.Conv1d(1, 1, kernel_size=16, stride=4),
nn.Sigmoid())
def forward(self, h_code, c_code=None):
# conditioning output
h_code = self.convd1d(h_code)
if self.bcondition and c_code is not None:
#print("mode c_code1 ",c_code.size())
c_code = c_code.view(-1, self.ef_dim, 1)
#print("mode c_code2 ",c_code.size())
c_code = c_code.repeat(1, 1, 16)
# state size (ngf+egf) x 16
#print("mode c_code ",c_code.size())
#print("mode h_code ",h_code.size())
h_c_code = torch.cat((h_code, c_code), 1)
else:
h_c_code = h_code
output = self.outlogits(h_c_code)
return output.view(-1)
# ############# Networks for stageI GAN #############
class STAGE1_G(nn.Module):
def __init__(self):
super(STAGE1_G, self).__init__()
self.gf_dim = 256 * 8 #cfg.GAN.GF_DIM
self.ef_dim = 10 #cfg.GAN.CONDITION_DIM
# self.z_dim = cfg.Z_DIM
self.define_module()
def define_module(self):
kernel_length = 41
ninput = self.ef_dim #self.z_dim + self.ef_dim
ngf = self.gf_dim
# TEXT.DIMENSION -> GAN.CONDITION_DIM
# self.ca_net = CA_NET()
self.cond_net = COND_NET()
# self.mesh_net = MESH_NET()
# -> ngf x 16
self.fc = nn.Sequential(
nn.Linear(ninput, ngf * 16, bias=False),
nn.BatchNorm1d(ngf * 16),
# nn.ReLU(True)
nn.PReLU())
# ngf x 16 -> ngf/2 x 64
self.upsample1 = upBlock4(ngf, ngf // 2)
# -> ngf/4 x 256
self.upsample2 = upBlock4(ngf // 2, ngf // 4)
# -> ngf/8 x 1024
self.upsample3 = upBlock4(ngf // 4, ngf // 8)
# -> ngf/16 x 4096
self.upsample4 = upBlock2(ngf // 8, ngf // 16)
self.upsample5 = upBlock2(ngf // 16, ngf // 16)
# -> 1 x 4096
self.RIR = nn.Sequential(
nn.ConvTranspose1d(ngf // 16,2,kernel_size=kernel_length,stride=1, padding=20),
# old_conv3x1(ngf // 16, 1), # conv3x3(ngf // 16, 3),
nn.Tanh())
def forward(self, text_embedding,mesh_embed):
# mesh_embed = self.mesh_net(data)
# c_code, mu, logvar = self.ca_net(text_embedding)
# c_code = self.cond_net(text_embedding)
# print("mesh_embed ", mesh_embed.shape)
# print("text_embedding ", text_embedding.shape)
full_embed= torch.cat((mesh_embed, text_embedding), 1)
c_code = self.cond_net(full_embed)
h_code = self.fc(c_code)
h_code = h_code.view(-1, self.gf_dim, 16)
# print("h_code 1 ",h_code.size())
h_code = self.upsample1(h_code)
# print("h_code 2 ",h_code.size())
h_code = self.upsample2(h_code)
# print("h_code 3 ",h_code.size())
h_code = self.upsample3(h_code)
# print("h_code 4 ",h_code.size())
h_code = self.upsample4(h_code)
h_code = self.upsample5(h_code)
# print("h_code 5 ",h_code.size())
# # state size 3 x 64 x 64
fake_RIR = self.RIR(h_code)
# print("fake_RIR ",fake_RIR.size())
# # return None, fake_RIR, mu, logvar
# #print("generator ", text_embedding.size())
# return None, fake_RIR, text_embedding #c_code
return None, fake_RIR, c_code
class STAGE1_D(nn.Module):
def __init__(self):
super(STAGE1_D, self).__init__()
self.df_dim = 96 #cfg.GAN.DF_DIM
self.ef_dim = 10 #cfg.GAN.CONDITION_DIM
self.define_module()
def define_module(self):
ndf, nef = self.df_dim, self.ef_dim
kernel_length =41
self.encode_RIR = nn.Sequential(
nn.Conv1d(2, ndf, kernel_length, 4, 20, bias=False),
nn.LeakyReLU(0.2, inplace=True),
# state size. (ndf) x 1024
nn.Conv1d(ndf, ndf * 2, kernel_length, 4, 20, bias=False),
nn.BatchNorm1d(ndf * 2),
nn.LeakyReLU(0.2, inplace=True),
# state size (ndf*2) x 256
nn.Conv1d(ndf*2, ndf * 4, kernel_length, 4, 20, bias=False),
nn.BatchNorm1d(ndf * 4),
nn.LeakyReLU(0.2, inplace=True),
# # state size (ndf*4) x 64
nn.Conv1d(ndf*4, ndf * 8, kernel_length, 4, 20, bias=False),
nn.BatchNorm1d(ndf * 8),
# state size (ndf * 8) x 16)
nn.LeakyReLU(0.2, inplace=True)
)
self.get_cond_logits = D_GET_LOGITS(ndf, nef)
self.get_uncond_logits = None
def forward(self, RIRs):
#print("model RIRs ",RIRs.size())
RIR_embedding = self.encode_RIR(RIRs)
#print("models RIR_embedding ",RIR_embedding.size())
return RIR_embedding