示例#1
0
def make_curves_for_unique_config(config_id):
    start_pos, start_lm = None, None
    for env_id in range(config_id, config_id + NEW_CONFIG_EVERY_N, 1):
        config = load_env_config(env_id)
        curve_path = get_curve_path(env_id)
        plot_path = get_curve_plot_path(env_id)
        start_pos, start_lm = make_new_curve(config, curve_path, plot_path,
                                             start_pos, start_lm)
def make_annotations(end_i):
    P.initialize_experiment()

    annotations = {"train": [], "test": [], "dev": []}

    train_range, dev_range, test_range = get_split_ranges(end_i)
    assert (
        train_range[1] - train_range[0]
    ) % NEW_CONFIG_EVERY_N == 0, "training set size must be a multiple of NEW_CONFIG_EVERY_N"

    for config_id in range(end_i):
        config_path = paths.get_env_config_path(config_id)
        path_path = paths.get_curve_path(config_id)
        instruction_path = paths.get_instructions_path(config_id)

        with open(config_path) as fp:
            config = json.load(fp)

        with open(path_path) as fp:
            curve = json.load(fp)

        with open(instruction_path) as fp:
            instruction = fp.readline()
        token_list = clean_instruction(instruction)

        curve_np = np.asarray(list(zip(curve["x_array"], curve["z_array"])))

        split = "train" if train_range[0] <= config_id < train_range[1] else \
                "dev" if dev_range[0] <= config_id < dev_range[1] else \
                "test" if test_range[0] <= config_id < test_range[1] else None

        #start_dir = np.asarray(config["startHeading"]) - np.asarray(config["startPos"])
        start_dir = curve_np[1] - curve_np[0]
        start_yaw = vec_to_yaw(start_dir)
        start_yaw_cfg = np.rad2deg(-start_yaw + np.pi / 2)

        dataset = {
            "id": str(config_id),
            "start_z": [curve["z_array"][0]],
            "start_x": [curve["x_array"][0]],
            "end_z": [curve["z_array"][-1]],
            "end_x": [curve["x_array"][-1]],
            "start_rot": [start_yaw_cfg],
            "config_file": "configs/random_config_%d.json" % config_id,
            "instructions_file":
            "instructions/instructions_%d.txt" % config_id,
            "path_file": "paths/random_curve_%d.json" % config_id,
            "moves": [],
            "valid": True,
            "num_tokens": [len(token_list)],
            "instructions": [instruction]
        }
        annotations[split].append(dataset)
        print("Added annotations for env: " + str(config_id))

    with open(paths.get_instruction_annotations_path(), "w") as fp:
        json.dump(annotations, fp)
示例#3
0
文件: env.py 项目: dxsun/drif
def load_path(env_id, anno=True):
    anno_curve_path = paths.get_anno_curve_path(env_id)
    if os.path.isfile(anno_curve_path) and anno:
        path = load_json(anno_curve_path)
    else:
        path = load_json(paths.get_curve_path(env_id))
    if path is None:
        print("Ground truth path not found for env: " + str(env_id))
        return path
    x_arr = path['x_array']
    y_arr = path['z_array']
    path = np.asarray(list(zip(x_arr, y_arr)))
    return path
