コード例 #1
0
def get_iters_map():
    # data_directory = '/home/doj2rng/dat/'
    data_directory = '/local/home/ful7rng/projects/occupancy/data'

    arrays = defaultdict(dict)
    for ix, data_set in enumerate(data_sets):
        input_str = data_directory + 'maps/' + maps[ix][0] + \
                    '/thresholded_20.png'
        arrays['inputs'][data_set], _, _ = load_map(input_str)

        label_str = data_directory + 'occ_maps/' + maps[ix][0] + '/' + \
                    method[ix] + '/' + os.path.split(method[ix])[-1] + '.npy'
        arrays['labels'][data_set] = np.load(label_str)

        mask_str = data_directory + 'occ_maps/' + method[ix] + '/' + \
                   maps[ix][0] + '_20.npy'
        arrays['masks'][data_set] = np.load(mask_str)

    iterators = [
        Iterator(arrays['inputs'][data_set][None, :, :],
                 arrays['labels'][data_set][None, :, :],
                 arrays['masks'][data_set][None, :, :], 1)
        for data_set in data_sets
    ]

    return iterators
コード例 #2
0
def map_portfolio(name):
    def propose_x_y(map):
        locations = np.array(np.where(1 - map)).T.tolist()
        idx = np.random.randint(0, len(locations), 1)[0]
        x, y = locations[idx]
        return x, y

    def make_T_section(size):
        map_size = size
        map = np.ones((map_size, map_size), dtype=bool)
        lane_length = size // 2

        for j in range(map_size / 2):
            map[j + 1:j + lane_length + 1, j] = 0
        for j in range(map_size / 2, map_size):
            map[map_size - j:map_size - j + lane_length, j] = 0
        for j in range(map_size / 2 - lane_length / 2,
                       map_size / 2 + lane_length / 2 + 1):
            map[map_size / 2:map_size, j] = 0
        x, y = map_size - map_size / 4, map_size / 2
        return map, x, y

    if name == 'minimum':
        map = np.array([[1, 0, 0, 0, 0, 0, 1]] * 20, dtype=bool)
        x, y = propose_x_y(map)

    elif name == 'T-section':
        map, x, y = make_T_section(31)

    elif name == 'empty_map':
        map_size = 62
        map = np.zeros((map_size, map_size), dtype=bool)
        x, y = int(map_size / 4 * 3), int(map_size / 2)

    elif name == 'big_T_section':
        map, x, y = make_T_section(91)

    elif name == 'simple_map':
        # load simple map
        map_path = '/local/home/ful7rng/projects/transition/data/maps/simple_map/thresholded_20.png'
        map, _, _ = load_map(map_path)
        x, y = propose_x_y(map)

    elif name == 'any':
        config_path = '/local/home/ful7rng/projects/transition/config.py'
        map = get_map_crop(config_path, num=1)[0]
        x, y = propose_x_y(map)

    width, height = map.shape
    return map, width, height, x, y
コード例 #3
0
ファイル: ped_sim.py プロジェクト: stomachacheGE/bofmp
def generate_trajectories(map_str, config_path, plot=False):
    """
    Generate trajectories for map specified as map_str.
    """
    cf = imp.load_source('config', config_path)
    mode = cf.trajectory_sampling_mode

    traj_folder = cf.data_folder + '/trajectories'
    res_str = str(int(cf.resolution * 100))
    mean_path_length = np.mean([cf.min_traj_length, cf.max_traj_length])

    print('Generate trajectories for map {}'.format(map_str))
    current_map_folder = cf.map_folder + '/' + map_str + '/'
    map_file = current_map_folder + 'thresholded_' + res_str + '.png'
    map_arr, _, _ = load_map(map_file)
    average_free_space = np.mean(free_space(map_arr))

    # sample paths
    file_name = os.path.join(traj_folder, map_str, cf.algo_str,
                             cf.algo_str + '.npy')

    if mode == 'random':
        num_trajectories = int(cf.trajectory_resampling_factor *
                               np.sqrt(average_free_space) * map_arr.size /
                               (mean_path_length / cf.resolution))
    elif mode == 'transverse':
        num_trajectories = cf.trajectory_resampling_factor

    trajectories = \
        get_npy(file_name, calc_trajectories,
                map_arr, num_trajectories, mode=mode, resolution=cf.resolution,
                min_dist=cf.min_traj_length, max_dist=cf.max_traj_length,
                diagonal=cf.diagonal, verbose=True)
    if plot:
        plot_trajectories(trajectories, map_arr, map_res=cf.resolution)
        # save plot
        plot_file = re.sub('data', 'media', file_name)
        plot_file = re.sub('.npy', '.svg', plot_file)
        ensure_dir(plot_file)
        plt.show()
        plt.savefig(plot_file, format='svg')
        plt.close()

    return map_file, file_name
