Update.
[pytorch.git] / minidiffusion.py
1 #!/usr/bin/env python
2
3 # Any copyright is dedicated to the Public Domain.
4 # https://creativecommons.org/publicdomain/zero/1.0/
5
6 # Written by Francois Fleuret <francois@fleuret.org>
7
8 import math, argparse
9
10 import matplotlib.pyplot as plt
11
12 import torch, torchvision
13 from torch import nn
14
15 device = torch.device('cuda' if torch.cuda.is_available() else 'cpu')
16
17 ######################################################################
18
19 def sample_gaussian_mixture(nb):
20     p, std = 0.3, 0.2
21     result = torch.randn(nb, 1) * std
22     result = result + torch.sign(torch.rand(result.size()) - p) / 2
23     return result
24
25 def sample_ramp(nb):
26     result = torch.min(torch.rand(nb, 1), torch.rand(nb, 1))
27     return result
28
29 def sample_two_discs(nb):
30     a = torch.rand(nb) * math.pi * 2
31     b = torch.rand(nb).sqrt()
32     q = (torch.rand(nb) <= 0.5).long()
33     b = b * (0.3 + 0.2 * q)
34     result = torch.empty(nb, 2)
35     result[:, 0] = a.cos() * b - 0.5 + q
36     result[:, 1] = a.sin() * b - 0.5 + q
37     return result
38
39 def sample_disc_grid(nb):
40     a = torch.rand(nb) * math.pi * 2
41     b = torch.rand(nb).sqrt()
42     N = 4
43     q = (torch.randint(N, (nb,)) - (N - 1) / 2) / ((N - 1) / 2)
44     r = (torch.randint(N, (nb,)) - (N - 1) / 2) / ((N - 1) / 2)
45     b = b * 0.1
46     result = torch.empty(nb, 2)
47     result[:, 0] = a.cos() * b + q
48     result[:, 1] = a.sin() * b + r
49     return result
50
51 def sample_spiral(nb):
52     u = torch.rand(nb)
53     rho = u * 0.65 + 0.25 + torch.rand(nb) * 0.15
54     theta = u * math.pi * 3
55     result = torch.empty(nb, 2)
56     result[:, 0] = theta.cos() * rho
57     result[:, 1] = theta.sin() * rho
58     return result
59
60 def sample_mnist(nb):
61     train_set = torchvision.datasets.MNIST(root = './data/', train = True, download = True)
62     result = train_set.data[:nb].to(device).view(-1, 1, 28, 28).float()
63     return result
64
65 samplers = {
66     'gaussian_mixture': sample_gaussian_mixture,
67     'ramp': sample_ramp,
68     'two_discs': sample_two_discs,
69     'disc_grid': sample_disc_grid,
70     'spiral': sample_spiral,
71     'mnist': sample_mnist,
72 }
73
74 ######################################################################
75
76 parser = argparse.ArgumentParser(
77     description = '''A minimal implementation of Jonathan Ho, Ajay Jain, Pieter Abbeel
78 "Denoising Diffusion Probabilistic Models" (2020)
79 https://arxiv.org/abs/2006.11239''',
80
81     formatter_class = argparse.ArgumentDefaultsHelpFormatter
82 )
83
84 parser.add_argument('--seed',
85                     type = int, default = 0,
86                     help = 'Random seed, < 0 is no seeding')
87
88 parser.add_argument('--nb_epochs',
89                     type = int, default = 100,
90                     help = 'How many epochs')
91
92 parser.add_argument('--batch_size',
93                     type = int, default = 25,
94                     help = 'Batch size')
95
96 parser.add_argument('--nb_samples',
97                     type = int, default = 25000,
98                     help = 'Number of training examples')
99
100 parser.add_argument('--learning_rate',
101                     type = float, default = 1e-3,
102                     help = 'Learning rate')
103
104 parser.add_argument('--ema_decay',
105                     type = float, default = 0.9999,
106                     help = 'EMA decay, < 0 is no EMA')
107
108 data_list = ', '.join( [ str(k) for k in samplers ])
109
110 parser.add_argument('--data',
111                     type = str, default = 'gaussian_mixture',
112                     help = f'Toy data-set to use: {data_list}')
113
114 args = parser.parse_args()
115
116 if args.seed >= 0:
117     # torch.backends.cudnn.deterministic = True
118     # torch.backends.cudnn.benchmark = False
119     # torch.use_deterministic_algorithms(True)
120     torch.manual_seed(args.seed)
121     if torch.cuda.is_available():
122         torch.cuda.manual_seed_all(args.seed)
123
124 ######################################################################
125
126 class EMA:
127     def __init__(self, model, decay):
128         self.model = model
129         self.decay = decay
130         if self.decay < 0: return
131         self.ema = { }
132         with torch.no_grad():
133             for p in model.parameters():
134                 self.ema[p] = p.clone()
135
136     def step(self):
137         if self.decay < 0: return
138         with torch.no_grad():
139             for p in self.model.parameters():
140                 self.ema[p].copy_(self.decay * self.ema[p] + (1 - self.decay) * p)
141
142     def copy(self):
143         if self.decay < 0: return
144         with torch.no_grad():
145             for p in self.model.parameters():
146                 p.copy_(self.ema[p])
147
148 ######################################################################
149
150 class ConvNet(nn.Module):
151     def __init__(self, in_channels, out_channels):
152         super().__init__()
153
154         ks, nc = 5, 64
155
156         self.core = nn.Sequential(
157             nn.Conv2d(in_channels, nc, ks, padding = ks//2),
158             nn.ReLU(),
159             nn.Conv2d(nc, nc, ks, padding = ks//2),
160             nn.ReLU(),
161             nn.Conv2d(nc, nc, ks, padding = ks//2),
162             nn.ReLU(),
163             nn.Conv2d(nc, nc, ks, padding = ks//2),
164             nn.ReLU(),
165             nn.Conv2d(nc, nc, ks, padding = ks//2),
166             nn.ReLU(),
167             nn.Conv2d(nc, out_channels, ks, padding = ks//2),
168         )
169
170     def forward(self, x):
171         return self.core(x)
172
173 ######################################################################
174 # Data
175
176 try:
177     train_input = samplers[args.data](args.nb_samples).to(device)
178 except KeyError:
179     print(f'unknown data {args.data}')
180     exit(1)
181
182 train_mean, train_std = train_input.mean(), train_input.std()
183
184 ######################################################################
185 # Model
186
187 if train_input.dim() == 2:
188     nh = 256
189
190     model = nn.Sequential(
191         nn.Linear(train_input.size(1) + 1, nh),
192         nn.ReLU(),
193         nn.Linear(nh, nh),
194         nn.ReLU(),
195         nn.Linear(nh, nh),
196         nn.ReLU(),
197         nn.Linear(nh, train_input.size(1)),
198     )
199
200 elif train_input.dim() == 4:
201
202     model = ConvNet(train_input.size(1) + 1, train_input.size(1))
203
204 model.to(device)
205
206 print(f'nb_parameters {sum([ p.numel() for p in model.parameters() ])}')
207
208 ######################################################################
209 # Train
210
211 T = 1000
212 beta = torch.linspace(1e-4, 0.02, T, device = device)
213 alpha = 1 - beta
214 alpha_bar = alpha.log().cumsum(0).exp()
215 sigma = beta.sqrt()
216
217 ema = EMA(model, decay = args.ema_decay)
218
219 for k in range(args.nb_epochs):
220
221     acc_loss = 0
222     optimizer = torch.optim.Adam(model.parameters(), lr = args.learning_rate)
223
224     for x0 in train_input.split(args.batch_size):
225         x0 = (x0 - train_mean) / train_std
226         t = torch.randint(T, (x0.size(0),) + (1,) * (x0.dim() - 1), device = x0.device)
227         eps = torch.randn_like(x0)
228         input = torch.sqrt(alpha_bar[t]) * x0 + torch.sqrt(1 - alpha_bar[t]) * eps
229         input = torch.cat((input, t.expand_as(x0[:,:1]) / (T - 1) - 0.5), 1)
230         loss = (eps - model(input)).pow(2).mean()
231         acc_loss += loss.item() * x0.size(0)
232
233         optimizer.zero_grad()
234         loss.backward()
235         optimizer.step()
236
237         ema.step()
238
239     print(f'{k} {acc_loss / train_input.size(0)}')
240
241 ema.copy()
242
243 ######################################################################
244 # Generate
245
246 def generate(size, model):
247     with torch.no_grad():
248         x = torch.randn(size, device = device)
249
250         for t in range(T-1, -1, -1):
251             z = torch.zeros_like(x) if t == 0 else torch.randn_like(x)
252             input = torch.cat((x, torch.full_like(x[:,:1], t / (T - 1) - 0.5)), 1)
253             x = 1/torch.sqrt(alpha[t]) \
254                 * (x - (1-alpha[t]) / torch.sqrt(1-alpha_bar[t]) * model(input)) \
255                 + sigma[t] * z
256
257         x = x * train_std + train_mean
258
259         return x
260
261 ######################################################################
262 # Plot
263
264 model.eval()
265
266 if train_input.dim() == 2:
267     fig = plt.figure()
268     ax = fig.add_subplot(1, 1, 1)
269
270     if train_input.size(1) == 1:
271
272         x = generate((10000, 1), model)
273
274         ax.set_xlim(-1.25, 1.25)
275         ax.spines.right.set_visible(False)
276         ax.spines.top.set_visible(False)
277
278         d = train_input.flatten().detach().to('cpu').numpy()
279         ax.hist(d, 25, (-1, 1),
280                 density = True,
281                 histtype = 'stepfilled', color = 'lightblue', label = 'Train')
282
283         d = x.flatten().detach().to('cpu').numpy()
284         ax.hist(d, 25, (-1, 1),
285                 density = True,
286                 histtype = 'step', color = 'red', label = 'Synthesis')
287
288         ax.legend(frameon = False, loc = 2)
289
290     elif train_input.size(1) == 2:
291
292         x = generate((1000, 2), model)
293
294         ax.set_xlim(-1.5, 1.5)
295         ax.set_ylim(-1.5, 1.5)
296         ax.set(aspect = 1)
297         ax.spines.right.set_visible(False)
298         ax.spines.top.set_visible(False)
299
300         d = x.detach().to('cpu').numpy()
301         ax.scatter(d[:, 0], d[:, 1],
302                    s = 2.0, color = 'red', label = 'Synthesis')
303
304         d = train_input[:x.size(0)].detach().to('cpu').numpy()
305         ax.scatter(d[:, 0], d[:, 1],
306                    s = 2.0, color = 'gray', label = 'Train')
307
308         ax.legend(frameon = False, loc = 2)
309
310     filename = f'diffusion_{args.data}.pdf'
311     print(f'saving {filename}')
312     fig.savefig(filename, bbox_inches='tight')
313
314     if hasattr(plt.get_current_fig_manager(), 'window'):
315         plt.get_current_fig_manager().window.setGeometry(2, 2, 1024, 768)
316         plt.show()
317
318 elif train_input.dim() == 4:
319     x = generate((128,) + train_input.size()[1:], model)
320     x = 1 - x.clamp(min = 0, max = 255) / 255
321     torchvision.utils.save_image(x, f'diffusion_{args.data}.png', nrow = 16, pad_value = 0.8)
322
323 ######################################################################