def train_statistical_shape_model(kpts):
    mean_shape = np.mean(kpts, axis=0)
    mean_shape = mean_shape.reshape(
        (1, mean_shape.shape[0], mean_shape.shape[1]))

    cov = create_covariance_matrix(kpts, mean_shape)
    pcs = pca(cov)

    stock_weights = np.zeros(pcs[0].shape)
    stock_weights.fill(1)
    neg_weights = np.zeros(pcs[0].shape)
    neg_weights.fill(-0.5)
    pos_weights = np.zeros(pcs[0].shape)
    pos_weights.fill(0.5)

    reconstruct_test_shape(kpts, mean_shape, pcs, stock_weights,
                           "Reconstruction, Weights = 1")
    reconstruct_test_shape(kpts, mean_shape, pcs, neg_weights,
                           "Reconstruction, Weights = -0.5")
    reconstruct_test_shape(kpts, mean_shape, pcs, pos_weights,
                           "Reconstruction, Weights = 0.5")
    utils.visualize_hands(kpts, "KPTS Values", delay=0.1, ax=None, clear=False)
    utils.visualize_hands(mean_shape, "Mu", delay=0.1, ax=None, clear=False)

    return mean_shape, pcs, stock_weights

    pass
Exemple #2
0
def procrustres_analysis(kpts, max_iter=int(1e3), min_error=1e-5):

    #kpts = normalize_hands_center_mass(kpts)

    num_instances = kpts.shape[0]

    aligned_kpts = kpts.copy()
    mean = calculate_mean_shape(aligned_kpts)
    continue_iteration = True
    all_means = mean.copy()
    all_means.shape = (1, 56, 2)

    #original_angle = get_angle_landmark(kpts[0], 0)
    reference_mean = kpts[0].copy()

    iter = 0
    while iter < max_iter and continue_iteration:

        reference_mean = normalize_mean(reference_mean)  #,1, original_angle)

        threed_mean = reference_mean.copy()
        threed_mean.shape = (1, 56, 2)

        all_means = np.append(all_means, threed_mean, axis=0)
        # align shapes to mean shape
        #aligned_kpts = procrustres_analysis_step(aligned_kpts, reference_mean)

        aligned_kpts, error_sum = procrustres_analysis_step(
            kpts, reference_mean)

        #
        reference_mean = calculate_mean_shape(aligned_kpts)

        RMS = compute_avg_error(aligned_kpts, reference_mean)

        ##################### Your Part Here #####################
        if (RMS < min_error):
            continue_iteration = False

        iter += 1
        ##########################################################
    #mean = calculate_mean_shape(aligned_kpts)
    #print("shap aligned",aligned_kpts.shape)
    #mean.shape=  (1, mean.shape[0], mean.shape[1])
    #print(mean)
    #print("all means shape:",all_means.shape)

    #utils.visualize_hands(mean,"first mean",delay=0.1)
    #utils.visualize_hands(all_means,"means",delay=0.00001)
    #utils.visualize_hands(kpts,"input",delay=0.001)
    # visualize

    utils.visualize_hands(aligned_kpts, "aligned", delay=0.0001)
    # visualize mean shape
    last_mean = all_means[-1]
    last_mean.shape = (1, mean.shape[0], mean.shape[1])
    utils.visualize_hands(last_mean, "mean", delay=0.00001)
    a = input("press any key to close windows")

    return aligned_kpts
Exemple #3
0
def reconstruct_test_shape(kpts, mean, pcs, pc_weight):
    #ToDo
    N = 56
    print("Hik: ", pc_weight)

    weighted_pc = pcs * np.expand_dims(pc_weight, axis=1)
    X = kpts + weighted_pc
    utils.visualize_hands(utils.convert_samples_to_xy(X), "reconstruction")

    pass
