X-Git-Url: https://fleuret.org/cgi-bin/gitweb/gitweb.cgi?a=blobdiff_plain;f=maze.py;h=6c3fe94fe189159a4a450fb90c33f02d73cb6e92;hb=7143cc544d2b0af03150d9ee05f3cf21319c693b;hp=f4a4840c352d26406e9bd80077ef4cf407f9bc95;hpb=d44d0605fed828b8cea08c8e1c5bda7e4528ea97;p=beaver.git diff --git a/maze.py b/maze.py index f4a4840..6c3fe94 100755 --- a/maze.py +++ b/maze.py @@ -113,18 +113,16 @@ def compute_policy(walls, i, j): ###################################################################### -def mark_path(walls, i, j, goal_i, goal_j): - policy = compute_policy(walls, goal_i, goal_j) +def mark_path(walls, i, j, goal_i, goal_j, policy): action = torch.distributions.categorical.Categorical( policy.permute(1, 2, 0) ).sample() - walls[i, j] = 4 n, nmax = 0, walls.numel() while i != goal_i or j != goal_j: di, dj = [(0, -1), (0, 1), (-1, 0), (1, 0)][action[i, j]] i, j = i + di, j + dj assert walls[i, j] == 0 - walls[i, j] = 4 + walls[i, j] = v_path n += 1 assert n < nmax @@ -160,32 +158,36 @@ def create_maze_data( ): mazes = torch.empty(nb, height, width, dtype=torch.int64) paths = torch.empty(nb, height, width, dtype=torch.int64) + policies = torch.empty(nb, 4, height, width) for n in progress_bar(range(nb)): maze = create_maze(height, width, nb_walls) - i = (1 - maze).nonzero() + i = (maze == v_empty).nonzero() while True: start, goal = i[torch.randperm(i.size(0))[:2]] if (start - goal).abs().sum() >= dist_min: break + start_i, start_j, goal_i, goal_j = start[0], start[1], goal[0], goal[1] + policy = compute_policy(maze, goal_i, goal_j) path = maze.clone() - mark_path(path, start[0], start[1], goal[0], goal[1]) - maze[start[0], start[1]] = v_start - maze[goal[0], goal[1]] = v_goal - path[start[0], start[1]] = v_start - path[goal[0], goal[1]] = v_goal + mark_path(path, start_i, start_j, goal_i, goal_j, policy) + maze[start_i, start_j] = v_start + maze[goal_i, goal_j] = v_goal + path[start_i, start_j] = v_start + path[goal_i, goal_j] = v_goal mazes[n] = maze paths[n] = path + policies[n] = policy - return mazes, paths + return mazes, paths, policies ###################################################################### -def save_image(name, mazes, target_paths, predicted_paths=None): +def save_image(name, mazes, target_paths, predicted_paths=None, path_correct=None): mazes, target_paths = mazes.cpu(), target_paths.cpu() colors = torch.tensor( @@ -204,7 +206,7 @@ def save_image(name, mazes, target_paths, predicted_paths=None): .reshape(target_paths.size() + (-1,)) .permute(0, 3, 1, 2) ) - img = torch.cat((mazes.unsqueeze(1), target_paths.unsqueeze(1)), 1) + imgs = torch.cat((mazes.unsqueeze(1), target_paths.unsqueeze(1)), 1) if predicted_paths is not None: predicted_paths = predicted_paths.cpu() @@ -213,17 +215,34 @@ def save_image(name, mazes, target_paths, predicted_paths=None): .reshape(predicted_paths.size() + (-1,)) .permute(0, 3, 1, 2) ) - img = torch.cat((img, predicted_paths.unsqueeze(1)), 1) - - img = img.reshape((-1,) + img.size()[2:]).float() / 255.0 - - torchvision.utils.save_image(img, name, padding=1, pad_value=0.85, nrow=6) + imgs = torch.cat((imgs, predicted_paths.unsqueeze(1)), 1) + + # NxKxCxHxW + if path_correct is None: + path_correct = torch.zeros(imgs.size(0)) <= 1 + path_correct = path_correct.cpu().long().view(-1, 1, 1, 1) + img = torch.tensor([224, 224, 224]).view(1, -1, 1, 1) * path_correct + torch.tensor( + [255, 0, 0] + ).view(1, -1, 1, 1) * (1 - path_correct) + img = img.expand( + -1, -1, imgs.size(3) + 2, 1 + imgs.size(1) * (1 + imgs.size(4)) + ).clone() + for k in range(imgs.size(1)): + img[ + :, + :, + 1 : 1 + imgs.size(3), + 1 + k * (1 + imgs.size(4)) : 1 + k * (1 + imgs.size(4)) + imgs.size(4), + ] = imgs[:, k] + + img = img.float() / 255.0 + + torchvision.utils.save_image(img, name, nrow=4, padding=1, pad_value=224.0 / 256) ###################################################################### if __name__ == "__main__": - device = torch.device("cuda" if torch.cuda.is_available() else "cpu") mazes, paths = create_maze_data(8) mazes, paths = mazes.to(device), paths.to(device)