Esempio n. 1
0
 def update_step_map(self, pos):
     masked_img = utils.mask_grid((pos[1], pos[0]),
                                  self.grid[0],
                                  3,
                                  one_is_free=False)
     self.step_map[0] = utils.accumulate_map(self.step_map[0],
                                             masked_img,
                                             one_is_free=False)
 def get_curr_visible_map(self, pos):
     """Get current visible field by given a valid position.
     Parameters
     ----------
     pos : tuple
         a valid position (x, y)
     Returns
     -------
     curr_vis_map : numpy.ndarray
         return a partially visible map by given position.
         Not if it's fully observable,
         this function will report the entire map
     """
     if self.is_pos_valid(pos):
         if self.is_po:
             return utils.mask_grid(pos, self.grid_map, self.mask_radius,
                                    one_is_free=self.one_is_free)[0]
         else:
             return self.grid_map
for grid_idx in xrange(0, len(sample_idx), 7):
    # get a grid
    grid, state, label, goal = sampler.get_grid(grid_idx)
    gt_collector.append(state)

    # define step map
    grid = 1 - grid[0]
    step_map = np.ones((8, 8), dtype=np.uint8)
    pos = [state[0, 1], state[0, 0]]
    path = [(pos[0], pos[1])]

    planner = Dstar(path[0], (goal[1], goal[0]), step_map.flatten(), (8, 8))

    for setp in xrange(n_steps):
        # masked image
        masked_img, coord = utils.mask_grid(pos, grid, 3, one_is_free=True)
        #  # step image
        step_map[coord[0], coord[1]] = grid[coord[0], coord[1]]
        #  step_map = utils.accumulate_map(step_map, masked_img)
        change = np.where(np.logical_xor(planner.grid, step_map.flatten()))[0]
        block_list = np.unravel_index(change, planner.imsize)
        print(block_list)
        for idx in xrange(block_list[0].shape[0]):
            planner.add_obstacle(block_list[0][idx], block_list[1][idx])

        errors, next_move = planner.replan()
        planner.reset_start_pos(next_move)
        if not errors and enable_vis:
            utils.plot_grid(step_map, (8, 8),
                            start=(path[0][1], path[0][0]),
                            pos=path[1:],
gt_collector = []
po_collector = []
diff_collector = []

grid, state, label, goal = sampler.get_grid(77)
gt_collector.append(state)

step_map = np.zeros((2, 28, 28))
step_map[0] = np.ones((28, 28))
step_map[1] = grid[1]
pos = [state[0, 0], state[0, 1]]
path = [(pos[0], pos[1])]
start = (pos[0], pos[1])
for step in xrange(32):
    masked_img = utils.mask_grid((pos[1], pos[0]),
                                 grid[0],
                                 3,
                                 one_is_free=False)
    step_map[0] = utils.accumulate_map(step_map[0],
                                       masked_img,
                                       one_is_free=False)

    action, reward, value = predict(step_map, pos, model, 20)
    dx, dy = get_action(action)
    pos[0] = pos[0] + dx
    pos[1] = pos[1] + dy
    path.append((pos[0], pos[1]))

    plt.figure()
    plt.imshow(value, cmap="jet")
    plt.colorbar()
    plt.scatter(x=[start[0]], y=[start[1]], marker="*", c="orange", s=50)
# load file
file_path = os.path.join(
        rlvision.RLVISION_MODEL, "grid28_paths",
        "grid_28_77_bad.pkl")

grid, gt_path, po_path, goal = utils.load_grid_selection(file_path)

gt = []
for idx in xrange(gt_path.shape[0]):
    gt.append((gt_path[idx, 0], gt_path[idx, 1]))

# plot grid
utils.plot_grid(1-grid, (28, 28),
                start=po_path[0],
                pos=gt[1:],
                goal=goal)

# plot po grid
step_map = np.ones((28, 28))
path = [po_path[0]]
for step in xrange(1, len(po_path)):
    masked_img = utils.mask_grid((path[-1][1], path[-1][0]),
                                 grid, 3, one_is_free=False)
    step_map = utils.accumulate_map(step_map, masked_img,
                                    one_is_free=False)
    path.append(po_path[step])
    utils.plot_grid(1-step_map, (28, 28),
                    start=path[0],
                    pos=path[1:],
                    goal=goal)
Esempio n. 6
0
# load grid data
db, imsize = utils.load_grid40(split=30)
radius = 7
mask_pos_1 = (7, 7)
mask_pos_2 = (12, 12)

# plot a grid
grid_im = db[utils.data_dict[0]]

grid = 1 - grid_im[0, :]

#  utils.plot_grid(grid, imsize)

grid = grid.reshape(imsize[0], imsize[1])
masked_grid_1 = utils.mask_grid(mask_pos_1, grid, radius, one_is_free=False)
masked_grid_2 = utils.mask_grid(mask_pos_2, grid, radius, one_is_free=False)
acc_grid = utils.accumulate_map(masked_grid_1,
                                masked_grid_2,
                                one_is_free=False)

plt.figure(figsize=(10, 10))
plt.subplot(231)
plt.imshow((1 - grid) * 255, cmap="gray")
plt.axis("off")
plt.subplot(232)
plt.imshow((1 - masked_grid_1) * 255, cmap="gray")
plt.axis("off")
plt.subplot(233)
plt.imshow((1 - masked_grid_2) * 255, cmap="gray")
plt.axis("off")