コード例 #4
0
def generate_transitional_map(map_path, traj_path, config_path, plot=False):
    cf = imp.load_source('config', config_path)
    map_, _, _ = load_map(map_path)

    trans_map_path = re.sub('trajectories', 'transition_maps', traj_path)
    trans_counts_path = re.sub('trajectories', 'transition_counts', traj_path)

    transition_probs, transition_counts = \
        get_npy([trans_map_path, trans_counts_path], calc_transitional_map, map_path,
                traj_path, config_path)

    if not os.path.isfile(trans_map_path):
        ensure_dir(trans_map_path)
        np.save(trans_map_path, transition_probs)

    if not os.path.isfile(trans_counts_path):
        ensure_dir(trans_counts_path)
        np.save(trans_counts_path, transition_counts)

    if plot:
        fig = plt.figure()
        for last in range(4):
            for next in range(4):
                order = last * 4 + next
                display_trans_map(transition_probs,
                                  map_,
                                  last,
                                  next,
                                  order,
                                  cost_map_res=cf.resolution)
        fig.subplots_adjust(left=0.05,
                            bottom=0.06,
                            right=1,
                            top=0.96,
                            wspace=0,
                            hspace=0.47)
        plt.show()

    return trans_map_path, trans_counts_path
コード例 #5
0
def calc_transitional_map(map_path, traj_path, config_path):
    cf = imp.load_source('config', config_path)

    map_, _, _ = load_map(map_path)
    mcm = Grid_HMM(np.array(map_.shape).astype(int))
    trajectories = np.load(traj_path)
    for idx, trajectory in enumerate(trajectories):
        if idx % 100 == 0:
            print("Add transitions of {}/{} trajectory to the mcm.".format(
                idx + 1, len(trajectories)))
        trajectory = np.round((trajectory / cf.resolution) - 0.5).astype(int)
        #print(str(trajectory))
        for t in range(trajectory.shape[0] - 2):

            from_ = trajectory[t, :]
            current = trajectory[t + 1, :]
            to = trajectory[t + 2, :]
            mcm.add_transition(from_, current, to)

    conditional = cf.conditional_prob
    transition_probs = mcm.get_transition_probs(conditional, cf.diagonal)

    return transition_probs, mcm.get_transition_counts()
コード例 #6
0
                                hspace=0.22)
            fig.colorbar(axes[0], ax=axes_.ravel().tolist(), shrink=0.75)
            plt.show()
            # raw_input("Press a key to continue")
            # plt.close()
    else:
        ############################################################################
        #################### show ground truth as a quiver plot ####################
        ############################################################################

        # randomly sample a map
        map_name = np.random.choice(cf.training_maps + cf.test_maps, 1)[0]
        current_map_folder = cf.map_folder + '/' + map_name + '/'
        res_str = str(int(cf.resolution * 100))
        map_file = current_map_folder + 'thresholded_' + res_str + '.png'
        map_arr, _, _ = load_map(map_file)

        trans_counts_path = cf.data_folder+'/transition_maps/' + map_name + '/' \
                            + cf.algo_str + '/' + cf.algo_str + '.npy'
        trans_counts = np.load(trans_counts_path)

        input_size = cf.nn_input_size
        output_size = cf.nn_output_size
        for i in range(show_num):
            left, top = [
                np.random.randint(0, map_arr.shape[ix] - input_size)
                for ix in [0, 1]
            ]
            x_in, y_in = [
                slice(start, start + input_size) for start in [left, top]
            ]
