-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathinpaint.py
160 lines (138 loc) · 4.87 KB
/
inpaint.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
import math
from inspect import isfunction
from functools import partial
from pathlib import Path
import matplotlib.pyplot as plt
import seaborn as sns
from tqdm.auto import tqdm
from einops import rearrange
import torch
from torch import nn, einsum
import torch.nn.functional as F
import numpy as np
import pandas as pd
import xarray as xr
import build_dataset, training_datasets
from torch.utils.data import DataLoader
from torchvision import transforms
from torchvision.utils import save_image
from torch.optim import Adam
import datetime
import myutils
class REpaint:
def __init__(self, ddpm, gt, gt_keep_mask, resampling_steps=1):
self.ddpm=ddpm
self.gt = gt
self.gt_keep_mask = gt_keep_mask
self.resampling_steps = resampling_steps
@torch.no_grad()
def p_sample_paint(self, x, t, t_index):
# timestep parameters
betas_t = myutils.extract(self.ddpm.betas, t, x.shape)
sqrt_one_minus_alphas_cumprod_t = myutils.extract(
self.ddpm.sqrt_one_minus_alphas_cumprod, t, x.shape
)
sqrt_recip_alphas_t = myutils.extract(self.ddpm.sqrt_recip_alphas, t, x.shape)
posterior_variance_t = myutils.extract(self.ddpm.posterior_variance, t, x.shape)
for u in range(self.resampling_steps):
# RePaint algorithm, line 4 and 6
if t_index == 0:
epsilon = 0
z = 0
else:
epsilon = torch.randn_like(x)
z = torch.randn_like(x)
# RePaint algorithm, line 5
x_tminus1_known = (
1 / sqrt_recip_alphas_t * self.gt
+ sqrt_one_minus_alphas_cumprod_t ** 2 * epsilon
)
# RePaint algorithm, line 4 and 7
x_tminus1_unknow = (
sqrt_recip_alphas_t
* (x - betas_t * self.ddpm.model(x, t) / sqrt_one_minus_alphas_cumprod_t)
+ torch.sqrt(posterior_variance_t) * z
)
x_tminus1 = x_tminus1_known * self.gt_keep_mask + x_tminus1_unknow * (
1 - self.gt_keep_mask
) * 1 / torch.sqrt(1 - posterior_variance_t)
# TODO This is used for debug: return the full infered dynamics.
if t_index == 0:
x_tminus1 = x_tminus1_unknow * 1 / torch.sqrt(1 - posterior_variance_t)
if u < self.resampling_steps - 1 and (t > 1).all():
# taken from q_sample:
noise = torch.randn_like(x)
sqrt_alphas_cumprod_t = myutils.extract(
self.ddpm.sqrt_alphas_cumprod, t - 1, x.shape
)
sqrt_one_minus_alphas_cumprod_t = myutils.extract(
self.ddpm.sqrt_one_minus_alphas_cumprod, t - 1, x.shape
)
x = (
sqrt_alphas_cumprod_t * x_tminus1
+ sqrt_one_minus_alphas_cumprod_t * noise
)
return x_tminus1
@torch.no_grad()
def p_sample_loop_paint(self, shape):
device = next(self.ddpm.model.parameters()).device
b = shape[0]
# start from pure noise (for each example in the batch)
img = torch.randn(shape, device=device) # this is x_T
imgs = []
# t_T = self.timesteps
# jump_len = 3
# jump_n_sample = 3
# jumps = {}
# for j in range(0, t_T - jump_len, jump_len):
# jumps[j] = jump_n_sample - 1
# t = t_T
# ts = []
# while t >= 1:
# t = t-1
# ts.append(t)
# if jumps.get(t, 0) > 0:
# jumps[t] = jumps[t] - 1
# for _ in range(jump_len):
# t=t+1
# ts.append(t)
# ts.append(-1)
# for i in tqdm(ts, desc='sampling loop time step', total=self.timesteps):
for i in tqdm(
reversed(range(0, self.ddpm.timesteps)),
desc="sampling loop time step",
total=self.ddpm.timesteps,
):
img = self.p_sample_paint(
x=img,
t=torch.full(
(b,), i, device=device, dtype=torch.long
), # an array of size "self.batch_size" containting i
t_index=i,
)
imgs.append(img.cpu().numpy())
return imgs
@torch.no_grad()
def sample_paint(self):
return self.p_sample_loop_paint(
shape=(self.ddpm.batch_size, self.ddpm.channels, self.ddpm.image_size, self.ddpm.image_size),
)
# schedule with J
# t_T = ddpm1.timesteps
# jump_len = 5
# jump_n_sample = 5
# jumps = {}
# for j in range(0, t_T - jump_len, jump_len):
# jumps[j] = jump_n_sample - 1
# t = t_T
# ts = []
# while t >= 1:
# t = t-1
# ts.append(t)
# if jumps.get(t, 0) > 0:
# jumps[t] = jumps[t] - 1
# for _ in range(jump_len):
# t=t+1
# ts.append(t)
# ts.append(-1)
# plt.plot(ts)