-
Notifications
You must be signed in to change notification settings - Fork 0
/
ddpm.py
197 lines (162 loc) · 7.36 KB
/
ddpm.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
import os
from typing import List
import torch
import torch.nn as nn
from matplotlib import pyplot as plt
import numpy as np
from tqdm import tqdm
from torch import optim
from utils import *
from modules import UNet
from guided_diffusion.script_util import create_model
from guided_diffusion.solverDIP import *
from guided_diffusion.gaussian_diffusion import get_named_beta_schedule
import logging
from torch.utils.tensorboard import SummaryWriter
logging.basicConfig(format="%(asctime)s - %(levelname)s: %(message)s", level=logging.INFO, datefmt="%I:%M:%S")
class Diffusion:
def __init__(self, noise_steps=1000, beta_start=1e-4, beta_end=0.02, img_size=256, device="cuda"):
self.noise_steps = noise_steps
self.beta_start = beta_start
self.beta_end = beta_end
self.img_size = img_size
self.device = device
self.beta = self.prepare_noise_schedule(schedule_name='cosine').to(device)
self.alpha = 1. - self.beta
self.alpha_hat = torch.cumprod(self.alpha, dim=0)
def prepare_noise_schedule(self,schedule_name='linear'):
#return torch.linspace(self.beta_start, self.beta_end, self.noise_steps) ////betas = get_named_beta_schedule('cosine',1000,0.02)
if schedule_name=='cosine':
return torch.tensor(get_named_beta_schedule('cosine',self.noise_steps,self.beta_end).copy(),dtype=torch.float32)
if schedule_name=='linear':
return torch.linspace(self.beta_start, self.beta_end, self.noise_steps)
def noise_images(self, x, t):
sqrt_alpha_hat = torch.sqrt(self.alpha_hat[t])[:, None, None, None]
sqrt_one_minus_alpha_hat = torch.sqrt(1 - self.alpha_hat[t])[:, None, None, None]
Ɛ = torch.randn_like(x)
return sqrt_alpha_hat * x + sqrt_one_minus_alpha_hat * Ɛ, Ɛ
def sample_timesteps(self, n):
return torch.randint(low=1, high=self.noise_steps, size=(n,))
def sample(self, model, n):
logging.info(f"Sampling {n} new images....")
model.eval()
with torch.no_grad():
x = torch.randn((n, 3, self.img_size, self.img_size)).to(self.device)
for i in tqdm(reversed(range(1, self.noise_steps)), position=0):
t = (torch.ones(n) * i).long().to(self.device)
predicted_noise = model(x, t)
alpha = self.alpha[t][:, None, None, None]
alpha_hat = self.alpha_hat[t][:, None, None, None]
beta = self.beta[t][:, None, None, None]
if i > 1:
noise = torch.randn_like(x)
else:
noise = torch.zeros_like(x)
x = 1 / torch.sqrt(alpha) * (x - ((1 - alpha) / (torch.sqrt(1 - alpha_hat))) * predicted_noise) + torch.sqrt(beta) * noise
model.train()
x = (x.clamp(-1, 1) + 1) / 2
x = (x * 255).type(torch.uint8)
return x
def sample_single(self, model):
n=1
logging.info(f"Sampling {n} new images with diffusion process....")
model.eval()
progress_img = []
with torch.no_grad():
x = torch.randn((n, 3, self.img_size, self.img_size)).to(self.device)
for i in tqdm(reversed(range(1, self.noise_steps)), position=0):
t = (torch.ones(n) * i).long().to(self.device)
predicted_noise = model(x, t)
alpha = self.alpha[t][:, None, None, None]
alpha_hat = self.alpha_hat[t][:, None, None, None]
beta = self.beta[t][:, None, None, None]
if i > 1:
noise = torch.randn_like(x)
else:
noise = torch.zeros_like(x)
x = 1 / torch.sqrt(alpha) * (x - ((1 - alpha) / (torch.sqrt(1 - alpha_hat))) * predicted_noise) + torch.sqrt(beta) * noise
if i%20==0:
progress_img.append(x)
model.train()
result = torch.cat(progress_img, dim=0)
result = (result.clamp(-1, 1) + 1) / 2
result = (result * 255).type(torch.uint8)
return result
def resample_single(self, model,i,x):
#logging.info(f"Sampling diffusion step {i} ")
model.eval()
n=1
with torch.no_grad():
t = (torch.ones(n) * i).long().to(self.device)
predicted_noise = model(x, t)
alpha = self.alpha[t]
alpha_hat = self.alpha_hat[t]
beta = self.beta[t]
if i > 1:
noise = torch.randn_like(x)
else:
noise = torch.zeros_like(x)
x = 1 / torch.sqrt(alpha) * (x - ((1 - alpha) / (torch.sqrt(1 - alpha_hat))) * predicted_noise) + torch.sqrt(beta) * noise
#x = (x.clamp(-1, 1) + 1) / 2
#x = (x * 255).type(torch.uint8)
return x
def train(args):
setup_logging(args.run_name)
device = args.device
dataloader = get_data(args)
#model = UNet().to(device)
model= create_model(image_size=128,num_channels=64,num_res_blocks=3)
model = model.to(device)
ckpt = torch.load("models/DDPM_cosine_27K/ckpt.pt") #only if u have a pre-trained model
model.load_state_dict(ckpt) #only if u have a pre-trained model
optimizer = optim.AdamW(model.parameters(), lr=args.lr)
mse = nn.MSELoss()
diffusion = Diffusion(img_size=args.image_size, device=device)
logger = SummaryWriter(os.path.join("runs", args.run_name))
l = len(dataloader)
for epoch in range(args.epochs):
logging.info(f"Starting epoch {epoch+1}/{args.epochs}:")
pbar = tqdm(dataloader)
for i, (images, _) in enumerate(pbar):
images = images.to(device)
#nt=100 # number of t's during the forward diffusion
t = diffusion.sample_timesteps(images.shape[0]).to(device)
x_t, noise = diffusion.noise_images(images, t)
predicted_noise = model(x_t, t)
loss = mse(noise, predicted_noise)
optimizer.zero_grad()
loss.backward()
optimizer.step()
pbar.set_postfix(MSE=loss.item())
logger.add_scalar("MSE", loss.item(), global_step=epoch * l + i)
if epoch%100==0:
print('saving images')
sampled_images = diffusion.sample(model, n=10)
save_images(sampled_images, os.path.join("results", args.run_name, f"{epoch+1+1000}.jpg"))
torch.save(model.state_dict(), os.path.join("models", args.run_name, f"ckpt.pt"))
def launch():
import argparse
parser = argparse.ArgumentParser()
args = parser.parse_args()
args.run_name = "DDPM_cosine_27K"
args.epochs = 1000
args.batch_size = 58
args.image_size = 128
args.dataset_path = "seismic27K"
args.device = "cuda"
args.lr = 3e-4
train(args)
if __name__ == '__main__':
launch()
# device = "cuda"
# model = UNet().to(device)
# ckpt = torch.load("./working/orig/ckpt.pt")
# model.load_state_dict(ckpt)
# diffusion = Diffusion(img_size=64, device=device)
# x = diffusion.sample(model, 8)
# print(x.shape)
# plt.figure(figsize=(32, 32))
# plt.imshow(torch.cat([
# torch.cat([i for i in x.cpu()], dim=-1),
# ], dim=-2).permute(1, 2, 0).cpu())
# plt.show()