e93c88a1a372c9f64aff923dfc063523ab38db5d
[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__(self, height=6, width=8, nb_birds=3, speed=1, nb_iterations=4):
48         self.height = height
49         self.width = width
50         self.nb_birds = nb_birds
51         self.speed = speed
52         self.nb_iterations = nb_iterations
53
54     def direction_tokens(self):
55         return self.token_forward, self.token_backward
56
57     def generate_frame_sequences(self, nb):
58         frame_sequences = []
59
60         for _ in tqdm.tqdm(range(nb), dynamic_ncols=True, desc="world generation"):
61             result = torch.zeros(
62                 self.nb_iterations, self.height, self.width, dtype=torch.int64
63             )
64
65             i, j, vi, vj = (
66                 torch.empty(self.nb_birds, dtype=torch.int64),
67                 torch.empty(self.nb_birds, dtype=torch.int64),
68                 torch.empty(self.nb_birds, dtype=torch.int64),
69                 torch.empty(self.nb_birds, dtype=torch.int64),
70             )
71
72             col = (
73                 torch.randperm(self.colors.size(0) - 1)[: self.nb_birds].sort().values
74                 + 1
75             )
76
77             for n in range(self.nb_birds):
78                 while True:
79                     i[n] = torch.randint(self.height, (1,))
80                     j[n] = torch.randint(self.width, (1,))
81                     vm = torch.randint(4, (1,))
82                     vi[n], vj[n] = (vm % 2) * 2 - 1, (vm // 2) * 2 - 1
83                     if (
84                         i[n] - vi[n] >= 0
85                         and i[n] - vi[n] < self.height
86                         and j[n] - vj[n] >= 0
87                         and j[n] - vj[n] < self.width
88                     ):
89                         break
90
91             for l in range(self.nb_iterations):
92                 for n in range(self.nb_birds):
93                     c = col[n]
94                     result[l, i[n], j[n]] = c
95                     result[l, i[n] - vi[n], j[n]] = c
96                     result[l, i[n], j[n] - vj[n]] = c
97
98                     if (i[n] == 0 and vi[n] == -1) or (
99                         i[n] == self.height - 1 and vi[n] == 1
100                     ):
101                         vi[n] = -vi[n]
102
103                     if (j[n] == 0 and vj[n] == -1) or (
104                         j[n] == self.width - 1 and vj[n] == 1
105                     ):
106                         vj[n] = -vj[n]
107
108                     i[n] += vi[n]
109                     j[n] += vj[n]
110
111             frame_sequences.append(result)
112
113         return frame_sequences
114
115     def generate_token_sequences(self, nb):
116         frame_sequences = self.generate_frame_sequences(nb)
117
118         result = []
119
120         for frame_sequence in frame_sequences:
121             a = []
122             if torch.rand(1) < 0.5:
123                 for frame in frame_sequence:
124                     if len(a) > 0:
125                         a.append(torch.tensor([self.token_forward]))
126                     a.append(frame.flatten())
127             else:
128                 for frame in reversed(frame_sequence):
129                     if len(a) > 0:
130                         a.append(torch.tensor([self.token_backward]))
131                     a.append(frame.flatten())
132
133             result.append(torch.cat(a, dim=0)[None, :])
134
135         return torch.cat(result, dim=0)
136
137     ######################################################################
138
139     def frame2img(self, x, scale=15):
140         x = x.reshape(-1, self.height, self.width)
141         m = torch.logical_and(
142             x >= 0, x < self.first_bird_token + self.nb_bird_tokens
143         ).long()
144         x = self.colors[x * m].permute(0, 3, 1, 2)
145         s = x.shape
146         x = x[:, :, :, None, :, None].expand(-1, -1, -1, scale, -1, scale)
147         x = x.reshape(s[0], s[1], s[2] * scale, s[3] * scale)
148
149         x[:, :, :, torch.arange(0, x.size(3), scale)] = 0
150         x[:, :, torch.arange(0, x.size(2), scale), :] = 0
151         x = x[:, :, 1:, 1:]
152
153         for n in range(m.size(0)):
154             for i in range(m.size(1)):
155                 for j in range(m.size(2)):
156                     if m[n, i, j] == 0:
157                         for k in range(2, scale - 2):
158                             for l in [0, 1]:
159                                 x[n, :, i * scale + k, j * scale + k - l] = 0
160                                 x[
161                                     n, :, i * scale + scale - 1 - k, j * scale + k - l
162                                 ] = 0
163
164         return x
165
166     def seq2img(self, seq, scale=15):
167         all = [
168             self.frame2img(
169                 seq[:, : self.height * self.width].reshape(-1, self.height, self.width),
170                 scale,
171             )
172         ]
173
174         separator = torch.full((seq.size(0), 3, self.height * scale - 1, 1), 0)
175
176         t = self.height * self.width
177
178         while t < seq.size(1):
179             direction_tokens = seq[:, t]
180             t += 1
181
182             direction_images = self.colors[
183                 torch.full(
184                     (direction_tokens.size(0), self.height * scale - 1, scale), 0
185                 )
186             ].permute(0, 3, 1, 2)
187
188             for n in range(direction_tokens.size(0)):
189                 if direction_tokens[n] == self.token_forward:
190                     for k in range(scale):
191                         for l in [0, 1]:
192                             direction_images[
193                                 n,
194                                 :,
195                                 (self.height * scale) // 2 - scale // 2 + k - l,
196                                 3 + scale // 2 - abs(k - scale // 2),
197                             ] = 0
198                 elif direction_tokens[n] == self.token_backward:
199                     for k in range(scale):
200                         for l in [0, 1]:
201                             direction_images[
202                                 n,
203                                 :,
204                                 (self.height * scale) // 2 - scale // 2 + k - l,
205                                 3 + abs(k - scale // 2),
206                             ] = 0
207                 else:
208                     for k in range(2, scale - 2):
209                         for l in [0, 1]:
210                             direction_images[
211                                 n,
212                                 :,
213                                 (self.height * scale) // 2 - scale // 2 + k - l,
214                                 k,
215                             ] = 0
216                             direction_images[
217                                 n,
218                                 :,
219                                 (self.height * scale) // 2 - scale // 2 + k - l,
220                                 scale - 1 - k,
221                             ] = 0
222
223             all += [
224                 separator,
225                 direction_images,
226                 separator,
227                 self.frame2img(
228                     seq[:, t : t + self.height * self.width].reshape(
229                         -1, self.height, self.width
230                     ),
231                     scale,
232                 ),
233             ]
234
235             t += self.height * self.width
236
237         return torch.cat(all, dim=3)
238
239     def seq2str(self, seq):
240         result = []
241         for s in seq:
242             result.append("".join([self.token2char[v] for v in s]))
243         return result
244
245     def save_image(self, input, result_dir, filename):
246         img = self.seq2img(input.to("cpu"))
247         image_name = os.path.join(result_dir, filename)
248         torchvision.utils.save_image(img.float() / 255.0, image_name, nrow=6, padding=4)
249
250     def save_quizzes(self, input, result_dir, filename_prefix):
251         self.save_image(input, result_dir, filename_prefix + ".png")
252
253
254 ######################################################################
255
256 if __name__ == "__main__":
257     import time
258
259     sky = Sky(height=6, width=8, speed=1, nb_iterations=4)
260
261     start_time = time.perf_counter()
262     seq = sky.generate_frame_sequences(nb=64)
263     delay = time.perf_counter() - start_time
264     print(f"{seq.size(0)/delay:02f} seq/s")
265
266     # print(sky.seq2str(seq[:4]))
267
268     # for t in range(len(it[0])):
269     # img = torch.cat([sky.frame2img(f[t]) for f in it], dim=0)
270     # torchvision.utils.save_image(
271     # img.float() / 255.0,
272     # f"/tmp/frame_{t:03d}.png",
273     # nrow=8,
274     # padding=6,
275     # pad_value=0,
276     # )
277
278     # m = (torch.rand(seq.size()) < 0.05).long()
279     # seq = (1 - m) * seq + m * 23
280
281     print(seq.size())
282     img = sky.seq2img(seq)
283     print(img.size())
284
285     torchvision.utils.save_image(
286         img.float() / 255.0, "/tmp/world.png", nrow=6, padding=6, pad_value=0
287     )