Ejemplo n.º 1
0
def mlp():
    from sklearn.neural_network import MLPClassifier

    # Prepare X_train, X_test
    X_train_names, y_train, X_test_names, y_test = prepare_dataset()
    X_train = []
    X_test = []

    signature_length = 200

    for file_path in X_train_names:
        im = cv2.imread(file_path, cv2.IMREAD_GRAYSCALE).astype("uint8")
        signature, _ = signature_descriptor(im, signature_length)
        X_train.append(signature)

    for file_path in X_test_names:
        im = cv2.imread(file_path, cv2.IMREAD_GRAYSCALE).astype("uint8")
        signature, _ = signature_descriptor(im, signature_length)
        X_test.append(signature)

    # Create MLP
    clf = MLPClassifier(random_state=1,
                        max_iter=300,
                        hidden_layer_sizes=(100, 50)).fit(X_train, y_train)
    print("MLP score", clf.score(X_test, y_test))

    # Confusion matrix
    from sklearn.metrics import plot_confusion_matrix
    import matplotlib.pyplot as plt
    plot_confusion_matrix(clf, X_test, y_test)
    plt.xticks(rotation=90)
    plt.show()
Ejemplo n.º 2
0
def main():
    X_train, y_train, X_test, y_test = prepare_dataset()
    name = 3
    file = f"./dataset/tests/{name}.png"
    file = X_train[8]

    img = cv2.imread(file, cv2.IMREAD_GRAYSCALE).astype("uint8")
    unl_fourier(img, 4)

    cv2.imshow("Original image", img)
    cv2.imshow("UNL", unl(img))
    cv2.waitKey(0)
def test():
    X_train, y_train, X_test, y_test = prepare_dataset()

    im = cv2.imread(X_train[8], cv2.IMREAD_GRAYSCALE).astype("uint8")
    contour = object_contour(im)

    # plt.imshow(im)
    # plt.show()

    # Center of contour
    x, y = center_of_contour(im, contour)

    # Plot image with center
    cv2.circle(im, (x, y), 7, (255, 255, 255), -1)
    cv2.imshow("Image", im)
    cv2.waitKey(0)

    # Contours, what are those?
    # Contour points start with first upper left point
    # for idx, point in enumerate(contour):
    #    print(point)
    #    cv2.circle(im, (point[0, 0], point[0, 1]), 7, (25 * idx, 25 * idx, 25 * idx), -1)
    #    if idx > 10:
    #       break
    # cv2.imshow("Image", im)
    # cv2.waitKey(0)

    # Calculate distance to every point
    dists = calculate_dists(contour, [x, y])

    # Plot signature
    # print(dists)
    # plt.plot(dists)
    # plt.show()

    # Interpolate dists vector to fixed length e.g. 200 elements
    downsampled_y, downsampled_x = scale_dists_length(dists, 200)

    # Plot points
    plt.plot(dists)
    plt.scatter(downsampled_x, downsampled_y)
    plt.show()

    # Downsampled points length
    print("Downsampled distances lentgh:", downsampled_y.shape)
    print("Distances lentgh:", dists.shape)

    plt.plot(downsampled_y)
    plt.show()
Ejemplo n.º 4
0
def main():
    from simple_shape_descriptors import prepare_dataset

    X_train, y_train, X_test, y_test = prepare_dataset()

    sizes = [5, 20, 30, 35, 40]

    for size in sizes:
        clf = FourierClassifier(size=size)
        clf.fit(X_train, y_train)
        y_pred = clf.predict(X_test)

        from sklearn.metrics import accuracy_score

        acc = accuracy_score(y_test, y_pred)
        print(f"Size: {size} Acc: {acc}")
Ejemplo n.º 5
0
def main():
    X_train, y_train, X_test, y_test = prepare_dataset()
    clf = SignatureClassifier(signature_length=200)
    clf.fit(X_train, y_train)
    y_pred = clf.predict(X_test)

    from sklearn.metrics import accuracy_score

    acc = accuracy_score(y_test, y_pred)
    print(f"Acc: {acc}")

    from sklearn.metrics import plot_confusion_matrix
    import matplotlib.pyplot as plt
    plot_confusion_matrix(clf, X_test, y_test)
    plt.xticks(rotation=90)
    plt.show()
Ejemplo n.º 6
0
def experiment_30():
    from simple_shape_descriptors import prepare_dataset

    X_train, y_train, X_test, y_test = prepare_dataset()
    size = 30

    clf = FourierClassifier(size=size)
    clf.fit(X_train, y_train)
    y_pred = clf.predict(X_test)

    from sklearn.metrics import accuracy_score

    acc = accuracy_score(y_test, y_pred)
    print(f"Size: {size} Acc: {acc}")

    from sklearn.metrics import plot_confusion_matrix
    import matplotlib.pyplot as plt

    plot_confusion_matrix(clf, X_test, y_test)
    plt.xticks(rotation=90)
    plt.tight_layout()
    plt.show()