X-Git-Url: https://fleuret.org/cgi-bin/gitweb/gitweb.cgi?a=blobdiff_plain;f=world.py;h=fb5d5c796990ef476ff79fb9ead66615a17fd7fd;hb=a8f039a9b491b1b4b47f6b9f8123c7261e758661;hp=bac9e761e248bb64547aada2bac7109f0099d38d;hpb=e38b98574f1966ea3a91ffb8fd9042f10a75ca88;p=picoclvr.git diff --git a/world.py b/world.py index bac9e76..fb5d5c7 100755 --- a/world.py +++ b/world.py @@ -30,7 +30,7 @@ class Box: return False -def scene2tensor(xh, yh, scene, size=512): +def scene2tensor(xh, yh, scene, size=64): width, height = size, size pixel_map = torch.ByteTensor(width, height, 4).fill_(255) data = pixel_map.numpy() @@ -48,11 +48,9 @@ def scene2tensor(xh, yh, scene, size=512): ctx.rel_line_to(-b.w * size, 0) ctx.close_path() ctx.set_source_rgba(b.r, b.g, b.b, 1.0) - ctx.fill_preserve() - ctx.set_source_rgba(0, 0, 0, 1.0) - ctx.stroke() + ctx.fill() - hs = size * 0.05 + hs = size * 0.1 ctx.set_source_rgba(0.0, 0.0, 0.0, 1.0) ctx.move_to(xh * size - hs / 2, yh * size - hs / 2) ctx.rel_line_to(hs, 0) @@ -69,7 +67,7 @@ def random_scene(): colors = [ (1.00, 0.00, 0.00), (0.00, 1.00, 0.00), - (0.00, 0.00, 1.00), + (0.60, 0.60, 1.00), (1.00, 1.00, 0.00), (0.75, 0.75, 0.75), ] @@ -87,7 +85,7 @@ def random_scene(): return scene -def sequence(length=10): +def sequence(nb_steps=10, all_frames=False): delta = 0.1 effects = [ (False, 0, 0), @@ -102,12 +100,14 @@ def sequence(length=10): ] while True: + frames = [] + scene = random_scene() xh, yh = tuple(x.item() for x in torch.rand(2)) - frame_start = scene2tensor(xh, yh, scene) + frames.append(scene2tensor(xh, yh, scene)) - actions = torch.randint(len(effects), (length,)) + actions = torch.randint(len(effects), (nb_steps,)) change = False for a in actions: @@ -137,14 +137,123 @@ def sequence(length=10): if xh < 0 or xh > 1 or yh < 0 or yh > 1: xh, yh = x, y - frame_end = scene2tensor(xh, yh, scene) + if all_frames: + frames.append(scene2tensor(xh, yh, scene)) + + if not all_frames: + frames.append(scene2tensor(xh, yh, scene)) + if change: break - return frame_start, frame_end, actions + return frames, actions + + +###################################################################### + + +# ||x_i - c_j||^2 = ||x_i||^2 + ||c_j||^2 - 2 +def sq2matrix(x, c): + nx = x.pow(2).sum(1) + nc = c.pow(2).sum(1) + return nx[:, None] + nc[None, :] - 2 * x @ c.t() + + +def update_centroids(x, c, nb_min=1): + _, b = sq2matrix(x, c).min(1) + b.squeeze_() + nb_resets = 0 + + for k in range(0, c.size(0)): + i = b.eq(k).nonzero(as_tuple=False).squeeze() + if i.numel() >= nb_min: + c[k] = x.index_select(0, i).mean(0) + else: + n = torch.randint(x.size(0), (1,)) + nb_resets += 1 + c[k] = x[n] + + return c, b, nb_resets + + +def kmeans(x, nb_centroids, nb_min=1): + if x.size(0) < nb_centroids * nb_min: + print("Not enough points!") + exit(1) + + c = x[torch.randperm(x.size(0))[:nb_centroids]] + t = torch.full((x.size(0),), -1) + n = 0 + + while True: + c, u, nb_resets = update_centroids(x, c, nb_min) + n = n + 1 + nb_changes = (u - t).sign().abs().sum() + nb_resets + t = u + if nb_changes == 0: + break + + return c, t + + +###################################################################### + + +def patchify(x, factor, invert_size=None): + if invert_size is None: + return ( + x.reshape( + x.size(0), #0 + x.size(1), #1 + factor, #2 + x.size(2) // factor,#3 + factor,#4 + x.size(3) // factor,#5 + ) + .permute(0, 2, 4, 1, 3, 5) + .reshape(-1, x.size(1), x.size(2) // factor, x.size(3) // factor) + ) + else: + return ( + x.reshape( + invert_size[0], #0 + factor, #1 + factor, #2 + invert_size[1], #3 + invert_size[2] // factor, #4 + invert_size[3] // factor, #5 + ) + .permute(0, 3, 1, 4, 2, 5) + .reshape(invert_size) + ) if __name__ == "__main__": - frame_start, frame_end, actions = sequence() - torchvision.utils.save_image(frame_start, "world_start.png") - torchvision.utils.save_image(frame_end, "world_end.png") + import time + + all_frames = [] + nb = 1000 + start_time = time.perf_counter() + for n in range(nb): + frames, actions = sequence(nb_steps=31) + all_frames += frames + end_time = time.perf_counter() + print(f"{nb / (end_time - start_time):.02f} samples per second") + + input = torch.cat(all_frames, 0) + x = patchify(input, 8) + y = x.reshape(x.size(0), -1) + print(f"{x.size()=} {y.size()=}") + centroids, t = kmeans(y, 4096) + results = centroids[t] + results = results.reshape(x.size()) + results = patchify(results, 8, input.size()) + + print(f"{input.size()=} {results.size()=}") + + torchvision.utils.save_image(input[:64], "orig.png", nrow=8) + torchvision.utils.save_image(results[:64], "qtiz.png", nrow=8) + + # frames, actions = sequence(nb_steps=31, all_frames=True) + # frames = torch.cat(frames, 0) + # torchvision.utils.save_image(frames, "seq.png", nrow=8)