コード例 #7
0
            file_list.append(os.path.join(root, filename))
    print(file_list)

    # filter out files containing tag
    file_list = [file for file in file_list for tag in tags if tag in file]
    # print(file_list)

    np.random.shuffle(file_list)
    for traj_file in file_list:
        print(traj_file)
        tags = ['/'] + traj_file.split('/')
        home_lvl = tags.index('home') + 4

        map_folder = re.sub('trajectories', 'maps',
                            os.path.join(*tags[:home_lvl + 3]))
        map_, map_origin, resolution = load_map(
            os.path.join(map_folder, 'thresholded_20.png'))

        trajectories = np.load(traj_file, encoding='latin1')

        occ_map_file = re.sub(
            'trajectories', 'occ_maps',
            os.path.join(os.path.join(*tags[:home_lvl + 3]), 'from_traj',
                         os.path.join(*tags[home_lvl + 3:])))

        if 'real' in tags:
            blur = 0.1
        else:
            blur = 0.25


        occ_map = \
コード例 #8
0
ファイル: map2nnio.py プロジェクト: stomachacheGE/bofmp
def sample_network_io(trans_map_path, trans_counts_path, map_path,
                      config_path):

    cf = imp.load_source('config', config_path)

    flatten = flatten_fn(cf.velocities, cf.conditional_prob,
                         cf.unique_vel_idxs)

    input_size = cf.nn_input_size
    output_size = cf.nn_output_size

    map_arr_, _, _ = load_map(map_path)
    trans_probs = np.load(trans_map_path)
    trans_counts = np.load(trans_counts_path)

    average_free_space = np.mean(free_space(map_arr_))
    # num_samples = int(cf.nn_io_resampling_factor * average_free_space * \
    #                   map_arr_.size / cf.nn_output_size ** 2)
    num_samples = int(cf.nn_io_resampling_factor * \
                      map_arr_.size / cf.nn_output_size ** 2)

    inputs, outputs, masks = [], [], []

    for sample_ix in range(num_samples):
        if sample_ix % 10 == 0:
            print('generating network samples: {}/{}'.format(
                sample_ix + 1, num_samples))
        while True:
            # find a map segment which is suitable for training
            left, top = [
                random.randint(0, map_arr_.shape[ix] - input_size)
                for ix in [0, 1]
            ]
            x_in, y_in = [
                slice(start, start + input_size) for start in [left, top]
            ]
            x_out, y_out = [
                slice(start + int((input_size - output_size) / 2), start + int(
                    (input_size + output_size) / 2)) for start in [left, top]
            ]

            input = map_arr_[x_in, y_in].copy()
            output = trans_probs[x_out, y_out].copy()
            trans_counts_patch = trans_counts[x_out, y_out].copy()
            if cf.conditional_prob:
                axis = (4, 5)
            else:
                axis = (2, 3, 4, 5)
            with np.errstate(divide='ignore', invalid='ignore'):
                mask = np.sum(trans_counts_patch, axis=axis,
                              keepdims=True) / np.sum(trans_counts_patch)
                mask[~np.isfinite(mask)] = 0
            for ax in axis:
                mask = np.repeat(mask, trans_counts.shape[ax], axis=ax)

            if np.mean(map_arr_[x_out, y_out]) >= 0.5 or np.mean(input) > 0.5:
                # not enough empty space to predict
                continue

            break

        # inputs.append(input.copy())
        # outputs.append(flatten(output).copy())
        # masks.append(flatten(mask).copy())

        # augment that segment 8 times
        # for mirror in [False, True]:
        #     input_ = input
        #     output_ = output
        #     mask_ = mask
        #     if mirror:
        #         input_ = np.fliplr(input)
        #         output_ = flip(output)
        #         mask_ = flip(mask)
        #     for num_rot in range(4):
        #         input__ = np.rot90(input_, k=num_rot)
        #         output__ = np.rot90(output_, k=num_rot)
        #         mask__ = np.rot90(mask_, k=num_rot)
        #         inputs.append(input__.copy())
        #         outputs.append(output__.copy())
        #         masks.append(mask__.copy())

        # augment that segment 8 times
        for mirror in [False, True]:
            input_ = input
            output_ = output
            mask_ = mask
            if mirror:
                # flip left/right visually means
                # filp up/down the numpy array
                input_ = np.flipud(input)
                output_ = flip(output)
                mask_ = flip(mask)
            for num_rot in range(4):
                input__ = np.rot90(input_, k=num_rot)
                output__ = rotate(output_, k=num_rot)
                mask__ = rotate_masks(mask_, k=num_rot)
                inputs.append(input__.copy())
                outputs.append(flatten(output__).copy())
                masks.append(flatten(mask__).copy())

    return inputs, outputs, masks