Example #1
0
def save_inverse_reachability(robot, arm, grasp_type, tool_link,
                              gripper_from_base_list):
    # TODO: store value of torso and roll joint for the IK database. Sample the roll joint.
    # TODO: hash the pr2 urdf as well
    filename = IR_FILENAME.format(grasp_type, arm)
    path = get_database_file(filename)
    data = {
        'filename': filename,
        'robot': get_body_name(robot),
        'grasp_type': grasp_type,
        'arm': arm,
        'torso': get_group_conf(robot, 'torso'),
        'carry_conf': get_carry_conf(arm, grasp_type),
        'tool_link': tool_link,
        'ikfast': is_ik_compiled(),
        'gripper_from_base': gripper_from_base_list,
    }
    write_pickle(path, data)

    if has_gui():
        handles = []
        for gripper_from_base in gripper_from_base_list:
            handles.extend(
                draw_point(point_from_pose(gripper_from_base),
                           color=(1, 0, 0)))
        wait_for_user()
    return path
Example #2
0
def save_experiments(experiments_dir, experiments):
    if experiments_dir is None:
        return None
    data_path = os.path.join(experiments_dir, 'experiments.pk{}'.format(get_python_version()))
    write_pickle(data_path, experiments)
    print('Saved', data_path)
    return data_path
Example #3
0
def save_learner(data_dir, learner):
    if data_dir is None:
        return False
    #domain = learner.func
    #data_dir = os.path.join(MODEL_DIRECTORY, domain.name)
    #name = learner.name
    name = get_label(learner.algorithm)
    mkdir(data_dir)
    learner_path = os.path.join(data_dir, '{}.pk{}'.format(name, get_python_version()))
    print('Saved', learner_path)
    write_pickle(learner_path, learner)
    return True
def create_inverse_reachability(robot, body, table, arm, grasp_type, num_samples=500):
    link = get_gripper_link(robot, arm)
    movable_joints = get_movable_joints(robot)
    default_conf = get_joint_positions(robot, movable_joints)
    gripper_from_base_list = []
    grasps = GET_GRASPS[grasp_type](body)

    while len(gripper_from_base_list) < num_samples:
        box_pose = sample_placement(body, table)
        set_pose(body, box_pose)
        grasp_pose = random.choice(grasps)
        gripper_pose = multiply(box_pose, invert(grasp_pose))
        set_joint_positions(robot, movable_joints, default_conf)
        base_conf = next(uniform_pose_generator(robot, gripper_pose))
        set_base_values(robot, base_conf)
        if pairwise_collision(robot, table):
            continue
        conf = inverse_kinematics(robot, link, gripper_pose)
        if (conf is None) or pairwise_collision(robot, table):
            continue
        gripper_from_base = multiply(invert(get_link_pose(robot, link)), get_pose(robot))
        gripper_from_base_list.append(gripper_from_base)
        print('{} / {}'.format(len(gripper_from_base_list), num_samples))

    filename = IR_FILENAME.format(grasp_type, arm)
    path = get_database_file(filename)
    data = {
        'filename': filename,
        'robot': get_body_name(robot),
        'grasp_type': grasp_type,
        'arg': arm,
        'carry_conf': get_carry_conf(arm, grasp_type),
        'gripper_link': link,
        'gripper_from_base': gripper_from_base_list,
    }
    write_pickle(path, data)
    return path