def reconstruct_test_shape(kpts, mean, pcs, pc_weight, title=""):

    pos = np.array(range(kpts.shape[0]))
    reconst = np.array(
        list(map(lambda x: rconstFunc(kpts, mean, pcs, pc_weight, x),
                 pos[:])))  ## Not a loop!

    utils.visualize_hands(reconst, title, delay=0.1, ax=None, clear=False)

    return reconst
Exemple #5
0
def task_1():
    # Loading Trainig Data
    kpts = get_keypoints(hands_orig_train)

    # calculate mean
    ##picking one data instance as the mean
    mean_data = kpts[0, :]

    # we want to visualize the data first

    utils.visualize_hands(kpts, "hands", delay=0.001)

    task1.procrustres_analysis(kpts)
Exemple #6
0
def visualize_impact_of_pcs(mean, pcs, pc_weights):
    # your part here
    # Section 17.5.2 (Prince)
    for wtIdx in range(pc_weights.shape[0]):
        stackShapes = mean.copy()
        for i in range(1, 3):
            tempShape = mean.copy()
            tempShape = tempShape.reshape(-1,2)
            # Set hidden weights as -1 * pc_weights or 1 * pc_weights
            weights = pcs[:,wtIdx] * pow(-1, i) * pc_weights[wtIdx]
            tempShape[0:pcs.shape[0], :] += weights.reshape(-1,1)
            stackShapes = np.vstack((stackShapes, tempShape[np.newaxis]))
        utils.visualize_hands(stackShapes, 'Component Weight=' + str(pc_weights[wtIdx]))
Exemple #7
0
def task_1():
    # Loading Trainig Data
    kpts = get_keypoints(hands_orig_train)

    # calculate mean
    # ToDO
    meanShape = task1.calculate_mean_shape(kpts)
    meanShape = meanShape[np.newaxis, ...]

    # we want to visualize the data first
    # ToDO
    utils.visualize_hands(meanShape, 'Mean Shape')

    task1.procrustres_analysis(kpts)
    time.sleep(5)
Exemple #8
0
def task_2_2(mean, pcs, pc_weights):

    kpts = get_keypoints(hands_aligned_test)

    reconst = task2.reconstruct_test_shape(
        kpts, mean, pcs, pc_weights, title="Test Reconstruction from Model")
    utils.visualize_hands(kpts,
                          "Actual Test Points",
                          delay=10,
                          ax=None,
                          clear=False)

    error = np.sqrt(np.sum(np.power(kpts - reconst, 2)) / kpts.shape[0])
    print("RMS Error: ", error)

    time.sleep(20)
Exemple #9
0
def task_1():
    # Loading Trainig Data
    kpts = get_keypoints(hands_orig_train)

    # calculate mean
    # ToDO
    mean_shape = task1.calculate_mean_shape(kpts)

    # we want to visualize the data first
    # ToDO

    utils.visualize_hands(utils.convert_samples_to_xy(kpts),
                          "Before Aligned",
                          delay=0.1)
    utils.visualize_hands(
        utils.convert_samples_to_xy(task1.calculate_mean_shape(kpts)),
        "Mean Shape Before Aligned")
    task1.procrustres_analysis(kpts)
Exemple #10
0
def visualize_impact_of_pcs(mean, pcs, pc_weights):
    # your part here

    utils.visualize_hands(utils.convert_samples_to_xy(
        np.expand_dims(mean, axis=0)),
                          "mean",
                          delay=1)

    # get positive and negative weights
    v = np.sqrt(5)
    positive_K_weights = pc_weights * v
    negative_K_wegihts = pc_weights * -v

    A = mean + np.expand_dims(np.sum(
        pcs * np.expand_dims(positive_K_weights, axis=1), axis=0),
                              axis=0)
    utils.visualize_hands(utils.convert_samples_to_xy(A),
                          "Mean with Sum of positive weighted PCs",
                          delay=1)

    B = mean + np.expand_dims(np.sum(
        pcs * np.expand_dims(negative_K_wegihts, axis=1), axis=0),
                              axis=0)
    utils.visualize_hands(utils.convert_samples_to_xy(B),
                          "Mean with Sum of negative weighted PCs",
                          delay=1)

    A = mean + np.expand_dims(pcs * np.expand_dims(positive_K_weights, axis=1),
                              axis=0)

    utils.visualize_hands(utils.convert_samples_to_xy(A[0]),
                          "Difference between each positive weighted PCs ",
                          delay=.4)

    B = mean + np.expand_dims(pcs * np.expand_dims(negative_K_wegihts, axis=1),
                              axis=0)
    utils.visualize_hands(utils.convert_samples_to_xy(B[0]),
                          "Difference between each negative weighted PCs",
                          delay=.4)

    return None
