Esempio n. 1
0
def part7(alpha, EPS, max_iters):
    '''
    Plot the performance of the classifiers on the training and validation sets vs the size of the training set.
    :param alpha: alpha
    :param EPS: epsilon
    :param max_iters: maximum iterations
    :return: void
    '''
    # Get training data, validation data and testing data from part2
    im_data_training, im_data_validation, im_data_testing = part2()
    # Get required x_train and prepare labels for training data
    x_train, y_train = dataExtraction.prepare_training_data_label_by_actor_order_2(
        im_data_training, [0, 1, 2, 3, 4, 5], 70)
    # Add constant values for each image data in x_train
    x_train = np.concatenate(
        (x_train, np.ones([x_train.shape[0], 1])), axis=1) / 255

    # Prepare for calculating gradient in two ways
    theta0 = np.ones((1025, 6)) * 0.01

    # Train classifiers
    theta, costs, iters = lr.gradient_descent(
        lr.new_quadratic_cost_function,
        lr.new_derivative_quadratic_cost_function, x_train, y_train, theta0,
        alpha, EPS, max_iters)

    # Performance on training set
    y_hypothesis = lr.hypothesis(theta, x_train)
    accuracy = accuracy_compute.accuracy_2(y_train, y_hypothesis)
    print(accuracy)

    # Performance on validation set
    x_valid, y_valid = dataExtraction.prepare_training_data_label_by_actor_order_2(
        im_data_validation, [0, 1, 2, 3, 4, 5], 10)
    x_valid = np.concatenate(
        (x_valid, np.ones([x_valid.shape[0], 1])), axis=1) / 255
    y_hypothesis = lr.hypothesis(theta, x_valid)
    accuracy = accuracy_compute.accuracy_2(y_valid, y_hypothesis)
    print(accuracy)

    return theta
Esempio n. 2
0
def part4():
    '''
    Display the θs that you obtain by training using the full training dataset, and by training using a training set that contains only two images of each actor.
    :return: void
    '''
    # Get training data, validation data and testing data from part2
    im_data_training, im_data_validation, im_data_testing = part2()

    # Using full training set of Baldwin and Carell
    # 500 iterations
    theta_full, iters_full = part3(1e-5, 1e-6, 5000)
    # 5000 iterations
    # theta_full, iters_full = part3(1e-5, 1e-6, 5000)
    # 50000 iterations
    # theta_full, iters_full = part3(1e-5, 1e-6, 50000)

    # Get required actors' image validation data
    x_valid, y_valid = dataExtraction.prepare_training_data_label_by_actor_order(
        im_data_validation, [3, 5], 10)
    # Add constant values for each image data in x_valid
    x_valid = np.concatenate(
        (x_valid, np.ones([x_valid.shape[0], 1])), axis=1) / 255
    # Apply hypothesis function
    y_hypothesis = lr.hypothesis(theta_full, x_valid)
    # Compute accuracy
    accuracy = accuracy_compute.accuracy(y_valid, y_hypothesis)
    print(accuracy)

    # Using two images of Baldwin and Carell
    # Get training data, validation data and testing data from part2
    im_data_training, im_data_validation, im_data_testing = part2()
    y_train = np.array([1, 1, 0, 0])
    x_train = im_data_training[3][10]
    x_train = np.vstack((x_train, im_data_training[3][11]))
    x_train = np.vstack((x_train, im_data_training[5][20]))
    x_train = np.vstack((x_train, im_data_training[5][21]))
    # Add constant values for each image in x_train
    x_train = np.concatenate(
        (x_train, np.ones([x_train.shape[0], 1])), axis=1) / 255
    # Theta initialization
    theta0 = np.ones(1025) * 0.01
    theta_2, costs, iters_2 = lr.gradient_descent(
        lr.quadratic_cost_function, lr.derivative_quadratic_cost_function,
        x_train, y_train, theta0, 1e-5, 1e-6, 50)

    # Show image of theta
    new_theta_full = np.reshape(theta_full[:1024], (32, 32))
    imshow(new_theta_full, cmap="RdBu", interpolation="spline16")
    show()
    new_theta_2 = np.reshape(theta_2[:1024], (32, 32))
    imshow(new_theta_2, cmap="RdBu", interpolation="spline16")
    show()
Esempio n. 3
0
def part5(alpha, EPS, max_iters):
    '''
    Plot the performance of the classifiers on the training and validation sets vs the size of the training set.
    :param alpha: alpha
    :param EPS: epsilon
    :param max_iters: maximum iterations
    :return: void
    '''
    i = 0
    accuracy_list = []
    no_of_images = [10, 20, 30, 40, 50, 60, 70]
    while i < 7:
        # Get training data, validation data and testing data from part2
        im_data_training, im_data_validation, im_data_testing = part2()
        # Get required actors' image training data
        # Male as 0, female as 1
        x_train, y_train = dataExtraction.prepare_training_data_label_by_gender(
            im_data_training, [0, 1, 2, 3, 4, 5], [1, 1, 1, 0, 0, 0],
            no_of_images[i])
        # Add constant values for each image data in x_train
        x_train = np.concatenate(
            (x_train, np.ones([x_train.shape[0], 1])), axis=1) / 255
        # Theta initialization (1024 plus a constant theta)
        theta0 = np.ones(1025) * 0.01
        # Train classifiers
        theta, costs, iters = lr.gradient_descent(
            lr.quadratic_cost_function, lr.derivative_quadratic_cost_function,
            x_train, y_train, theta0, alpha, EPS, max_iters)

        # Get required actors' image validation data
        # Male as 0, female as 1
        x_valid, y_valid = dataExtraction.prepare_training_data_label_by_gender(
            im_data_validation, [0, 1, 2, 3, 4, 5], [1, 1, 1, 0, 0, 0], 10)
        # Add constant values for each image data in x_valid
        x_valid = np.concatenate(
            (x_valid, np.ones([x_valid.shape[0], 1])), axis=1) / 255
        # Apply hypothesis function
        y_hypothesis = lr.hypothesis(theta, x_valid)
        # Compute accuracy
        accuracy = accuracy_compute.accuracy(y_valid, y_hypothesis)
        accuracy_list.append(accuracy)

        i = i + 1

    figure(1)
    plot(no_of_images, accuracy_list)
    xlabel('number of training images for each actor')
    ylabel('classifier accuracy')
    show()