-
Notifications
You must be signed in to change notification settings - Fork 1
/
manifold.py
174 lines (150 loc) · 4.16 KB
/
manifold.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
import numpy as np
import torch as th
from custom import Builder
from vq import VAE, VQVAE
device = th.device("cuda" if th.cuda.is_available() else "cpu")
def encoder(
in_ch,
hid_dim,
emb_dim,
in_size,
out_size,
nbf=128,
n_res=2,
res_dim=32,
conv="Conv2dRelu",
res="ResidualReluStack",
):
modules = [["Bnorm2d", [in_ch]]]
n_pool = int(np.log2(in_size / out_size))
in_ch = [in_ch, nbf // 2] + [nbf] * (n_pool - 2)
out_ch = in_ch[1:] + [nbf]
for ic, oc in zip(in_ch, out_ch):
modules.append([conv, [ic, oc, 4, 2, 1]])
modules.append([res, [nbf, hid_dim, n_res, res_dim]])
modules.append(["Conv2d", [hid_dim, emb_dim, 1, 1, 0]])
return modules
def decoder(
out_ch,
hid_dim,
emb_dim,
in_size,
out_size,
nbf=128,
n_res=2,
res_dim=32,
conv="TConv2dRelu",
res="ResidualReluStack",
):
n_up = int(np.log2(out_size / in_size))
in_chs = [out_ch, nbf // 2] + [nbf] * (n_up - 2)
out_chs = in_chs[1:] + [nbf]
modules = [
["Conv2d", [emb_dim, hid_dim, 3, 1, 1]],
[res, [hid_dim, nbf, n_res, res_dim]],
]
for ic, oc in zip(out_chs[::-1][:-1], in_chs[::-1][:-1]):
modules.append([conv, [ic, oc, 4, 2, 1]])
modules.append(["TConv2d", [nbf // 2, out_ch, 4, 2, 1]])
return modules
class Manifold(th.nn.Module):
def __init__(self, in_ch, out_ch, in_size, out_size, ckpt_f=None):
super().__init__()
ckpt = th.load(ckpt_f, map_location=th.device("cpu"))
self.vq = VQVAE(
Builder(
encoder(
in_ch,
ckpt["hid_dim"],
ckpt["emb_dim"],
in_size,
out_size,
)
),
Builder(
decoder(
out_ch,
ckpt["hid_dim"],
ckpt["emb_dim"],
out_size,
in_size,
)
),
ckpt["emb_num"],
ckpt["emb_dim"],
)
if ckpt_f:
self.vq.load_state_dict(ckpt["model"])
self.vq.eval()
@th.no_grad()
def indices(self, x):
o = self.vq._vq_vae(self.vq._enc(x))[-1]
return o
@th.no_grad()
def encode(self, x):
o = self.vq._enc(x)
return o
@th.no_grad()
def decode(self, x):
# o = self.vq.test(x)
o = self.vq._vq_vae(x)[1]
o = self.vq._dec(o)
return o
def decode_with_gradients(self, x):
# o = self.vq.test(x)
o = self.vq._vq_vae(x)[1]
o = self.vq._dec(o)
return o
class VAEManifold(th.nn.Module):
def __init__(self, in_ch, out_ch, in_size, out_size, ckpt_f=None):
super().__init__()
ckpt = th.load(ckpt_f, map_location=th.device("cpu"))
self.vq = VAE(
Builder(
encoder(
in_ch,
ckpt["hid_dim"],
ckpt["emb_dim"],
in_size,
out_size,
)
),
Builder(
decoder(
out_ch,
ckpt["hid_dim"],
ckpt["emb_dim"],
out_size,
in_size,
)
),
ckpt["emb_num"],
ckpt["emb_dim"],
)
if ckpt_f:
self.vq.load_state_dict(ckpt["model"])
self.vq.eval()
@th.no_grad()
def indices(self, x):
raise NotImplementedError
@th.no_grad()
def encode(self, x):
o = self.vq._enc(x)
return o
@th.no_grad()
def decode(self, x):
# o = self.vq.test(x)
# o = self.vq._vq_vae(x)[1]
o = self.vq._dec(x)
return o
class Manifolds(th.nn.Module):
def __init__(self, **kwargs):
super().__init__()
self.main = {k: Manifold(v) for k, v in kwargs.items()}
def indices(self, **kwargs):
out = {
k: self.main[k].indices(v) for k, v in kwargs.items() if v.numel()
}
return out
def decode(self, **kwargs):
pass