3 # Any copyright is dedicated to the Public Domain.
4 # https://creativecommons.org/publicdomain/zero/1.0/
6 # Written by Francois Fleuret <francois@fleuret.org>
10 import matplotlib.pyplot as plt
12 import torch, torchvision
15 device = torch.device('cuda' if torch.cuda.is_available() else 'cpu')
17 print(f'device {device}')
19 ######################################################################
21 def sample_gaussian_mixture(nb):
23 result = torch.randn(nb, 1) * std
24 result = result + torch.sign(torch.rand(result.size()) - p) / 2
28 result = torch.min(torch.rand(nb, 1), torch.rand(nb, 1))
31 def sample_two_discs(nb):
32 a = torch.rand(nb) * math.pi * 2
33 b = torch.rand(nb).sqrt()
34 q = (torch.rand(nb) <= 0.5).long()
35 b = b * (0.3 + 0.2 * q)
36 result = torch.empty(nb, 2)
37 result[:, 0] = a.cos() * b - 0.5 + q
38 result[:, 1] = a.sin() * b - 0.5 + q
41 def sample_disc_grid(nb):
42 a = torch.rand(nb) * math.pi * 2
43 b = torch.rand(nb).sqrt()
45 q = (torch.randint(N, (nb,)) - (N - 1) / 2) / ((N - 1) / 2)
46 r = (torch.randint(N, (nb,)) - (N - 1) / 2) / ((N - 1) / 2)
48 result = torch.empty(nb, 2)
49 result[:, 0] = a.cos() * b + q
50 result[:, 1] = a.sin() * b + r
53 def sample_spiral(nb):
55 rho = u * 0.65 + 0.25 + torch.rand(nb) * 0.15
56 theta = u * math.pi * 3
57 result = torch.empty(nb, 2)
58 result[:, 0] = theta.cos() * rho
59 result[:, 1] = theta.sin() * rho
63 train_set = torchvision.datasets.MNIST(root = './data/', train = True, download = True)
64 result = train_set.data[:nb].to(device).view(-1, 1, 28, 28).float()
68 'gaussian_mixture': sample_gaussian_mixture,
70 'two_discs': sample_two_discs,
71 'disc_grid': sample_disc_grid,
72 'spiral': sample_spiral,
73 'mnist': sample_mnist,
76 ######################################################################
78 parser = argparse.ArgumentParser(
79 description = '''A minimal implementation of Jonathan Ho, Ajay Jain, Pieter Abbeel
80 "Denoising Diffusion Probabilistic Models" (2020)
81 https://arxiv.org/abs/2006.11239''',
83 formatter_class = argparse.ArgumentDefaultsHelpFormatter
86 parser.add_argument('--seed',
87 type = int, default = 0,
88 help = 'Random seed, < 0 is no seeding')
90 parser.add_argument('--nb_epochs',
91 type = int, default = 100,
92 help = 'How many epochs')
94 parser.add_argument('--batch_size',
95 type = int, default = 25,
98 parser.add_argument('--nb_samples',
99 type = int, default = 25000,
100 help = 'Number of training examples')
102 parser.add_argument('--learning_rate',
103 type = float, default = 1e-3,
104 help = 'Learning rate')
106 parser.add_argument('--ema_decay',
107 type = float, default = 0.9999,
108 help = 'EMA decay, <= 0 is no EMA')
110 data_list = ', '.join( [ str(k) for k in samplers ])
112 parser.add_argument('--data',
113 type = str, default = 'gaussian_mixture',
114 help = f'Toy data-set to use: {data_list}')
116 args = parser.parse_args()
119 # torch.backends.cudnn.deterministic = True
120 # torch.backends.cudnn.benchmark = False
121 # torch.use_deterministic_algorithms(True)
122 torch.manual_seed(args.seed)
123 if torch.cuda.is_available():
124 torch.cuda.manual_seed_all(args.seed)
126 ######################################################################
129 def __init__(self, model, decay):
133 with torch.no_grad():
134 for p in model.parameters():
135 self.mem[p] = p.clone()
138 with torch.no_grad():
139 for p in self.model.parameters():
140 self.mem[p].copy_(self.decay * self.mem[p] + (1 - self.decay) * p)
142 def copy_to_model(self):
143 with torch.no_grad():
144 for p in self.model.parameters():
147 ######################################################################
149 class ConvNet(nn.Module):
150 def __init__(self, in_channels, out_channels):
155 self.core = nn.Sequential(
156 nn.Conv2d(in_channels, nc, ks, padding = ks//2),
158 nn.Conv2d(nc, nc, ks, padding = ks//2),
160 nn.Conv2d(nc, nc, ks, padding = ks//2),
162 nn.Conv2d(nc, nc, ks, padding = ks//2),
164 nn.Conv2d(nc, nc, ks, padding = ks//2),
166 nn.Conv2d(nc, out_channels, ks, padding = ks//2),
169 def forward(self, x):
172 ######################################################################
176 train_input = samplers[args.data](args.nb_samples).to(device)
178 print(f'unknown data {args.data}')
181 train_mean, train_std = train_input.mean(), train_input.std()
183 ######################################################################
186 if train_input.dim() == 2:
189 model = nn.Sequential(
190 nn.Linear(train_input.size(1) + 1, nh),
196 nn.Linear(nh, train_input.size(1)),
199 elif train_input.dim() == 4:
201 model = ConvNet(train_input.size(1) + 1, train_input.size(1))
205 print(f'nb_parameters {sum([ p.numel() for p in model.parameters() ])}')
207 ######################################################################
210 def generate(size, alpha, alpha_bar, sigma, model, train_mean, train_std):
212 with torch.no_grad():
214 x = torch.randn(size, device = device)
216 for t in range(T-1, -1, -1):
217 z = torch.zeros_like(x) if t == 0 else torch.randn_like(x)
218 input = torch.cat((x, torch.full_like(x[:,:1], t / (T - 1) - 0.5)), 1)
219 x = 1/torch.sqrt(alpha[t]) \
220 * (x - (1-alpha[t]) / torch.sqrt(1-alpha_bar[t]) * model(input)) \
223 x = x * train_std + train_mean
227 ######################################################################
231 beta = torch.linspace(1e-4, 0.02, T, device = device)
233 alpha_bar = alpha.log().cumsum(0).exp()
236 ema = EMA(model, decay = args.ema_decay) if args.ema_decay > 0 else None
238 for k in range(args.nb_epochs):
241 optimizer = torch.optim.Adam(model.parameters(), lr = args.learning_rate)
243 for x0 in train_input.split(args.batch_size):
244 x0 = (x0 - train_mean) / train_std
245 t = torch.randint(T, (x0.size(0),) + (1,) * (x0.dim() - 1), device = x0.device)
246 eps = torch.randn_like(x0)
247 xt = torch.sqrt(alpha_bar[t]) * x0 + torch.sqrt(1 - alpha_bar[t]) * eps
248 input = torch.cat((xt, t.expand_as(x0[:,:1]) / (T - 1) - 0.5), 1)
249 loss = (eps - model(input)).pow(2).mean()
250 acc_loss += loss.item() * x0.size(0)
252 optimizer.zero_grad()
256 if ema is not None: ema.step()
258 print(f'{k} {acc_loss / train_input.size(0)}')
260 if ema is not None: ema.copy_to_model()
262 ######################################################################
267 if train_input.dim() == 2:
270 ax = fig.add_subplot(1, 1, 1)
273 if train_input.size(1) == 1:
275 x = generate((10000, 1), alpha, alpha_bar, sigma,
276 model, train_mean, train_std)
278 ax.set_xlim(-1.25, 1.25)
279 ax.spines.right.set_visible(False)
280 ax.spines.top.set_visible(False)
282 d = train_input.flatten().detach().to('cpu').numpy()
283 ax.hist(d, 25, (-1, 1),
285 histtype = 'stepfilled', color = 'lightblue', label = 'Train')
287 d = x.flatten().detach().to('cpu').numpy()
288 ax.hist(d, 25, (-1, 1),
290 histtype = 'step', color = 'red', label = 'Synthesis')
292 ax.legend(frameon = False, loc = 2)
294 # Nx2 -> scatter plot
295 elif train_input.size(1) == 2:
297 x = generate((1000, 2), alpha, alpha_bar, sigma,
298 model, train_mean, train_std)
300 ax.set_xlim(-1.5, 1.5)
301 ax.set_ylim(-1.5, 1.5)
303 ax.spines.right.set_visible(False)
304 ax.spines.top.set_visible(False)
306 d = x.detach().to('cpu').numpy()
307 ax.scatter(d[:, 0], d[:, 1],
308 s = 2.0, color = 'red', label = 'Synthesis')
310 d = train_input[:x.size(0)].detach().to('cpu').numpy()
311 ax.scatter(d[:, 0], d[:, 1],
312 s = 2.0, color = 'gray', label = 'Train')
314 ax.legend(frameon = False, loc = 2)
316 filename = f'diffusion_{args.data}.pdf'
317 print(f'saving {filename}')
318 fig.savefig(filename, bbox_inches='tight')
320 if hasattr(plt.get_current_fig_manager(), 'window'):
321 plt.get_current_fig_manager().window.setGeometry(2, 2, 1024, 768)
325 elif train_input.dim() == 4:
327 x = generate((128,) + train_input.size()[1:], alpha, alpha_bar, sigma,
328 model, train_mean, train_std)
329 x = 1 - x.clamp(min = 0, max = 255) / 255
330 torchvision.utils.save_image(x, f'diffusion_{args.data}.png', nrow = 16, pad_value = 0.8)
332 ######################################################################