Update.
[culture.git] / world.py
index a5e9601..ab02c82 100755 (executable)
--- a/world.py
+++ b/world.py
@@ -27,9 +27,9 @@ colors = torch.tensor(
 )
 
 token_background = 0
-first_fish_token = 1
-nb_fish_tokens = len(colors) - 1
-token_forward = first_fish_token + nb_fish_tokens
+first_bird_token = 1
+nb_bird_tokens = len(colors) - 1
+token_forward = first_bird_token + nb_bird_tokens
 token_backward = token_forward + 1
 
 token2char = "_" + "".join([str(n) for n in range(len(colors) - 1)]) + "><"
@@ -49,9 +49,9 @@ def generate(
         f_end = torch.zeros(height, width, dtype=torch.int64)
         n = torch.arange(f_start.size(0))
 
-        nb_fish = torch.randint(max_nb_obj, (1,)).item() + 1
+        nb_birds = torch.randint(max_nb_obj, (1,)).item() + 1
         for c in (
-            (torch.randperm(nb_fish_tokens) + first_fish_token)[:nb_fish].sort().values
+            (torch.randperm(nb_bird_tokens) + first_bird_token)[:nb_birds].sort().values
         ):
             i, j = (
                 torch.randint(height - 2, (1,))[0] + 1,
@@ -103,12 +103,13 @@ def generate(
 
 
 def sample2img(seq, height, width, upscale=15):
-    f_start = seq[:, : height * width].reshape(-1, height, width)
-    f_end = seq[:, height * width + 1 :].reshape(-1, height, width)
+    f_first = seq[:, : height * width].reshape(-1, height, width)
+    f_second = seq[:, height * width + 1 :].reshape(-1, height, width)
+    direction = seq[:, height * width]
 
     def mosaic(x, upscale):
         x = x.reshape(-1, height, width)
-        m = torch.logical_and(x >= 0, x < first_fish_token + nb_fish_tokens).long()
+        m = torch.logical_and(x >= 0, x < first_bird_token + nb_bird_tokens).long()
         x = colors[x * m].permute(0, 3, 1, 2)
         s = x.shape
         x = x[:, :, :, None, :, None].expand(-1, -1, -1, upscale, -1, upscale)
@@ -124,7 +125,46 @@ def sample2img(seq, height, width, upscale=15):
 
         return x
 
-    return torch.cat([mosaic(f_start, upscale), mosaic(f_end, upscale)], dim=3)
+    direction_symbol = torch.full((direction.size(0), height * upscale, upscale), 0)
+    direction_symbol = colors[direction_symbol].permute(0, 3, 1, 2)
+    separator = torch.full((direction.size(0), 3, height * upscale, 1), 0)
+
+    for n in range(direction_symbol.size(0)):
+        if direction[n] == token_forward:
+            for k in range(upscale):
+                direction_symbol[
+                    n,
+                    :,
+                    (height * upscale) // 2 - upscale // 2 + k,
+                    3 + abs(k - upscale // 2),
+                ] = 0
+        elif direction[n] == token_backward:
+            for k in range(upscale):
+                direction_symbol[
+                    n,
+                    :,
+                    (height * upscale) // 2 - upscale // 2 + k,
+                    3 + upscale // 2 - abs(k - upscale // 2),
+                ] = 0
+        else:
+            for k in range(2, upscale - 2):
+                direction_symbol[
+                    n, :, (height * upscale) // 2 - upscale // 2 + k, k
+                ] = 0
+                direction_symbol[
+                    n, :, (height * upscale) // 2 - upscale // 2 + k, upscale - 1 - k
+                ] = 0
+
+    return torch.cat(
+        [
+            mosaic(f_first, upscale),
+            separator,
+            direction_symbol,
+            separator,
+            mosaic(f_second, upscale),
+        ],
+        dim=3,
+    )
 
 
 def seq2str(seq):
@@ -141,7 +181,7 @@ if __name__ == "__main__":
 
     height, width = 6, 8
     start_time = time.perf_counter()
-    seq = generate(nb=64, height=height, width=width, max_nb_obj=3)
+    seq = generate(nb=90, height=height, width=width, max_nb_obj=3)
     delay = time.perf_counter() - start_time
     print(f"{seq.size(0)/delay:02f} samples/s")
 
@@ -154,5 +194,5 @@ if __name__ == "__main__":
     print(img.size())
 
     torchvision.utils.save_image(
-        img.float() / 255.0, "/tmp/world.png", nrow=8, padding=2
+        img.float() / 255.0, "/tmp/world.png", nrow=6, padding=4
     )