Exemple #11
0
def reconstruct_test_shape(kpts, mean, pcs, pc_weight):
    #ToDo
    ref = mean.copy()
    recon = kpts.copy()
    ref = np.tile(ref,(pc_weight.shape[0],1,1))
    hidWeights = (pcs * pc_weight).T
    print('------------------------------------')
    print('Hidden Weights:')    
    print(hidWeights)
    ref[:,0:pcs.shape[0],:] += hidWeights[..., np.newaxis]
    error = np.sqrt(np.sum(np.square(ref - mean), axis=2))/ref.shape[1]
    error = np.sum(error, axis=1)
    print('------------------------------------')
    print('RMS Errors: {}'.format(error))
    # Create the reconstruction for hidden weights with least error
    ref = ref[np.argmin(error)]
    ref = ref[np.newaxis, ...]
    recon = np.vstack((recon, ref))
    utils.visualize_hands(mean, 'Mean Shape')
    utils.visualize_hands(recon, 'Original+Reconstructed Shape')
    
Exemple #12
0
def procrustres_analysis(kpts, max_iter=int(1e3), min_error=1e-5):

    aligned_kpts = kpts.copy()
    # utils.visualize_hands(kpts,'Before alignment')

    counter = 0
    oldError = np.zeros((kpts.shape[0]))
    for iter in range(max_iter):

        reference_mean = calculate_mean_shape(aligned_kpts)

        # align shapes to mean shape
        aligned_kpts = procrustres_analysis_step(aligned_kpts, reference_mean)

        ##################### Your Part Here #####################

        ##########################################################
        error = compute_avg_error(kpts, reference_mean)
        if (oldError == error).all():
            counter += 1
        else:
            oldError = error
        if counter == 10:
            print('Shapes have converged')
            print('----------------------------------------')
            break
        print('Iteration = {} || error = {}'.format(iter, error))
        if np.min(error) <= 1e-5:
            break

    # visualize
    utils.visualize_hands(aligned_kpts, 'Aligned shapes')

    # visualize mean shape
    meanShape = calculate_mean_shape(aligned_kpts)
    meanShape = meanShape[np.newaxis, ...]
    utils.visualize_hands(meanShape, 'New Mean Shape')

    return aligned_kpts
Exemple #13
0
def procrustres_analysis(kpts, max_iter=int(1e3), min_error=1e-5):

    aligned_kpts = kpts.copy()
    aligned_kpts = utils.convert_samples_to_xy(aligned_kpts)
    start_mean = calculate_mean_shape(aligned_kpts)
    for iter in range(max_iter):
        print(iter)
        ##################### Your Part Here #####################
        reference_mean = calculate_mean_shape(aligned_kpts)
        # align shapes to mean shape
        aligned_kpts = procrustres_analysis_step(aligned_kpts, reference_mean)

        new_mean = calculate_mean_shape(aligned_kpts)
        err = compute_avg_error(aligned_kpts, new_mean)
        if err < min_error:
            break
        ##########################################################

    # visualize
    utils.visualize_hands(aligned_kpts, "After Aligned", delay=.1)
    # visualize mean shape
    utils.visualize_hands(calculate_mean_shape(aligned_kpts),
                          "Mean Shape After Aligned")
    return aligned_kpts