Example #5
0
def main():
    parser = argparse.ArgumentParser()
    parser.add_argument('paths', nargs='*', help='Paths to the data.')
    #parser.add_argument('-a', '--active', type=int, default=0, # None
    #                    help='The number of active samples to collect')
    parser.add_argument('-l', '--learner', default=None,
                        help='Path to the learner that should be used')
    parser.add_argument('-n', '--num_trials', type=int, default=100,
                        help='The number of samples to collect')
    parser.add_argument('-s', '--save', action='store_true',
                        help='Whether to save the learners')
    parser.add_argument('-r', '--num_rounds', type=int, default=1,
                        help='The number of rounds to collect')
    #parser.add_argument('-t', '--num_train', type=int, default=None,
    #                    help='The size of the training set')
    args = parser.parse_args()

    # TODO: be careful that paging isn't altering the data
    # TODO: don't penalize if the learner identifies that it can't make a good prediction
    # TODO: use a different set of randomized parameters for train and test

    include_none = False
    serial = is_darwin()


    #training_sizes = inclusive_range(50, 500, 25)
    #training_sizes = inclusive_range(25, 100, 5)
    #training_sizes = inclusive_range(25, 100, 5)
    training_sizes = inclusive_range(10, 50, 5)
    #training_sizes = inclusive_range(100, 1000, 100)
    #training_sizes = [20]
    #training_sizes = [1500]

    #kernels = ['RBF', 'Matern52', 'MLP']
    kernels = ['MLP']

    #hyperparameters = [None]
    #hyperparameters = [True]
    hyperparameters = [True, None] # None,

    query_type = BEST # BEST | CONFIDENT | REJECTION | ACTIVE # type of query used to evaluate the learner
    is_adaptive = False
    max_test = 50 #
    #alphas = np.linspace(0.0, 0.9, num=5, endpoint=True)
    alphas = [0.0, .8, .9, .99]
    #alphas = [None]  # Use the default (i.e. GP parameters)

    use_vars = [True]
    binary = False
    split = UNIFORM # BALANCED

    # Omitting failed labels is okay because they will never be executed
    algorithms = []
    #algorithms += [(Algorithm(BATCH_GP, kernel=kernel, hyperparameters=hype, use_var=use_var), [num_train])
    #               for num_train, kernel, hype, use_var in product(training_sizes, kernels, hyperparameters, use_vars)]
    algorithms += [(Algorithm(STRADDLE_GP, kernel, hype, use_var), training_sizes)
                   for kernel, hype, use_var in product(kernels, hyperparameters, use_vars)]
    #algorithms += [(Algorithm(rf_model, p_explore=None, use_var=use_var), [num_train])
    #               for rf_model, num_train, use_var in product(RF_MODELS, training_sizes, use_vars)]
    #algorithms += [(Algorithm(nn_model, p_explore=None), [num_train])
    #               for nn_model, num_train in product(NN_MODELS, training_sizes)]
    #algorithms += [(Algorithm(RANDOM), None), (Algorithm(DESIGNED), None)]
    print('Algorithms:', algorithms)
    print('Split:', split)

    trials_per_round = sum(1 if train_sizes is None else
                           (train_sizes[-1] - train_sizes[0] + len(train_sizes))
                           for _, train_sizes in algorithms)
    num_experiments = args.num_rounds*trials_per_round

    date_name = datetime.datetime.now().strftime(DATE_FORMAT)
    size_str = '[{},{}]'.format(training_sizes[0], training_sizes[-1])
    #size_str = '-'.join(map(str, training_sizes))
    experiments_name = '{}_r={}_t={}_n={}'.format(date_name, args.num_rounds, size_str, args.num_trials) #'19-08-09_21-44-58_r=5_t=[10,150]_n=1'#
    #experiments_name = 't={}'.format(args.num_rounds)
    # TODO: could include OS and username if desired

    domain = load_data(args.paths)
    print()
    print(domain)
    X, Y, W = domain.get_data(include_none=include_none)
    print('Total number of examples:', len(X))
    if binary:
        # NN can fit perfectly when binary
        # Binary seems to be outperforming w/o
        Y = threshold_scores(Y)

    max_train = len(X) - max_test #min(max([0] + [active_sizes[0] for _, active_sizes in algorithms
                     #          if active_sizes is not None]), len(X))

    #parameters = {
    #    'include None': include_none,
    #    'binary': binary,
    #    'split': split,
    #}

    print('Name:', experiments_name)
    print('Experiments:', num_experiments)
    print('Max train:', max_train)
    print('Include None:', include_none)
    print('Examples: n={}, d={}'.format(*X.shape))
    print('Binary:', binary)
    print('Estimated hours:', num_experiments * SEC_PER_EXPERIMENT / HOURS_TO_SECS)
    user_input('Begin?')
    # TODO: residual learning for sim to real transfer
    # TODO: can always be conservative and add sim negative examples

    # TODO: combine all data to write in one folder
    data_dir = os.path.join(DATA_DIRECTORY, domain.name) # EXPERIMENT_DIRECTORY
    experiments_dir = os.path.join(data_dir, experiments_name)
    mkdir(experiments_dir)
    start_time = time.time()
    experiments = []
    for round_idx in range(args.num_rounds):
        round_dir = os.path.join(data_dir, experiments_name, str(round_idx))
        mkdir(round_dir)
        seed = hash(time.time())
        train_test_file = os.path.join(round_dir, 'data.pk3')
        if not os.path.exists(train_test_file):
            X_train, Y_train, X_test, Y_test = split_data(X, Y, split, max_train)
            X_test, Y_test = X_test[:max_test], Y_test[:max_test]
            write_pickle(train_test_file, (X_train, Y_train, X_test, Y_test))
        else:
            X_train, Y_train, X_test, Y_test = read_pickle(train_test_file)

        print('Train examples:', X_train.shape)
        print('Test examples:', X_test.shape)
        # TODO: need to be super careful when running with multiple contexts

        for algorithm, active_sizes in algorithms:
            # active_sizes = [first #trainingdata selected from X_train, #active exploration + #trainingdata]
            print(SEPARATOR)
            print('Round: {} | {} | Seed: {} | Sizes: {}'.format(round_idx, algorithm, seed, active_sizes))
            # TODO: allow keyboard interrupt
            if active_sizes is None:
                learner = algorithm.name
                active_size = None
                train_confusion = None
                experiments.append(evaluate_learner(domain, seed, train_confusion, X_test, Y_test, algorithm, learner,
                                                    active_size, args.num_trials, alphas,
                                                    serial))
            else:
                # [10 20 25] take first 10 samples from X_train to train the model, 10 samples chosen actively
                # sequentially + evaluate model, 5 samples chosen actively sequentially + evaluate model
                # Could always keep around all the examples and retrain
                # TODO: segfaults when this runs in parallel
                # TODO: may be able to retrain in parallel if I set OPENBLAS_NUM_THREADS
                learner_prior_nx = 0
                '''
                if algorithm.hyperparameters:
                    if domain.skill == 'pour':
                        learner_file = '/Users/ziw/ltamp_pr2/data/pour_19-06-13_00-59-21/19-08-09_19-30-01_r=10_t=[50,400]_n=1/{}/gp_active_mlp_true_true.pk3'.format(
                            round_idx)
                    elif domain.skill == 'scoop':
                        learner_file = '/Users/ziw/ltamp_pr2/data/scoop_19-06-10_20-16-59_top-diameter/19-08-09_19-34-56_r=10_t=[50,400]_n=1/{}/gp_active_mlp_true_true.pk3'.format(
                            round_idx)
                    learner = read_pickle(learner_file)
                    learner_prior_nx = learner.nx
                    learner.retrain(newx=X_train[:active_sizes[0]], newy=Y_train[:active_sizes[0], None])
                else:
                '''
                learner, train_confusion = create_learner(domain, X_train, Y_train, split, algorithm,
                                                          num_train=active_sizes[0], query_type=query_type,
                                                          is_adaptive=is_adaptive)

                if algorithm.name == STRADDLE_GP:
                    X_select, Y_select = X_train[active_sizes[0]:], Y_train[active_sizes[0]:]

                for active_size in active_sizes:
                    num_active = active_size - learner.nx + learner_prior_nx# learner.nx is len(learner.xx)
                    print('\nRound: {} | {} | Seed: {} | Size: {} | Active: {}'.format(
                        round_idx, algorithm, seed, active_size, num_active))
                    if algorithm.name == STRADDLE_GP:
                        X_select, Y_select = active_learning_discrete(learner, num_active, X_select, Y_select)
                    #if args.save:
                    save_learner(round_dir, learner)
                    experiments.append(evaluate_learner(domain, seed, None, X_test, Y_test,
                                                        algorithm, learner,
                                                        active_size, args.num_trials, alphas,
                                                        serial))
                    save_experiments(experiments_dir, experiments)

    print(SEPARATOR)
    if experiments:
        save_experiments(experiments_dir, experiments)
        plot_experiments(domain, experiments_name, experiments_dir, experiments,
                         include_none=False)
                         #include_none=include_none)
        print('Experiments:', experiments_dir)
    print('Total experiments:', len(experiments))
    print('Total hours:', elapsed_time(start_time) / HOURS_TO_SECS)
