-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathutils.py
128 lines (104 loc) · 3.94 KB
/
utils.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
import random
import os
import numpy as np
import torch
import torch.nn.functional as F
from tqdm.auto import tqdm
from torchvision.utils import save_image
def seed_torch(seed=2022):
random.seed(seed)
os.environ['PYTHONHASHSEED'] = str(seed)
np.random.seed(seed)
torch.manual_seed(seed)
torch.cuda.manual_seed(seed)
torch.cuda.manual_seed_all(seed)
torch.backends.cudnn.benchmark = False
torch.backends.cudnn.deterministic = True
def extract(a, t, x_shape):
batch_size = t.shape[0]
out = a.gather(-1, t.cpu())
return out.reshape(batch_size, *((1,) * (len(x_shape) - 1))).to(t.device)
# forward diffusion (using the nice property)
def q_sample(x_start, t, sqrt_one_minus_alphas_cumprod, sqrt_alphas_cumprod, noise=None):
if noise is None:
noise = torch.randn_like(x_start)
sqrt_alphas_cumprod_t = extract(sqrt_alphas_cumprod, t, x_start.shape)
sqrt_one_minus_alphas_cumprod_t = extract(
sqrt_one_minus_alphas_cumprod, t, x_start.shape
)
return sqrt_alphas_cumprod_t * x_start + sqrt_one_minus_alphas_cumprod_t * noise
def p_losses(denoise_model, x_start, x_Guide, t,
sqrt_one_minus_alphas_cumprod, sqrt_alphas_cumprod,
noise=None, loss_type="l1"):
if noise is None:
noise = torch.randn_like(x_start) # channel = 3, x0
x_noisy = q_sample(
x_start=x_start, t=t, noise=noise,
sqrt_one_minus_alphas_cumprod=sqrt_one_minus_alphas_cumprod,
sqrt_alphas_cumprod=sqrt_alphas_cumprod
)
x_to_model = torch.cat([x_Guide, x_noisy], dim=1) # channel = 6
predicted_noise = denoise_model(x_to_model, t)
if loss_type == 'l1':
loss = F.l1_loss(noise, predicted_noise)
elif loss_type == 'l2':
loss = F.mse_loss(noise, predicted_noise)
elif loss_type == "huber":
loss = F.smooth_l1_loss(noise, predicted_noise)
else:
raise NotImplementedError()
return loss
def p_sample(model, X_Guide,
x, t, t_index,
betas,
sqrt_one_minus_alphas_cumprod,
sqrt_recip_alphas,
posterior_variance):
betas_t = extract(betas, t, x.shape)
sqrt_one_minus_alphas_cumprod_t = extract(
sqrt_one_minus_alphas_cumprod, t, x.shape
)
sqrt_recip_alphas_t = extract(sqrt_recip_alphas, t, x.shape)
# Equation 11 in the paper
# Use our model (noise predictor) to predict the mean
with torch.no_grad():
model_mean = sqrt_recip_alphas_t * (
x - betas_t * model(torch.cat([X_Guide, x], dim=1), t) / sqrt_one_minus_alphas_cumprod_t
)
if t_index == 0:
return model_mean
else:
posterior_variance_t = extract(posterior_variance, t, x.shape)
noise = torch.randn_like(x)
# Algorithm 2 line 4:
return model_mean + torch.sqrt(posterior_variance_t) * noise
# Algorithm 2 (including returning all images)
def p_sample_loop(
result_path,
model, X_Guide,
shape, timesteps,
betas,
sqrt_one_minus_alphas_cumprod,
sqrt_recip_alphas,
posterior_variance):
device = next(model.parameters()).device
b = shape[0]
# start from pure noise (for each example in the batch)
img = torch.randn(shape, device=device)
for i in tqdm(reversed(range(0, timesteps)), desc='sampling loop time step', total=timesteps):
with torch.no_grad():
img = p_sample(
model,
X_Guide,
img,
torch.full((b,), i, device=device, dtype=torch.long),
i,
betas,
sqrt_one_minus_alphas_cumprod,
sqrt_recip_alphas,
posterior_variance
)
save_path = os.path.join(result_path, 'process{}.png'.format(i))
save_image(img, save_path, normalize=True )
#print(i, '--', img.max(), img.min())
return img