11641853d8e8081f1cc7d0cf63d112f1ba30b518
[culture.git] / sky.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, sys, tqdm, os
9
10 import torch, torchvision
11
12 from torch import nn
13 from torch.nn import functional as F
14
15 ######################################################################
16
17 import problem
18
19
20 class Sky(problem.Problem):
21     colors = torch.tensor(
22         [
23             [255, 255, 255],
24             [255, 0, 0],
25             [0, 192, 0],
26             [0, 0, 255],
27             [255, 192, 0],
28             [0, 255, 255],
29             [255, 0, 255],
30             [192, 255, 192],
31             [255, 192, 192],
32             [192, 192, 255],
33             [192, 192, 192],
34         ]
35     )
36
37     token_background = 0
38     first_bird_token = 1
39     nb_bird_tokens = colors.size(0) - 1
40     token_forward = first_bird_token + nb_bird_tokens
41     token_backward = token_forward + 1
42
43     token2char = (
44         "_" + "".join([chr(ord("A") + n) for n in range(len(colors) - 1)]) + "><"
45     )
46
47     def __init__(
48         self,
49         height=6,
50         width=8,
51         nb_birds=3,
52         speed=2,
53         nb_iterations=2,
54         avoid_collision=True,
55     ):
56         self.height = height
57         self.width = width
58         self.nb_birds = nb_birds
59         self.speed = speed
60         self.nb_iterations = nb_iterations
61         self.avoid_collision = avoid_collision
62
63     def direction_tokens(self):
64         return self.token_forward, self.token_backward
65
66     def generate_frame_sequences(self, nb):
67         frame_sequences = []
68
69         for _ in tqdm.tqdm(range(nb), dynamic_ncols=True, desc="world generation"):
70             i, j, vi, vj = (
71                 torch.empty(self.nb_birds, dtype=torch.int64),
72                 torch.empty(self.nb_birds, dtype=torch.int64),
73                 torch.empty(self.nb_birds, dtype=torch.int64),
74                 torch.empty(self.nb_birds, dtype=torch.int64),
75             )
76
77             def collision_okay():
78                 if not self.avoid_collision:
79                     return True
80
81                 count = torch.zeros(self.height, self.width, dtype=torch.int64)
82
83                 for n in range(self.nb_birds):
84                     count[i[n], j[n]] += 1
85                     count[i[n] - vi[n], j[n]] += 1
86                     count[i[n], j[n] - vj[n]] += 1
87
88                 return count.max() <= 1
89
90             col = (
91                 torch.randperm(self.colors.size(0) - 1)[: self.nb_birds].sort().values
92                 + 1
93             )
94
95             while True:
96                 while True:
97                     for n in range(self.nb_birds):
98                         while True:
99                             i[n] = torch.randint(self.height, (1,))
100                             j[n] = torch.randint(self.width, (1,))
101                             vm = torch.randint(4, (1,))
102                             vi[n], vj[n] = (vm % 2) * 2 - 1, (vm // 2) * 2 - 1
103                             if (
104                                 i[n] - vi[n] >= 0
105                                 and i[n] - vi[n] < self.height
106                                 and j[n] - vj[n] >= 0
107                                 and j[n] - vj[n] < self.width
108                             ):
109                                 break
110
111                     if collision_okay():
112                         break
113
114                 result = torch.zeros(
115                     self.nb_iterations * self.speed,
116                     self.height,
117                     self.width,
118                     dtype=torch.int64,
119                 )
120
121                 fine = torch.empty(self.nb_iterations * self.speed)
122
123                 t_to_keep = (
124                     torch.arange(self.nb_iterations, device=result.device) * self.speed
125                 )
126
127                 for l in range(self.nb_iterations * self.speed):
128                     fine[l] = collision_okay()
129                     for n in range(self.nb_birds):
130                         c = col[n]
131                         result[l, i[n], j[n]] = c
132                         result[l, i[n] - vi[n], j[n]] = c
133                         result[l, i[n], j[n] - vj[n]] = c
134
135                         if (i[n] == 0 and vi[n] == -1) or (
136                             i[n] == self.height - 1 and vi[n] == 1
137                         ):
138                             vi[n] = -vi[n]
139
140                         if (j[n] == 0 and vj[n] == -1) or (
141                             j[n] == self.width - 1 and vj[n] == 1
142                         ):
143                             vj[n] = -vj[n]
144
145                         i[n] += vi[n]
146                         j[n] += vj[n]
147
148                 result = result[t_to_keep]
149                 fine = fine[t_to_keep]
150
151                 if fine[-1]:
152                     break
153
154             frame_sequences.append(result)
155
156         return frame_sequences
157
158     ######################################################################
159
160     def generate_token_sequences(self, nb):
161         frame_sequences = self.generate_frame_sequences(nb)
162
163         result = []
164
165         for frame_sequence in frame_sequences:
166             a = []
167             if torch.rand(1) < 0.5:
168                 for frame in frame_sequence:
169                     if len(a) > 0:
170                         a.append(torch.tensor([self.token_forward]))
171                     a.append(frame.flatten())
172             else:
173                 for frame in reversed(frame_sequence):
174                     if len(a) > 0:
175                         a.append(torch.tensor([self.token_backward]))
176                     a.append(frame.flatten())
177
178             result.append(torch.cat(a, dim=0)[None, :])
179
180         return torch.cat(result, dim=0)
181
182     ######################################################################
183
184     def frame2img(self, x, scale=15):
185         x = x.reshape(-1, self.height, self.width)
186         m = torch.logical_and(
187             x >= 0, x < self.first_bird_token + self.nb_bird_tokens
188         ).long()
189         x = self.colors[x * m].permute(0, 3, 1, 2)
190         s = x.shape
191         x = x[:, :, :, None, :, None].expand(-1, -1, -1, scale, -1, scale)
192         x = x.reshape(s[0], s[1], s[2] * scale, s[3] * scale)
193
194         x[:, :, :, torch.arange(0, x.size(3), scale)] = 0
195         x[:, :, torch.arange(0, x.size(2), scale), :] = 0
196         x = x[:, :, 1:, 1:]
197
198         for n in range(m.size(0)):
199             for i in range(m.size(1)):
200                 for j in range(m.size(2)):
201                     if m[n, i, j] == 0:
202                         for k in range(2, scale - 2):
203                             for l in [0, 1]:
204                                 x[n, :, i * scale + k, j * scale + k - l] = 0
205                                 x[
206                                     n, :, i * scale + scale - 1 - k, j * scale + k - l
207                                 ] = 0
208
209         return x
210
211     def seq2img(self, seq, scale=15):
212         all = [
213             self.frame2img(
214                 seq[:, : self.height * self.width].reshape(-1, self.height, self.width),
215                 scale,
216             )
217         ]
218
219         separator = torch.full((seq.size(0), 3, self.height * scale - 1, 1), 0)
220
221         t = self.height * self.width
222
223         while t < seq.size(1):
224             direction_tokens = seq[:, t]
225             t += 1
226
227             direction_images = self.colors[
228                 torch.full(
229                     (direction_tokens.size(0), self.height * scale - 1, scale), 0
230                 )
231             ].permute(0, 3, 1, 2)
232
233             for n in range(direction_tokens.size(0)):
234                 if direction_tokens[n] == self.token_forward:
235                     for k in range(scale):
236                         for l in [0, 1]:
237                             direction_images[
238                                 n,
239                                 :,
240                                 (self.height * scale) // 2 - scale // 2 + k - l,
241                                 3 + scale // 2 - abs(k - scale // 2),
242                             ] = 0
243                 elif direction_tokens[n] == self.token_backward:
244                     for k in range(scale):
245                         for l in [0, 1]:
246                             direction_images[
247                                 n,
248                                 :,
249                                 (self.height * scale) // 2 - scale // 2 + k - l,
250                                 3 + abs(k - scale // 2),
251                             ] = 0
252                 else:
253                     for k in range(2, scale - 2):
254                         for l in [0, 1]:
255                             direction_images[
256                                 n,
257                                 :,
258                                 (self.height * scale) // 2 - scale // 2 + k - l,
259                                 k,
260                             ] = 0
261                             direction_images[
262                                 n,
263                                 :,
264                                 (self.height * scale) // 2 - scale // 2 + k - l,
265                                 scale - 1 - k,
266                             ] = 0
267
268             all += [
269                 separator,
270                 direction_images,
271                 separator,
272                 self.frame2img(
273                     seq[:, t : t + self.height * self.width].reshape(
274                         -1, self.height, self.width
275                     ),
276                     scale,
277                 ),
278             ]
279
280             t += self.height * self.width
281
282         return torch.cat(all, dim=3)
283
284     def seq2str(self, seq):
285         result = []
286         for s in seq:
287             result.append("".join([self.token2char[v] for v in s]))
288         return result
289
290     def save_image(self, input, result_dir, filename):
291         img = self.seq2img(input.to("cpu"))
292         image_name = os.path.join(result_dir, filename)
293         torchvision.utils.save_image(img.float() / 255.0, image_name, nrow=6, padding=4)
294
295     def save_quizzes(self, input, result_dir, filename_prefix):
296         self.save_image(input, result_dir, filename_prefix + ".png")
297
298
299 ######################################################################
300
301 if __name__ == "__main__":
302     import time
303
304     sky = Sky(height=6, width=8, speed=4, nb_iterations=2)
305
306     start_time = time.perf_counter()
307     token_sequences = sky.generate_token_sequences(nb=64)
308     delay = time.perf_counter() - start_time
309     print(f"{token_sequences.size(0)/delay:02f} seq/s")
310
311     # print(sky.seq2str(seq[:4]))
312
313     # for t in range(len(it[0])):
314     # img = torch.cat([sky.frame2img(f[t]) for f in it], dim=0)
315     # torchvision.utils.save_image(
316     # img.float() / 255.0,
317     # f"/tmp/frame_{t:03d}.png",
318     # nrow=8,
319     # padding=6,
320     # pad_value=0,
321     # )
322
323     # m = (torch.rand(seq.size()) < 0.05).long()
324     # seq = (1 - m) * seq + m * 23
325
326     # print(seq.size())
327     img = sky.seq2img(token_sequences)
328     # print(img.size())
329
330     torchvision.utils.save_image(
331         img.float() / 255.0, "/tmp/world.png", nrow=6, padding=6, pad_value=0
332     )