-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdebug_script.py
More file actions
78 lines (57 loc) · 1.74 KB
/
debug_script.py
File metadata and controls
78 lines (57 loc) · 1.74 KB
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
# %%
import torch
from py_source.sampling.repaint import repaint_svd
from py_source.sampling.epsilon_net import EpsilonNetSVD
from py_source.utils import load_epsilon_net, load_image, display_image
device = "cuda:0"
torch.set_default_device(device)
# load the image
img_path = "./material/ffhq_img/00018.png"
x_origin = load_image(img_path, device)
# load the degradation operator
path_operator = f"./material/degradation_operators/outpainting_half.pt"
degradation_operator = torch.load(path_operator, map_location=device)
# apply degradation operator
y = degradation_operator.H(x_origin[None])
y = y.squeeze(0)
# add noise
sigma = 0.01
y = y + sigma * torch.randn_like(y)
inverse_problem = (y, degradation_operator, sigma)
# %%
# load model with 500 diffusion steps
n_steps = 300
eps_net = load_epsilon_net("ffhq", n_steps, device)
eps_net_svd = EpsilonNetSVD(
net=eps_net.net,
alphas_cumprod=eps_net.alphas_cumprod,
timesteps=eps_net.timesteps,
H_func=degradation_operator,
device=device,
)
# solve problem
initial_noise = torch.randn((1, 3, 256, 256), device=device)
reconstruction = repaint_svd(initial_noise, inverse_problem, eps_net_svd)
# %%
display_image(reconstruction[0])
# %%
reconstruction.shape
# %%
# plot results
import math
import matplotlib.pyplot as plt
# reshape y
n_channels = 3
n_pixel_per_channel = y.shape[0] // n_channels
hight = width = int(math.sqrt(n_pixel_per_channel))
y_reshaped = y.reshape(n_channels, hight, width)
# init figure
fig, axes = plt.subplots(1, 3)
images = (x_origin, y_reshaped, reconstruction[0])
titles = ("original", "degraded", "reconstruction")
# display figures
for ax, img, title in zip(axes, images, titles):
display_image(img, ax)
ax.set_title(title)
fig.tight_layout()
# %%