-
Notifications
You must be signed in to change notification settings - Fork 8
/
model_define.py
executable file
·182 lines (165 loc) · 7.09 KB
/
model_define.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
import torch
import torch.nn as nn
from torch.autograd import Variable
class lcc_sampling(nn.Module):
def __init__(self, basis_num, embedding_dim):
super(lcc_sampling, self).__init__()
self.basis_num = basis_num
self.embedding_dim = embedding_dim
self.register_buffer('basis', torch.zeros(self.basis_num, self.embedding_dim))
def reset_basis(self, basis):
if torch.is_tensor(basis):
self.basis.copy_(basis)
else:
self.basis.copy_(basis.data)
def forward(self, x):
batch_size = x.size(0)
sparsity = x.size(1)
assert sparsity <= self.basis_num
out = Variable(torch.zeros(batch_size, self.basis_num))
if self.training:
index = torch.LongTensor(batch_size).random_(self.basis_num)
else:
index = torch.LongTensor(batch_size).zero_()
if x.is_cuda:
index = index.cuda()
basis_select = self.basis[index]
basis_expand = self.basis.view(1, self.basis_num, self.embedding_dim).expand(batch_size, self.basis_num, self.embedding_dim)
select_expand = basis_select.view(batch_size, 1, self.embedding_dim).expand(batch_size, self.basis_num, self.embedding_dim)
distance = torch.norm(basis_expand-select_expand, 2, 2) # batch_size x basis_num
_, indices = torch.sort(distance)
indices = Variable(indices[:, 0:sparsity]) # batch_size x sparsity
if x.is_cuda:
out = out.cuda()
indices = indices.cuda()
out = out.scatter_(1, indices, x)
out = torch.mm(out, Variable(self.basis))
return out.view(out.size(0), out.size(1), 1, 1)
class _netG(nn.Module):
def __init__(self, basis_num, embedding_dim, nz, ngf, nc):
super(_netG, self).__init__()
self.basis_num = basis_num
self.embedding_dim = embedding_dim
self.nz = nz
self.ngf = ngf
self.nc = nc
self.lcc = lcc_sampling(self.basis_num, self.embedding_dim)
# DCGAN
self.main = nn.Sequential(
# input is Z, going into a convolution
nn.ConvTranspose2d(self.embedding_dim, self.ngf * 8, 4, 1, 0, bias=False),
nn.BatchNorm2d(self.ngf * 8),
nn.ReLU(True),
# state size. (ngf*8) x 4 x 4
nn.ConvTranspose2d(self.ngf * 8, self.ngf * 4, 4, 2, 1, bias=False),
nn.BatchNorm2d(self.ngf * 4),
nn.ReLU(True),
# state size. (ngf*4) x 8 x 8
nn.ConvTranspose2d(self.ngf * 4, self.ngf * 2, 4, 2, 1, bias=False),
nn.BatchNorm2d(self.ngf * 2),
nn.ReLU(True),
# state size. (ngf*2) x 16 x 16
nn.ConvTranspose2d(self.ngf * 2, self.ngf * 1, 4, 2, 1, bias=False),
nn.BatchNorm2d(self.ngf * 1),
nn.ReLU(True),
# state size. (ngf*1) x 32 x 32
nn.ConvTranspose2d(self.ngf * 1, self.nc, 4, 2, 1, bias=False),
nn.Tanh()
# state size. (ngf) x 64 x 64
)
def reset_basis(self, basis):
self.lcc.reset_basis(basis)
def forward(self, input):
output = self.lcc(input)
output = self.main(output)
return output
class _netD(nn.Module):
def __init__(self, nc, ndf):
super(_netD, self).__init__()
self.nc = nc
self.ndf = ndf
# DCGAN
self.main = nn.Sequential(
# input is (nc) x 64 x 64
nn.Conv2d(self.nc, self.ndf * 2, 4, 2, 1, bias=False),
nn.LeakyReLU(0.2, inplace=True),
# state size. (ndf*2) x 32 x 32
nn.Conv2d(self.ndf * 2, self.ndf * 4, 4, 2, 1, bias=False),
nn.BatchNorm2d(self.ndf * 4),
nn.LeakyReLU(0.2, inplace=True),
# state size. (ndf*4) x 16 x 16
nn.Conv2d(self.ndf * 4, self.ndf * 8, 4, 2, 1, bias=False),
nn.BatchNorm2d(self.ndf * 8),
nn.LeakyReLU(0.2, inplace=True),
# state size. (ndf*8) x 8 x 8
nn.Conv2d(self.ndf * 8, self.ndf * 8, 4, 2, 1, bias=False),
nn.BatchNorm2d(self.ndf * 8),
nn.LeakyReLU(0.2, inplace=True),
# state size. (ndf*8) x 4 x 4
nn.Conv2d(self.ndf * 8, 1, 4, 1, 0, bias=False),
nn.Sigmoid()
)
def forward(self, input):
output = self.main(input)
return output.view(-1, 1)
class _decoder(nn.Module):
def __init__(self, nc, ngf, embedding_dim):
super(_decoder, self).__init__()
self.nc = nc
self.ngf = ngf
self.embedding_dim = embedding_dim
self.main = nn.Sequential(
# input is Z, going into a convolution
nn.ConvTranspose2d(self.embedding_dim, self.ngf * 8, 4, 1, 0, bias=False),
nn.BatchNorm2d(self.ngf * 8),
nn.ReLU(True),
# state size. (ngf * 8) x 4 x 4
nn.ConvTranspose2d(self.ngf * 8, self.ngf * 4, 4, 2, 1, bias=False),
nn.BatchNorm2d(self.ngf * 4),
nn.ReLU(True),
# state size. (ngf * 4) x 8 x 8
nn.ConvTranspose2d(self.ngf * 4, self.ngf * 2, 4, 2, 1, bias=False),
nn.BatchNorm2d(self.ngf * 2),
nn.ReLU(True),
# state size. (ngf * 2) x 16 x 16
nn.ConvTranspose2d(self.ngf * 2, self.ngf * 1, 4, 2, 1, bias=False),
nn.BatchNorm2d(self.ngf * 1),
nn.ReLU(True),
# state size. (ngf * 1) x 32 x 32
nn.ConvTranspose2d(self.ngf * 1, self.nc, 4, 2, 1, bias=False),
nn.Tanh(),
# state size. (nc) x 64 x 64
)
def forward(self, input):
output = self.main(input)
return output
class _encoder(nn.Module):
def __init__(self, nc, ndf, embedding_dim):
super(_encoder, self).__init__()
self.nc = nc
self.ndf = ndf
self.embedding_dim = embedding_dim
self.main = nn.Sequential(
# input is (nc) x 64 x 64
nn.Conv2d(self.nc, self.ndf * 2, 4, 2, 1, bias=False),
nn.LeakyReLU(0.2, inplace=True),
# state size. (ndf * 2) x 32 x 32
nn.Conv2d(self.ndf * 2, self.ndf * 4, 4, 2, 1, bias=False),
nn.BatchNorm2d(self.ndf * 4),
nn.LeakyReLU(0.2, inplace=True),
# state size. (ndf * 4) x 16 x 16
nn.Conv2d(self.ndf * 4, self.ndf * 8, 4, 2, 1, bias=False),
nn.BatchNorm2d(self.ndf * 8),
nn.LeakyReLU(0.2, inplace=True),
# state size. (ndf * 4) x 8 x 8
nn.Conv2d(self.ndf * 8, self.ndf * 8, 4, 2, 1, bias=False),
nn.BatchNorm2d(self.ndf * 8),
nn.LeakyReLU(0.2, inplace=True),
# state size. (ndf * 8) x 4 x 4
nn.Conv2d(self.ndf * 8, self.embedding_dim, 4, 1, 0, bias=False),
# state size. (embedding_dim) x 1 x 1
)
def forward(self, input):
output = self.main(input)
# output = output.view(-1, self.embedding_dim)
return output