Example #6
0
def train_parallel(args, n=1):
    from extrusion.run import plan_extrusion
    assert SKIP_PERCENTAGE == 0
    initial_time = time.time()

    problems = sorted(set(enumerate_problems()) - set(EXCLUDE))
    #problems = ['four-frame']
    #problems = ['simple_frame', 'topopt-101_tiny', 'topopt-100_S1_03-14-2019_w_layer']

    algorithms = list(ALGORITHMS)
    if args.disable:
        for algorithm in LOOKAHEAD_ALGORITHMS:
            if algorithm in algorithms:
                algorithms.remove(algorithm)
    #algorithms = ['regression']

    heuristics = HEURISTICS
    #heuristics = DISTANCE_HEURISTICS + COST_HEURISTICS

    seeds = list(range(args.num))
    if n is None:
        n = len(seeds)
    groups = list(chunks(seeds, n=n))

    print('Chunks: {}'.format(len(groups)))
    print('Problems ({}): {}'.format(len(problems), problems))
    #problems = [path for path in problems if 'simple_frame' in path]
    print('Algorithms ({}): {}'.format(len(algorithms), algorithms))
    print('Heuristics ({}): {}'.format(len(heuristics), heuristics))
    jobs = [[
        Configuration(seed, problem, algorithm, heuristic, args.max_time,
                      args.cfree, args.disable, args.stiffness, args.motions,
                      args.ee_only) for seed, algorithm, heuristic in product(
                          group, algorithms, heuristics)
    ] for problem, group in product(problems, groups)]
    # TODO: separate out the algorithms again
    # TODO: print the size per job
    print('Jobs: {}'.format(len(jobs)))

    serial = is_darwin()
    available_cores = cpu_count()
    num_cores = max(1, min(1 if serial else available_cores - 4, len(jobs)))
    print('Max Cores:', available_cores)
    print('Serial:', serial)
    print('Using Cores:', num_cores)
    date = datetime.datetime.now().strftime(DATE_FORMAT)
    filename = '{}.pk{}'.format(date, get_python_version())
    path = os.path.join(EXPERIMENTS_DIR, filename)
    print('Data path:', path)

    user_input('Begin?')
    start_time = time.time()
    timeouts = 0
    pool = Pool(processes=num_cores)  # , initializer=mute)
    generator = pool.imap_unordered(plan_extrusion, jobs, chunksize=1)
    results = []
    while True:
        # TODO: randomly sort instead
        last_time = time.time()
        try:
            for config, data in generator.next():  # timeout=2 * args.max_time)
                results.append((config, data))
                print('{}/{} completed | {:.3f} seconds | timeouts: {} | {}'.
                      format(len(results), len(jobs), elapsed_time(start_time),
                             timeouts,
                             datetime.datetime.now().strftime(DATE_FORMAT)))
                print(config, data)
            if results:
                write_pickle(path, results)
                print('Saved', path)
        except StopIteration:
            break
        # except TimeoutError:
        #     # TODO: record this as a failure? Nothing is saved though...
        #     timeouts += 1
        #     #traceback.print_exc()
        #     print('Error! Timed out after {:.3f} seconds'.format(elapsed_time(last_time)))
        #     break # This kills all jobs
        #     #continue # This repeats jobs until success
    print('Total time:', elapsed_time(initial_time))
    return results
