Beispiel #1
0
def gaussian_lpf_hpf(sigma, folder_name, show=False):
    # load the images
    def load_positive(path):
        filenames = glob.glob(path)
        images = [cv2.imread(img) for img in filenames]
        positives = []
        for img in images:
            imge = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
            lowpass = ndimage.gaussian_filter(imge, sigma)
            lowpass2 = ndimage.gaussian_filter(lowpass, sigma)
            gauss_highpass_lpf = lowpass - lowpass2
            positives.append(gauss_highpass_lpf)
        return np.array(positives)

    def load_negative(path):
        filenames = glob.glob(path)
        images = [cv2.imread(img) for img in filenames]
        negatives = []
        for img in images:
            imge = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
            lowpass = ndimage.gaussian_filter(imge, sigma)
            lowpass2 = ndimage.gaussian_filter(lowpass, sigma)
            gauss_highpass_lpf = lowpass - lowpass2
            negatives.append(gauss_highpass_lpf)
        return np.array(negatives)

    def show_images(X):
        fig, axes = plt.subplots(5,
                                 5,
                                 figsize=(9, 9),
                                 subplot_kw={
                                     'xticks': [],
                                     'yticks': []
                                 },
                                 gridspec_kw=dict(hspace=0.01, wspace=0.01))
        for i, ax in enumerate(axes.flat):
            ax.imshow(X[i], cmap='gray')
        plt.show()

    yes = load_positive(folder_name + "/yes/*.png")
    no = load_negative(folder_name + "/no/*.png")

    #print('Number of Positive Images: ' + str(yes.shape[0]))
    #print('Number of Negative Images: ' + str(no.shape[0]))

    yes_labels = np.ones(yes.shape[0])
    no_labels = np.zeros(no.shape[0])

    X = np.vstack((yes, no))
    y = np.hstack((yes_labels, no_labels))

    nsamples, nx, ny = X.shape
    X_flat = X.reshape((nsamples, nx * ny))
    X_train, X_test, y_train, y_test = train_test_split(X_flat,
                                                        y,
                                                        test_size=0.3,
                                                        random_state=0)
    X_train, X_val, y_train, y_val = train_test_split(X_flat,
                                                      y,
                                                      test_size=0.4,
                                                      random_state=0)
    if show == True:
        show_images(X, 'Read Images')

    # Show the dimensionality reduction #
    if show == True:
        pca = PCA(n_components=0.95)
        X_pca = pca.fit_transform(X_flat)
        approximation = pca.inverse_transform(X_pca)
        approximation = approximation.reshape((nsamples, nx, ny))
        show_images(approximation, 'Reduced Images')

    # Start applying supervised learning #
    scaler = MinMaxScaler()

    # Fit on training set only.
    scaler.fit(X_train)

    # Apply transform to both the training set and the test set.
    X_train = scaler.transform(X_train)
    X_val = scaler.transform(X_val)
    X_test = scaler.transform(X_test)

    predictions_svm = svm_classifier(X_train,
                                     y_train,
                                     X_test,
                                     y_test,
                                     'rbf',
                                     gamma='auto',
                                     plotting=False)

    return predictions_svm
Beispiel #2
0
print(x.shape, y.shape)

# Normalize the data
x = preprocessing.scale(x)
# Split the data into training and testing
X_train, X_test, y_train, y_test = train_test_split(x,
                                                    y,
                                                    test_size=0.4,
                                                    random_state=3169)

# Apply different machine learning classifiers

svm_classifier(X_train,
               y_train,
               X_test,
               y_test,
               'linear',
               'auto',
               plotting=False)

dtree_classifier(X_train, y_train, X_test, y_test, plotting=False)

boost_classifier(X_train, y_train, X_test, y_test, plotting=False)

knn_classifier(X_train, y_train, X_test, y_test, neighbors=2, plotting=False)

