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]),
38 self.colors = torch.tensor([c for _, c in self.named_colors])
39 self.name2color = dict([(p[0], i) for i, p in enumerate(self.named_colors)])
43 ######################################################################
45 def frame2img(self, x, scale=15):
46 x = x.reshape(x.size(0), self.height, -1)
47 x = self.colors[x].permute(0, 3, 1, 2)
49 x = x[:, :, :, None, :, None].expand(-1, -1, -1, scale, -1, scale)
50 x = x.reshape(s[0], s[1], s[2] * scale, s[3] * scale)
52 x[:, :, :, torch.arange(0, x.size(3), scale)] = 0
53 x[:, :, torch.arange(0, x.size(2), scale), :] = 0
64 predicted_prompts=None,
65 predicted_answers=None,
67 prompts = prompts.reshape(prompts.size(0), self.height, -1)
68 answers = answers.reshape(answers.size(0), self.height, -1)
70 if predicted_prompts is None:
71 predicted_prompts = 255
73 if predicted_answers is None:
74 predicted_answers = 255
76 def add_frame(x, c, margin, bottom=False):
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, n, min_height=3, min_width=3):
174 collision = x.new(x.size())
180 i1, i2 = torch.randint(x.size(0), (2,))
181 if i1 + min_height <= i2:
184 j1, j2 = torch.randint(x.size(1), (2,))
185 if j1 + min_width <= j2:
187 collision[i1:i2, j1:j2] += 1
188 if collision.max() > 1:
190 result.append((i1, j1, i2, j2))
191 if collision.max() == 1:
195 ######################################################################
197 def task_replace_color(self, A, f_A, B, f_B):
199 c = torch.randperm(len(self.colors) - 1)[: N + 1] + 1
200 for X, f_X in [(A, f_A), (B, f_B)]:
201 r = self.rec_coo(X, N)
203 i1, j1, i2, j2 = r[n]
204 X[i1:i2, j1:j2] = c[n]
205 f_X[i1:i2, j1:j2] = c[n if n > 0 else -1]
207 def task_move(self, A, f_A, B, f_B):
208 di, dj = torch.randint(2, (2,)) * 2 - 1
210 c = torch.randperm(len(self.colors) - 1)[:N] + 1
211 for X, f_X in [(A, f_A), (B, f_B)]:
213 r = self.rec_coo(X, N)
214 i1, j1, i2, j2 = r[N - 1]
217 and i2 + di < X.size(0)
219 and j2 + dj < X.size(1)
224 i1, j1, i2, j2 = r[n]
225 X[i1:i2, j1:j2] = c[n]
227 f_X[i1 + di : i2 + di, j1 + dj : j2 + dj] = c[n]
229 f_X[i1:i2, j1:j2] = c[n]
231 def task_grow(self, A, f_A, B, f_B):
232 di, dj = torch.randint(2, (2,)) * 2 - 1
234 c = torch.randperm(len(self.colors) - 1)[:N] + 1
235 direction = torch.randint(2, (1,))
236 for X, f_X in [(A, f_A), (B, f_B)]:
238 r = self.rec_coo(X, N)
239 i1, j1, i2, j2 = r[N - 1]
240 if i1 + 3 < i2 and j1 + 3 < j2:
244 i1, j1, i2, j2 = r[n]
247 X[i1 + 1 : i2 - 1, j1 + 1 : j2 - 1] = c[n]
248 f_X[i1:i2, j1:j2] = c[n]
250 X[i1:i2, j1:j2] = c[n]
251 f_X[i1 + 1 : i2 - 1, j1 + 1 : j2 - 1] = c[n]
253 X[i1:i2, j1:j2] = c[n]
254 f_X[i1:i2, j1:j2] = c[n]
256 def task_color_grow(self, A, f_A, B, f_B):
257 di, dj = torch.randint(2, (2,)) * 2 - 1
259 c = torch.randperm(len(self.colors) - 1)[: 2 * N] + 1
260 direction = torch.randint(2, (1,))
261 for X, f_X in [(A, f_A), (B, f_B)]:
262 r = self.rec_coo(X, N)
264 i1, j1, i2, j2 = r[n]
266 X[i1:i2, j1:j2] = c[2 * n]
267 X[i : i + 1, j1:j2] = c[2 * n + 1]
268 f_X[i1:i2, j1:j2] = c[2 * n]
270 f_X[i:i2, j1:j2] = c[2 * n + 1]
272 f_X[i : i + 1, j1:j2] = c[2 * n + 1]
274 def task_frame(self, A, f_A, B, f_B):
276 c = torch.randperm(len(self.colors) - 1)[: N + 1] + 1
277 for X, f_X in [(A, f_A), (B, f_B)]:
278 r = self.rec_coo(X, N)
280 i1, j1, i2, j2 = r[n]
281 X[i1:i2, j1:j2] = c[n]
282 f_X[i1:i2, j1:j2] = c[n]
284 f_X[i1 + 1 : i2 - 1, j1 + 1 : j2 - 1] = 0
286 def task_detect(self, A, f_A, B, f_B):
288 c = torch.randperm(len(self.colors) - 1)[: N + 1] + 1
289 for X, f_X in [(A, f_A), (B, f_B)]:
290 r = self.rec_coo(X, N)
292 i1, j1, i2, j2 = r[n]
293 X[i1:i2, j1:j2] = c[n]
296 ######################################################################
298 def generate_prompts_and_answers(self, nb):
300 self.task_replace_color,
303 self.task_color_grow,
307 prompts = torch.zeros(nb, self.height, self.width * 3, dtype=torch.int64)
308 answers = torch.zeros(nb, self.height, self.width, dtype=torch.int64)
310 for prompt, answer in zip(prompts, answers):
311 A = prompt[:, 0 * w : 1 * w]
312 f_A = prompt[:, 1 * w : 2 * w]
313 B = prompt[:, 2 * w : 3 * w]
315 task = tasks[torch.randint(len(tasks), (1,))]
317 return prompts.flatten(1), answers.flatten(1)
325 predicted_prompts=None,
326 predicted_answers=None,
330 filename_prefix + ".png",
338 ######################################################################
340 if __name__ == "__main__":
345 start_time = time.perf_counter()
346 prompts, answers = lang.generate_prompts_and_answers(100)
347 delay = time.perf_counter() - start_time
348 print(f"{prompts.size(0)/delay:02f} seq/s")
350 # predicted_prompts = torch.rand(prompts.size(0)) < 0.5
351 # predicted_answers = torch.logical_not(predicted_prompts)
358 # You can add a bool to put a frame around the predicted parts
359 # predicted_prompts, predicted_answers