Esempio n. 1
0
def passive_label_query(number_points, scaler):

    params = ap.set_params()
    dim = params["dim"]
    time_bound = params["time_bound"]
    # Pool of number_points input points, randomly selected
    x_samples = np.zeros((number_points, dim))
    for i in range(number_points):
        x_samples[i, :] = ap.rand_state()
    x_samples_scaled = scaler.transform(x_samples)

    start = time.time()
    # CALLING MATLAB FUNCTION
    eng = matlab.engine.start_matlab()
    x_add, y_add = eng.gen_labels(matlab.double((x_samples.T).tolist()),
                                  matlab.double([time_bound]),
                                  nargout=2)
    eng.quit()
    #x_add = np.array(x_add).T
    y_add = np.array(y_add[0])

    print("PASSIVE STAGING: uncertain points labeled in {} seconds".format(
        time.time() - start))

    return x_samples_scaled, y_add
Esempio n. 2
0
def active_label_query(sess,
                       type_rej_rule,
                       number_points,
                       scaler,
                       bnn,
                       rej_rule,
                       n_emp_samples,
                       decision_threshold=None):

    params = ap.set_params()
    dim = params["dim"]
    time_bound = params["time_bound"]
    # Pool of number_points input points, randomly selected
    x_samples = np.zeros((number_points, dim))
    for i in range(number_points):
        x_samples[i, :] = ap.rand_state()
    x_samples_scaled = scaler.transform(x_samples)

    samples_avg_probs, samples_std_probs, samples_avg_pred_class = compute_avg_std_pred_probs(
        sess, x_samples_scaled, n_emp_samples, bnn)
    samples_unc = np.vstack((samples_avg_probs, samples_std_probs))
    if type_rej_rule == "SVC":
        acc_rej_samples = apply_svc_rejection_rule(rej_rule, samples_unc)
    elif type_rej_rule == "GPC":
        acc_rej_samples = apply_gpc_rejection_rule(rej_rule, samples_unc,
                                                   decision_threshold)
    else:
        print("Unknown type of rejection rule!")
    #x_rej = x_samples[(1-acc_rej_samples).astype(bool)]
    #x_rej_scaled = x_samples_scaled[(1-acc_rej_samples).astype(bool)]
    x_rej = x_samples[(acc_rej_samples < 0)]
    x_rej_scaled = x_samples_scaled[(acc_rej_samples < 0)]
    print('Number of points to add: {}\n'.format(x_rej_scaled.shape[0]))
    start = time.time()
    #rej_samples_avg_pred_class = samples_avg_pred_class[(1-acc_rej_samples).astype(bool)]
    #rej_samples_avg_probs = samples_avg_probs[(1-acc_rej_samples).astype(bool)]
    #rej_samples_std_probs = samples_std_probs[(1-acc_rej_samples).astype(bool)]
    rej_samples_avg_pred_class = samples_avg_pred_class[(acc_rej_samples < 0)]
    rej_samples_avg_probs = samples_avg_probs[(acc_rej_samples < 0)]
    rej_samples_std_probs = samples_std_probs[(acc_rej_samples < 0)]

    rej_samples_unc = np.vstack((rej_samples_avg_probs, rej_samples_std_probs))
    # CALLING MATLAB FUNCTION
    eng = matlab.engine.start_matlab()
    x_add, y_add = eng.gen_labels(matlab.double((x_rej.T).tolist()),
                                  matlab.double([time_bound]),
                                  nargout=2)
    eng.quit()
    #x_add = np.array(x_add).T
    y_add = np.array(y_add[0])

    print(
        "STAGING: uncertain points labeled in {} seconds".format(time.time() -
                                                                 start))

    return x_rej_scaled, y_add, rej_samples_unc, rej_samples_avg_pred_class
Esempio n. 3
0
def active_label_query(number_points,
                       scaler,
                       local_guide,
                       rej_rule,
                       n_emp_samples,
                       decision_threshold=None):
    start = time.time()
    params = ap.set_params()
    dim = params["dim"]
    time_bound = params["time_bound"]
    # Pool of number_points input points, randomly selected
    x_samples = np.zeros((number_points, dim))
    for i in range(number_points):
        x_samples[i, :] = ap.rand_state()
    x_samples_scaled = scaler.transform(x_samples)
    x_samples_scaled_t = torch.FloatTensor(x_samples_scaled)
    samples_avg_pred_classes, samples_avg_probs, samples_std_probs = predictions_on_set(
        x_samples_scaled_t, number_points, n_emp_samples, local_guide)

    samples_unc = np.vstack((samples_avg_probs, samples_std_probs))

    acc_rej_samples = apply_gpc_rejection_rule(rej_rule, samples_unc,
                                               decision_threshold)

    x_rej = x_samples[(acc_rej_samples < 0)]
    x_rej_scaled = x_samples_scaled[(acc_rej_samples < 0)]

    eng = matlab.engine.start_matlab()
    x_add, y_add = eng.gen_labels(matlab.double((x_rej.T).tolist()),
                                  matlab.double([time_bound]),
                                  nargout=2)
    eng.quit()
    #x_add = np.array(x_add).T
    y_add = np.array(y_add[0])

    print(
        "STAGING: uncertain points labeled in {} seconds".format(time.time() -
                                                                 start))

    return x_rej_scaled, y_add
Esempio n. 4
0
def active_label_query(number_points, scaler, alphas, nsc, rej_rule, phase):

    params = ap.set_params()
    dim = params["dim"]
    time_bound = params["time_bound"]
    # Pool of number_points input points, randomly selected
    x_samples = np.zeros((number_points, dim))
    for i in range(number_points):
        x_samples[i, :] = ap.rand_state()

    x_samples_scaled = scaler.transform(x_samples)

    # NSC prob prediction on these samples
    samples_unc, samples_pred_probs = compute_test_uncertainties(
        nsc, alphas, x_samples_scaled)

    acc_rej_samples = apply_svc_rejection_rule(rej_rule, samples_unc)
    x_rej = x_samples[(
        1 - acc_rej_samples).astype(bool)]  #.reshape((number_points,))
    x_rej_scaled = x_samples_scaled[(1 - acc_rej_samples).astype(bool)]
    print('Number of points to add: {}\n'.format(x_rej.shape[0]))

    start = time.time()

    # CALLING MATLAB FUNCTION
    eng = matlab.engine.start_matlab()
    x_add, y_add = eng.gen_labels(matlab.double((x_rej.T).tolist()),
                                  matlab.double([time_bound]),
                                  nargout=2)
    eng.quit()
    #x_add = np.array(x_add).T
    y_add = np.array(y_add[0])

    print(
        "STAGING: uncertain points labeled in {} seconds".format(time.time() -
                                                                 start))

    return x_rej_scaled, y_add  #scaler.transform(x_add)