Example #7
0
def main():
    parser = argparse.ArgumentParser()
    parser.add_argument('trainsize',
                        default=2000,
                        type=int,
                        help='training set size')
    parser.add_argument('expid', default=1, type=int, help='experiment ID')
    parser.add_argument(
        'beta_lambda',
        type=float,
        default=0.9,
        help='lambda parameter for computing beta from best beta')
    parser.add_argument('sample_strategy_id', default=1, type=int)  # 1, 2, 3
    parser.add_argument(
        'paths',
        default=[os.path.join(get_data_dir('pour'), 'trials_n=10000.json')],
        nargs='*',
        help='Paths to the data.')
    parser.add_argument('-u',
                        '--use_hyper',
                        action='store_true',
                        help='When enabled, use existing hyper parameter.')
    parser.add_argument('-o',
                        '--use_obstacle',
                        action='store_true',
                        help='When enabled, no obstacle is used in the scene.')

    args = parser.parse_args()
    beta_lambda = args.beta_lambda
    sample_strategy = SAMPLE_STRATEGIES[args.sample_strategy_id]
    global SEED
    SEED = args.expid
    set_seed(SEED)
    n_train_tasks = 50
    n_test_tasks = 20

    train_tasks_seeds = get_seeds(n_train_tasks)
    test_tasks_seeds = get_seeds(n_test_tasks)
    print('loading data')
    domain = load_data(args.paths)
    data = domain.create_dataset(include_none=True, binary=False)
    data.shuffle()
    X, Y, W = data.get_data()
    print('finished obtaining x y data')

    n_train = args.trainsize
    X = X[:n_train]
    Y = Y[:n_train]

    print('initializing ActiveGP with #datapoints = {}'.format(len(X)))

    hype = None

    if 'pour' in args.paths[0] and args.use_hyper:
        hype = POUR_MLP_HYPERPARAM_3000
    elif 'scoop' in args.paths[0] and args.use_hyper:
        hype = SCOOP_MLP_HYPERPARAM_3000

    learner = ActiveGP(domain,
                       initx=X,
                       inity=Y,
                       hyperparameters=hype,
                       sample_time_limit=60,
                       beta_lambda=beta_lambda)
    learner.retrain(num_restarts=10)

    exp_file = 'tasklengthscale_sampling_trainsize={}_beta_lambdda={}_strategy_{}_obs_{}_expid_{}.pk3'.format(
        len(X), beta_lambda, args.sample_strategy_id, int(args.use_obstacle),
        args.expid)
    exp_dirname = os.path.dirname(args.paths[0])
    if args.use_hyper:
        exp_dirname = os.path.join(exp_dirname, 'default_hyper/')
        mkdir(exp_dirname)

    exp_file = os.path.join(exp_dirname, exp_file)
    print('saving results to ', exp_file)

    results = []

    if sample_strategy != DIVERSELK:
        n_train_tasks = 0
        # no need to train

    prev_tasklengthscale = None
    for i in range(n_train_tasks + 1):
        test_results = []
        print('task_lengthscal = {}'.format(learner.task_lengthscale))
        print('================BEGIN TESTING==============')
        if prev_tasklengthscale is not None and (
                learner.task_lengthscale == prev_tasklengthscale).all():
            test_results = results[-1][1]
        else:
            for j in range(n_test_tasks):
                if sample_strategy == DIVERSELK:
                    test_sample_strategy = DIVERSE
                else:
                    test_sample_strategy = sample_strategy
                seed = test_tasks_seeds[j]
                test_result = eval_task_with_seed(
                    domain,
                    seed,
                    learner,
                    sample_strategy=test_sample_strategy,
                    obstacle=args.use_obstacle)
                test_results.append(test_result)

        prev_tasklengthscale = learner.task_lengthscale.copy()

        if i != n_train_tasks:
            seed = train_tasks_seeds[i]
            train_result = eval_task_with_seed(domain,
                                               seed,
                                               learner,
                                               sample_strategy=sample_strategy,
                                               obstacle=args.use_obstacle)
        else:
            train_result = None

        results.append((train_result, test_results))
        write_pickle(exp_file,
                     (results, SEED, train_tasks_seeds, test_tasks_seeds))
Example #8
0
def save_experiments(data_path, experiments):
    if data_path is None:
        return None
    write_pickle(data_path, experiments)
    print('Saved experiments:', data_path)
    return data_path