neural_net(X_train,
           y_train,
           X_test,
           y_test,
           learning_rate=1e-01,
Beispiel #3
0
import json

import matplotlib.pyplot as plt

from classifiers import gaussian_naive_bayes, decision_tree_classifier, sgd_classifier, linear_svc_classifier, \
    svm_classifier, k_nearest_neighbors

if __name__ == "__main__":
    with open('data/sampledata.json') as data:
        json_data = [json.loads(line) for line in data]

    decision_tree_classifier(json_data=json_data)
    k_nearest_neighbors(json_data=json_data)
    svm_classifier(json_data=json_data)
    linear_svc_classifier(json_data=json_data)
    sgd_classifier(json_data=json_data)
    gaussian_naive_bayes(json_data=json_data)

    plt.show()
X_test = scaler.transform(X_test)

neural_net(X_train,
           y_train,
           X_test,
           y_test,
           learning_rate=1e-04,
           plotting=False)
# Now we have normalized datasets. We apply PCA. Note tha PCA above was just for visualization.
pca = PCA(n_components=0.9)

pca.fit(X_train)

X_train_reduced = pca.transform(X_train)
X_test_reduced = pca.transform(X_test)

svm_classifier(X_train_reduced,
               y_train,
               X_test_reduced,
               y_test,
               'rbf',
               gamma='auto',
               plotting=False)
neural_net(X_train_reduced,
           y_train,
           X_test_reduced,
           y_test,
           learning_rate=1e-04,
           plotting=False)
knn_classifier(X_train, y_train, X_test, y_test, neighbors=10, plotting=False)
Beispiel #5
0
def question_4():
    svm_words = ["svm", "svM", "sVm", "Svm", "sVM", "SvM", "SVm", "SVM"]
    knn_words = ['knn', 'knN', 'kNn', 'Knn', 'kNN', 'KnN', 'KNn', 'KNN']
    print("The process might take a few minutes. Especially the SVM and the KNN classifiers.")
    print("We use the self chosen neural network as a control and then use the knn and svm for comparision.")
    print("The feature vector being used is a 180 length HOG feature vector extracted using skimage.")
    print("Choose what you want to see first.")
    print("Enter 1 for the neural network or 2 for the classifiers.")
    chosen_number = int(input("\n"))
    if(chosen_number == 1):
        print(
            "The neuralnetwork used is of the architecture [180, 120, 60, 25, 10] where the first and last are input and output layers respectively and the others are hidden layers.")
        print("The activation used is ReLU.\n")
        print("Initializing network.....")
        print("Load data.......")
        self_chosen_network = nn.network(
            [180, 120, 60, 25, 10], TRAINING_DATA, TEST_DATA)
        print("Initialize the weights and biases.........")
        weights, biases = self_chosen_network.initialize_weights()
        print("Training the network......")
        self_chosen_network.train_network(
            TRAINING_DATA, weights, biases, 'ReLU', 0.01, 15, 64, True, False, False, 0, 0, False, 0, True, 'hog')
        print("Enter if you want to see other parameters like precision, recall, f1 score.")
        print("Enter y if yes or n otherwise.")
        choice = input("\n")
        if (choice in yes):
            print("Precision -------", self_chosen_network._precision)
            print("Recall----------------", self_chosen_network._recall)
            print("F1 score -------------------------",
                  self_chosen_network._f1_score)
        else:
            pass
        print("Enter if you want to see the confusion matrix.")
        print("Enter y for yes and n for no.")
        choice = input("\n")
        if (choice in yes):
            print("Confusion Matrix")
            print(self_chosen_network._confusion_mat)
    elif(chosen_number == 2):
        print("Choose the type of classifier.")
        print("Enter 'svm' (without the quotes) for svm classifier and 'knn' for the knn classifier.")
        type_of_classifier = input("\n")
        if (type_of_classifier in svm_words):
            try:
                print("Initializing the classifier........")
                classifier = clsf.svm_classifier(TRAINING_DATA, TEST_DATA)
                classifier.initiate_classifier()
                print("Getting the test data..............")
                classifier.get_test_data()
                print("Training the classifier........")
                classifier.train()
                print("Testing on the test set...........")
                print("The accuracy achieved on the test set is...  ",
                      classifier.accuracy())
            except KeyboardInterrupt:
                print("\nExiting....")
                sys.exit(0)
        elif (type_of_classifier in knn_words):
            try:
                print("Enter the number of neighbors you want (default 5).")
                print("Just press enter if you want to stick to the default value.")
                try:
                    neighbors = int(input("\n"))
                except:
                    neighbors = 5
            except KeyboardInterrupt:
                print("\nExiting....")
            try:
                print("Initializing the classifier........")
                classifier = clsf.knn_classifier(TRAINING_DATA, TEST_DATA)
                classifier.initiate_classifier(neighbors)
                print("Getting the test data..............")
                classifier.get_test_data()
                print("Training the classifier........")
                classifier.train()
                print("Testing on the test set...........")
                print("The accuracy achieved on the test set is...  ",
                      classifier.accuracy())
            except KeyboardInterrupt:
                print("\nExiting....")
                sys.exit(0)
        else:
            print("Please enter a valid classifier type and try again.\n")

    else:
        print("Invalid Input. Please try again.")
    pass
Beispiel #6
0
def pause_detector_test():
    print(100 * "-")
    print(100 * "-")
    print(100 * "-")
    print("pause vs gesture classifier")
    sizes_list = ["100", "200", "400"]
    svm_results = {}
    mlp_results = {}
    ks = [20, 30, 40, 50, 64]
    for k in ks:
        svm_results[k] = {}
        mlp_results[k] = {}
    size_w = len(sizes_list[1:])
    size_s = len(sizes_list)
    for w in sizes_list[1:]:
        for s in sizes_list:
            print(100 * '-')
            print("window width: {}".format(w))
            print("window step: {}".format(s))
            train_file = "train_features_" + w + "_" + s + ".csv"
            train_features = pd.read_csv(train_file, sep=",")
            train_features.loc[train_features['label'] != 0, 'label'] = 1
            train_features = train_features.to_numpy()

            validation_file = "validation_features_" + w + "_" + s + ".csv"
            validation_features = pd.read_csv(validation_file, sep=",")
            validation_features.loc[validation_features['label'] != 0,
                                    'label'] = 1
            validation_features = validation_features.to_numpy()

            print(36 * '-' + "Standardization of datasets" + 37 * '-')
            t = time()
            train_nc = train_features.shape[1] - 1
            x_train = train_features[:, :train_nc]
            y_train = train_features[:, train_nc]

            valid_nc = validation_features.shape[1] - 1
            x_valid = validation_features[:, :valid_nc]
            y_valid = validation_features[:, valid_nc]

            scaler = StandardScaler()
            scaler.fit(x_train)
            x_train = scaler.transform(x_train)
            x_valid = scaler.transform(x_valid)
            print("time: {:.2f}".format(time() - t))
            print(100 * '-')

            print(41 * '-' + "feature selection" + 42 * '-')
            for k in ks:

                print("{} features".format(k))
                selector = SelectKBest(mutual_info_classif, k=k)
                selector.fit(x_train, y_train)
                train = selector.transform(x_train)
                valid = selector.transform(x_valid)
                print(100 * "-")
                print("MLP classifier")
                mlp_results[k][w + 'x' + s] = int(
                    round(
                        mlp_classifier(train, y_train, valid, y_valid)[0] *
                        100))
                print(100 * "-")
                print("SVM classifier")
                svm_results[k][w + 'x' + s] = int(
                    round(
                        svm_classifier(train, y_train, valid, y_valid)[0] *
                        100))
                print(100 * "-")
    return [svm_results, mlp_results], [size_w, size_s]