示例#4
0
def make_annotations(start_i, end_i):
    P.initialize_experiment()

    annotations = {
        "train": [],
        "test": [],
        "dev": []
    }

    for config_id in range(start_i, end_i):
        config_path = paths.get_env_config_path(config_id)
        path_path = paths.get_curve_path(config_id)
        instruction_path = paths.get_instructions_path(config_id)

        with open(config_path) as fp:
            config = json.load(fp)

        with open(path_path) as fp:
            curve = json.load(fp)

        with open(instruction_path) as fp:
            instruction = fp.readline()
        token_list = clean_instruction(instruction)

        split = get_split((config_id % 100) / 100.0)

        start_dir = np.asarray(config["startHeading"]) - np.asarray(config["startPos"])
        start_yaw = vec_to_yaw(start_dir)
        start_yaw_cfg = np.rad2deg(-start_yaw + np.pi/2)

        dataset = {
            "id": str(config_id),
            "start_z": [curve["z_array"][0]],
            "start_x": [curve["x_array"][0]],
            "end_z": [curve["z_array"][-1]],
            "end_x": [curve["x_array"][-1]],
            "start_rot": [start_yaw_cfg],
            "config_file": "configs/random_config_%d.json" % config_id,
            "instructions_file": "instructions/instructions_%d.txt" % config_id,
            "path_file": "paths/random_curve_%d.json" % config_id,
            "moves": [],
            "valid": True,
            "num_tokens": [len(token_list)],
            "instructions": [instruction]
        }
        annotations[split].append(dataset)
        print ("Added annotations for env: " + str(config_id))

    with open(paths.get_instruction_annotations_path(), "w") as fp:
        json.dump(annotations, fp)
示例#5
0
def make_template_curve(config, config_id):
    # try making curve until it succeeds (doesn't go out of bounds)
    print("making curve for config id " + str(config_id))

    cnt = 0
    while True:
        if PERMUTATION_TEST:
            num_objects = len(config["landmarkName"])
            permutation, landmark_idx, side_idx = get_permutation_side_selection(
                config_id, num_objects)
            landmark_choices = [config["landmarkName"][landmark_idx]]
            side_choices = ["left"] if side_idx == 0 else ["right"]
            template = generate_template(template_types,
                                         landmark_choices,
                                         sampling="consistent",
                                         side_choices=side_choices)
        else:
            # Create a template for one of the template types that we can choose from
            template = generate_template(template_types,
                                         config["landmarkName"],
                                         sampling=SAMPLING_MODE)

        pos_array = try_make_template_curve(config, template)
        cnt += 1
        if pos_array is not None:
            break
        elif cnt > 500:
            print(f"FAILED GENERATING CURVES FOR ENV: {config_id}")

    assert isinstance(pos_array, list)

    # write pos array to file
    pos_lists = {
        "x_array": [x for x, _ in pos_array],
        "z_array": [y for _, y in pos_array]
    }
    curve_path = paths.get_curve_path(config_id)
    os.makedirs(os.path.dirname(curve_path), exist_ok=True)
    with open(curve_path, 'w') as fp:
        json.dump(pos_lists, fp)

    # Write the template to file
    template_data = {
        "type": str(template.type),
        "landmark1": str(template.landmark1),
        "landmark2": str(template.landmark2),
        "side": str(template.side),
        "dir": str(template.dir),
        "instruction": str(template.instruction)
    }

    template_path = paths.get_template_path(config_id)
    os.makedirs(os.path.dirname(template_path), exist_ok=True)
    with open(template_path, "w") as fp:
        json.dump(template_data, fp)

    instruction_path = paths.get_instructions_path(config_id)
    os.makedirs(os.path.dirname(instruction_path), exist_ok=True)
    with open(instruction_path, "w") as fp:
        fp.write(template.instruction)

    landmark_pos = get_landmark_pos(config)
    lake_pos = get_lake_pos(config)

    plt.figure()
    axes = plt.axes()
    # plot landmarks
    axes.plot([x for x, _ in landmark_pos], [y for _, y in landmark_pos], "bo")
    # plot route starting point
    x_start, y_start = pos_array[0]
    axes.plot([x_start], [y_start], "ro", ms=10.0)
    axes.plot([x for x, _ in pos_array[1:]], [y for _, y in pos_array[1:]],
              "r.")
    # plot lake
    axes.plot([x for x, _ in lake_pos], [y for _, y in lake_pos], "b.")

    axes.set_xlim(list(X_RANGE))
    axes.set_ylim(list(Y_RANGE))
    #plt.show()
    plot_path = os.path.join(paths.get_plots_dir(),
                             "generated_path_" + str(config_id) + ".png")
    os.makedirs(os.path.dirname(plot_path), exist_ok=True)
    plt.savefig(plot_path)