示例#1
0
def main():
    print('Running main function')

    # Loading images in color
    width_resize = 28
    height_resize = 28
    instance_load = ClassImageDataSet(positive_dir, negative_dir, width_resize,
                                      height_resize)

    train_data, train_labels = instance_load.load_train_set(load_color=True)
    eval_data, eval_labels = instance_load.load_eval_set(load_color=True)

    print('Printing shapes')
    print(train_data.shape)
    print(train_labels.shape)

    print('Generating instance')
    model_dir = '/tmp/cnn_color'
    n_classes = 2
    channels = 3

    classifier = ClassCNN(model_dir=model_dir,
                          classes=n_classes,
                          width=width_resize,
                          height=height_resize,
                          channels=channels,
                          train_steps=5000)

    var = input('Set 1 to train, 2 to predict. Otherwise to eval ')

    if var == '1':
        print('Training model')
        classifier.train_model(train_data, train_labels)
    elif var == '2':
        print('Predict model')
        print('Total elements: ' + str(eval_data.shape[0]))
        index = 0
        eval_item = eval_data[index]
        print(eval_item.shape)

        result = classifier.predict_model(eval_item)
        print('Result obtained: ' + str(result['classes']))
        print('Print probabilities')
        print(result['probabilities'])

        print('Real result: ' + str(eval_labels[index]))
    else:
        print('Evaluating model')
        classifier.eval_model(eval_data, eval_labels)

    print('Done!')
示例#2
0
def train_model_one_vs_all():
    print('Training MNIST model one vs all')

    train_data, train_labels = ClassImageDataSet.load_train_mnist()
    data = train_data[0]
    print('Data: {0}'.format(data))
    classes = 10

    list_models = list()

    for cls in range(classes):
        list_labels = list()

        for i in range(train_labels.shape[0]):
            if cls == int(train_labels[i]):
                list_labels.append(1)
            else:
                list_labels.append(0)

        new_labels_np = np.array(list_labels, dtype=np.int)

        print('Generating values to train in SVM: {0}'.format(cls))

        start_time = time.time()
        clf = svm.SVC(gamma=0.001, C=100.)
        clf.fit(train_data, new_labels_np)
        end_time = time.time()

        print('Total time: {0}'.format((end_time - start_time)))

        path_model = '/tmp/model{0}.pkl'.format(cls)
        print('Generating model into: {0}'.format(path_model))

        joblib.dump(clf, path_model)
        list_models.append(clf)

    print('Evaluating model list')
    eval_data, eval_labels = ClassImageDataSet.load_eval_mnist()

    print('Getting first data!')
    print('Label: {0}'.format(eval_labels[0]))

    for index, model in enumerate(list_models):
        array = np.expand_dims(eval_data[0], axis=0)
        result = model.predict(array)

        print('Result for class {0}: {1}'.format(index, result))

    print('Done!')
def main():
    print('Loading dataset')
    width_resize = 28
    height_resize = 28
    instance = ClassImageDataSet(positive_dir, negative_dir, width_resize, height_resize)

    pos_train, neg_train = instance.load_train_set()
    pos_eval, neg_eval = instance.load_eval_set()

    print('Test loading mnist dataset')
    pos_mnist_train, pos_mnist_eval = instance.load_eval_mnist()

    print('Train dataset')
    print(pos_mnist_train[0])

    print('Done!')
示例#4
0
def eval_model():
    print('Evaluating MNIST model')
    print('Loading model')
    clf = joblib.load('/tmp/model.pkl')  # type: svm.SVC

    print('Loading train data')
    test, labels_test = ClassImageDataSet.load_eval_mnist()

    elem = test[0]

    print('Evaluating model in elements')
    elems = 0
    pos = 0
    for elem in test:
        array = np.expand_dims(elem, axis=0)

        prediction = clf.predict(array)

        if prediction[0] == labels_test[elems]:
            pos = pos + 1

        elems = elems + 1
        if elems % 10 == 0:
            print('Elems: ' + str(elems) + ' pos: ' + str(pos) + ' total: ' +
                  str(labels_test.shape))

    precision = pos / elems

    print('Pos: ' + str(pos))
    print('Elems: ' + str(elems))
    print('Precision: ' + str(precision))
    print('Done!')
