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>
8 import math, sys, tqdm, os, warnings
10 import torch, torchvision
13 from torch.nn import functional as F
15 ######################################################################
20 class Lang(problem.Problem):
22 ("white", [255, 255, 255]),
24 ("green", [0, 192, 0]),
25 ("blue", [0, 0, 255]),
26 ("orange", [255, 192, 0]),
27 ("cyan", [0, 255, 255]),
28 ("violet", [255, 0, 255]),
29 ("lightgreen", [192, 255, 192]),
30 ("pink", [255, 192, 192]),
31 ("lightblue", [192, 192, 255]),
32 ("gray", [192, 192, 192]),
39 self.colors = torch.tensor([c for _, c in self.named_colors])
40 self.name2color = dict([(p[0], i) for i, p in enumerate(self.named_colors)])
43 self.nb_iterations = nb_iterations
45 ######################################################################
47 def frame2img(self, x, scale=15):
48 x = x.reshape(x.size(0), self.height, -1)
49 x = self.colors[x].permute(0, 3, 1, 2)
51 x = x[:, :, :, None, :, None].expand(-1, -1, -1, scale, -1, scale)
52 x = x.reshape(s[0], s[1], s[2] * scale, s[3] * scale)
54 x[:, :, :, torch.arange(0, x.size(3), scale)] = 0
55 x[:, :, torch.arange(0, x.size(2), scale), :] = 0
66 predicted_prompts=None,
67 predicted_answers=None,
69 if predicted_prompts is None:
70 predicted_prompts = 255
72 if predicted_answers is None:
73 predicted_answers = 255
75 def add_frame(x, c, margin, bottom=False):
76 print(f"{type(x)=} {type(c)=}")
78 h, w, di, dj = x.size(2) + margin, x.size(3), 0, 0
81 x.size(2) + 2 * margin,
82 x.size(3) + 2 * margin,
87 y = x.new_full((x.size(0), x.size(1), h, w), 0)
93 c = c * torch.tensor([192, 192, 192], device=c.device) + (
95 ) * torch.tensor([255, 255, 255], device=c.device)
96 y[...] = c[:, :, None, None]
98 y[:, :, di : di + x.size(2), dj : dj + x.size(3)] = x
104 img_prompts = torch.cat(
107 add_frame(self.frame2img(x), c=0, margin=1),
111 for x in prompts.to("cpu").split(split_size=self.width, dim=2)
116 h = img_prompts.size(2)
117 img_answers = add_frame(
118 add_frame(self.frame2img(answers.to("cpu")), c=0, margin=1),
123 separator_size = 2 * margin
125 separator = img_prompts.new_full(
135 marker = img_prompts.new_full(
145 # marker[:, :, 0] = 0
146 # marker[:, :, h - 1] = 0
148 for k in range(1, 2 * separator_size - 8):
149 i = k - (separator_size - 4)
150 j = separator_size - 5 - abs(i)
151 marker[:, :, h // 2 - 1 + i, 2 + j] = 0
152 marker[:, :, h // 2 - 1 + i + 1, 2 + j] = 0
163 image_name = os.path.join(result_dir, filename)
164 torchvision.utils.save_image(
165 img.float() / 255.0, image_name, nrow=4, padding=margin * 4, pad_value=1.0
168 ######################################################################
170 def nb_token_values(self):
171 return len(self.colors)
173 def rec_coo(self, x):
175 i1, i2 = torch.randint(x.size(0), (2,))
179 j1, j2 = torch.randint(x.size(1), (2,))
182 return i1, j1, i2, j2
184 def task_red_to_green(self, A, f_A, B, f_B):
185 i1, j1, i2, j2 = self.rec_coo(A)
186 A[i1:i2, j1:j2] = self.name2color["red"]
187 f_A[i1:i2, j1:j2] = self.name2color["green"]
188 i1, j1, i2, j2 = self.rec_coo(B)
189 B[i1:i2, j1:j2] = self.name2color["red"]
190 f_B[i1:i2, j1:j2] = self.name2color["green"]
192 def generate_prompts_and_answers(self, nb):
193 prompts = torch.zeros(nb, self.height, self.width * 3, dtype=torch.int64)
194 answers = torch.zeros(nb, self.height, self.width, dtype=torch.int64)
196 for prompt, answer in zip(prompts, answers):
197 self.task_red_to_green(
198 prompt[:, 0 * w : 1 * w],
199 prompt[:, 1 * w : 2 * w],
200 prompt[:, 2 * w : 3 * w],
203 return prompts, answers
211 predicted_prompts=None,
212 predicted_answers=None,
216 filename_prefix + ".png",
224 ######################################################################
226 if __name__ == "__main__":
229 lang = Lang(nb_iterations=4)
231 prompts, answers = lang.generate_prompts_and_answers(24)
233 predicted_prompts = torch.rand(prompts.size(0)) < 0.5
234 predicted_answers = torch.logical_not(predicted_prompts)
237 "/tmp", "test", prompts, answers, predicted_prompts, predicted_answers
240 # start_time = time.perf_counter()
241 # token_sequences = lang.generate_token_sequences(nb=64)
242 # delay = time.perf_counter() - start_time
243 # print(f"{token_sequences.size(0)/delay:02f} seq/s")
245 # print(lang.seq2str(seq[:4]))
247 # for t in range(len(it[0])):
248 # img = torch.cat([lang.frame2img(f[t]) for f in it], dim=0)
249 # torchvision.utils.save_image(
250 # img.float() / 255.0,
251 # f"/tmp/frame_{t:03d}.png",
257 # m = (torch.rand(seq.size()) < 0.05).long()
258 # seq = (1 - m) * seq + m * 23
261 # img = lang.seq2img(token_sequences)
264 # torchvision.utils.save_image(
265 # img.float() / 255.0, "/tmp/world.png", nrow=6, padding=6, pad_value=0