-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathsample_odetexture.py
229 lines (191 loc) · 6.98 KB
/
sample_odetexture.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
import argparse
from datetime import datetime
import numpy as np
from PIL import Image
from copy import deepcopy
import os.path as opath
import jax, jax.numpy as jnp, diffrax
import nets_jax
from metrics_jax import pad2d_constant
pad2d_constant_batched = jax.jit(jax.vmap(pad2d_constant))
from utils_jax import seed_all, preprocess_exemplar, l2i, seed_batch, load_gif
import equinox as eqx
import os
import time
import subprocess
parser = argparse.ArgumentParser("ODE texture")
# sampling solver
parser.add_argument(
"--solver", type=str, default="heun", choices=["euler", "tsit5", "heun"]
)
parser.add_argument("--tol", type=float, default=1e-2)
parser.add_argument("--step_size", type=float, default=1e-2)
parser.add_argument("--n_aug_channels", type=int, default=9)
parser.add_argument("--dim", type=int, default=32)
parser.add_argument("--dim_mults", type=str, default="1,2,4")
parser.add_argument("--n_attn_heads", type=int, default=4)
parser.add_argument("--attn_head_dim", type=int, default=8)
parser.add_argument("--size", type=int, default=128)
parser.add_argument("--sample_size", type=int, default=256)
# sampling config
parser.add_argument("--exemplars_path", type=str)
parser.add_argument("--checkpoint_path", type=str)
parser.add_argument("--exp_path", type=str)
parser.add_argument("--comment", type=str, default="")
args = parser.parse_args()
if __name__ == "__main__":
# reproducibility
key = seed_all(42)
# jax.config.update("jax_enable_x64", True)
dtype = jnp.float32
# for recording
workspace = datetime.now().strftime("%Y_%m_%d_%H_%M_%S")
ws_path = opath.join(args.exp_path, workspace if not args.comment else args.comment)
if not opath.exists(ws_path):
os.makedirs(ws_path, exist_ok=True)
# load GIF
exemplars, time_between = load_gif(args.exemplars_path)
assert (
time_between == 0.05
), f"time between frames is {time_between}, but we expect 0.05"
n_frames = len(exemplars)
exemplars = [preprocess_exemplar(e, (args.size, args.size)) for e in exemplars]
## to numpy
exemplars_np = np.stack(
[np.array(e, dtype=dtype).transpose(2, 0, 1) / 255.0 for e in exemplars]
)
# create model
key, subkey = jax.random.split(key)
n_channels = 3 + args.n_aug_channels
odefunc = nets_jax.NDAE(
args.dim,
in_dim=n_channels,
out_dim=n_channels,
dim_mults=[int(m) for m in args.dim_mults.split(",")],
attn_heads=args.n_attn_heads,
attn_head_dim=args.attn_head_dim,
key=subkey,
)
if args.solver == "euler":
solver = diffrax.Euler()
dt0 = args.step_size
stepsize_controller = diffrax.ConstantStepSize()
elif args.solver == "tsit5":
solver = diffrax.Tsit5()
dt0 = None
stepsize_controller = diffrax.PIDController(
rtol=args.tol,
atol=args.tol,
pcoeff=0.0,
icoeff=1.0,
dcoeff=0,
)
elif args.solver == "heun":
solver = diffrax.Heun()
dt0 = None
stepsize_controller = diffrax.PIDController(
rtol=args.tol,
atol=args.tol,
pcoeff=0.0,
icoeff=1.0,
dcoeff=0,
)
else:
raise NotImplementedError(f"Solver {args.solver} not implemented")
diffeqsolve_args = {
"solver": solver,
"dt0": dt0,
"stepsize_controller": stepsize_controller,
}
reg_fns = ()
model = nets_jax.NeuralODE(odefunc, reg_fns)
checkpoint_path = args.checkpoint_path
model = eqx.tree_deserialise_leaves(opath.join(checkpoint_path, "model.eqx"), model)
to_rgb = lambda x: l2i(x[..., :3, :, :])
@eqx.filter_jit
def inference(key, model, n, size, diffeqsolve_args):
xs = seed_batch(key, n, n_channels, size)
solutions = model(
-syn_t, duration, xs, get_reg=False, key=None, **diffeqsolve_args
)
return solutions
## Duration of the input
duration = n_frames * time_between
## synthesis duration
syn_t = duration * 0.2
ts_synthesis = jnp.logspace(0.0, jnp.log10(1.0 + syn_t), 50) - 1.0 - syn_t
ts_transition = jnp.linspace(0.0, duration, n_frames)
ts_samples = jnp.concatenate([ts_synthesis, ts_transition])
sampling_args = deepcopy(diffeqsolve_args)
sampling_args.update({"saveat": diffrax.SaveAt(ts=ts_samples)})
def make_directory(dir):
dir_path = opath.join(ws_path, dir)
if not opath.exists(dir_path):
os.makedirs(dir_path, exist_ok=True)
return dir_path
def save_images(images_np, dir, duration=100):
frames = []
save_path = make_directory(dir)
for i, image in enumerate(images_np):
image = Image.fromarray(
(np.clip(image, a_min=0.0, a_max=1.0) * 255)
.astype(np.uint8)
.transpose(1, 2, 0)
)
image.save(opath.join(save_path, f"frame_{i:04d}.png"))
frames.append(image)
frames[0].save(
opath.join(save_path, f"{dir}.gif"),
save_all=True,
append_images=frames[1:],
duration=duration,
loop=0,
)
ffmpeg_command = [
"ffmpeg",
"-y",
"-i",
f"{opath.join(save_path, dir)}.gif",
"-c:v",
"rawvideo",
"-r",
f"{1000 / duration}",
f"{opath.join(save_path, dir)}.avi",
]
subprocess.run(
ffmpeg_command,
check=True,
stdout=subprocess.DEVNULL,
stderr=subprocess.DEVNULL,
)
_ = inference(key, model, 1, args.sample_size, sampling_args)
print("----------------------------------------------------")
print(
f"Start sampling with {args.solver} solver [atol={args.tol}, rtol={args.tol}, stepsize={args.step_size}]..."
)
start_time = time.time()
solutions = inference(key, model, 1, args.sample_size, sampling_args)
_ = solutions.ys["x"].block_until_ready()
cur_time = time.time()
print(f"Sampling finished in {cur_time - start_time:.4f} seconds.")
start_time = cur_time
images = to_rgb(solutions.ys["x"])
images_synthesis = np.array(
images[: len(ts_synthesis), ...].transpose(1, 0, 2, 3, 4)
)[0]
images_transition = np.array(
images[len(ts_synthesis) :, ...].transpose(1, 0, 2, 3, 4)
)[0]
padded_exemplars = np.zeros(images_transition.shape)
padded_exemplars[..., : args.size, : args.size] = exemplars_np
save_images(images_synthesis, "synthesis")
save_images(images_transition, "transition", time_between * 1000)
save_images(exemplars_np, "exemplars", time_between * 1000)
save_images(
np.concatenate([images_transition, padded_exemplars], axis=3),
"comparisons",
time_between * 1000,
)
cur_time = time.time()
print(f"Saving images finished in {cur_time - start_time:.4f} seconds.")
print("----------------------------------------------------")