def main():
    print('Init main function')
    print('Loading models')
    print('Load model in grayscale first')

    width = 28
    height = 28

    # Test as reshape image
    train_data, train_labels = ClassImageDataSet.load_train_mnist(reshape=True)
    eval_data, eval_labels = ClassImageDataSet.load_eval_mnist(reshape=True)

    n_classes = 10
    channels = 1

    model_dir = '/tmp/model_example_mnist'
    classifier = ClassCNN(model_dir=model_dir,
                          channels=channels,
                          classes=n_classes,
                          width=width,
                          height=height,
                          train_steps=5000)

    var = input('Set 1 to train, 2 to predict. Otherwise to eval ')

    if var == '1':
        print('Training model')
        classifier.train_model(train_data, train_labels)
    elif var == '2':
        print('Predict model')
        print('Total elements: ' + str(eval_data.shape[0]))
        index = 1100
        eval_item = eval_data[index]
        print(eval_item.shape)

        result = classifier.predict_model(eval_item)
        print('Result obtained: ' + str(result['classes']))
        print('Print probabilities')
        print(result['probabilities'])

        print('Real result: ' + str(eval_labels[index]))
    else:
        print('Evaluating model')
        classifier.eval_model(eval_data, eval_labels)

    print('Done!')
示例#6
0
def main():
    print('Initializing main function')
    print('Loading datasets')

    train_data, train_labels = ClassImageDataSet.load_train_mnist()
    eval_data, eval_labels = ClassImageDataSet.load_eval_mnist()

    print('PCA with training data')
    n_features = 18

    pca = PCA(n_components=n_features, svd_solver='randomized').fit(train_data)
    train_pca = pca.transform(train_data)
    n_classes = 10
    hidden_neurons = 100
    eval_pca = pca.transform(eval_data)

    print('Printing shapes')
    print(train_data.shape)
    print(train_pca.shape)
    model_dir = '/tmp/model_example_pca'
    classifier = ClassNN(model_dir, n_classes, hidden_neurons)

    var = input('Set 1 to train, 2 to predict. Otherwise to eval ')

    if var == '1':
        print('Training model')
        classifier.train_model(train_pca, train_labels)
    elif var == '2':
        print('Predict model')
        print('Total elements: ' + str(eval_pca.shape[0]))
        index = 1100
        eval_item = eval_pca[index]
        print(eval_item.shape)

        result = classifier.predict_model(eval_item)
        print('Result obtained: ' + str(result['classes']))
        print('Print probabilities')
        print(result['probabilities'])

        print('Real result: ' + str(eval_labels[index]))
    else:
        print('Evaluating model')
        classifier.eval_model(eval_pca, eval_labels)

    print('Done!')
def main():
    print('Initializing main function')

    instance_train = ClassImageDataSet(positive_dir, negative_dir, width_resize, height_resize)

    train_data, train_labels = instance_train.load_train_mnist()
    eval_data, eval_labels = instance_train.load_eval_mnist()

    res = input('Press 1 to train, 2 to eval, 3 to eval fast, 4 to eval fast pose: ')
    if res == '1':
        train(train_data, train_labels)
    elif res == '2':
        evaluating(eval_data)
    elif res == '3':
        evaluating_fast(eval_data)
    elif res == '4':
        evaluating_fast_nn()
    else:
        raise Exception('Option not recognized!')
示例#8
0
def train_model():
    print('Training MNIST model')

    train, labels = ClassImageDataSet.load_train_mnist()

    print('Generating values to train in SVM')
    clf = svm.SVC(gamma=0.001, C=100.)

    clf.fit(train, labels)
    print('Saving model in temp folder')
    joblib.dump(clf, '/tmp/model.pkl')
def main():
    print('Initializing main function')
    train_data, train_labels = ClassImageDataSet.load_train_mnist()
    components = 48
    pca = PCA(n_components=components,
              svd_solver='randomized').fit(train_data)  # type: PCA

    train_pca = pca.transform(train_data)
    print(pca.explained_variance_ratio_.sum())
    plt.hist(pca.explained_variance_ratio_, bins=components, log=True)
    plt.show()

    print('Done!')
示例#10
0
def eval_model_one_vs_all():
    print('Evaluating SVN multi class')

    print('Loading models')
    classes = 10
    list_models = list()

    for cls in range(classes):
        path_model = '/tmp/model{0}.pkl'.format(cls)

        print('Loading model {0}'.format(cls))
        model = joblib.load(path_model)
        list_models.append(model)

    # Loading eval classes
    eval_data, eval_labels = ClassImageDataSet.load_eval_mnist()

    data_ok = 0
    total_data = eval_labels.shape[0]
    for index in range(total_data):
        data = eval_data[index]
        array = np.expand_dims(data, axis=0)

        selected_cls = -1
        for cls in range(classes):
            model = list_models[cls]
            res = model.predict(array)

            if res[0] == 1:
                selected_cls = cls

        if selected_cls == int(eval_labels[index]):
            data_ok += 1

    print('Data ok: {0}'.format(data_ok))
    print('Total data: {0}'.format(total_data))

    precision = data_ok / total_data
    print('Precision: {0}'.format(precision))

    print('Done!')