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 torch, torchvision
13 from torch.nn import functional as F
15 ######################################################################
18 colors = torch.tensor(
51 nb_bird_tokens = colors.size(0) - 1
52 token_forward = first_bird_token + nb_bird_tokens
53 token_backward = token_forward + 1
55 token2char = "_" + "".join([chr(ord("A") + n) for n in range(len(colors) - 1)]) + "><"
67 for n in tqdm.tqdm(range(nb), dynamic_ncols=True, desc="world generation"):
68 f_start = torch.zeros(height, width, dtype=torch.int64)
69 f_end = torch.zeros(height, width, dtype=torch.int64)
70 n = torch.arange(f_start.size(0))
73 (torch.randperm(nb_bird_tokens) + first_bird_token)[:nb_birds].sort().values
76 torch.randint(height - 2, (1,))[0] + 1,
77 torch.randint(width - 2, (1,))[0] + 1,
79 vm = torch.randint(4, (1,))[0]
80 vi, vj = (vm // 2) * (2 * (vm % 2) - 1), (1 - vm // 2) * (2 * (vm % 2) - 1)
83 f_start[i - vi, j - vj] = c
84 f_start[i + vj, j - vi] = c
85 f_start[i - vj, j + vi] = c
87 for l in range(nb_iterations):
90 if i < 0 or i >= height or j < 0 or j >= width:
98 f_end[i - vi, j - vj] = c
99 f_end[i + vj, j - vi] = c
100 f_end[i - vj, j + vi] = c
102 pairs.append((f_start, f_end))
106 if torch.rand(1) < 0.5:
109 [p[0].flatten(), torch.tensor([token_forward]), p[1].flatten()],
116 [p[1].flatten(), torch.tensor([token_backward]), p[0].flatten()],
121 return torch.cat(result, dim=0)
124 def sample2img(seq, height, width, upscale=15):
125 f_first = seq[:, : height * width].reshape(-1, height, width)
126 f_second = seq[:, height * width + 1 :].reshape(-1, height, width)
127 direction = seq[:, height * width]
129 def mosaic(x, upscale):
130 x = x.reshape(-1, height, width)
131 m = torch.logical_and(x >= 0, x < first_bird_token + nb_bird_tokens).long()
132 x = colors[x * m].permute(0, 3, 1, 2)
134 x = x[:, :, :, None, :, None].expand(-1, -1, -1, upscale, -1, upscale)
135 x = x.reshape(s[0], s[1], s[2] * upscale, s[3] * upscale)
137 x[:, :, :, torch.arange(0, x.size(3), upscale)] = 0
138 x[:, :, torch.arange(0, x.size(2), upscale), :] = 0
141 for n in range(m.size(0)):
142 for i in range(m.size(1)):
143 for j in range(m.size(2)):
145 for k in range(2, upscale - 2):
146 x[n, :, i * upscale + k, j * upscale + k] = 0
147 x[n, :, i * upscale + upscale - 1 - k, j * upscale + k] = 0
151 direction_symbol = torch.full((direction.size(0), height * upscale - 1, upscale), 0)
152 direction_symbol = colors[direction_symbol].permute(0, 3, 1, 2)
153 separator = torch.full((direction.size(0), 3, height * upscale - 1, 1), 0)
155 for n in range(direction_symbol.size(0)):
156 if direction[n] == token_forward:
157 for k in range(upscale):
161 (height * upscale) // 2 - upscale // 2 + k,
162 3 + abs(k - upscale // 2),
164 elif direction[n] == token_backward:
165 for k in range(upscale):
169 (height * upscale) // 2 - upscale // 2 + k,
170 3 + upscale // 2 - abs(k - upscale // 2),
173 for k in range(2, upscale - 2):
175 n, :, (height * upscale) // 2 - upscale // 2 + k, k
178 n, :, (height * upscale) // 2 - upscale // 2 + k, upscale - 1 - k
183 mosaic(f_first, upscale),
187 mosaic(f_second, upscale),
196 result.append("".join([token2char[v] for v in s]))
200 ######################################################################
202 if __name__ == "__main__":
206 start_time = time.perf_counter()
207 seq = generate(nb=90, height=height, width=width)
208 delay = time.perf_counter() - start_time
209 print(f"{seq.size(0)/delay:02f} samples/s")
211 print(seq2str(seq[:4]))
213 # m = (torch.rand(seq.size()) < 0.05).long()
214 # seq = (1 - m) * seq + m * 23
216 img = sample2img(seq, height, width)
219 torchvision.utils.save_image(
220 img.float() / 255.0, "/tmp/world.png", nrow=6, padding=6, pad_value=0